实验报告 11
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
System.out.println("坐如钟,站如松,睡如弓");
}
}
class AmericanPeople extends People {
public void speakHello() {
System.out.println("How do you do");
} //重写public void speakHello()方法,输出"How do you do"
americanPeople.averageHeight();
beijingPeople.averageHeight();
chinaPeople.averageWeight();
americanPeople.averageWeight();
beijingPeople.averageWeight();
speed=speed-d;//将成员变量speed与参数d的差赋值给成员变量speed
}
void setPower(int p) {
power=p;//将参数p的值赋值给成员变量power
}
int getPower() {
return power;//返回成员变量power的值
}
double getSpeed() {
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[]) {
return speed;
}
}
class User{
public static void main(String args[]) {
Vehicle car1,car2;
car1=new Vehicle();//使用new 运算符和默认的构造方法创建对象car1
car2=new Vehicle();//使用new 运算符和默认的构造方法创建对象car2
car2.speedUp(80);//car2调用speedUp方法将自己的speed的值增加80
System.out.println("car1目前的速度:"+car1.getSpeed());
System.out.println("car2目前的速度:"+car2.getSpeed());
car1.speedDown(10);
double speed;//声明double型变量speed,刻画速度
int power;//声明int型变量power,刻画功率
void speedUp(int s) {
speed=s+speed;//将参数s的值与成员变量speed的和赋值给成员变量speed
}
void speedDown(int d) {
int year;
double interest;
double interestRate = 0.29;
public double computerInterest() {
interest=year*interestRate*savedMoney;
return interest;
}
public void setInterestRate(double rate) {
father.setSurname("张");// father调用setSurName(String s),并向s传递"张"
System.out.println("父亲:"+father.surname+father.name);
System.out.println("大儿子:"+sonOne.surname+sonOne.name);
3、编写一个创建对象和使用对象的方法的程序;
2、实验内容
1.设计类来描述真实客观世界中的事物,使用类的成员变量来表示事物的属性和状态,使用类的成员方法来提供对成员变量的访问或修改,按以下各个题目的功能完成程序,并给出运行结果
2家族的姓氏
3中国人与美国人
4. 银行与利息
3、过程及结果
1.代码:
class Vehicle {
interestRate = rate;
}
}
class ConstructionBank extends Bank {
double year;
public double computerInterest() {
super.year=(int)year;
double r = year-(int)year;
}
public void averageHeight() {
height=173;
System.out.println("average height:"+height);
}
public void averageWeight() {
weight=70;
System.out.println("average weight:"+weight);
}
}
class ChinaPeople extends People {
public void speakHello() {
System.out.println("您好");
}
public void averageHeight() {
height = 168.78;
System.out.println("中国人的平均身高:"+height+" 厘米");
System.out.println("花脸、青衣、花旦和老生");
}
}
class Example {
public static void main(String args[]) {
ChinaPeople chinaPeople=new ChinaPeople();
AmericanPeople americanPeople=new AmericanPeople();
System.out.println("父亲:"+father.surname+father.name);
System.out.println("大儿子:"+sonOne.surname+sonOne.name);
System.out.prΒιβλιοθήκη Baiduntln("二儿子:"+sonTwo.surname+sonTwo.name);
public void averageWeight() {
weight = 75;
System.out.println("American's average weight:"+weight+" kg");
}
public void americanBoxing() {
System.out.println("直拳、钩拳、组合拳");
}
}
class BeijingPeople extends ChinaPeople {
public void averageHeight() {
height = 172.5;
System.out.println("北京人的平均身高:"+height+" 厘米");
}
//重写public void averageHeight()方法, 输出:"北京人的平均身高:172.5厘米"
FamilyPerson surname=new FamilyPerson();
surname.setSurname("李");//用类名FamilyPerson访问surname,并为surname赋值:"李"
FamilyPerson father,sonOne,sonTwo;
father = new FamilyPerson();
sonOne = new FamilyPerson();
sonTwo = new FamilyPerson();
father.setName("向阳");//father调用setName(String s),并向s传递"向阳"
sonOne.setName("抗日");
sonTwo.setName("抗战");
System.out.println("二儿子:"+sonTwo.surname+sonTwo.name);
}
}
运行结果:
3中国人与美国人
代码:
class People {
protected double weight,height;
public void speakHello() {
System.out.println("yayayaya");
chinaPeople.chinaGongfu();
americanPeople.americanBoxing();
beijingPeople.beijingOpera() ;
beijingPeople.chinaGongfu();
}
}运行结果:
代码:
class Bank {
int savedMoney;
car1.setPower(128);
car2.setPower(76);
System.out.println("car1的功率是:"+car1.getPower());
System.out.println("car2的功率是:"+car2.getPower());
car1.speedUp(80);//car1调用speedUp方法将自己的speed的值增加80
public void averageWeight() {
weight = 70;
System.out.println("北京人的平均体重:"+weight+" 公斤");
}
//重写public void averageWeight()方法,输出:"北京人的平均体重:70公斤"
public void beijingOpera() {
car2.speedDown(20);
System.out.println("car1目前的速度:"+car1.getSpeed());
System.out.println("car2目前的速度:"+car2.getSpeed());
}
}
运行结果:
2家族的姓氏
代码:
class FamilyPerson {
int day=(int)(r*1000);
double yearInterest =super.computerInterest();//super调用隐藏的computerInterest()方法
double dayInterest = day*0.0001*savedMoney;
interest= yearInterest+dayInterest;
BeijingPeople beijingPeople=new BeijingPeople();
chinaPeople.speakHello();
americanPeople.speakHello();
beijingPeople.speakHello();
chinaPeople.averageHeight();
实验题目
实验地点
实验楼409
实验日期
2014-05-07
机器号
10
1、实验目的及要求
通过编程和上机实验掌握程序设计用法,理解Java语言是如何体现面向对象编程基本思想,了解类的封装方法,以及如何创建类和对象,了解成员变量和成员方法的特性,掌握面向对象程序设计的方法。
1、掌握自定义方法及递归算法
2、编写一个体现面向对象思想的程序;
}
public void averageWeight() {
weight = 65;
System.out.println("中国人的体重身高:"+weight+" 公斤");
}//重写public void averageWeight()方法,输出:"中国人的平均体重:65公斤"
public void chinaGongfu() {
public void averageHeight() {
height=176;
System.out.println("American's average height:176 cm"+height);
}//重写public void averageHeight()方法,输出"American's average height:176 cm"
System.out.printf("%d元存在建设银行%d年零%d天的利息:%f元\n",
savedMoney,super.year,day,interest);
return interest;
}
}
class BankOfDalian extends Bank {
}
}
class AmericanPeople extends People {
public void speakHello() {
System.out.println("How do you do");
} //重写public void speakHello()方法,输出"How do you do"
americanPeople.averageHeight();
beijingPeople.averageHeight();
chinaPeople.averageWeight();
americanPeople.averageWeight();
beijingPeople.averageWeight();
speed=speed-d;//将成员变量speed与参数d的差赋值给成员变量speed
}
void setPower(int p) {
power=p;//将参数p的值赋值给成员变量power
}
int getPower() {
return power;//返回成员变量power的值
}
double getSpeed() {
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[]) {
return speed;
}
}
class User{
public static void main(String args[]) {
Vehicle car1,car2;
car1=new Vehicle();//使用new 运算符和默认的构造方法创建对象car1
car2=new Vehicle();//使用new 运算符和默认的构造方法创建对象car2
car2.speedUp(80);//car2调用speedUp方法将自己的speed的值增加80
System.out.println("car1目前的速度:"+car1.getSpeed());
System.out.println("car2目前的速度:"+car2.getSpeed());
car1.speedDown(10);
double speed;//声明double型变量speed,刻画速度
int power;//声明int型变量power,刻画功率
void speedUp(int s) {
speed=s+speed;//将参数s的值与成员变量speed的和赋值给成员变量speed
}
void speedDown(int d) {
int year;
double interest;
double interestRate = 0.29;
public double computerInterest() {
interest=year*interestRate*savedMoney;
return interest;
}
public void setInterestRate(double rate) {
father.setSurname("张");// father调用setSurName(String s),并向s传递"张"
System.out.println("父亲:"+father.surname+father.name);
System.out.println("大儿子:"+sonOne.surname+sonOne.name);
3、编写一个创建对象和使用对象的方法的程序;
2、实验内容
1.设计类来描述真实客观世界中的事物,使用类的成员变量来表示事物的属性和状态,使用类的成员方法来提供对成员变量的访问或修改,按以下各个题目的功能完成程序,并给出运行结果
2家族的姓氏
3中国人与美国人
4. 银行与利息
3、过程及结果
1.代码:
class Vehicle {
interestRate = rate;
}
}
class ConstructionBank extends Bank {
double year;
public double computerInterest() {
super.year=(int)year;
double r = year-(int)year;
}
public void averageHeight() {
height=173;
System.out.println("average height:"+height);
}
public void averageWeight() {
weight=70;
System.out.println("average weight:"+weight);
}
}
class ChinaPeople extends People {
public void speakHello() {
System.out.println("您好");
}
public void averageHeight() {
height = 168.78;
System.out.println("中国人的平均身高:"+height+" 厘米");
System.out.println("花脸、青衣、花旦和老生");
}
}
class Example {
public static void main(String args[]) {
ChinaPeople chinaPeople=new ChinaPeople();
AmericanPeople americanPeople=new AmericanPeople();
System.out.println("父亲:"+father.surname+father.name);
System.out.println("大儿子:"+sonOne.surname+sonOne.name);
System.out.prΒιβλιοθήκη Baiduntln("二儿子:"+sonTwo.surname+sonTwo.name);
public void averageWeight() {
weight = 75;
System.out.println("American's average weight:"+weight+" kg");
}
public void americanBoxing() {
System.out.println("直拳、钩拳、组合拳");
}
}
class BeijingPeople extends ChinaPeople {
public void averageHeight() {
height = 172.5;
System.out.println("北京人的平均身高:"+height+" 厘米");
}
//重写public void averageHeight()方法, 输出:"北京人的平均身高:172.5厘米"
FamilyPerson surname=new FamilyPerson();
surname.setSurname("李");//用类名FamilyPerson访问surname,并为surname赋值:"李"
FamilyPerson father,sonOne,sonTwo;
father = new FamilyPerson();
sonOne = new FamilyPerson();
sonTwo = new FamilyPerson();
father.setName("向阳");//father调用setName(String s),并向s传递"向阳"
sonOne.setName("抗日");
sonTwo.setName("抗战");
System.out.println("二儿子:"+sonTwo.surname+sonTwo.name);
}
}
运行结果:
3中国人与美国人
代码:
class People {
protected double weight,height;
public void speakHello() {
System.out.println("yayayaya");
chinaPeople.chinaGongfu();
americanPeople.americanBoxing();
beijingPeople.beijingOpera() ;
beijingPeople.chinaGongfu();
}
}运行结果:
代码:
class Bank {
int savedMoney;
car1.setPower(128);
car2.setPower(76);
System.out.println("car1的功率是:"+car1.getPower());
System.out.println("car2的功率是:"+car2.getPower());
car1.speedUp(80);//car1调用speedUp方法将自己的speed的值增加80
public void averageWeight() {
weight = 70;
System.out.println("北京人的平均体重:"+weight+" 公斤");
}
//重写public void averageWeight()方法,输出:"北京人的平均体重:70公斤"
public void beijingOpera() {
car2.speedDown(20);
System.out.println("car1目前的速度:"+car1.getSpeed());
System.out.println("car2目前的速度:"+car2.getSpeed());
}
}
运行结果:
2家族的姓氏
代码:
class FamilyPerson {
int day=(int)(r*1000);
double yearInterest =super.computerInterest();//super调用隐藏的computerInterest()方法
double dayInterest = day*0.0001*savedMoney;
interest= yearInterest+dayInterest;
BeijingPeople beijingPeople=new BeijingPeople();
chinaPeople.speakHello();
americanPeople.speakHello();
beijingPeople.speakHello();
chinaPeople.averageHeight();
实验题目
实验地点
实验楼409
实验日期
2014-05-07
机器号
10
1、实验目的及要求
通过编程和上机实验掌握程序设计用法,理解Java语言是如何体现面向对象编程基本思想,了解类的封装方法,以及如何创建类和对象,了解成员变量和成员方法的特性,掌握面向对象程序设计的方法。
1、掌握自定义方法及递归算法
2、编写一个体现面向对象思想的程序;
}
public void averageWeight() {
weight = 65;
System.out.println("中国人的体重身高:"+weight+" 公斤");
}//重写public void averageWeight()方法,输出:"中国人的平均体重:65公斤"
public void chinaGongfu() {
public void averageHeight() {
height=176;
System.out.println("American's average height:176 cm"+height);
}//重写public void averageHeight()方法,输出"American's average height:176 cm"
System.out.printf("%d元存在建设银行%d年零%d天的利息:%f元\n",
savedMoney,super.year,day,interest);
return interest;
}
}
class BankOfDalian extends Bank {