一、实验目的:
1、学会定义并实现类。
2、学会定义并创建类的对象,通过类的对象访问类的成员属性与方法。
3、学会定义并实现派生类,学会使用派生类的对象。
4、理解并学会使用类的多态性。
二、实验环境:
Eclipse+Windoe10+Java
三、实验内容:
1.定义并实现一个长方体类(Cube),包含长(length)、宽(width)与高(height)等三个属性,包含计算体积(calVolume)与计算表面积(calArea)等两个方法,类的属由构造函数进行初始化或通过成员函数赋值。编写一段程序,测试该类。
package code3;
public class Cube {
float length,width,heigth;
double calVolume(){
return length*width*heigth;
}
double calArea(){
return 2*(length*width + width*heigth + length*heigth);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Cube c=new Cube();
c.heigth=5;
c.length=4;
c.width=3;
System.out.println("长方体面积为:"+c.calArea());
System.out.println("长方体体积为:"+c.calVolume());
}
}
2.定义并实现一个三角形类(Triangle),其三个边长(edge1, edge2, edge3)为其属性,包含判断其是否为三角形(isTriangle)、计算周长(calPerimeter)及计算面积(calArea)等三个方法,类的属由构造函数进行初始化或通过成员函数赋值。编写一段程序,测试该类。
package code3;
public class Triangle {
static double edge1,edge2,edge3;
boolean isTriangle(){
if(edge1+edge2>edge3 && edge2+edge3>edge1 && edge1+edge3>edge2)
return true;
return false; }
double calPerimeter(){
return edge1+edge2+edge3;
}
double calArea(){
double a,b;
a=(edge1+edge2+edge3)/2;
b=(float)Math.sqrt(a*(a-edge1)*(a-edge2)*(a-edge3));
return b;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Triangle c=new Triangle();
c.edge1=3;
c.edge2=4;
c.edge3=5;
System.out.println("三角形的三边为:"+edge1+" "+edge2+" "+edge3);
if(c.isTriangle()) {
System.out.println();
System.out.println("构成一个三角形");
System.out.println("三角形面积:"+c.calArea());
System.out.println("三角形体积:"+c.calPerimeter());
}
else System.out.println("不是三角形");
}
}
3.定义并实现一个Person类,包含姓名(name)与编号(code)等两个属性,通过构造函数为属性赋值,拥有显示属性值的方法(showInfo)。从Person类派生出一个Student类,拥有数学成绩、英语成绩、Java成绩等三个属性,拥有输入成绩、计算平均成绩、显示信息(姓名、编号及平均值)等方法。编写一段程序,测试这两个类。
package code3;
import java.util.Scanner;
class person {
double code;
String name;
public person(int code, String name) {
super();
this.code = code;
this.name = name;
}
public double getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getName() {
return name; }
public void setName(String name) {
this.name = name;
}
public void showInfo(){
System.out.println("姓名为:" + getName());
System.out.println("编号为:" + getCode());
}}
class student extends person{
public student(int code, String name) {
super(code,name);
this.code = code;
this.name = name;
}
double math,eng,java;
public double getmath() {
return math;}
public void setmath(double math) {
this.math = math ;}
public double getEng() {
return eng ;
}
public void setEng (double eng ) {
this.eng = eng ;}
public double getJava () {
return java ;}
public void setJava (double java ) {
this.java= java;}
public void intpu(){
Scanner input = new Scanner(System.in);
System.out.println("请分别输入数学、英语、java成绩:");
double math = input.nextDouble();
double eng = input.nextDouble();
double java = input.nextDouble();
setmath (math);
setEng (eng);
setJava (java );
input.close(); }
public double s(){
return (math+eng+java)/3;
}
public void show(){
person p = new person(1,"小红");
p.showInfo();
System.out.println(p.getName()+"数学成绩为:" + getmath());
System.out.println(p.getName()+"英语成绩为:" + getEng());
System.out.println(p.getName()+"java成绩为:" + getJava());
double avg = s();
System.out.print(p.getName()+"平均成绩为:" + avg);
}}
public class text {
public static void main(String[] args) {
// TODO Auto-generated method stub
student stu = new student(0,null);
stu.intpu();
stu.show();
}}
4.定义并实现一个Circle类,属性为圆的半径radius,其值由构造函数初始化。包含计算周长(calPerimeter)与计算面积(calArea),显示信息(半径、周长与面积)(showInfo)等方法。从Circle类派生出Cylinder类,拥有高(height)这个属性,其值由构造函数初始化。包含计算表面积(calArea)、计算体积(calVolume)及显示信息(半径、表面积、体积)(showInfo)等方法。编写一段程序,测试这两个类。
package code3;
class Circle{
private double radius;
public Circle() {
super(); }
public Circle(double radius) {
super();
this.radius = radius; }
public double getRadius() {
return radius; }
public void setRadius(double radius) {
this.radius = radius; }
public double calPerimeter(){
return 2*Math.PI*getRadius(); // 2*π*R
}
public double calArea(){
return Math.PI*Math.pow(getRadius(), 2); //π*R*R
}
public void showInfo(){
System.out.println("圆的半径为:" + getRadius());
String str = String.format("圆的周长为:%.2f", calPerimeter());
System.out.println(str);
String str2 = String.format("圆的面积为:%.2f", calArea());
System.out.println(str2);
} }
class Cylinder extends Circle {
private double height;
Circle cir = new Circle(1.0);
public Cylinder() {
super(); }
public Cylinder(double height) {
super();
this.height = height; }
public double getHeight() {
return height; }
public void setHeight(double height) {
this.height = height; }
public double calArea2(){
return (cir.calArea()*2 + cir.calPerimeter()*getHeight()); }
public double calVolume(){
return cir.calArea()*getHeight(); }
public void showInfo2(){
cir.showInfo();
System.out.println("圆柱体的半径为:" + cir.getRadius());
String str3 = String.format("圆柱体表面积为:%.2f", calArea2()); System.out.println(str3);
String str4 = String.format("圆柱体的体积为:%.2f", calVolume()); System.out.println(str4); }}
public class five {
public static void main(String[] args) {
// TODO Auto-generated method stub
Cylinder cyl = new Cylinder(4.0);
cyl.showInfo2(); }}
5.定义并实现如下三个类:(1)Shape类,无属性,有一个抽象方法calArea;(2)Rectangle类,从Shape类派生,有长度(length)与宽度(width)两个属性,需重写calArea方法;(3)Circle类,从Shape类派生,有半径(Radius)一个属性,需重写calArea方法。编写一段程序来测试这几个类。
package code3;
abstract class Shape {
public abstract double calArea();
}
class Rectangle extends Shape {
private double length;
private double width;
public Rectangle() {
super(); }
public Rectangle(double length, double width) {
super();
this.length = length;
this.width = width; }
public double getLength() {
return length; }
public void setLength(double length) {
this.length = length; }
public double getWidth() {
return width; }
public void setWidth(double width) {
this.width = width; }
public double calArea(){
return getLength()*getWidth(); }
}
class Circle extends Shape {
private double Radius;
public Circle() {
super(); }
public Circle(double radius) {
super();
Radius = radius; }
public double getRadius() {
return Radius; }
public void setRadius(double radius) {
Radius = radius; }
public double calArea() {
return Math.PI*(Math.pow(getRadius(), 2));
}}
public class six {
public static void main(String[] args) {
// TODO Auto-generated method stub
Rectangle R = new Rectangle(5.0,7.0);
System.out.println("矩形的面积为:" + R.calArea());
Circle C = new Circle(1.0);
System.out.println("圆形的面积为:" + C.calArea());
}}
6.在5的基础上,从Rectangle类派生Cube类,有属性高度(width),有计算表面积(calArea)及计算体积(calVolume)等方法。编写一段程序来测试这几个类。
package code3;
class Cube extends Rectangle {
private double width;
Rectangle r = new Rectangle(2,3);
public Cube(double width) {
super();
this.width = width; }
public Cube() {
super();
// TODO Auto-generated constructor stub
}
public Cube(double length, double width) {
super(length, width);
// TODO Auto-generated constructor stub }
public double getWidth() {
return width; }
public void setWidth(double width) {
this.width = width;
public double calArea(){
return 2*(r.getLength()*r.getWidth()+r.getLength()*getWidth()+r.getWidth()*getWidth());
}
public double calVolume(){
return r.calArea()*getWidth(); }
}
public class seven {
public static void main(String[] args) {
// TODO Auto-generated method stub
Cube c = new Cube(2);
System.out.println("长方体的面积为:" + c.calArea());
System.out.println("长方体的体积为:" + c.calVolume());
}}