实验1阅读或调试程序
一、实验目的
通过对程序的阅读,了解程序执行的过程,掌握Java语句的用法
二、实验内容
1、说出下列E类中【代码1】~【代码3】的输出结果。
class Fish{
int weight=1;
}
class Lake{
Fish fish;
void setFish(Fish s) {
fish=s;
}
void foodFish(int m) {
fish.weight=fish.weight+m;
}
}
public class E {
public static void main(String args[]) {
Fish redFish=new Fish();
System.out.println(redFish.weight);//【代码1】
Lake lake=new Lake();
lake.setFish(redFish);
lake.foodFish(120);
System.out.println(redFish.weight);//【代码2】
System.out.println(lake.fish.weight);//【代码3】
}
}
2、上机实习下列程序,了解可变参数,得出输出结果
public class E {
public static void main(String args[]) {
f(1,2);
f(-1,-2,-3,-4);
f(9,7,6);
}
public static void f(int...x) {
for(int i=0;i<x.length;i++) {
System.out.println(x[i]);
}
}
}
3、类的字节码进入内存时,类中的静态块会立刻被执行。实习下列程序,了解静态块,并得出输出结果。
class AAA{
static {
System.out.println("我是AAA中的静态块!");
}
}
public class E {
static {
System.out.println("我是最先被执行的静态块!");
}
public static void main(String args[]) {
AAA a=new AAA();
System.out.println("我在了解静态(static)块");
}
}
实验2 家中的电视
一、实验目的
成员变量也可能是引用类型,通过实验让学生掌握对象的组合以及参数传递
二、实验内容
编写一个Java应用程序,模拟家中买了一台电视机,家庭将电视机作为自己的一个成员。具体要求如下:
1、有三个源文件:
TV.java 负责创建电视类TV类
Family.java 负责创建家庭类Famliy类
MainClass.java 测试的主类
2、在主类的main()方法中首先使用TV类创建一个对象haierTV,然后使用Family类创建对象ZhangSanFamily,并将先前的TV类的对象实例haierTV的引用传递给ZhangSanFamily对象的成员变量homeTV.
3、两个类的UML图如下
TV
public class TV {
int channel; //电视频道
void setChannel(int m) {
if(m>=1){
channel=m;
}
}
int getChannel(){
return channel;
}
void showProgram(){
switch(channel) {
case 1 : System.out.println("综合频道");
break;
case 2 : System.out.println("经济频道");
break;
case 3 : System.out.println("文艺频道");
break;
case 4 : System.out.println("国际频道");
break;
case 5 : System.out.println("体育频道");
break;
default : System.out.println("不能收看"+channel+"频道");
}
}
}
Family
public class Family {
TV homeTV;
void buyTV(TV tv) {
homeTV=tv; //将参数tv赋值给homeTV
}
void remoteControl(int m) {
homeTV.setChannel(m);
}
void seeTV() {
homeTV.showProgram(); //homeTV调用showProgram()方法
}
}
MainClass
public class MainClass {
public static void main(String args[]) {
TV haierTV = new TV();
haierTV.setChannel(5); //haierTV调用setChannel(int m),并向参数m传递5
System.out.println("haierTV的频道是"+haierTV.getChannel());
Family zhangSanFamily = new Family();
zhangSanFamily.buyTV(haierTV);//zhangSanFamily调用void buyTV(TV tv)方法,并将haierTV传递给参数TV
System.out.println("zhangSanFamily开始看电视节目");
zhangSanFamily.seeTV();
int m=2;
System.out.println("zhangSanFamily将电视更换到"+m+"频道");
zhangSanFamily.remoteControl(m);
System.out.println("haierTV的频道是"+haierTV.getChannel());
System.out.println("zhangSanFamily在看电视节目");
zhangSanFamily.seeTV();
Family lisiFamily=new Family();
lisiFamily.buyTV(haierTV);
lisiFamily.seeTV();
}
}
4、实验完成后的改进
1)省略【代码2】程序能否通过编译?若能通过编译,程序输出的结果是怎样的?
2)在主类main()方法的最后增添下列代码,并解释运行结果。
Family lisiFamily=new Family();
lisiFamily.buyTV(haierTV);
lisiFamily.seeTV();
实验3 中国人、北京人和美国人
一、实验目的
1.了解子类的继承性
2.掌握子类对象的创建过程
3.成员变量的继承与隐藏
4.掌握方法的继承与重写
二、实验内容
编写程序模拟中国人、美国人是人,北京人是中国人。除主类外,程序中还有4个类:People、ChinaPeople、AmericanPeople和BeijingPeople类。要求如下:
1.People类有权限是protected的double型成员变量height和weight,以及public修饰的speakHello()、averageHeight()和averageWeight()方法。
2.ChinaPeople类是People的子类,新增public修饰的chinaGongfu()方法,重写父类的三个方法
3.AmericanPeople类是People的子类,新增public修饰的americanBoxing()方法,重写父类的三个方法
4.Beijing类是ChinaPeople的子类,新增public修饰的beijingOPera()方法,重写父类继承People的三个方法
5.四个类之间的UML图如下:
People
public class People {
protected double weight,height;
public void speakHello() {
System.out.println("yayayaya");
}
public void averageHeight() {
height=173;
System.out.println("average height:"+height);
}
public void averageWeight() {
weight=70;
System.out.println("average weight:"+weight);
}
}
ChinaPeople
public class ChinaPeople extends People {
public void speakHello() {
System.out.println("您好");
}
public void averageHeight() {
height = 168.78;
System.out.println("中国人的平均身高:"+height+" 厘米");
}
public void averageWeight() {
weight=65;
System.out.println("中国人的平均体重:"+weight+"千克");
}
//重写public void averageWeight()方法,输出:"中国人的平均体重:65千克"
public void chinaGongfu() {
System.out.println("坐如钟,站如松,睡如弓");
}
}
AmericanPeople
public class AmericanPeople extends People {
public void speakHello() {
System.out.println("How do you do");
} //重写public void speakHello()方法,输出"How do you do"
public void averageHeight() {
height=176;
System.out.println("American's average height:"+height+"cm");
} //重写public void averageHeight()方法,输出"American's average height:176 cm"
public void averageWeight() {
weight = 75;
System.out.println("American's average weight:"+weight+" kg");
}
public void americanBoxing() {
System.out.println("直拳、钩拳、组合拳");
}
}
BeijingPeople
public class BeijingPeople extends ChinaPeople {
public void averageHeight() {
height=172.5;
System.out.println("北京人的平均身高:"+height+"厘米");
} //重写public void averageHeight()方法,输出:"北京人的平均身高: 172.5厘米"
public void averageWeight() {
weight=70;
System.out.println("北京人的平均体重:"+weight+"千克");
}//重写public void averageWeight()方法,输出:"北京人的平均体重:70千克"
public void beijingOpera() {
System.out.println("花脸、青衣、花旦和老生");
}
}
TestPeople
public class TestPeople {
public static void main(String args[]) {
ChinaPeople chinaPeople=new ChinaPeople();
AmericanPeople americanPeople=new AmericanPeople();
BeijingPeople beijingPeople=new BeijingPeople();
chinaPeople.speakHello();
americanPeople.speakHello();
beijingPeople.speakHello();
chinaPeople.averageHeight();
americanPeople.averageHeight();
beijingPeople.averageHeight();
chinaPeople.averageWeight();
americanPeople.averageWeight();
beijingPeople.averageWeight();
chinaPeople.chinaGongfu();
americanPeople.americanBoxing();
beijingPeople.beijingOpera() ;
beijingPeople.chinaGongfu();
}
}
实验4 银行计算利息
一、实验目的
让学生掌握重写的目的以及怎样使用super关键字
二、实验内容
假设银行Bank已经有了按整年year计算利息的一般方法,其中year只能取正整数。
建设银行ConstructionBank是Bank的子类,准备隐藏继承的成员变量year,并重写计算利息的方法,即自己声明一个double型的year变量,比如当year取值是5.216时,表示要计算5年零216天的利息,但希望首先按银行Bank的computerInstrest()计算5整年的利息,然后自己计算216天的利息。那么,建设银行就必须把5.216的整数部分赋值给隐藏的year,并让super调用隐藏的、按整年计算利息的方法。
要求ConstructionBank和BankOfDalian类是Bank类的子类,ConstructionBank和BankOfDalian都是用super调用隐藏的成员变量和方法。
Bank
public class Bank {
int savedMoney;
int year;
double interest;
double interestRate = 0.29;
public double computerInterest() {
interest=year*interestRate*savedMoney;
return interest;
}
public void setInterestRate(double rate) {
interestRate = rate;
}
}
ConstructionBank
public class ConstructionBank extends Bank {
double year;
public double computerInterest() {
super.year=(int)year;
double r = year-(int)year;
int day=(int)(r*1000);
double yearInterest = super.computerInterest(); //super调用隐藏的computerInterest()方法
double dayInterest = day*0.0001*savedMoney;
interest= yearInterest+dayInterest;
System.out.printf("%d元存在建设银行%d年零%d天的利息:%f元\n",
savedMoney,super.year,day,interest);
return interest;
}
}
BankOfDalian
public class BankOfDalian extends Bank {
double year;
public double computerInterest() {
super.year=(int)year;
double r = year-(int)year;
int day=(int)(r*1000);
double yearInterest = super.computerInterest();// super调用隐藏的computerInterest()方法
double dayInterest = day*0.00012*savedMoney;
interest= yearInterest+dayInterest;
System.out.printf("%d元存在大连银行%d年零%d天的利息:%f元\n",
savedMoney,super.year,day,interest);
return interest;
}
}
SaveMoney
public class SaveMoney {
public static void main(String args[]) {
int amount=8000;
ConstructionBank bank1 = new ConstructionBank();
bank1.savedMoney = amount;
bank1.year = 8.236;
bank1.setInterestRate(0.035);
double interest1 = bank1.computerInterest();
BankOfDalian bank2 = new BankOfDalian();
bank2.savedMoney = amount;
bank2.year = 8.236;
bank2.setInterestRate(0.035);
double interest2=bank2.computerInterest();
System.out.printf("两个银行利息相差%f元\n",interest2-interest1);
}
}