ImageVerifierCode 换一换
格式:DOCX , 页数:37 ,大小:663.75KB ,
资源ID:2740927      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bingdoc.com/d-2740927.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(实验三 +五.docx)为本站会员(b****1)主动上传,冰点文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰点文库(发送邮件至service@bingdoc.com或直接QQ联系客服),我们立即给予删除!

实验三 +五.docx

1、实验三 +五实验三 面向对象编程(2)1实验目的(1)掌握Java中的继承机制及包(package)、接口(interface)等的设计方法。(2)掌握static、this、super等关键字的使用。2实验内容实验题1 有图形类的父类Shape,参照圆Circle类补充完整正方性Square和三角形Triangle类,并分析运行结果。class Shape void draw() void erase() class Circle extends Shape void draw() System.out.println(Circle.draw(); void erase() System.o

2、ut.println(Circle.erase();class Square extends Shape void draw() void erase() class Triangle extends Shape void draw() void erase() public class Shapes public static Shape randShape() switch(int)(Math.random() * 3) default: / To quiet the compiler case 0: return new Circle(); case 1: return new Squa

3、re(); case 2: return new Triangle(); public static void main(String args) Shape s = new Shape9; / Fill up the array with shapes: for(int i = 0; i s.length; i+) si = randShape(); / Make polymorphic method calls: for(int i = 0; i s.length; i+) si.draw(); class Shape void draw() System.out.println(Shap

4、e.draw(); void erase() System.out.println(Shape.draw()); 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(); c

5、lass Triangle extends Shape void draw() System.out.println(Rriangle.draw(); void erase() System.out.println(Triangle.erase(); public class Shapes public static Shape randShape() switch (int) (Math.random() * 3) default: / To quiet the compiler case 0: return new Circle(); case 1: return new Square()

6、; case 2: return new Triangle(); public static void main(String args) Shape s = new Shape9; / Fill up the array with shapes: for (int i = 0; i s.length; i+) si = randShape(); / Make polymorphic method calls: for (int i = 0; i s.length; i+) si.draw(); 实验题2 有两个类:MobileManagement和Mobile,分别描述如图3.7所示两部手机

7、名称及价格,类MobileManagement在包cn.edu.nwsuaf.jp.p3中,而Mobile在包cn.edu.nwsuaf.jp.p3.data中。它们代码如下。运行MobileManagement.java,你应该看到如图3.8所示结果。基本要求 在空白处填写相关代码并修改上面程序,使程序能够显示两部手机的价格和数量,运行结果如图3.9 所示。 E365, 1780 RMB M330, 1450 RMB图3.4 手机及价格图图3.5 运行结果图程序Mobile.java源代码: public class Mobile /* Holds the name of the mobil

8、e. */ private String name; /* Holds the price of the mobile. */ private float price; /* Creates a new mobile object. */ public Mobile(String name, float price) this.name = name; this.price = price; /* Gets the name of the mobile. */ public String getName() return name; /* Gets the price of the mobil

9、e. */ public float getPrice() return price; 程序MobileManagement.java源代码: import javax.swing.JOptionPane; 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); Mo

10、bile mobile2 = new Mobile(M330, 1450); / Displays the two mobile phones in a dialog box.JOptionPane.showMessageDialog(null, Mobile phones:nn+mobile1.getName()+n+mobile2.getName(); 图3.6 运行结果图。package cn.edu.nwsuaf.jp.p3.data;public class Mobile public String name; public float price; public Mobile(St

11、ring name, float price) this.name = name; this.price = price; public String getName() return name; public float getPrice() return price; package cn.edu.nwsuaf.jp.p3;import javax.swing.JOptionPane;import cn.edu.nwsuaf.jp.p3.data.Mobile;public class MobileManagement /* Defines the entry point of the a

12、pplication. */ 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()+n+mobile2.

13、getName();JOptionPane.showMessageDialog(null, Mobile phones:nn+mobile1.getName()+mobile1.getPrice()+n+mobile2.getName()+mobile2.getPrice(); 实验题3 有四个类,主类Store在包cn.edu.nwsuaf.jp.p4中,Mobile、Mp3Player、Product在包cn.edu.nwsuaf.jp.p4.data中,Mobile、Mp3Player是Product的子类,Product和Store代码如下:Store.java源代码:package

14、cn.edu.nwsuaf.jp.p4;import java.util.Arrays;import javax.swing.JOptionPane;import cn.edu.nwsuaf.jp.p4.data.Mobile;import cn.edu.nwsuaf.jp.p4.data.Mp3Player;import cn.edu.nwsuaf.jp.p4.data.Product;public class Store /* Defines the entry point of the application. */public static void main(String args)

15、 / Creates two mobile phone objects. Mobile mobile1 = new Mobile(China Mobile, E365,1780); Mobile mobile2 = new Mobile(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 MP

16、3 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.toString()+n; / Displays the two mobile phones in a dialog box. JOptionPane.showMessageDialog(null,The products

17、 are:nn+text+nThere are +Product.getCount() + products.);Product.java源代码:package cn.edu.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 o

18、f products. */ protected static int count; /* Create a new product object. */ protected Product(String name, float price) this.name = 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(

19、) 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); 基本要求 设计类Mobile和类MP3Player,使它们和类Product、Store组成一个完整的程序,且运行结果如图3

20、.10所示。图3.7 运行结果图package cn.edu.nwsuaf.jp.p4.data;public abstract class Product implements Comparable protected String name; protected float price; protected static int count; protected Product(String name, float price) this.name = name; this.price = price; +count; /* Gets the name of the product. */

21、 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(pr

22、oduct.getPrice().compareTo(price); package cn.edu.nwsuaf.jp.p4.data;import cn.edu.nwsuaf.jp.p4.data.Product;public class Mobile extends Product public String type ; public Mobile(String type,String name,float price) super( name,price); this.type=type; public String getType() return this.type; public

23、 String tostring() return type+on+name+,+price+RMB; package cn.edu.nwsuaf.jp.p4.data;public class MP3Player extends Product public int memory; public MP3Player(String name,int memory,float price) super(name,price); this.memory=memory; public int getMemory() return this.memory; public String toString

24、() return name+(+memory+ MB),+price+ RMB; package cn.edu.nwsuaf.jp.p4;import java.util.Arrays;import javax.swing.JOptionPane;import cn.edu.nwsuaf.jp.p4.data.Mobile;import cn.edu.nwsuaf.jp.p4.data.MP3Player;import cn.edu.nwsuaf.jp.p4.data.Product;public class Store / * Defines the entry point of the

25、application. */public static void main(String args) / Creates two mobile phone objects. Mobile mobile1 = new Mobile(China Mobile, E365,1780); Mobile mobile2 = new Mobile(China Mobile, M330,1450); MP3Player player1 = new MP3Player(Meizo X3, 256, 399); MP3Player player2 = new MP3Player(Meizo E5, 512,

26、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.toString()+n; / Displays the two mobile phones in a dialog b

27、ox. JOptionPane.showMessageDialog(null,The products are:nn+text+nThere are +Product.getCount()+ products.);实验题4 用LIST存放对象。利用面向对象的思想,创建以下类:Person类,包含Person的姓名和身份证号码。Student类,首先是一个Person,除此之外,包含学生的语文、数学、英文课的成绩。Teacher类,首先是一个Person,除此之外,包含教师的工资。 请创建 姓名 张三 身份证号 12310000的Person对象。 请创建 姓名 李四 身份证号 12320000

28、 语文: 89 数学: 93 英文: 94的Student对象。 请创建 姓名 王五 身份证号 12330000 工资: 3000的Teacher对象。 将这些对象存放在List中,并打印出List中存放的内容。import java.util.*;public class Mylist public static void main(String args) LinkedList list=new LinkedList(); Person p1=new Person(张三,12310000); Student p2=new Student(李四,12320000,89,93,94); Teacher p3=new Teacher(王五,12330000,3000); list.add(p1); list.add(p2); list.add(p3); for(int i=0;ilist.size();i+) Person a=list.get(i); a.print(); class Person public String Pname; public int Pnum; public Person(String pname,in

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2