




已阅读5页,还剩6页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
信 息 工 程 学 院实验报告的内容与格式按任课教师的要求书写。 Java程序设计 实验/实习报告学院:理学院班级:计算101姓名:洪龙龙学号:2010014549 成绩:A面向对象编程1、 实验/实习过程实验题1:解释下列源程序文件class CCircle /定义一个类double pi; /声明一个double类型的变量表示圆周率 double radius; /声明一个double类型成员表示的半径 double getRadius() /声明了一个友好方法,获取的半径并返回 return radius; void setCircle(double r, double p)/设置圆周率pi和的半径的值 pi=p; /this.pi=p radius=r; /this.radius=r public class Ex2_1 /主类public static void main(String args) /程序的执行入口 CCircle cir1=new CCircle(); /创建了一个类实例 cir1.setCircle(2.0,3.1416); /设置的半径和圆周率分别为2.0和3.1416 System.out.println(radius=+cir1.getRadius(); /打印出的半径值 程序的运行结果:实习题2:设计一个汽车类:源代码:/* Ex2_1.java */class Car String ownerName;/车主姓名 double curSpeed; /当前车速 double curAngle; /车方向盘的当前角度 public void setSpeed(double speed) /设置汽车的速度 curSpeed=speed; public void brake()/停车 curSpeed=0.00; public class Ex2_1public static void main(String args)Car car=new Car();car.setSpeed(60.0);System.out.println(当前车速为:+car.curSpeed);car.brake();System.out.println(刹车后车速为:+car.curSpeed);实习题3:源代码: /* Test.java */class MyProgram private int data;/类中的私有int数据成员 private String str;/类中的私有String数据成员 public void setData(int d)/公有方法,用于设置成员变量data的值 data=d; public void setStr(String s)/公有方法,用于设置str的值 str=s; public int getData()/获取成员data的值 return data; public String getStr()/获取成员str的值 return str; public String toStr()/将str和data分别以字符串的形式打印出来 Integer newData=new Integer(data); return 字符串本身:+str.toString()+t数字转换为字符串后的结果:+newData.toString(); public class Test public static void main(String args) MyProgram program=new MyProgram(); program.setData(315); program.setStr(消费者权益保护日); System.out.println(整数成员:+program.getData(); System.out.println(字符串成员:+program.getStr(); System.out.println(program.toStr(); 程序的运行结果:实习题4:源代码: /* MyCount.java */class Accountprivate double leftMoney;/私有数据成员,类对象必须通过调用get方法获得数据private int number;public Account(double money,int num) /定义了一个带参构造器来初始化类leftMoney=money;number=num;public void saveMoney(double s) /存款,将存入的累积到leftMoney中leftMoney+=s;public double getLeftMoney() /获得当前存款return leftMoney;public void getMoney(double money) /取款if(money=leftMoney)/如果取款小于等于存款,则从存款减去取款,剩余的 leftMoney-=money; /重新计入存款else /反之则不能取款System.out.println(只能取:+leftMoney);public class MyCount public static void main(String args)Account ba=new Account(888123,1000);ba.saveMoney(21000);System.out.println(存入21000元后余额为:+ba.getLeftMoney();ba.getMoney(11500);System.out.println(取出11500元后余额为:+ba.getLeftMoney();程序运行结果:实习题5:源代码: /* Shapes.java */class Shapevoid draw() /基类Shape的draw方法void erase() /基类Shape的erase方法class Circle extends Shape /Circle类直接继承父类Shapevoid draw() /子类Circle对父类Shape的draw方法进行覆盖System.out.println(Circle.draw();void erase()/子类Circle对父类Shape的erase方法进行覆盖System.out.println(Circle.erase();class Square extends Shape/Square类直接继承父类Shapevoid draw()/子类Suqare对父类Shape的draw方法进行覆盖System.out.println(Square.draw();void erase()/子类Square对父类Shape的erase方法进行覆盖System.out.println(Square.erase();class Triangle extends Shape/Triangle类直接继承父类Shapevoid draw()/子类Triangle对父类Shape的draw方法进行覆盖System.out.println(Triangle.draw();void erase()/子类Triangle对父类Shape的erase方法进行覆盖System.out.println(Triangle.erase();public class Shapes public static Shape randShape() /randShape方法返回Shape类的子类对象 switch(int)(Math.random()*3) default: 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; for(int i=0;is.length;i+) si=randShape(); for(int i=0;is.length;i+) si.draw(); 程序的运行结果:程序解释:该java程序中几个类的泛化关系(不知道在UML类图里面友好方法是否用”+”修饰)如下:Shape类是子类Circle、Square、Triangle类的直接父类.由于在for循环中,si是Shape类型的,但si是各个子类的上转型对象,在main方法中被调用时,由于子类覆盖了父类的draw和erase方法,父类的方法被隐藏,因此调用子类的draw、erase方法,体现了java的运行多态性.运行结果如上截图.实习题6:修改之后的源代码文件:/* Mobile.java */public class Mobile /*Holds the name of mobile*/private String name;/*Holds the price of mobile*/private float price;/*Creates a new mobile object*/*Gets the total number of mobiles*/private static int count=0; /该私有静态成员变量用于记录手机对象的个数public Mobile(String name,float price)=name;this.price=price;count+; /每创建一个对象,个数累积增1./*Gets the name of mobile*/public String getName()return name;/*Gets the price of mobile*/public float getPrice()return price;/*Gets the total number of Mobile objects*/public static int getCount()return count;/* MobileManagement.java */import javax.swing.JOptionPane;public class MobileManagement /*Defines the entry point of the application*/public static void main(String args)/ Create two mobile phone objectsMobile mobile1=new Mobile(E365,1780);Mobile mobile2=new Mobile(M330,1450);/Display the two mobile phones in a dialog box.JOptionPane.showMessageDialog(null,Mobile phones:nn+mobile1.getName()+ +mobile1.getPrice()+ RMB+n+mobile2.getName()+ +mobile2.getPrice()+ RMB+nn+There are +Mobile.getCount()+ mobile phones.);程序的运行结果:实习题7:源程序 /*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 the 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 total number of products.*/ public static int getCount() return count; /*Compares the product with the given product.*/ public int compareTo(Product product) return new Float(product.getPrice().compareTo(price); /*Mobile.java*/package .nwsuaf.jp.p4.data;class Mobile extends Product protected String manufacture; protected Mobile(String m,String n,float p) super(n,p); manufacture=m; public String getManufacture() return manufacture; public String toString() return name+ on+getManufacture()+,+getPrice()+ RMB; /*Mp3Player.java*/package .nwsuaf.jp.p4.data;class Mp3Player extends Product protected int memorySize; protected Mp3Player(String n,int m,float p) super(n,p); memorySize=m; public int getMemorySize() return memorySize; public String toString() return name+(+memorySize+ MB)+,+getPrice()+ RMB; /*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);
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2020-2025年中国胡麻油行业发展前景预测及投资战略研究报告
- 2025年 甘肃公务员考试行测试题A类附答案
- 2025年 滨州阳信县翟王镇城镇公益性岗位招聘考试笔试试题附答案
- 中国货运车辆监管系统行业市场发展现状及投资方向研究报告
- 2025年中国转轴寿命试验机行业市场深度研究及投资战略规划报告
- 2020-2025年中国车载HUD行业投资研究分析及发展前景预测报告
- 中国大型停车篷项目投资可行性研究报告
- 中国竹席竹椅行业发展前景预测及投资战略咨询报告
- 教学设备采购合同
- 2025-2030年中国板桥行业深度研究分析报告
- 软件工程复习英文
- 钢花管注浆施工方案范本
- 乳房健康知识
- SH/T 1485.4-1995工业用二乙烯苯中特丁基邻苯二酚含量的测定分光光度法
- GB/T 6414-1999铸件尺寸公差与机械加工余量
- GB/T 38807-2020超级奥氏体不锈钢通用技术条件
- 2022年石家庄交通投资发展集团有限责任公司招聘笔试试题及答案解析
- 中国华电集团公司信访事项处理程序
- 特种设备制造内审及管理评审资料汇编经典版
- EDI超纯水系统操作说明书
- 金属监督监理实施细则
评论
0/150
提交评论