当前位置:首页 > 实验四 Java类继承
}
public void setPtel(String ptel){ this.ptel=ptel; }
public String toString(){ //output return pname+\+paddress+\+ptel; }
}
package Ex4_1;
public class Employee extends Person { private String eoffice; private double ewage;
public Employee(String pname,String paddress,String ptel,String eoffice,double ewage){
super(pname,paddress,ptel); this.eoffice=eoffice; this.ewage=ewage; }
public Employee(){ super(); }
public String getEoffice(){ return eoffice; }
public void setEoffice(String eoffice){ this.eoffice=eoffice; }
public double getEwage(){ return ewage; }
public void setEwage(double ewage){ this.ewage=ewage; }
}
package Ex4_1;
public class Test {
public static void main(String[] args) {
Employee e1=new Employee(\,\,\,\,10000);
System.out.println(e1.toString()+\+e1.getEoffice()+\+e1.getEwage()); } }
3 编写一个抽象的图形类Shape,里面有方法area()计算面积以及方法displayArea()显示面积,编写子类矩形类和三角型类分别实现两个方法
package Ex4_2;
public abstract class Shape{
public abstract double area();
public abstract void displayArea(); }
package Ex4_2;
public class MyRectangle extends Shape { private double w,h;
public Rectangle(double w,double h){ this.w=w; this.h=h; }
public double area(){ return w*h; }
public void displayArea(){
System.out.println(\+this.area()); } }
package Ex4_2;
public class MyTriangle extends Shape{ private double a,b,c;
public Triangle(double a,double b,double c){ this.a=a; this.b=b; this.c=c; }
public double area(){ double q=(a+b+c)/2;
return Math.sqrt(q*(q-a)*(q-b)*(q-c)); }
public void displayArea(){
System.out.println(\+this.area()); } }
package Ex4_2;
public class Test {
public static void main(String[] args) { Shape t1=new Rectangle(10,5); t1.displayArea();
Shape t2=new Triangle(3,4,5); t2.displayArea(); } }
4、编写一个完整的Java Application 程序。包含接口Shape,MyRectangle类,MyTriangle类及Test类,具体要求如下:
⑴、接口Shape: double area():求一个形状的面积
double perimeter ():求一个形状的周长 ⑵、类 MyRectangle: 实现Shape接口,并有以下属性和方法:
① 属性
width: double类型,表示矩形的长 height: double类型,表示矩形的高 ② 方法
MyRectangle(double w, double h):构造函数
toString()方法 :输出矩形的描述信息,如“width=1.0,height=2.0, perimeter=6.0,
area=2.0”
⑶、类MyTriangle: 实现Shape接口,并有以下属性和方法:
① 属性
x,y,z: double型,表示三角形的三条边
s: 周长的1/2(注:求三角形面积公式为s(s?x)(s?y)(s?z),s=(x+y+z)/2 ,
开方可用Math.sqrt(double)方法)
② 方法
MyTriangle(double x, double y, double z):构造函数,给三条边和s赋初
值。
toString():输出矩形的描述信息,如“three sides:3.0,4.0,5.0,perimeter=12.0,area=6.0” ⑷、Test类作为主类要完成测试功能
① 生成MyRectangle对象
② 调用对象的toString方法,输出对象的描述信息
package Ex4_4;
public interface Shape{
public abstract double area();
public abstract void displayArea(); }
package Ex4_4;
public class MyRectangle implements Shape { private double w,h;
public MyRectangle(double w,double h){
this.w=w; this.h=h; }
public double area(){ return w*h; }
public double perimeter(){ return 2*w+2*h; }
public void displayArea(){
System.out.println(\+this.area()); }
public String toString(){ return \width=\+w+\\+\+h+\\+\+this.perimeter()+\+\+this.area(); } }
package Ex4_4;
public class MyTriangle implements Shape{ private double a,b,c;
public MyTriangle(double a,double b,double c){ this.a=a; this.b=b; this.c=c; }
public double area(){ double q=(a+b+c)/2;
return Math.sqrt(q*(q-a)*(q-b)*(q-c)); }
public double perimeter(){ return a+b+c; }
public void displayArea(){
System.out.println(\+this.area()); }
public String toString(){ return \three sides:\+a+\+b+\+c+\perimeter=\+this.perimeter()+\\+\+this.area(); } }
package Ex4_4;
public class Test {
public static void main(String[] args) { Shape t1=new MyRectangle(10,5);
}
System.out.println(t1.toString()); Shape t2=new MyTriangle(3,4,5); System.out.println(t2.toString()); }
5、非编程题:作为本次实验的结束,请对Java学习做一个小结,含 1)评价一下自己这段时间学习Java效果?
2)如果Java难,难在什么地方,即什么地方没有弄清楚? 3)对Java教学有什么意见或建议?请指出。
这些只是对大家学习了解,取到对今后教学的一个促进作用。不针对任何个人。大家可以写在本次实验的小结中。
共分享92篇相关文档