




已阅读5页,还剩21页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
实验2 面向对象编程(Part one)实验目的掌握Java程序面向对象编程的基本架构,会运用面向对象的思想编写Java程序。信息133 魏达 2013013272实验题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(); 基本要求 运行程序并观察运行结果。 思考问题 试述程序中主要语句的作用。运行结果:radius=2.0主要语句的作用: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(); /输出结果实验题2 设计一个用来描述汽车的类Car,使用类的非静态成员变量来表示汽车的车主姓名name、当前的速率speed:1. 创建构造函数为成员变量赋初值。2. 使用类的非静态成员方法来表示改车主姓名changeName操作, 并通过该方法显示修改后的车主名称。3. 使用类的非静态成员方法来表示改变汽车的速率changSpeed操作,并通过该方法显示修改后的车速。4. 使用类的非静态成员方法来表示停车stop操作,并通过该方法显示停车后的车速。5. 创建一个Car类的对象,初始状态时,车主名称是自己的名字,speed=0。6. 分别调用这三个方法,更改车名为朋友的名字,车速为20,停车车速为0,并且得到如下的结果:public class Test2 public static void main(String args) Car car=new Car();car.setName(好友);car.setSpeed(20);System.out.println(当前速度为:+car.getSpeed();System.out.println(车主为:+car.getName();System.out.println(停车后,车速为:+car.stop();car.changName(好人啊);car.changSpeed(50);System.out.println(当前速度为:+car.getSpeed();System.out.println(车主为:+car.getName();System.out.println(停车后,车速为:+car.stop();class CarString name;double speed;public String getName() return name;public void setName(String name) = name;public double getSpeed() return speed;public void setSpeed(double speed) this.speed = speed;public String changName(String name)=name;return ;public double changSpeed(double speed)this.speed=speed;return this.speed;public double stop()speed=0;return speed;实验题3 定义一个类MyProgram,包含两个属性:一个是private的整型属性data、一个是private的String类型的属性str,封装这两个属性的四个方法setData()和getData()、setStr( )和getStr();将这两个属性转变为字符串的方法是toStr()。编写程序,使用MyProgram类,实现数据的访问和修改,并调用toStr()方法显示该类的属性。基本要求 编写完整程序。思考问题 试述程序中各个方法的作用。程序:public class Test3 public static void main(String args) MyProgram program=new MyProgram();program.setData(20);program.setStr(bac); System.out.println(getData():+program.getData(); System.out.println(getStr():+program.getStr();System.out.println(toStr():+program.toStr();class MyProgramprivate int data;private String str;public void setData(int data)this.data=data;public int getData()/System.out.println(+data);return data;public void setStr(String str)this.str=str;public String getStr()return str;public String toStr()String str_one;str_one= str+data;return str_one;程序中各个方法的作用:setData():设置data属性getData():获取data属性setStr():设置str属性getStr():获取str属性toStr():将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();程序:public class Test4 public static void main(String args) BankAccount ba=new BankAccount(888123,1000);ba.savemoney(21000);System.out.println(存入21000元后余额为:+ba.getleftmoney();ba.getmoney(11500);System.out.println(取款11500元后余额为:+ba.getleftmoney();class BankAccount private int leftmoney;private int account;public int getAccount() return account;public int getleftmoney() return leftmoney;public void savemoney(double money) leftmoney += money;public void getmoney(double money) if (money 0) this.width=w; public double getWidth() return width; public void setLength (double h) if(length 0) this.length =h; public double getLength () return length; 程序:Rectangle.javapublic class Rectangle double width,length; public void setWidth(double w) if(w 0) this.width=w; public double getWidth() return width; public void setLength (double h) if(h 0) this.length =h; public double getLength () return length; Cuboid.javapublic class Cuboid Rectangle rect;int height;Cuboid(Rectangle rect,int height)this.rect=rect;this.height=height;public void setBottomWidth(double a)rect.setWidth(a);public double getBottomWidth()return rect.getWidth();public void setBottomLength(double b)rect.setLength(b);public double getBottomLength()return rect.length; public double getVolme() double m; double n; m=rect.getWidth(); n=rect.getLength();return m*n*height; 实验题6 有图形类的父类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 Shape3; / 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(); 程序:package TestFive;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() System.out.println(Squre.draw();void erase() System.out.println(Square.erase();class Triangle extends Shape void draw() System.out.println(Triangle.draw();void erase() System.out.println(Triangle.erase();public class Shapes public static Shape randShape() switch (int) (Math.random() * 3) default: / To quiet the compilercase 0:return new Circle();case 1:return new Square();case 2:return new Triangle();public static void main(String args) Shape s = new Shape3; / 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();实验题7有两个类:MobileManagement和Mobile,分别描述如图所示两部手机名称及价格,类MobileManagement在包.nwsuaf.jp.p3中,而Mobile在包.nwsuaf.jp.p3.data中。它们代码如下。运行MobileManagement.java,你应该看到如图所示结果。基本要求 在空白处填写相关代码并修改上面程序,使程序能够显示两部手机的价格和数量,运行结果如图所示。E365, 1780 RMB M330, 1450 RMB手机及价格图程序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(); 实验题8有四个类,主类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 productsare: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组成一个完整的程序,且运行结果如图所示。程序:package cn.eau.nwsuaf.jp.p4.data;public class Mobile extends ProductString size;public String getSize() return size;public void setSize(String size) this.size = size;Public Mobile(String name,String size,float price)super(name,price);this.size=size;public String toString()return size+on+name+,+price+ RMB;package cn.eau.nwsuaf.jp.p4.data;public class Mp3Player extends Productint RAM;public int getRAM() return RAM;public void setRAM(int rAM) RAM = rAM;public Mp3Player(String name,int RAM,float price)super(name,price);this.RAM=RAM;public String toString()return name+(+RAM+MB),+price+RMB;实验题9实现一个名为Person的类和它的子类Employee, Manager是Employee的子类,设计一个接口Add用于涨工资,接口中有一个抽象方法addVage()。普通员工一次能涨10,经理能涨20。具体要求如下:(1)Person类中的属性有:姓名name(String类型),地址address(String类型)并写出该类的构造方法;(2)Employee类中的属性有:工号ID(String型),工资wage(double类型),工龄(int型),写出该类的构造方法;(3)Manager类中的属性有:级别level(String类型),写出该类的构造方法;编写一个测试类,产生一个员工和一个经理并输出其具有的信息, 之后给这两个人增加工资,然后再次输出信息。程序:package test;public interface Add double addVage();package test;public class Person String name;String address;public String getName() return name;public void setName(String name) = name;public String getAddress() return address;public void setAddress(String address) this.address = address;person (String name,String address)=name;this.address=address;package test;public class Employee extends Person implements Add String ID;double vage;int workAge;public String getID() return ID;public void setID(String ID) this.ID = ID;public double getVage() return vage;public void setVage(double vage) this.vage = vage;public int getWorkAge() return workAge;publ
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 品牌授权使用协议及商品质量保证条款
- 2025厨房设备购买合同格式
- 2025房屋租赁居间服务合同(逐字修订、调整格式、方便使用)
- 财务报销申请单填写规范及审核标准
- 庐山谣诗的主题深度解读教案
- 专业知识护师题目及答案
- 合同管理标准化合同模板及审核工具
- 一本珍贵的书作文500字(10篇)
- 叉车操作业务培训课件
- 产品开发与改进标准化工作流程
- 粤教版六年级科学上册第一单元《光》单元课件
- 兼任宗教活动场所管理组织负责人备案表
- 华中科技大学青年长江学者答辩模板
- 顶储罐施工方案
- 形婚协议书版
- 血液灌流操作流程课件
- 电力系统分析(郝亮亮)
- 查缉酒驾实战培训课件
- 铁路客运规章全套教学课件
- 计算机组成原理-鲲鹏
- 2023年各地中考语文卷名著《昆虫记》阅读题汇集练附答案解析
评论
0/150
提交评论