java_SSD3_实验报告_对象和类

第一题

【矩形类Rectangle】遵照9.2节中Circle类的例子,设计一个名为Rectangle的类表示矩形。这个类包括:

  • 两个名为width和height的double型数据域,它们分别表示矩形的宽和高。width和height的默认值都为1。
  • 创建默认矩形的无参构造方法。
  • 创建width和height为指定值的矩形的构造方法。
  • 一个名为getArea()的方法返回这个矩形的面积。
  • 一个名为getPerimeter()的方法返回矩形周长。

画出该类的UML图并实现这个类。编写一个测试程序,创建两个Rectangle对象:一个矩形的宽为4高为40,另一个矩形的宽为3.5高为35.9。依次显示每个矩形的宽、高、面积和周长。

(1)UML图:

在这里插入图片描述

(2)运行结果

在这里插入图片描述

(3)结果分析

两个矩形都是用有参构造函数给宽和高赋值的,面积和周长根据宽与高得出

(4)心得体会

四个题中最基础的一题

(5)源代码

Rectangle类:

package 实验3;

public class Rectangle {
   
	
	public double width=1;
	public double height=1;
	//无参构造函数
	public Rectangle(){
   
		width = 1;
		height = 1;
	}
	//有参构造函数
	public Rectangle(double w,double h){
   
		width = w;
		height = h;
	}
	//面积
	public double getArea(){
   
		return width*height;
	}
	//周长
	public double getPerimeter(){
   
		return 2*(width+height);
	}
}

Program1:

package 实验3;

public class Program1 {
   
	public static void main(String[] args) {
   
		//新建两个Rectangle对象
		Rectangle rect1 = new Rectangle(4,40);
		Rectangle rect2 = new Rectangle(3.2,35.9);
		System.out.println("第一个矩形的宽为:"+rect1.height+"   高为:"+rect1.width+"   面积为:"+
		rect1.getArea()+"   周长为:"+rect1.getPerimeter());
		System.out.println("第二个矩形的宽为:"+rect2.height+"   高为:"+rect2.width+"   面积为:"+
				rect2.getArea()+"   周长为:"+rect2.getPerimeter());
	}
}

第二题

【风扇类Fan】设计一个名为Fan的类表示一个风扇。这个类包括:

  • 三个名为SLOW、MEDIUM和FAST而值为1、2、3的常量表示风扇的速度。
  • 一个名为speed的int类型私有数据域表示风扇的速度(默认值为SLOW)。
  • 一个名为on的boolean类型私有数据域表示风扇是否打开(默认值为false)。
  • 一个名为radius的double类型私有数据域表示风扇的半径(默认值为5)。
  • 一个名为color的String类型数据域表示风扇的颜色(默认值为blue)。
  • 这四个数据域的访问器和修改器。
  • 一个创建默认风扇的无参构造方法。
  • 一个名为toString()的方法返回描述风扇的字符串。如果风扇是打开的,那么该方法在一个组合的字符串中返回风扇的速度、颜色和半径。如果风扇没有打开,该方法返回一个由“fan is off”和风扇颜色、半径组成的字符串。

画出该类的UML图。实现这个类。编写一个测试程序,创建两个Fan对象。将第一个对象设置为最大速度、半径为10、颜色为yellow、状态为打开。将第二个对象设置为中等速度、半径为5、颜色为blue、状态为关闭。通过调用它们的toString方法显示这些对象。

(1)UML图:

在这里插入图片描述

(2)运行结果

在这里插入图片描述

(3)结果分析

对象fan1的变量都是由修改器函数赋值的
对象fan2调用无参构造函数直接给变量赋值

(4)心得体会

此题要学会用set和get函数去访问和处理类的私有变量。难度不大。

(5)源代码

Fan类 :

package 实验3;



public class Fan {
   
	//用数字设定风速档位
	private static final int SLOW = 1;
	
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Multiple-Choice Quiz 1 aaaba aadda 1.Which method must exist in every Java application? (a) main (b) paint (c) begin (d) init 2.Which of the following is the string concatenation operator in Java? (a) + (b) ^ (c) & (d) ++ 3.Which of the following statements is (are) true about the use of an asterisk (*) in a Java import statement? I. It does not incur run-time overhead. II. It can be used to import multiple packages with a single statement. III. It can be used to import multiple classes with a single statement. (a) I and III only (b) III only (c) I only (d) I, II, and III 4.A difference between the methods print and println of the class java.io.PrintWriter is that (a) print inserts a new line at the beginning of its output, but println does not (b) println appends a new line to the end of its output, but print does not (c) println inserts a new line at the beginning of its output, but print does not (d) print appends a new line to the end of its output, but println does not 5.What will be output when the following Java program segment is executed? int x = 5; int y = 2; System.out.println(x + y);//这种运算是顺序进行的,试试System.out.println(x + y + “1”); (a) 7 (b) 5 2 (c) 5+2 (d) 52 6.What is the right way to handle abnormalities in input on Java? (a) By handling these problems by providing exception handlers (b) By writing while loops to guard against bad input (c) By using the class FileFilter which gracefully filters out bad input data (d) By always specifying the throws clause in every method header where file I/O is performed 7.All Java exception classes are derived from the class (a) java.lang.Throwable (b) java.lang.Error (c) java.io.IOException (d) java.lang.RuntimeException
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值