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

    实验02 面向对象编程.docx

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

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

    实验02 面向对象编程.docx

    1、实验02 面向对象编程实验二 面向对象编程1实验目的(1)掌握类与对象基本知识;(2)Java中的继承机制及包(package)、接口(interface)等的设计方法;(3)掌握static、this、super等关键字的使用;(4)掌握Java中两种比较器的用法。2实验内容实验题1 定义一个类Book,包含两个属性:一个是private的String类型的属性title、一个是private的float类型的属性listPrice,封装这两个属性的四个方法setTitle()和getTitle()、setListPrice( )和geListPrice()。基本要求:(1) 设计类Book

    2、及类BookManagement,在类BookManagement中输出Book类的对象的两个private属性;(2)重写父类的toString()方法,实现Book类实例的两个属性的输出。package cn.edu.nwsuaf.jp.p2.data;public class Book private String title; private float listPrice; public static int num;public Book(String t, double p) this.title = t; this.listPrice = (float) p; num+;pub

    3、lic String getTitle() return title;public void setTitle(String title) this.title = title;public float getListPrice() return listPrice;public void setListPrice(float listPrice) this.listPrice = listPrice;Overridepublic String toString() return getTitle() + n¥:+ getListPrice();package cn.edu.nwsuaf.jp

    4、.p2;import javax.swing.JOptionPane;import cn.edu.nwsuaf.jp.p2.data.Book;public class BookManagement public static void main(String args) Book book1 = new Book(闲话中国人,28.5); Book book2 = new Book(呼啸山庄,20); /System.out.println(book.toString(); JOptionPane.showMessageDialog(null, Book:n + book1 + n + bo

    5、ok2 + n +There are + Book.num + books.); 实验题2 有两个类:MobileManagement和Mobile,Mobile类的的两个实例分别描述如图3.4所示两部手机名称及价格,类MobileManagement在包cn.edu.nwsuaf.jp.p3中,而Mobile在包cn.edu.nwsuaf.jp.p3.data中。基本要求:设计Mobile类与MobileManagement类,在类MobileManagement类中实例化Mobile类的对象使程序能够显示两部手机的价格和数量,运行结果如图3.5。 E365, 1780 RMB M330,

    6、1450 RMB 图3.4 手机及价格图 图3.5 运行结果package cn.edu.nwsuaf.jp.p3.data;public class Mobile public static int num;private String name;private float price;public Mobile(String name, float price) this.name = name; this.price = price; num+;public String getName() return name;public float getPrice() return price

    7、;package cn.edu.nwsuaf.jp.p3;import javax.swing.JOptionPane;import cn.edu.nwsuaf.jp.p3.data.Mobile;public class MobileManagement public static void main(String args) Mobile mobile1 = new Mobile(E365, 1780);Mobile mobile2 = new Mobile(M330, 1450);JOptionPane.showMessageDialog(null, Mobile phones:nn+

    8、mobile1.getName()+: +mobile1.getPrice()+RMB+ n+ mobile2.getName()+: +mobile2.getPrice()+RMB+ nn+There are +Mobile.num+ mobile phones);实验题3 有四个类,主类Store在包cn.edu.nwsuaf.jp.p4中,Mobile、Mp3Player、Product在包cn.edu.nwsuaf.jp.p4.data中,Mobile、Mp3Player是Product的子类,Product类为抽象类。基本要求: 设计类Mobile和类MP3Player,使它们和类P

    9、roduct、Store组成一个完整的程序,且运行结果如图3.6所示。 图3.6 运行结果package cn.edu.nwsuaf.jp.p4.data;public class Mobile extends Product private String MakePlace; public Mobile(String title, float Price, String makePlace) super(title, Price); this.MakePlace = makePlace; public String getMakePlace() return MakePlace; publi

    10、c void setMakePlace(String makePlace) MakePlace = makePlace; Override public String getName() / TODO Auto-generated method stub return title + on + MakePlace + Mobile, + Price + RMBn; package cn.edu.nwsuaf.jp.p4.data;public class Mp3Player extends Product private long MemorySize; public Mp3Player(St

    11、ring title, float Price, long memorySize) super(title, Price); this.MemorySize = memorySize; public long getMemorySize() return MemorySize; public void setMemorySize(long memorySize) MemorySize = memorySize; public String getName() return title + ( + MemorySize + MB + ), + Price + RMB; package cn.ed

    12、u.nwsuaf.jp.p4.data;public abstract class Product protected String title; protected float Price; protected static int num; public Product(String title, float Price) super(); this.title = title; this.Price = Price; num+; public String getTitle() return title; public void setTitle(String title) this.t

    13、itle = title; public float getPrice() return Price; public void setPrice(float price) Price = price; public static int getNum() return num; public static void setNum(int num) Product.num = num; public abstract String getName(); Override public String toString() return getName(); package cn.edu.nwsua

    14、f.jp.p4;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 public static void main(String args) Mobile mobile1 = new Mobile(E365, 1780, China);Mobile mobile2 = new Mobile(M330, 145

    15、0, China); Mp3Player mp3Player1 = new Mp3Player(Meizo X3, 399 ,256);Mp3Player mp3Player2 = new Mp3Player(Meizo E5, 580 ,512);Mp3Player mp3Player3 = new Mp3Player(Xlive XM MP3 Player, 930 ,256);JOptionPane.showMessageDialog(null, The products are:nn + mp3Player1 +n + mp3Player2 + n + mp3Player3 + n +

    16、 mobile1 + mobile2 +nThere are + Product.getNum() + products.);实验题4 有四个类,主类Store在包cn.edu.nwsuaf.jp.p4中,Mobile、Mp3Player、Product在包cn.edu.nwsuaf.jp.p4.data中,Mobile、Mp3Player是Product的子类, Product类实现Comparable接口,请重写Comparable接口中方法compareTo,实现product对象按照价格排序,运行结果如图3.8所示。 图3.7 运行结果package cn.edu.nwsuaf.jp.

    17、p5.data;public class Mobile extends Product private String bus; public Mobile(String buss,String name, float price) super(name,price); this.bus = buss; public String getName() return name; public float getPrice() return price; public static int getCount() return count; public String toString() retur

    18、n name + on +bus+, +String.valueOf(price)+ RMB; package cn.edu.nwsuaf.jp.p5.data;public class Mp3Player extends Product private int momery; public Mp3Player(String name,int mm, int price) super(name,(float)price); this.momery = mm; public String getName() return name; public float getPrice() return

    19、price; public static int getCount() return count; public String toString() return name + (+momery+ MB+), +String.valueOf(price)+ RMB; package cn.edu.nwsuaf.jp.p5.data;public abstract class Product implements Comparable protected String name; protected float price; protected static int count; protect

    20、ed Product(String name, float price) this.name = name; this.price = price; +count; public String getName() return name; public float getPrice() return price; public static int getCount() return count; public int compareTo(Product product) return new Float(product.getPrice().compareTo(price); package

    21、 cn.edu.nwsuaf.jp.p5;import java.util.Arrays;import javax.swing.JOptionPane;import cn.edu.nwsuaf.jp.p5.data.Mobile;import cn.edu.nwsuaf.jp.p5.data.Mp3Player;import cn.edu.nwsuaf.jp.p5.data.Product;public class Store public static void main(String args) Mobile mobile1 = new Mobile(China Mobile, E365,

    22、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 MP3 Play,256, 930);Product products=mobile1,mobile2,player1,player2, player3;Arrays.sort(p

    23、roducts);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 are:nn+text+nThere are +Product.getCount()+ products.);3实验总结在这次实习中,主要把课本上这段时间学过的知识又重新运用了一下,更深刻地了解了java面向对象的思想,掌握java程序面向对象编程的基本架构,会运用面向对象的思想编写简单的Java程序。在编写代码中也遇到了一些问题,比如,有些的输出结果与要求的不同,调用问题等,最后通过问同学、查资料解决了相关问题。通过这次实习,更清晰的了解了java中类,继承,接口,等的设计方法和理念,能更好的编写代码。


    注意事项

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

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




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

    copyright@ 2008-2022 冰点文档网站版权所有

    经营许可证编号:鄂ICP备2022015515号-1

    收起
    展开