《Java面向对象程序设计实验指导代码.doc》由会员分享,可在线阅读,更多相关《Java面向对象程序设计实验指导代码.doc(144页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、JAVA面向对象程序设计实验指导代码内容简介 本书是Java面向对象程序设计的配套实验指导和习题解答。本书的第一部分为16次上机实践的内容,每次上机实践由若干个实验组成。每个实验由相关知识点、实验目的、实验要求、实验模版、实验知道和实验报告组成。在进行实验之前,首先通过实验目的了解实验要完成的关键主题、通过实验要求知道本实验应达到怎样的标准,然后,完成实验模板,填写实验报告。本书的第二部分为主教材的习题参考解答。上机实践1 Java入门实验1 一个简单的应用程序5程序模板Hello.javapublic class Hello public static void main (String a
2、rgs ) 【代码1】 /命令行窗口输出你好,很高兴学习Java A a=new A(); a.fA();class A void fA() 【代码2】 /命令行窗口输出We are students实验2 教室、老师和学生程序模板请按模板要求,将【代码】替换为Java程序代码。ClassRoom.javapublic class ClassRoom public static void main (String args ) 【代码1】/命令行窗口输出教学活动从教室开始 Teacher zhang = new Teacher(); Student jiang = new Student();
3、 zhang.introduceSelf(); jiang.introduceSelf(); Teacher.javapublic class Teacher void introduceSelf() 【代码2】 /命令行窗口输出我是张老师Student.javapublic class Student void introduceSelf() 【代码3】/ /命令行窗口输出我是学生,名字是:奖励上机实践2 基本数据类型实验1 输出特殊边傍的汉字程序模板请按模板要求,将【代码】替换为Java程序代码。InputChinese.javapublic class E public static v
4、oid main (String args ) char ch=研,zifu=0; int p=22920,count=5,position=0; System.out.printf(输出%d个石字傍的汉字:n,count); for(char c=ch;c=ch+count;c+) 【代码1】 /c进行int型转换据运算,并将结果赋值给position System.out.printf(%c(%d),c,position); System.out.printf(n输出%d个女字傍的汉字:n,count); for(int n=p;n=p+count;n+) 【代码2】 / n做char型转
5、换运算,并将结果赋值给zifu System.out.printf(%c(%d),zifu,n);实验2 输入、输出学生的基本信息程序模板请按模板要求,将【代码】替换为Java程序代码。InputMess.javaimport java.util.Scanner;public class InputMess public static void main(String args) Scanner reader=new Scanner(System.in); System.out.println(输入姓名(回车确认):);String name=【代码1】 /从键盘为name赋值 System.
6、out.println(输入年龄(回车确认):); byte age=【代码2】 /从键盘为age赋值 System.out.println(输入身高(回车确认):); float height=【代码3】 /从键盘为height赋值 System.out.printf(%28sn,-基本信息-); System.out.printf(%10s%-10s,姓名:,name); System.out.printf(%4s%-4d,年龄:,age); System.out.printf(%4s%-4.2f,身高:,height);实验3 超大整数的加法程序模板 仔细阅读模板代码,完成实验后的练习。
7、HandleLargeNumber.javapublic class HandleLargeNumber public static void main(String args) int a = 0,9,9,7,9,4,5,6,7,2,3,4,5,6,4,7,8,9,8,7,6,9; int b = 0,0,5,9,1,6,4,5,6,2,3,4,5,7,2,1,3,4,0,3,2,9; int c= new inta.length; int i = 0,result =0,k=0; for(i=0;ia.length;i+) if(ai!=0) k=i; break; for(i=k;ia.
8、length;i+) System.out.printf(%d,ai); System.out.printf(n加上:n); for(i=0;ib.length;i+) if(bi!=0) k=i; break; for(i=k;i=0;i-) result = ai+bi; if(result=10) ci = result%10; ai-1 = ai-1+1; else ci = result; System.out.printf(n等于:n); for(i=0;ic.length;i+) if(ci!=0) k=i; break; for(i=k;i=0) 【代码4】 /将speed-d
9、赋值给speed else speed = 0; void setBulletAmount(int m) bulletAmount = m; int getBulletAmount() return bulletAmount; double getSpeed() return speed; void fire() if(bulletAmount=1) 【代码5】 /将bulletAmount-1赋值给bulletAmount System.out.println(打出一发炮弹); else System.out.println(没有炮弹了,无法开火);Fight.javapublic clas
10、s Fight public static void main(String args) Tank tank1,tank2; tank1 = new Tank(); tank2 = new Tank(); tank1.setBulletAmount(10); tank2.setBulletAmount(10); System.out.println(tank1的炮弹数量:+tank1.getBulletAmount(); System.out.println(tank2的炮弹数量:+tank2.getBulletAmount(); tank1.speedUp(80); tank2.speedU
11、p(90); System.out.println(tank1目前的速度:+tank1.getSpeed(); System.out.println(tank2目前的速度:+tank2.getSpeed(); tank1.speedDown(15); tank2.speedDown(30); System.out.println(tank1目前的速度:+tank1.getSpeed(); System.out.println(tank2目前的速度:+tank2.getSpeed(); System.out.println(tank1开火:); tank1.fire(); System.out.
12、println(tank2开火:); tank2.fire(); tank2.fire(); System.out.println(tank1的炮弹数量:+tank1.getBulletAmount(); System.out.println(tank2的炮弹数量:+tank2.getBulletAmount(); 实验2 计算机与光盘程序模板请按模板要求,将【代码】替换为Java程序代码。CD.javapublic class CD int size; int content; public void setSize(int size) this.size = size; content =
13、 new intsize; public int getSize() return size; public int getContent() return content; public void setContent(int b) int min=Math.min(content.length,b.length); for(int i=0;imin;i+) contenti = bi;Computer.javapublic class Computer int data; CD includeCD; public void putCD(CD cd) includeCD = cd; int
14、size=includeCD.getSize(); data=new intsize; void copyToComputer() int b = includeCD.getContent(); int min=Math.min(data.length,b.length); for(int i=0;imin;i+) datai = bi; public void addData(int m) for(int i=0;idata.length;i+) datai = datai+m; void copyToCD() includeCD.setContent(data); void showDat
15、a() for(int i=0;idata.length;i+) System.out.printf(%3d,datai); User.javapublic class User public static void main(String args) CD dataCD = new CD(); int b =1,2,3,4,5,6,7,8; dataCD.setSize(b.length); dataCD.setContent(b); int a=dataCD.getContent(); System.out.println(dataCD上的内容:); for(int i=0;ia.leng
16、th;i+) System.out.printf(%3d,ai); Computer computerIMB = new Computer(); 【代码1】/computerIMB调用putCD(CD cd)方法,将dataCD的引用传递给cd System.out.println(n将dataCD的数据复制到计算机:computerIMB.); 【代码2】/computerIMB调用copyToComputer()方法 System.out.println(computerIMB上的内容:); computerIMB.showData(); int m=12; System.out.prin
17、tln(ncomputerIMB将每个数据增加+m); computerIMB.addData(m); System.out.println(computerIMB将增值后的数据复制到CD:dataCD); 【代码3】/computerIMB调用copyToCD()方法 System.out.println(dataCD上的内容:); a=dataCD.getContent(); for(int i=0;ia.length;i+) System.out.printf(%3d,ai); 实验3 家族的姓氏程序模板请按模板要求,将【代码】替换为Java程序代码。FamilyPerson.javap
18、ublic class FamilyPerson static String surname; String name; public static void setSurname(String s) surname = s; public void setName(String s) name = s; MainClass.javapublic class MainClass public static void main(String args) 【代码1】 /用类名FamilyPerson访问surname,并为surname赋值:李 FamilyPerson father,sonOne
19、,sonTwo; father = new FamilyPerson(); sonOne = new FamilyPerson(); sonTwo = new FamilyPerson(); 【代码2】 /father调用setName(String s),并向s传递向阳 sonOne.setName(抗日); sonTwo.setName(抗战); System.out.println(父亲:+father.surname+father.name); System.out.println(大儿子:+sonOne.surname+sonOne.name); System.out.println
20、(二儿子:+sonTwo.surname+sonTwo.name); 【代码3】/ father调用setSurName(String s),并向s传递张 System.out.println(父亲:+father.surname+father.name); System.out.println(大儿子:+sonOne.surname+sonOne.name); System.out.println(二儿子:+sonTwo.surname+sonTwo.name); 上机实践5 继承与接口实验1 中国人与美国人程序模板请按模板要求,将【代码】替换为Java程序代码。People.javapub
21、lic 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.javapublic class C
22、hinaPeople extends People public void speakHello() System.out.println(您好); public void averageHeight() height = 168.78; System.out.println(中国人的平均身高:+height+ 厘米); 【代码1】 /重写public void averageWeight()方法,输出:中国人的平均体重:65公斤 public void chinaGongfu() System.out.println(坐如钟,站如松,睡如弓);AmericanPeople.javapubli
23、c class AmericanPeople extends People 【代码2】 /重写public void speakHello()方法,输出How do you do 【代码3】 /重写public void averageHeight()方法,输出Americans average height:176 cm public void averageWeight() weight = 75; System.out.println(Americans average weight:+weight+ kg); public void americanBoxing() System.ou
24、t.println(直拳、钩拳、组合拳);BeijingPeople.javapublic class BeijingPeople extends ChinaPeople 【代码4】 /重写public void averageHeight()方法, 输出:北京人的平均身高:172.5厘米 【代码5】 /重写public void averageWeight()方法,输出:北京人的平均体重:70公斤 public void beijingOpera() System.out.println(花脸、青衣、花旦和老生);Example.javapublic class Example public
25、 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(); americanPeopl
26、e.averageHeight(); beijingPeople.averageHeight(); chinaPeople.averageWeight(); americanPeople.averageWeight(); beijingPeople.averageWeight(); chinaPeople.chinaGongfu(); americanPeople.americanBoxing(); beijingPeople.beijingOpera() ; beijingPeople.chinaGongfu();实验2 银行与利息程序模板请按模板要求,将【代码】替换为Java程序代码。Ba
27、nk.javapublic 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.javapublic class ConstructionBank ext
28、ends 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年零%d天的利息:%f元n, savedMoney,super.year,day,interest); return interest;BankOfDalian.javapublic 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()方法