欢迎来到冰点文库! | 帮助中心 分享价值,成长自我!
冰点文库
全部分类
  • 临时分类>
  • IT计算机>
  • 经管营销>
  • 医药卫生>
  • 自然科学>
  • 农林牧渔>
  • 人文社科>
  • 工程科技>
  • PPT模板>
  • 求职职场>
  • 解决方案>
  • 总结汇报>
  • ImageVerifierCode 换一换
    首页 冰点文库 > 资源分类 > DOCX文档下载
    分享到微信 分享到微博 分享到QQ空间

    实验三 +五.docx

    • 资源ID:2740927       资源大小:663.75KB        全文页数:37页
    • 资源格式: DOCX        下载积分:3金币
    快捷下载 游客一键下载
    账号登录下载
    微信登录下载
    三方登录下载: 微信开放平台登录 QQ登录
    二维码
    微信扫一扫登录
    下载资源需要3金币
    邮箱/手机:
    温馨提示:
    快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。
    如填写123,账号就是123,密码也是123。
    支付方式: 支付宝    微信支付   
    验证码:   换一换

    加入VIP,免费下载
     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    实验三 +五.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


    注意事项

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

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

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

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


    收起
    展开