实验三、数与对象 一、实验目的: 1、学会定义并实现类。 2、学会定义并创建类的对象,通过类的对象访问类的成员属性与方法。 3、学会定义并实现派生类,学会使用派生类的对象。 4、理解并学会使用类的多态性,理解并能使用运算符重载。
二、实验环境: BLUEJ 三、实验内容: 1.定义并实现一个长方体类(Cube),包含长(length)、宽(width)与高(height)等三个属性,包含计算体积(calVolume)与计算表面积(calArea)等两个方法,类的属由构造函数进行初始化或通过成员函数赋值。编写一段程序,测试该类。 代码: Cube类中:
public class Cube { double length; double width; double height; Cube(double length,double width,double height) { this.length=length; this.width=width; this.height=height; } void resetproperty(double length,double width,double height) { this.length=length; this.width=width; this.height=height; } double calArea() { return this.length*this.width*2+this.length*this.height*2+this.width*this.height*2; } double calVolume() { return this.length*this.width*this.height; } }主函数:
public class CubeMain { public static void main(String[] args) { Cube cube=new Cube(5,4,3); /*方法二:使用调用成员函数的方法初始化Cube属性的值 * Scanner scanner=new Scanner(System.in); * double length=scanner.nextDouble(); * double width=scanner.nextDouble(); * double height=scanner.nextDouble(); *cube.resetproperty(length, width, height); **/ System.out.println("这个长方体的表面积为:"+cube.calArea()); System.out.println("这个长方体的体积为:"+cube.calVolume()); } } (选做)定义并实现一个三角形类(Triangle),其三个边长(edge1, edge2, edge3)为其属性,包含判断其是否为三角形(isTriangle)、计算周长(calPerimeter)及计算面积(calArea)等三个方法,类的属由构造函数进行初始化或通过成员函数赋值。编写一段程序,测试该类。 代码: Triangle类: import java.lang.Math; public class Triangle { double edge1,edge2,edge3; Triangle()//构造函数 { edge1=0.0; edge2=0.0; edge3=0.0; } void setTriangle(double a,double b,double c) { edge1=a; edge2=b; edge3=c; } boolean isTriangle() { if( (edge1+edge2)>=edge3 && (edge2+edge3)>=edge1 && (edge1+edge3)>edge2 ) return true; else return false; } double calPerimeter() { return edge1+edge2+edge3; } double calArea() { double p=(edge1+edge2+edge3)/2; return Math.sqrt(p*(p-edge1)*(p-edge2)*(p-edge3)); } }主函数:
public class TriangleMain { public static void main(String[] args) { boolean flag=true; Triangle triangle=new Triangle(); triangle.setTriangle(3,4,5); if(triangle.isTriangle()) { System.out.println("这是一个三角形"); } else { System.out.println("这不是一个三角形"); flag=false; } if(flag) { System.out.println("这个三角形的周长为:"+triangle.calPerimeter()); System.out.println("这个三角形的面积为:"+triangle.calArea()); } } } 定义并实现一个复数类(Complex),包含实部(real)及虚部(image)两个属性,包含计算两个复数的和(add)、积(multiply)以及打印复数(print)等三个方法,类的属由构造函数进行初始化或通过成员函数赋值。编写一段程序,测试该类。 代码: Complex类: public class Complex { int real=0; int image=0; Complex(int real,int image) { this.real=real; this.image=image; } void resetproperty(int real,int image) { this.real=real; this.image=image; } Complex add(Complex c) { Complex temp=new Complex(0,0); temp.image=this.image+c.image; temp.real=this.real+c.real; return temp; } Complex multiply(Complex c) { Complex temp=new Complex(0,0); temp.real=this.real*c.real-this.image*c.image; temp.image=this.image*c.real+this.real*c.image; return temp; } void Printmultipy() { System.out.println(this.real+"+"+this.image+"i"); } }主函数:
public class ComplexMain { public static void main(String[] args) { Complex a=new Complex(4,3); Complex b=new Complex(2,5); Complex temp=new Complex(0,0); temp=a.add(b); System.out.println("加法结果:"); temp.Printmultipy(); temp=a.multiply(b); System.out.println("乘法结果:"); temp.Printmultipy(); } } 定义并实现一个Person类,包含姓名(name)与编号(code)等两个属性,通过构造函数为属性赋值,拥有显示属性值的方法(showInfo)。从Person类派生出一个Student类,拥有数学成绩、英语成绩、Java成绩等三个属性,拥有输入成绩、计算平均成绩、显示信息(姓名、编号及平均值)等方法。编写一段程序,测试这两个类。 代码: Person代码: public class Person { String name; String code; Person(String a,String b) { name=a; code=b; } void showInfo() { System.out.println("名字:"+name+"\n编号:"+code); } }Student代码:
public class Student extends Person { double MathGrade,EnglishGrade,JavaGrade; Student(String a,String b) { super(a,b); MathGrade=0; EnglishGrade=0; JavaGrade=0; } void SetGrade(double Math,double English,double Java) { MathGrade=Math; EnglishGrade=English; JavaGrade=Java; } double AveGrade() { return (MathGrade+EnglishGrade+JavaGrade)/3; } void showInfo() { super.showInfo(); System.out.println("数学成绩:"+MathGrade+"\n英语成绩:"+EnglishGrade+"\nJava成绩:"+JavaGrade); System.out.println("平均成绩:"+AveGrade()); } }主函数:
public class PSMain { public static void main(String[] args) { Student student=new Student("张三","JAVA123"); student.SetGrade(98,94,100); student.showInfo(); } }5.(选做)定义并实现一个Circle类,属性为圆的半径radius,其值由构造函数初始化。包含计算周长(calPerimeter)与计算面积(calArea),显示信息(半径、周长与面积)(showInfo)等方法。从Circle类派生出Cylinder类,拥有高(height)这个属性,其值由构造函数初始化。包含计算表面积(calArea)、计算体积(calVolume)及显示信息(半径、表面积、体积)(showInfo)等方法。编写一段程序,测试这两个类。 Circle类:
public class Circle { double radius; Circle(double r) { radius=r; } double calPerimeter() { return 2*3.14*radius; } double calArea() { return 3.14*radius*radius; } void showInfo() { System.out.println("Circle:半径为:"+radius+"\n周长为:"+calPerimeter()+"\n面积为:"+calArea()); } }Cylinder类:
public class Cylinder extends Circle { double height; Cylinder(double r,double h) { super(r); height=h; } double calArea() { return super.calPerimeter()*height; } double calVolume() { return super.calArea()*height; } void showInfo() { System.out.println("Cylinder:半径为:"+radius+"\n表面积为:"+calArea()+"\n体积为:"+calVolume()); } }主函数:
public class CCMain { public static void main(String[] args) { /*测试Circle类*/ Circle circle=new Circle(5); circle.showInfo(); Cylinder cylinder=new Cylinder(5,7); cylinder.showInfo(); } }6.定义并实现如下三个类:(1)Shape类,无属性,有一个纯虚函数calArea;(2)Rectangle类,从Shape类派生,有长度(length)与宽度(width)两个属性,需重写calArea方法;(3)Circle类,从Shape类派生,有半径(Radius)一个属性,需重写calArea方法。编写一段程序来测试这几个类。 Shape类:
abstract class Shape { abstract double calArea(); } Rectangle类: public class Rectangle extends Shape { double length,width; Rectangle(double l,double w) { length=l; width=w; } double calArea() { return length*width; } }Circle类:
public class Circle6 extends Shape { double Radius; Circle6(double r) { Radius=r; } double calArea() { return Radius*3.14*3.14; } }主函数:
public class SHCMain { public static void main(String[] args) { Rectangle rectangle=new Rectangle(3,6); System.out.println("这个矩形的面积为:"+rectangle.calArea()); Circle6 circle=new Circle6(5); System.out.println("这个圆的面积为"+circle.calArea()); } } 在6的基础上,从Rectangle类派生Cube类,有属性高度(width),有计算表面积(calArea)及计算体积(calVolume)等方法。编写一段程序来测试这几个类。 Cube类: public class Cube7 extends Rectangle { double height; Cube7(double l,double w,double h) { super(l,w); height=h; } double calArea() { return (length*width+length*height+width*height)*2; } double calVolume() { return length*width*height; } }主函数:
public class Cube7Main { public static void main(String[] args) { Cube7 cube=new Cube7(6,7,8); System.out.println("这个立方体的面积为:"+cube.calArea()); System.out.println("这个立方体的体积为:"+cube.calVolume()); } }8.(选做)在7的基础上,随机产生一系列图形(Rectangle,Circle或Cube),把它们按面积大小排序。 代码:
import java.util.Random; public class Main8 { static void output(double array[],int arraygraph[]) { for(int i=0;array[i]!=0;i++) { System.out.print((i+1)+"、"); if(arraygraph[i]==0) System.out.print("矩形:"); else if(arraygraph[i]==1) System.out.print("圆形:"); else if(arraygraph[i]==2) System.out.print("立方体:"); System.out.println(String.format("%.2f",array[i])); } } public static void main(String[] args) { Random r=new Random(); int i,j,n; int arraygraph[]=new int[10]; double array[]=new double[10]; int temp=0; while(temp<=1) { temp=r.nextInt(10); } System.out.println("共生成"+temp+"个图形"); for(i=0;i<temp;i++) { n=r.nextInt(3); if(n==0) { Rectangle rectangle=new Rectangle(Math.random()*10,Math.random()*10); System.out.println("矩形面积:"+String.format("%.2f",rectangle.calArea())); array[i]=rectangle.calArea(); arraygraph[i]=0; } else if(n==1) { Circle6 circle=new Circle6(Math.random()*10); System.out.println("圆形面积:"+String.format("%.2f",circle.calArea())); array[i]=circle.calArea(); arraygraph[i]=1; } else if(n==2) { Cube7 cube=new Cube7(Math.random()*10,Math.random()*10,Math.random()*10); System.out.println("立方体面积:"+String.format("%.2f",cube.calArea())); array[i]=cube.calArea(); arraygraph[i]=2; } } for(i=0;i<array.length;i++) for(j=0;j<array.length-1;j++) { if(array[j]<array[j+1]) { double z1=array[j]; int x=arraygraph[j]; array[j]=array[j+1]; arraygraph[j]=arraygraph[j+1]; array[j+1]=z1; arraygraph[j+1]=x; } } output(array,arraygraph); } }四、心得体会: 通过这个实验为我掌握了类的应用,包括类的继承、类中方法的重写以及抽象类的应用,也了解到了在类的继承中,子类的构造方法是会自动引用父类的(自动调用super()),但是为了添加一些固有的参数,我就加上了“super(参数1,参数2)”,在一开始的时候是调用失败了,提示我的参数列表不足什么的,后来一看是没有写上继承(extends),所以在写类的继承时应该一开始就写上extends,避免出现重复的错误。
若代码有误欢迎各位朋友批评指正 要是有代码无法运行的情况也可留言