类的设计与封装实验报告
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
浙江大学城市学院实验报告课程名称面向对象程序设计
实验项目名称类的设计与封装实验(一)
学生姓名高宁雯专业班级计算1005 学号31001221
一. 实验目的和要求
1.掌握使用类将对象的属性与功能封装在类中的特点
2.掌握实例方法与构造方法
二. 实验内容
1. 分析Time类的设计
2. 分析并设计Product类
3. 分析并设计Fraction类
4. 设计Rectangle类
5. 设计Student类及其测试类School类
三. 实验结果与分析(可将程序运行结果截屏,也可分析运行结果)
1. 阅读以下关于Time类的代码,分析程序并回答相关的问题
import java.text.DecimalFormat;
public class Time{
private int hour;
private int minute;
private int second;
Time(){hour=0;minute=0;second=0;}
Time(int h){ hour=h;minute=0;second=0;}
Time(int h,int m){hour=h;minute=m;second=0;}
Time(int h,int m,int s){hour=h;minute=m;second=s;}
Time(Time time){
this.hour=time.getHour();
this.minute=time.getMinute();
this.second=time.getSecond();
}
public int getHour(){return hour;}
public int getMinute(){ return minute;}
public int getSecond(){return second;}
public String toUniversalString(){
DecimalFormat twoDigits=new DecimalFormat("00");
return twoDigits.format(getHour())+":"+
twoDigits.format(getMinute())+":"+
twoDigits.format(getSecond());
}
public String toStandardString(){
DecimalFormat twoDigits=new DecimalFormat("00");
return ((getHour()==12|| getHour()==0)? 12:getHour()%12)+":"+、twoDigits.format(getMinute())+":"+twoDigits.format(getSecond())+(getHour()<12?"AM":"PM");
}
}
(1) Time类共有几个构造器(即构造方法),作用有什么不同?
5种
Time(){hour=0;minute=0;second=0;}
无参数构造,初始化hour,minute,second;
Time(int h){ hour=h;minute=0;second=0;}
定义参数h,将其赋值给hour;
Time(int h,int m){hour=h;minute=m;second=0;}
定义参数h和m,将其赋值给hour,minute;
Time(int h,int m,int s){hour=h;minute=m;second=s;}
定义参数h,m和s,将其赋值给hour,minute,second;
(2) getTime(),getMinute(),getSecond()方法的作用是什么?如果去掉这些方法,程序可以完
成同样的功能吗?如何修改?
以实例的方法,在Time(Time time)为hour,minute,second赋值,
去掉后改为Time(Time time){
this.hour=hour;
this.minute=minute;
this.second=second;
}
(3) 如果以下代码段出现在Time类的测试程序的main方法中,输出结果是什么?
Time t1=new Time(5);
System.out.println("The time is "+t1.toStandardString());
输出“The time is 4AM”
(4) 如果以下代码段出现在Time类的测试程序的main方法中,输出结果是什么?
Time t2=new Time(13,59,60);
System.out.println("The time is"+t2.toStandardString());
输出“The time is 1:59:60PM”
(5) 如果以下代码段出现在Time类的测试程序的main方法中,输出结果是什么?
Time t3=new Time(13,30,0);
Time t4=new Time(t3);
System.out.println("The time is"+t4.toUniversalString());
“The time is 1:30:00PM”
2. 确定以下代码段中是否存在错误,如果有错误,指出是逻辑错误还是语法错误,请在程序中用“~~~~~”标出错误的地方,并在第个练习的空白处写下正确的代码。如果代码中没有错误,则在答案中写上“没有错误”。注意:在每个程序段中可能包含多处错误。
(1) 以下代码定义了Product类,它有一个不带参数的构造器,将产品的名称设置为一个空的
String,将price设置为0.00;该类还有一个toProductString方法,它返回一个包含产品的name和price的String
import java.text.*;
public class Product{
private String name;
private double price;
private static DecimalFormat money=new DecimalFormat("$0.00");
public Product(){
name=" ";
price=0.00;
}
public String toProductString(){
return name+" costs "+money.format(price);
}
}//end class Product
答案:
没有错误。
(2) 如下代码定义了Product类的另一个构造器,它有2个参数,并将这些参数赋给相应的实
例变量
public Product(String name, double price){
name= name;
price=price;
}
答案:
= name;
this.price=price;
(3) 如下代码定义了两个set方法,用于设置Product类的name和price
public setName( ){
=name;
}
public setPrice( ){