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