实验2面向对象编程.doc_第1页
实验2面向对象编程.doc_第2页
实验2面向对象编程.doc_第3页
实验2面向对象编程.doc_第4页
实验2面向对象编程.doc_第5页
已阅读5页,还剩20页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

java课程实习报告信息工程学院Java语言课内实习报告 (201201学年第 二学期) 实习题目:面向对象编程 姓 名: 学 号:专 业:计算机科学与技术 年级班级:一、实习目的掌握Java程序面向对象编程的基本架构,会运用面向对象的思想编写Java程序。二、实习设计过程题目1:阅读如下程序,完成思考题。class CCircle double pi; double radius; double getRadius()/得到所设置的圆的半径 return radius; void setCircle(double r, double p)/初始化圆的半径以及pi pi=p; radius=r; public class Ex2_1 public static void main(String args) CCircle cir1=new CCircle(); /创建一个CCircle类型的对象 cir1.setCircle(2.0,3.1416);/初始化对象cir1 System.out.println(radius=+cir1.getRadius(); /打印出所设置的圆类型的半径在命令行中运行结果如下图所示: 主要语句的作用: double getRadius()/得到所设置的圆的半径 void setCircle(double r, double p)/初始化圆的半径以及pi CCircle cir1=new CCircle(); /创建一个CCircle类型的对象 cir1.setCircle(2.0,3.1416);/初始化对象cir1 System.out.println(radius=+cir1.getRadius(); /打印设置圆类型的半径题目2: 设计一个用来描述汽车的类Car,使用类的非静态成员变量来表示汽车的车主姓名name、当前的速率speed主函数部分: Car car=new Car(123,0,0);/初始化Car对象 car.changSpeed(20);/改变车速为20 car.changeName(456);/改变车主为:456car.stop(0);/停车操作运行结果如下图所示:题目3:定义一个类MyProgram,包含两个属性:一个是private的整型属性data、一个是private的String类型的属性str,封装这两个属性的四个方法setData()和getData()、setStr( )和getStr();将这两个属性转变为字符串的方法是toStr()。编写程序,使用MyProgram类,实现数据的访问和修改,并调用toStr()方法显示该类的属性。主函数部分: MyProgram my=new MyProgram(5,这是开始初始化的数字);/初始化my.setData(10);/设置data的值my.setStr(这是个数字并且是个整数!);/设置的字符串my.getData();/得到datamy.getStr();/得到字符串System.out.println(组合后:+my.toStr(my.getData()+my.getStr();/输出组合后结果运行结果: 题目4:定义一个类实现银行帐户的概念,包括的变量有帐号和存款余额,包括的方法有存款、取款、查询余额和”显示帐号”。定义主类,创建帐户类的对象,并完成相应操作主函数部分: Accout ba=new Accout(888123,1000);/初始化银行账户。账户888123余额1000 ba.savemoney(21000);/存入21000 System.out.println(存入21000元后余额为:+ba.getleftmoney();/得到账户余额,打印 ba.getmoney(11500);/取款11500 System.out.println(取款11500元后余额为:+ba.getleftmoney();/得到账户余额,打印 System.out.println(余额为:+ba.getleftmoney();/得到账户余额,打印 System.out.println(该账户为:+ba.getNameid();/得到账户账户运行结果:题目5:完成课本4.13(102页)jar文件的生成并产生Use类的运行结果。生成的jar文件: Use类的运行结果:题目6:对象的组合。现有一个Rectangle类,请设计一个柱形类cuboid。以现有的Rectangle类为柱形的底,用Rectangle初始化柱形的底,实现对象的组合Cuboid的设计如下:class CuboidRectangle rect;double height;protected double getHeight() return height;protected void setHeight(double height) this.height = height;Cuboid (Rectangle rect,double height)/构造函数this.rect=rect;this.height=height;public double getVolme()/计算柱形的体积return rect.length*rect.width*this.height;public double getBottomWidth()return rect.width;public double getBottomLength()return rect.length;public void setBottomLength(double l)rect.setLength(l);public void setBottomWidth(double d)rect.setWidth(d);运行结果: 题目7:有图形类的父类Shape,参照圆Circle类补充完整正方性Square和三角形Triangle类,并分析运行结果。运行结果1:运行结果2: 运行结果3: 本题中用到了上转型对象,并且利用随机函数生成要构造的形状主函数部分: Shape s = new Shape3; /为Shape型对象分配3个空间for(int i = 0; i s.length; i+)si = randShape(); /随机生成要构造的shape类型 for(int i = 0; i s.length; i+) si.draw(); si.erase(); 题目8:在空白处填写相关代码并修改上面程序,使程序能够显示两部手机的价格和数量主函数部分: Mobile mobile1 = new Mobile(E365, 1780);Mobile mobile2 = new Mobile(M330, 1450);JOptionPane.showMessageDialog(null,Mobilephones:nn+mobile1.getName()+:+mobile1.getPrice()+RMBn+mobile2.getName()+:+mobile2.getPrice()+RMBnn+There are 2 mobile phones. );运行结果如图所示: 题目9:有四个类,主类Store在包.nwsuaf.jp.p4中,Mobile、Mp3Player、Product在包.nwsuaf.jp.p4.data中,Mobile、Mp3Player是Product的子类 主函数部分: Mobile mobile1 = new Mobile( on China Mobile, E365,1780);Mobile mobile2 = new Mobile( on China Mobile, M330,1450);/Mobilel类型的对象的初始化Mp3Player player1 = new Mp3Player(Meizo X3, 256, 399);Mp3Player player2 = new Mp3Player(Meizo E5, 512, 580);Mp3Player player3 = new Mp3Player(Xlive XM MP3 Play,256, 930);/Mp3Player类型的初始化Product products=mobile1,mobile2,player1,player2, player3;/上转型对象Arrays.sort(products);/按照价格排序String text = ;/转化为字符串连接for(int index = 0; index products.length; +index)text += productsindex.print()+n;/ Displays the two mobile phones in a dialog box.JOptionPane.showMessageDialog(null,The products are:nn+text+nThere are +Product.getCount()+ products.); 运行结果如下图所示: 题目10:实现一个名为Person的类和它的子类Employee, Manager是Employee的子类,设计一个接口Add用于涨工资,接口中有一个抽象方法addVage()。普通员工一次能涨10,经理能涨20。主函数部分: Person person=new Person2 ;/创建Personl类型的对象person0=new Employee(zhangsan,bejing,001,1000,2);/上转型对象的初始化,调用Employee中的构造函数person0.print()/调用Employee类中的打印函数person0.addWage();/调用Employee中的涨工资函数person0.print();/调用Employee类中的打印函数person1=new Manager(liuchan,bejing,002,10000,5,D);/上转型对象的初始化,调用Manager中的构造函数person1.print();/调用Manager类中的打印函数person1.addWage();/调用Manager中的涨工资函数person1.print();/调用Manager类中的打印函数 运行结果:三、调试过程中存在问题分析存在问题1:上转型对象初始化,调用子类的构造函数,但是有的函数式子类新添加的,不是继承的。父类不能调用。解决办法:父类中提供抽象的函数方法。存在问题2:不同包之间的继承,有些方法可以继承到,有些方法不可以。解决办法:父类中的方法限定词均设为public。四、心得、体会与建议 在这次实习过程中,对继承的了解更加深了一步,继承在同一个包下,或者在不同包下的继承都可以掌握。Jar文件的生成与导入也可以独立完成。上转型对象的应用可以更好的实现多态。附录:题目1:class CCircle double pi; double radius; double getRadius() return radius; void setCircle(double r, double p) pi=p; radius=r; public class Ex2_1 public static void main(String args) CCircle cir1=new CCircle(); cir1.setCircle(2.0,3.1416); System.out.println(radius=+cir1.getRadius(); 题目2:public class Car String name;int speed;int speed2;/* * * param name开始时车主的名字 * param speed开始时的速度 * param speed2停车时的速度 */Car(String name,int speed,int speed2)=name;this.speed=speed;this.speed2=speed2;/* * * param name 要改成的车主的名字 */void changeName(String name)=name;System.out.println(车主为:+);/* * * param speed 改变后的车速 */void changSpeed(int speed)this.speed=speed;System.out.println(当前车速为:+this.speed);/* * * param speed2停车后车速大小 */void stop(int speed2)this.speed2=speed2;System.out.println(停车后,车速为:+this.speed2);public static void main(String args) / TODO Auto-generated method stub Car car=new Car(123,0,0); car.changSpeed(20); car.changeName(456); car.stop(0);题目3:public class MyProgram private int data;private String str;/* * * param data 用来初始化int类型的data * param str用来初始化string类型的str */MyProgram(int data, String str) this.data = data; this.str = str; /* * * return data的具体值 */public int getData() return data;/* * * param data设置对象的data值 */public void setData(int data) this.data = data;System.out.println(设置的数字是:+this.data);/* * * return str的具体值 */public String getStr() return str;/* * * param str设置的字符串的值 */public void setStr(String str) this.str = str;System.out.println(设置的字符串是:+this.str);/* * * param data将data转化为字符串 * return */public String toStr(int data) String s=data+;return s;public static void main(String args) / TODO Auto-generated method stubMyProgram my=new MyProgram(5,这是开始初始化的数字);my.setData(10);my.setStr(这是个数字并且是个整数!);my.getData();my.getStr();System.out.println(组合后:+my.toStr(my.getData()+my.getStr();题目4:public class Accout String nameid;int leftmoney;/* * * param nameid账户账号 * param leftmoney 余额 */Accout(String nameid, int leftmoney) id = nameid;this.leftmoney = leftmoney;/* * 得到账户账号 * return 账户账号 */public String getNameid() return nameid;/* * 得到账户余额 * return 账户的余额 */public int getleftmoney() return leftmoney;public void savemoney(double money) leftmoney += money; /* * 完成取款功能 * param money取款金额 */public void getmoney(double money) if (money 0)this.width=w;public double getWidth()return width;public void setLength (double h) if(h 0)this.length =h;public double getLength () return length;public static void main(String args) Rectangle r= new Rectangle(); r.setWidth(10.0); r.setLength(10.0); Cuboid c=new Cuboid(r,5.0); System.out.println(柱形的底的长度为:+c.getBottomLength(); System.out.println(柱形的底的宽度为:+c.getBottomWidth(); System.out.println(柱形的高度为:+c.getHeight(); System.out.println(柱形的体积为:+c.getVolme(); c.setBottomLength(20.0); System.out.println(柱形重新设置底的长度为:+c.getBottomLength(); c.setBottomWidth(20.0); System.out.println(柱形重新设置底的宽度为:+c.getBottomWidth();class CuboidRectangle rect;double height;protected double getHeight() return height;protected void setHeight(double height) this.height = height;Cuboid()height=0;Cuboid (Rectangle rect,double height)this.rect=rect;this.height=height;public double getVolme()return rect.length*rect.width*this.height;public double getBottomWidth()return rect.width;public double getBottomLength()return rect.length;public void setBottomLength(double l)rect.setLength(l);public void setBottomWidth(double d)rect.setWidth(d);题目7:public class Shapes public static Shape randShape() switch(int)(Math.random() * 3) default: / To quiet the compilercase 0: return new Circle();case 1: return new Square();case 2: return new Triangle();public static void main(String args) Shape s = new Shape3; for(int i = 0; i s.length; i+)si = randShape(); for(int i = 0; i s.length; i+)si.draw();si.erase();class Shape void draw() void erase() class Circle extends Shape void draw() System.out.println(Circle.draw();void erase() System.out.println(Circle.erase();class Square extends Shape void draw() System.out.println(Square.draw();void erase() System.out.println(Square.erase();class Triangle extends Shape void draw() System.out.println(Triangle.draw();void erase() System.out.println(Triangle.erase();题目8:Mobile.javapackage .nwsuaf.jp.p3.data;public class Mobile /* Holds the name of the mobile. */private String name;/* Holds the price of the mobile. */private float price;/* Creates a new mobile object. */public Mobile(String name, float price) = name;this.price = price;/* Gets the name of the mobile. */public String getName() return name;/* Gets the price of the mobile. */public float getPrice() return price;MobileManagement.javapackage .nwsuaf.jp.p3;import javax.swing.JOptionPane;import .nwsuaf.jp.p3.data.Mobile;public class MobileManagement /* Defines the entry point of the application. */public static void main(String args) / Creates two mobile phone objects.Mobile mobile1 = new Mobile(E365, 1780);Mobile mobile2 = new Mobile(M330, 1450);/ Displays the two mobile phones in a dialog box.JOptionPane.showMessageDialog(null, Mobile phones:nn+mobile1.getName()+: +mobile1.getPrice()+RMBn+mobile2.getName()+: +mobile2.getPrice()+RMBnn+There are 2 mobile phones. ); 题目9:Store.javapackage .nwsuaf.jp.p4;import java.util.Arrays;import javax.swing.JOptionPane;import .nwsuaf.jp.p4.data.Mobile;import .nwsuaf.jp.p4.data.Mp3Player;import .nwsuaf.jp.p4.data.Product;public class Store /* Defines the entry point of the application. */public static void main(String args) / Creates two mobile phone objects.Mobile mobile1 = new Mobile( on China Mobile, E365,1780);Mobile mobile2 = new Mobile( on China Mobile, M330,1450);Mp3Player player1 = new Mp3Player(Meizo X3, 256, 399);Mp3Player player2 = new Mp3Player(Meizo E5, 512, 580);Mp3Player player3 = new Mp3Player(Xlive XM MP3 Play,256, 930);Product products=mobile1,mobile2,player1,player2, player3;Arrays.sort(products);String text = ;for(int index = 0; index products.length; +index)text += productsindex.print()+n;/ Displays the two mobile phones in a dialog box.JOptionPane.showMessageDialog(null,The products are:nn+text+nThere are +Product.getCount()+ products.);Product.javapackage .nwsuaf.jp.p4.data;public abstract class Product implements Comparable /* Holds the name of the product. */protected String name;/* Holds the price of the product. */protected float price;/* Holds the number of products. */protected static int count;/* Create a new product object. */protected Product(String name, float price) = name;this.price = price;+count;/* Gets the name of the product. */public String getName() return name;/* Gets the price of the product. */public float getPrice() return price;/* Gets the number of products. */public static int getCount() return count;/* Compares this product with the given product. */public int compareTo(Product product) return new Float(product.getPrice().compareTo(price);public String print() / TODO Auto-generated method stubreturn null;Mp3Player,javapackage .nwsuaf.jp.p4.data;public class Mp3Player extends Product int i;public Mp3Player(String name, int i,float price) super(name, price);/ TODO Auto-generated constructor stubthis.i=i;public String print()return +(+this.i+MB),+this.price+RMB;Mobile.javapackage .nwsuaf.jp.p4.data;public class Mobile extends Product String string;public Mobile(String name, String string, float pr

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论