类与对象

桂 林 理 工 大 学
实 验 报 告
实验名称 类与对象 日期 2020年 06 月10 日
一、实验目的:
1、学会定义并实现类。
2、学会定义并创建类的对象,通过类的对象访问类的成员属性与方法。
3、学会定义并实现派生类,学会使用派生类的对象。
4、理解并学会使用类的多态性。

二、实验环境:

PC+Windows10+Eclipse

三、实验内容:
1.定义并实现一个长方体类(Cube),包含长(length)、宽(width)与高(height)等三个属性,包含计算体积(calVolume)与计算表面积(calArea)等两个方法,类的属由构造函数进行初始化或通过成员函数赋值。编写一段程序,测试该类。
代码:

package lei;
public class Cube {
	private double length=0.0;
	private double width=0.0;
	private double height=0.0;
	public void SetCube(double length,double width,double height) {
		this.length=length;
		this.width=width;
		this.height=height;
	}
	public double calVolume() {
		return length*width*height;
	}
	public double calArea() {
		return length*width*2+length*height*2+width*height*2;
	}
}
package lei;
import java.util.Scanner;
public class Cubetest {
	public static void main(String[] args) {
		Cube a=new Cube();
		Scanner scanner=new Scanner(System.in);
		a.SetCube(3, 4, 5);
		System.out.println("calVolume="+a.calVolume());
		System.out.println("calArea="+a.calArea());
	}

}

2.定义并实现一个三角形类(Triangle),其三个边长(edge1, edge2, edge3)为其属性,包含判断其是否为三角形(isTriangle)、计算周长(calPerimeter)及计算面积(calArea)等三个方法,类的属由构造函数进行初始化或通过成员函数赋值。编写一段程序,测试该类。
代码:

package lei;
public class Triangle {
	private float edge1;
	private float edge2;
	private float edge3;
	public void SetTriangle(float edge1,float edge2,float edge3) {
		this.edge1=edge1;
		this.edge2=edge2;
		this.edge3=edge3;
	}
	boolean isTriangle() {
		if(edge1+edge2>edge3&&edge1+edge3>edge2&&edge3+edge2>edge1)
			return true;
		return false;
	}
	double calPerimeter() {
		return edge1+edge2+edge3;
	}
	double calArea() {
		float p;
		p=(edge1+edge2+edge3)/2;
		return (float)(Math.sqrt(p*(p-edge1)*(p-edge2)*(p-edge3)));
	}

}
package lei;
public class Triangletest {
	public static void main(String[] args) {
		Triangle a = new Triangle();
		a.SetTriangle(3, 4, 5);
		if(a.isTriangle()) {
			System.out.println("该三边可以构成三角形!");
			System.out.println("calPerimeter=" + a.calPerimeter());
			System.out.println("calArea=" + a.calArea());
		}
		else
			System.out.println("该三边不能构成三角形!");
	}
}
  1. 定义并实现一个复数类(Complex),包含实部(real)及虚部(image)两个属性,包含计算两个复数的和(add)、积(multiply)以及打印复数(print)等三个方法,类的属由构造函数进行初始化或通过成员函数赋值。编写一段程序,测试该类。
    代码:
package lei;
public class Complex {
	private double real;
	private double image;
	
	public double getreal() {
		return real;
	}
	
	public double getimage() {
		return image;
	}
	
	public void SetComplex(double real,double image) {
		this.real=real;
		this.image=image;
	}
	
	public void add(Complex a,Complex b) {
		this.real=a.getreal()+b.getreal();
		this.image=a.getimage()+b.getimage();
		System.out.print("两个复数相加:");
		Print();
	}
	
	public void multiply(Complex a,Complex b) {
		if(a.getimage()!=0&&b.getimage()!=0) {
			this.real=a.getreal()*b.getreal()-a.getimage()*b.getimage();
			this.image=a.getreal()*b.getimage()+a.getimage()*b.getreal();
			System.out.print("两个复数相乘:");
			Print();
		}
		else {
			this.real=a.getreal()*b.getreal();
			this.image=a.getreal()*b.getimage()+a.getimage()*b.getreal();
			System.out.print("两个复数相乘:");
			Print();
		}
	}
	
	public void Print() {
		if(image>0)
			System.out.println(real + "+" + image + "i");
		else if(real==0)
			System.out.println(image+"i");
		else if(image==0)
			System.out.println(real);
		
		else
			System.out.println(real + "-" + (-image) + "i");
	}
}
package lei;
public class Complextest {
	public static void main(String[] args) {
		Complex a=new Complex();
		Complex b=new Complex();
		Complex c=new Complex();
		a.SetComplex(3, 4);
		b.SetComplex(5,-6);
		c.add(a,b);
		c.multiply(a, b);
	}

}
  1. 定义并实现一个Person类,包含姓名(name)与编号(code)等两个属性,通过构造函数为属性赋值,拥有显示属性值的方法(showInfo)。从Person类派生出一个Student类,拥有数学成绩、英语成绩、Java成绩等三个属性,拥有输入成绩、计算平均成绩、显示信息(姓名、编号及平均值)等方法。编写一段程序,测试这两个类。
    程序:
