java实验报告实验二 面向对象编程(1).doc_第1页
java实验报告实验二 面向对象编程(1).doc_第2页
java实验报告实验二 面向对象编程(1).doc_第3页
java实验报告实验二 面向对象编程(1).doc_第4页
java实验报告实验二 面向对象编程(1).doc_第5页
免费预览已结束,剩余18页可下载查看

下载本文档

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

文档简介

信 息 工 程 学 院实验报告的内容与格式按任课教师的要求书写。 Java程序设计 实习报告实验二 面向对象编程(1)1实验目的掌握Java程序面向对象编程的基本架构,会运用面向对象的思想编写Java程序。2实验内容实验题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(); 基本要求 运行程序并观察运行结果。思考问题 试述程序中主要语句的作用。CCircle cir1=new CCircle(); /新建一个CCircle对象cir1.setCircle(2.0,3.1416); /初始化对象System.out.println(radius=+cir1.getRadius(); /输出对象的属性实验题2 设计一个用来描述汽车的类,使用类的非静态成员变量来表示汽车的车主姓名、当前的速率和当前方向盘的转向角度,使用类的非静态成员方法来表示改变汽车的速率和停车两个操作。主要代码如下:package package2_2;class Car private String user;private int speed;private int angle;public Car(String name,int speed,int angle)user=name;this.speed=speed;this.angle=angle;public void changeSpeed(int speed2)speed=speed2;public void carStop()speed=0;angle=0;System.out.println(车辆处于停车状态);public void display()System.out.println(车主姓名: +user);System.out.println(当前车速: +speed);System.out.println(方向盘角度:+angle);实验题3 定义一个类MyProgram,包含两个属性:一个是private的整型属性data、一个是private的String类型的属性str,封装这两个属性的四个方法setData()和getData()、setStr( )和getStr();将这两个属性转变为字符串的方法是toStr()。编写程序,使用MyProgram类,实现数据的访问和修改,并调用toStr()方法显示该类的属性。基本要求 编写完整程序。思考问题 试述程序中各个方法的作用。setData()和setStr( ) /为对象的属性赋值getData()和getStr() /得到对象属性的值主要代码如下:package package2_3;public class MyProgram private int data;private String str;public void setData(int data)this.data=data;public void setStr(String str)this.str=str;public int getData()return data;public String getStr()return str;public String toStr()return String.valueOf(data)+str;实验题4定义一个类实现银行帐户的概念,包括的变量有帐号和存款余额,包括的方法有存款、取款、查询余额和”显示帐号”。定义主类,创建帐户类的对象,并完成相应操作。提示:关键代码如下:public int getleftmoney()return leftmoney;public void savemoney(double money)leftmoney+=money;public void getmoney(double money)if(money=leftmoney)leftmoney-=money;elseSystem.out.println(只能取:+leftmoney);bankaccount ba=new bankaccount(888123,1000);ba.savemoney(21000);System.out.println(存入21000元后余额为:+ba.getleftmoney();ba.getmoney(11500);System.out.println(11500元后余额为:+ba.getleftmoney(); 实验题5 定义链表类,实现单链表基本操作。提示:关键代码如下:public class Node public int data; Node next; public Node(int data) this.data = data; this.next=null; public void NodeDisplay() System.out.println (+data+); public class Link private Node first; public Link () this.first=null; public boolean isEmpty() public void insertHeadNode(int data) public Node deleteHeadNode() public void findNode(int k) public void displayLink () public static void main (String args) 主要代码如下:package package2_5;public class Link private Node first;public Link() this.first = null;public boolean isEmpty() return first = null;public void insertHeadNode(int data) Node m = new Node(data);m.next = first;first = m;first.NodeDisplay();public Node deleteHeadNode() Node m = first;first = first.next;return m;public void findNode(int k) Node m = first;int i = 1;while (m != null) if (i = k) System.out.print(第 + k + 个结点已找到,是:);m.NodeDisplay();m = m.next;i+;public void displayLink() Node m = first;while (m != null) m.NodeDisplay();m = m.next;public static void main(String args) Link temp = new Link();temp.insertHeadNode(1);temp.insertHeadNode(2);temp.insertHeadNode(3);temp.insertHeadNode(4);temp.insertHeadNode(5);temp.findNode(3);temp.deleteHeadNode(); temp.displayLink(); 实验三 面向对象编程(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.out.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 Square(); 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所示两部手机名称及价格,类MobileManagement在包.nwsuaf.jp.p3中,而Mobile在包.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 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.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);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.getName(); 图3.6 运行结果图程序运行结果如下:实验题3 有四个类,主类Store在包.nwsuaf.jp.p4中,Mobile、Mp3Player、Product在包.nwsuaf.jp.p4.data中,Mobile、Mp3Player是Product的子类,Product和Store代码如下:Store.java源代码:package .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(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 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 box.JOptionPane.showMessageDialog(null,The products are:nn+text+nThere are +Product.getCount()+ products.);Product.java源代码:package .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); 基本要求 设计类Mobile和类MP3Player,使它们和类Product、Store组成一个完整的程序,且运行结果如图3.10所示。图3.7 运行结果图程序运行结果如下:主要代码如下:Mobile.Javapackage .nwsuaf.jp.p4.data;public class Mobile extends Product private String bus;/* Create a new product object. */public Mobile(String buss,String name, float price)super(name,price); this.bus = buss;/* 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;public String toString()return name + on +bus+,+String.valueOf(price)+ RMB;/* Compares this product with the given product. */Mp3Player.Javapackage .nwsuaf.jp.p4.data;public class Mp3Player extends Product private int momery;/* Create a new product object. */public Mp3Player(String name,int mm, int price) super(name,(float)price); this.momery = mm;/* 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;public String toString()return name +momery+),+String.valueOf(price)+ RMB;实验题4 用LIST存放对象。利用面向对象的思想,创建以下类:Person类,包含Person的姓名和身份证号码。Student类,首先是一个Person,除此之外,包含学生的语文、数学、英文课的成绩。Teacher类,首先是一个Person,除此之外,包含教师的工资。 请创建 姓名 张三 身份证号 12310000的Person对象。 请创建 姓名 李四 身份证号 12320000 语文: 89 数学: 93 英文: 94的Student对象。 请创建 姓名 王五 身份证号 12330000 工资: 3000的Teacher对象。 将这些对象存放在List中,并打印出List中存放的内容。程序运行结果如下:主要代码如下:Person.javapackage package3_4;public class Person protected String Name;protected String Id;protected Person(String name1,String Id1)Name = name1;Id=Id1;public void Display()System.out.println(name: +Name);System.out.println(id: +Id);Student.javapackage package3_4;class Student extends Person private int Chinese;private int Math;private int English;protected Student(String name,String id,int c,int m,int e)super(name,id);Chinese = c;Math = m;English = e;public void Display()System.out.println(name: +Name);System.out.println(id: +Id);System.out.println(Chinese: +Chinese);System.out.println(Math: +Math);System.out.println(English: +English);Teacher.Javapackage package3_4;public class Teacher extends Person private int Salary;protected Teacher(String name,String id,int s)super(name,id);Salary = s;public void Display()System.out.println(name: +Name);System.out.println(id: +Id);System.out.println(Salary: +Salary);实验题5 用SET存放对象。利用面向对象的思想,创建以下类:Person类,包含Person的姓名和身份证号码。 请创建三个对象,放在HashSet中: 姓名 张三 身份证号 12310000 姓名 李四 身份证号 12320000 姓名 王五 身份证号 12330000 再创建对象: 姓名 张三 身份证号 12310000能放入到刚才创建的HashSet中么?答:不能,HashSet中不能存在相同的元素。 把集合中的元素打印出来。 用TreeSet存放上面三个人,要求按照身份证号排序,打印出来。能够把上面步骤中创建的对象放到这个TreeSet中么。答:不能。主要代码如下:package package3_5;import java.util.HashSet;import java.util.Iterator;import java.util.Set;public class Person implements Comparable private String name; private long id; public Person(String name,long id) =name; this.id=id; public long getId() return id; public void setId(long id) this.id = id; public String getName() return name; public void setName(String name

温馨提示

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

评论

0/150

提交评论