实验报告 11

更新时间:2024-04-01 17:31:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

实验题目 类与继承 实验楼409 实验日期 2014-05-07 机器号 10 实验地点 一、实验目的及要求 实验目的 通过编程和上机实验掌握程序设计用法,理解Java语言是如何体现面向对象编程基本思想,了解类的封装方法,以及如何创建类和对象,了解成员变量和成员方法的特性,掌握面向对象程序设计的方法。 实验要求 1、 掌握自定义方法及递归算法 2、 编写一个体现面向对象思想的程序; 3、 编写一个创建对象和使用对象的方法的程序; 二、实验内容 实验内容 1. 设计类来描述真实客观世界中的事物,使用类的成员变量来表示事物的属性和状态,使用类的成员方法来提供对成员变量的访问或修改,按以下各个题目的功能完成程序,并给出运行结果 1) 设计一个用来描述交通工具的类(Vehicle),使用类的非静态成员变量来表示交通工具的速度和功率,使用类的非静态成员方法来表示加速、减速、封装功率等操作。并设计一个用户类(User)来完成交通工具的测试。 Vehicle.java文件代码如下 public class Vehicle { 【代码1】//声明double型变量speed,刻画速度 【代码2】//声明int型变量power,刻画功率 void speedUp(int s) { 【代码3】 //将参数s的值与成员变量speed的和赋值给成员变量speed } void speedDown(int d) { 【代码4】 //将成员变量speed与参数d的差赋值给成员变量speed } void setPower(int p) { 【代码5】 //将参数p的值赋值给成员变量power } int getPower() { 【代码6】 //返回成员变量power的值 } double getSpeed() { return speed; } User.java文件代码如下 public class User { public static void main(String args[]) { Vehicle car1,car2; 【代码7】 //使用new 运算符和默认的构造方法创建对象car1 【代码8】 //使用new 运算符和默认的构造方法创建对象car2 car1.setPower(128); car2.setPower(76); System.out.println(\的功率是:\ System.out.println(\的功率是:\ 【代码9】 //car1调用speedUp方法将自己的speed的值增加80 【代码10】 //car2调用speedUp方法将自己的speed的值增加80 System.out.println(\目前的速度:\ System.out.println(\目前的速度:\ car1.speedDown(10); car2.speedDown(20); System.out.println(\目前的速度:\ System.out.println(\目前的速度:\ } } 2家族的姓氏 程序模板 请按模板要求,将【代码】替换为Java程序代码。 FamilyPerson.java public class FamilyPerson { static String surname; String name; public static void setSurname(String s){ surname = s; } public void setName(String s) { name = s; } } MainClass.java public class MainClass { public static void main(String args[]) { 【代码1】 //用类名FamilyPerson访问surname,并为surname赋值:\李\ FamilyPerson father,sonOne,sonTwo; father = new FamilyPerson(); sonOne = new FamilyPerson(); sonTwo = new FamilyPerson(); 【代码2】 //father调用setName(String s),并向s传递\向阳\ sonOne.setName(\抗日\ sonTwo.setName(\抗战\ System.out.println(\父亲:\ System.out.println(\大儿子:\ System.out.println(\二儿子:\ 【代码3】// father调用setSurName(String s),并向s传递\张\ System.out.println(\父亲:\ System.out.println(\大儿子:\ System.out.println(\二儿子:\ } } 3中国人与美国人 程序模板 请按模板要求,将【代码】替换为Java程序代码。 People.java public class People { protected double weight,height; public void speakHello() { System.out.println(\ } public void averageHeight() { height=173; System.out.println(\ } public void averageWeight() { weight=70; System.out.println(\ } } ChinaPeople.java public class ChinaPeople extends People { public void speakHello() { System.out.println(\您好\ } public void averageHeight() { height = 168.78; System.out.println(\中国人的平均身高:\厘米\ } 【代码1】 //重写public void averageWeight()方法,输出:\中国人的平均体重:65公斤\ public void chinaGongfu() { System.out.println(\坐如钟,站如松,睡如弓\ } } AmericanPeople.java public class AmericanPeople extends People { 【代码2】 //重写public void speakHello()方法,输出\ 【代码3】 //重写public void averageHeight()方法,输出\ public void averageWeight() { weight = 75; System.out.println(\ } public void americanBoxing() { System.out.println(\直拳、钩拳、组合拳\ } } BeijingPeople.java public class BeijingPeople extends ChinaPeople { 【代码4】 //重写public void averageHeight()方法, 输出:\北京人的平均身高:172.5厘米\ 【代码5】 //重写public void averageWeight()方法,输出:\北京人的平均体重:70公斤\ public void beijingOpera() { System.out.println(\花脸、青衣、花旦和老生\ } } Example.java public class Example { 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. 银行与利息 程序模板 请按模板要求,将【代码】替换为Java程序代码。 Bank.java 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.java 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 = 【代码1】 //super调用隐藏的computerInterest()方法 double dayInterest = day*0.0001*savedMoney; interest= yearInterest+dayInterest; System.out.printf(\元存在建设银行%d年零%d天的利息:%f元\\n\ savedMoney,super.year,day,interest); return interest; } } BankOfDalian.java 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 = 【代码2】// super调用隐藏的computerInterest()方法 double dayInterest = day*0.00012*savedMoney; interest= yearInterest+dayInterest; System.out.printf(\元存在大连银行%d年零%d天的利息:%f元\\n\ savedMoney,super.year,day,interest); return interest; } } SaveMoney.java 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\ } } 三、过程及结果 实验内容 1. 设计类来描述真实客观世界中的事物,使用类的成员变量来表示事物的属性和状态,使用类的成员方法来提供对成员变量的访问或修改,按以下各个题目的功能完成程序,并给出运行结果 1) 设计一个用来描述交通工具的类(Vehicle),使用类的非静态成员变量来表示交通工具的速度和功率,使用类的非静态成员方法来表示加速、减速、封装功率等操作。并设计一个用户类(User)来完成交通工具的测试。 Vehicle.java文件代码如下 public class Vehicle { 【代码1】//声明double型变量speed,刻画速度 【代码2】//声明int型变量power,刻画功率 void speedUp(int s) { 【代码3】 //将参数s的值与成员变量speed的和赋值给成员变量speed } void speedDown(int d) { 【代码4】 //将成员变量speed与参数d的差赋值给成员变量speed } void setPower(int p) { 【代码5】 //将参数p的值赋值给成员变量power } int getPower() { 【代码6】 //返回成员变量power的值 } double getSpeed() { return speed; } User.java文件代码如下 public class User { public static void main(String args[]) { Vehicle car1,car2; 【代码7】 //使用new 运算符和默认的构造方法创建对象car1 【代码8】 //使用new 运算符和默认的构造方法创建对象car2 car1.setPower(128); car2.setPower(76); System.out.println(\的功率是:\ System.out.println(\的功率是:\ 【代码9】 //car1调用speedUp方法将自己的speed的值增加80 【代码10】 //car2调用speedUp方法将自己的speed的值增加80 System.out.println(\目前的速度:\ System.out.println(\目前的速度:\ car1.speedDown(10); car2.speedDown(20); System.out.println(\目前的速度:\ System.out.println(\目前的速度:\ } } 代码: class Vehicle { double speed;//声明double型变量speed,刻画速度 int power;//声明int型变量power,刻画功率 void speedUp(int s) { speed=s+speed;//将参数s的值与成员变量speed的和赋值给成员变量speed } void speedDown(int d) { speed=speed-d;//将成员变量speed与参数d的差赋值给成员变量speed } void setPower(int p) { power=p;//将参数p的值赋值给成员变量power } int getPower() { return power;//返回成员变量power的值 } double getSpeed() { return speed; } } class User{ public static void main(String args[]) { Vehicle car1,car2; car1=new Vehicle();//使用new 运算符和默认的构造方法创建对象car1 car2=new Vehicle();//使用new 运算符和默认的构造方法创建对象car2 car1.setPower(128); car2.setPower(76); System.out.println(\的功率是:\System.out.println(\的功率是:\car1.speedUp(80);//car1调用speedUp方法将自己的speed的值增加80 car2.speedUp(80);//car2调用speedUp方法将自己的speed的值增加80 System.out.println(\目前的速度:\System.out.println(\目前的速度:\car1.speedDown(10); car2.speedDown(20); System.out.println(\目前的速度:\System.out.println(\目前的速度:\} } 运行结果: 2家族的姓氏 程序模板 请按模板要求,将【代码】替换为Java程序代码。 FamilyPerson.java public class FamilyPerson { static String surname; String name; public static void setSurname(String s){ surname = s; } public void setName(String s) { name = s; } } MainClass.java public class MainClass { public static void main(String args[]) { 【代码1】 //用类名FamilyPerson访问surname,并为surname赋值:\李\ FamilyPerson father,sonOne,sonTwo; father = new FamilyPerson(); sonOne = new FamilyPerson(); sonTwo = new FamilyPerson(); 【代码2】 //father调用setName(String s),并向s传递\向阳\ sonOne.setName(\抗日\ sonTwo.setName(\抗战\ System.out.println(\父亲:\ System.out.println(\大儿子:\ System.out.println(\二儿子:\ 【代码3】// father调用setSurName(String s),并向s传递\张\ System.out.println(\父亲:\ System.out.println(\大儿子:\ System.out.println(\二儿子:\ } } 代码: class FamilyPerson { static String surname; String name; public static void setSurname(String s){ surname = s; } public void setName(String s) { name = s; } } class MainClass { public static void main(String args[]) { FamilyPerson surname=new FamilyPerson(); surname.setSurname(\李\用类名FamilyPerson访问surname,并为surname赋值:\李\ FamilyPerson father,sonOne,sonTwo; father = new FamilyPerson(); sonOne = new FamilyPerson(); sonTwo = new FamilyPerson(); father.setName(\向阳\调用setName(String s),并向s传递\向阳\ sonOne.setName(\抗日\ sonTwo.setName(\抗战\ System.out.println(\父亲:\ System.out.println(\大儿子:\ System.out.println(\二儿子:\ father.setSurname(\张\调用setSurName(String s),并向s传递\张\ System.out.println(\父亲:\ System.out.println(\大儿子:\ System.out.println(\二儿子:\ } } 运行结果: 3中国人与美国人 程序模板 请按模板要求,将【代码】替换为Java程序代码。 People.java public class People { protected double weight,height; public void speakHello() { System.out.println(\ } public void averageHeight() { height=173; System.out.println(\ } public void averageWeight() { weight=70; System.out.println(\ } } ChinaPeople.java

public class ChinaPeople extends People { public void speakHello() { System.out.println(\您好\ } public void averageHeight() { height = 168.78; System.out.println(\中国人的平均身高:\厘米\ } 【代码1】 //重写public void averageWeight()方法,输出:\中国人的平均体重:65公斤\ public void chinaGongfu() { System.out.println(\坐如钟,站如松,睡如弓\ } } AmericanPeople.java public class AmericanPeople extends People { 【代码2】 //重写public void speakHello()方法,输出\ 【代码3】 //重写public void averageHeight()方法,输出\ public void averageWeight() { weight = 75; System.out.println(\ } public void americanBoxing() { System.out.println(\直拳、钩拳、组合拳\ } } BeijingPeople.java public class BeijingPeople extends ChinaPeople { 【代码4】 //重写public void averageHeight()方法, 输出:\北京人的平均身高:172.5厘米\ 【代码5】 //重写public void averageWeight()方法,输出:\北京人的平均体重:70公斤\ public void beijingOpera() { System.out.println(\花脸、青衣、花旦和老生\ } } Example.java public class Example { 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(); } } 代码: class People { protected double weight,height; public void speakHello() { System.out.println(\ } public void averageHeight() { height=173; System.out.println(\ } public void averageWeight() { weight=70; System.out.println(\ } } class ChinaPeople extends People { public void speakHello() { System.out.println(\您好\ } public void averageHeight() { height = 168.78; System.out.println(\中国人的平均身高:\厘米\ } public void averageWeight() { weight = 65; System.out.println(\中国人的体重身高:\公斤\ }//重写public void averageWeight()方法,输出:\中国人的平均体重:65公斤\ public void chinaGongfu() { System.out.println(\坐如钟,站如松,睡如弓\ } } class AmericanPeople extends People { public void speakHello() { System.out.println(\ } //重写public void speakHello()方法,输出\ public void averageHeight() { height=176; System.out.println(\ }//重写public void averageHeight()方法,输出\ public void averageWeight() { weight = 75; System.out.println(\ } public void americanBoxing() { System.out.println(\直拳、钩拳、组合拳\ } } class BeijingPeople extends ChinaPeople { public void averageHeight() { height = 172.5; System.out.println(\北京人的平均身高:\厘米\ } //重写public void averageHeight()方法, 输出:\北京人的平均身高:172.5厘米\ public void averageWeight() { weight = 70; System.out.println(\北京人的平均体重:\公斤\ } //重写public void averageWeight()方法,输出:\北京人的平均体重:70公斤\ public void beijingOpera() { System.out.println(\花脸、青衣、花旦和老生\ } } class Example { 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. 银行与利息 程序模板 请按模板要求,将【代码】替换为Java程序代码。 Bank.java 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.java 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 = 【代码1】 //super调用隐藏的computerInterest()方法 double dayInterest = day*0.0001*savedMoney; interest= yearInterest+dayInterest; System.out.printf(\元存在建设银行%d年零%d天的利息:%f元\\n\ savedMoney,super.year,day,interest); return interest; } } BankOfDalian.java 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 = 【代码2】// super调用隐藏的computerInterest()方法 double dayInterest = day*0.00012*savedMoney; interest= yearInterest+dayInterest; System.out.printf(\元存在大连银行%d年零%d天的利息:%f元\\n\ savedMoney,super.year,day,interest); return interest; } } SaveMoney.java 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\ } } 代码: 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;

} } 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天的利息:%f元\\n\ savedMoney,super.year,day,interest); return interest; } } 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天的利息:%f元\\n\ savedMoney,super.year,day,interest); return interest; } } 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\ } } 运行结果:

本文来源:https://www.bwwdw.com/article/yzir.html

Top