package lei;
public class Person {
	private String name;
	private String code;
	public void SetPerson(String name,String code) {
		this.name=name;
		this.code=code;
	}
	public void showInfo() {
		System.out.println("姓名:"+name+" 学号:"+code);
	}
}
package lei;
import java.util.Scanner;
public class Student extends Person{
	private double mathgrade;
	private double englishgrade;
	private double javagrade;
	public void Setmath(double mathgrade) {
		this.mathgrade=mathgrade;
	}
	public void Setenglish(double englishgrade) {
		this.englishgrade=englishgrade;
	}
	public void Setjava(double javagrade) {
		this.javagrade=javagrade;
	}
	public double Getmath() {
		return mathgrade;
	}
	public double Getenglish() {
		return englishgrade;
	}
	public double Getjava() {
		return javagrade;
	}
	public void inputGrade() {
		Scanner scanner = new Scanner(System.in);
		System.out.print("请输入数学、英语、java的成绩:");
		double mathgrade=scanner.nextDouble();
		double englishgrade=scanner.nextDouble();
		double javagrade=scanner.nextDouble();
		Setmath(mathgrade);
		Setenglish(englishgrade);
		Setjava(javagrade);
		scanner.close();
	}
	public double avg() {
		return (Getmath()+Getenglish()+Getjava())/3;
	}
	public void show() {
		showInfo();
		System.out.println("数学成绩为"+Getmath());
		System.out.println("英语成绩为"+Getenglish());
		System.out.println("java成绩为"+Getjava());
		System.out.println("平均成绩为"+avg());
	}
	public static void main(String[] args) {
		Student student1=new Student();
		student1.SetPerson("张三", "1234");
		student1.inputGrade();
		student1.show();
	}

}
  1. 定义并实现一个Circle类,属性为圆的半径radius,其值由构造函数初始化。包含计算周长(calPerimeter)与计算面积(calArea),显示信息(半径、周长与面积)(showInfo)等方法。从Circle类派生出Cylinder类,拥有高(height)这个属性,其值由构造函数初始化。包含计算表面积(calArea)、计算体积(calVolume)及显示信息(半径、表面积、体积)(showInfo)等方法。编写一段程序,测试这两个类。
    代码:
package lei;
public class Circle {
	private double radius;

	/*
	 * 无参构造
	 */
	public Circle() {
		super();
		// TODO Auto-generated constructor stub
	}

	/*
	 * 含参构造
	 */
	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);
	}

}
package lei;
public class Cylinder extends Circle {
	private double height;
	Circle cir = new Circle(3.0);
	
	
	/*
	 * 无参构造
	 */
	public Cylinder() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	/*
	 * 含参构造
	 */
	public Cylinder(double height) {
		super();
		this.height = height;
	}

	public double getHeight() {
		return height;
	}

	public void setHeight(double height) {
		this.height = height;
	}
	
	/*
	 * 计算圆柱体表面积的方法
	 * 圆底面积(π*R*R)*2+侧面积,就是底面周长*高(2*π*R*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 static void main(String[] args) {	
			Cylinder cyl = new Cylinder(4.0);		
			cyl.showInfo2();
}
}
  1. 定义并实现如下三个类:(1)Shape类,无属性,有一个抽象方法calArea;(2)Rectangle类,从Shape类派生,有长度(length)与宽度(width)两个属性,需重写calArea方法;(3)Circle类,从Shape类派生,有半径(Radius)一个属性,需重写calArea方法。编写一段程序来测试这几个类。
    代码:
package lei;
public abstract class Shape {
	public abstract double claArea();
}
package lei;
public 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 double Getwidth() {
		return width;
	}
	public void Setlength(double length) {
		this.length=length;
	}
	public void Setwidth(double width) {
		this.width=width;
	}
	public double calArea() {
		return Getlength()*Getwidth();
	}
	@Override
	public double claArea() {
		// TODO 自动生成的方法存根
		return 0;
	}
}
package lei;
public class Circle2 extends Shape {
	private double R;
	public Circle2() {
		super();
	}
	public Circle2(double R) {
		super();
		this.R=R;
	}
	public double GetR() {
		return R;
	}
	public void SetR(double R) {
		this.R=R;
	}
	public double calArea() {
		return Math.PI*(Math.pow(GetR(), 2));
	}
	@Override
	public double claArea() {
		// TODO 自动生成的方法存根
		return 0;
	}
}
package lei;

public class Test {

	public static void main(String[] args) {
		Rectangle R = new Rectangle(3.0,4.0);
		System.out.println("矩形的面积为:"+R.calArea());
		Circle c = new Circle(3.0);
		System.out.println("圆形的面积为:"+c.calArea());
	}

}
  1. 在6的基础上,从Rectangle类派生Cube类,有属性高度(width),有计算表面积(calArea)及计算体积(calVolume)等方法。编写一段程序来测试这几个类。
    代码:
package lei;
public class Cube2 extends Rectangle {
	private double width;
	Rectangle r = new Rectangle(3,4);

	public Cube2(double width) {
		super();
		this.width = width;
	}

	public Cube2() {
		super();
	}

	public Cube2(double length, double width) {
		super(length, width);
	}

	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 static void main(String[] args) {
		Cube2 c = new Cube2(5);
		System.out.println("长方体的面积为" + c.calArea());
		System.out.println("长方体的体积为:" + c.calVolume());
	}

}

运行截图:

四、心得体会:
通过此次实验,了解了封装是一种操作和操作所涉及的数据捆绑在一起,使其免受外界干扰和误用的机制;继承是指一个新的类继承原有类的基本特征,并可增加新的特性,继承具有传递性;继承是一种基于已有类创建新类的机制,利用继承可以先创建一个具有广泛意义的类,然后通过派生创建新类,并添加一些特殊的属性和行为。被继承的类称为父类,其派生得到的类称为子类。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值