JAVA实习报告_2and3_第1页
JAVA实习报告_2and3_第2页
JAVA实习报告_2and3_第3页
JAVA实习报告_2and3_第4页
JAVA实习报告_2and3_第5页
已阅读5页,还剩27页未读 继续免费阅读

下载本文档

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

文档简介

信 息 工 程 学 院实验报告的内容与格式按任课教师的要求书写。 Java程序设计 实习报告学院:信息工程学院班级:姓名:学号: 成绩:1实验目的掌握Java程序面向对象编程的基本架构,会运用面向对象的思想编写Java程序。2.实验过程熟悉JAVA的编程环境实验题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(); / 输出圆半径 程序运行结果如下:实验题2 设计一个用来描述汽车的类,使用类的非静态成员变量来表示汽车的车主姓名、当前的速率和当前方向盘的转向角度,使用类的非静态成员方法来表示改变汽车的速率和停车两个操作。程序源代码如下:public class car String name;double speed;double angle;void car()=user;this.speed=0;this.angle=0;public String getname()return name;public void setuser(String name)=name;public void setSeppd(double speed)this.speed=speed;public double getspeed()return speed;public void setangle(double angle)this.speed=angle;public double getangle()return angle;public void stopcar()this.setangle(0);this.setSeppd(0);/* * param args */public static void main(String args) / TODO Auto-generated method stubcar A=new car();A.setuser(zhangyong);A.setSeppd(150);A.setangle(35);System.out.println(before stop car:);System.out.println(this cars belong to:+A.getname();System.out.println(now the speed is:+A.getspeed();System.out.println(now the car angle is+A.getangle();A.stopcar();System.out.println(stop car);System.out.println(this cars belong to:+A.getname();System.out.println(now the speed is:+A.getspeed();System.out.println(now the car angle is+A.getangle();运行结果:实验题3 定义一个类MyProgram,包含两个属性:一个是private的整型属性data、一个是private的String类型的属性str,封装这两个属性的四个方法setData()和getData()、setStr( )和getStr();将这两个属性转变为字符串的方法是toStr()。编写程序,使用MyProgram类,实现数据的访问和修改,并调用toStr()方法显示该类的属性。程序源代码如下:public class Myprogrem 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 this.data; public String getStr() return this.Str; public String toString(int d) return String.valueOf(d); public static void main(String args) / TODO Auto-generated method stub Myprogrem M=new Myprogrem(); System.out.println(before change the values:); M.SetData(15); M.SetStr(zhangyong); System.out.println(M.data); System.out.println(M.Str); System.out.println(change the values:); M.SetData(20); M.SetStr(asdf); System.out.println(M.data); System.out.println(M.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 bankaccount String banknumber;double accountbalance;void bankaccoutn(String b)banknumber=b;accountbalance=5000;public String getaccountnumber()return this.banknumber;public double getaccountbalance()return this.accountbalance;public double getleftmoney()return accountbalance;public void savemoney(double money)accountbalance+=money;public void getmoney(double money)if(money=accountbalance)accountbalance-=money;elseSystem.out.println(只能取:+accountbalance);public bankaccount() / TODO Auto-generated constructor stub/* * param args */public static void main(String args) / TODO Auto-generated method stub bankaccount B=new bankaccount(); B.bankaccoutn(799524545253321346501); System.out.println(当前账号为:+B.getaccountnumber(); System.out.println(当前账号余额为:+B.getaccountbalance(); B.savemoney(21000); System.out.println(存入21000元后余额为:+B.getleftmoney(); B.getmoney(11500); System.out.println(11500元后余额为:+B.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 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) 源代码如下:public class link private Node first;public link() / TODO Auto-generated constructor stubthis.first=null;public boolean isEmpty()if(this.first=null)return true;elsereturn false; public void insertHeadNode(int data)Node N=new Node(0);N.data=data;N.next=this.first.next;this.first.next=N; public Node deleteHeadNode() if(!isEmpty()this.first=this.first.next;return this.first.next;elseSystem.out.println(this link is enpty!);return null; public void findNode(int k)Node N=new Node(0);N=first.next;int i=0;while(N!=null)if(N.data=k)System.out.println(k+在节点中的位置为:+(i+1);break;N=N.next;+i;if(N=null)System.out.println(无此数!); public void displayLink ()if(this.first.next!=null)Node pro=new Node(0);pro=first.next;while(pro!=null)pro.NodeDisplay();pro=pro.next;elseSystem.out.println(link is empty!); public static void main (String args)link link = new link();link.first = new Node(0);System.out.println(没有插入数据:);link.displayLink();System.out.println(插入数据后:);link.insertHeadNode(1);link.insertHeadNode(2);link.insertHeadNode(3);link.insertHeadNode(4);link.insertHeadNode(5);link.displayLink();System.out.println(删除头结点:);link.deleteHeadNode();link.displayLink();System.out.println(查找数据3:);link.findNode(3);System.out.println(查找数据1:);link.findNode(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.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(); 程序源代码如下:Shape类:public class shape void draw()void erase()public shape() / TODO Auto-generated constructor stub/* * param args */public static void main(String args) / TODO Auto-generated method stubSquare类:public class square extends shapevoid draw()System.out.println(square.draw();void erase() System.out.println(suqre.erase();public square() / TODO Auto-generated constructor stub/* * param args */public static void main(String args) / TODO Auto-generated method stubTriangle类:public class Triangle extends shapevoid draw()System.out.println(Triangle.draw(); void erase() System.out.println(Triangle.erase();public Triangle() / TODO Auto-generated constructor stub/* * param args */public static void main(String args) / TODO Auto-generated method stubCircle类:public class circle extends shapevoid draw() System.out.println(Circle.draw();void erase() System.out.println(Circle.erase();public circle() / TODO Auto-generated constructor stub/* * param args */public static void main(String args) / TODO Auto-generated method stubShapes类: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 shape9; for(int i = 0; i s.length; i+) / Fill up the array with shapessi = randShape();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 运行结果图程序源代码如下:.nwsuaf.jp.p3下的MobileManagement类:package .nwsuaf.jp.p3;import javax.swing.JOptionPane;public class MobileManagement /* Defines the entry point of the application. */public static void main(String args) Mobile mobile1 = new Mobile(E365, 1780);Mobile mobile2 = new Mobile(M330, 1450);JOptionPane.showMessageDialog(null, Mobile phones:nn+mobile1.getName()+n+mobile2.getName();.nwsuaf.jp.p3.data下的Mobile类:package .nwsuaf.jp.p3.data;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;运行结果如下:实验题3 有四个类,主类Store在包.nwsuaf.jp.p4中,Mobile、Mp3Player、Product在包.nwsuaf.jp.p4.data中,Mobile、Mp3Player是Product的子类.基本要求 设计类Mobile和类MP3Player,使它们和类Product、Store组成一个完整的程序,且运行结果如图3.10所示。程序源码如下:.nwsuaf.jp.p4.data中Product类:package .nwsuaf.jp.p4.data;public abstract class Prouduct 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 Prouduct() /* 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(Prouduct product) return new Float(product.getPrice().compareTo(price);.nwsuaf.jp.p4.data中Mp3Player类:package .nwsuaf.jp.p4.data;public class MP3player extends Prouduct private String name; private float price; private int memory;public MP3player(String name,int memory,float price) / TODO Auto-generated constructor =name;this.price=price;this.memory=memory;+count;public String getMP3playername()return ;public float getMP3playerprice()return this.price;public int getmemory()return this.memory;public String toString () return +(+this.memory+ MB),+this.price+ RMB;.nwsuaf.jp.p4.data中Mobile类:package .nwsuaf.jp.p4.data;public class Mobile extends Prouductprivate String name;private float price;private String WTO;public Mobile(String WTO,String name,float price) / TODO Auto-generated constructor =name;this.price=price;this.WTO=WTO;+count;public String getName()return ;public float getprice()return this.price;public String getWTO()return this.WTO;public String toString()return + on +this.WTO+, +this.price+ RMB;.nwsuaf.jp.p4中的主类Store:package .nwsuaf.jp.p4;import .nwsuaf.jp.p4.data.*;public class Store extends Prouduct public String toString()return + +this.price+ ;/* 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);Prouduct prouducts=mobile1,mobile2,player1,player2, player3;Arrays.sort(prouducts);String text = ;for(int index = 0; index prouducts.length; +index)text += prouductsindex.toString()+n;/ Displays the two mobile phones in a dialog box.JOptionPane.showMessageDialog(null,The products are:nn+text+nThere are +Prouduct.getCount()+ products.);程序运行结果如下:实验题4 用LIST存放对象。利用面向对象的思想,创建以下类:Person类,包含Person的姓名和身份证号码。Student类,首先是一个Person,除此之外,包含学生的语文、数学、英文课的成绩。Teacher类,首先是一个Person,除此之外,包含教师的工资。 请创建 姓名 张三 身份证号 12310000的Person对象。 请创建 姓名 李四 身份证号 12320000 语文: 89 数学: 93 英文: 94的Student对象。 请创建 姓名 王五 身份证号 12330000 工资: 3000的Teacher对象。 将这些对象存放在List中,并打印出List中存放的内容。程序源代码如下:Person类:public class Person implements Comparable String name;String ID;public Person() / TODO Auto-generated constructor stubpublic void setperson(String name,String ID)=name;this.ID=ID;public String getname()return ;public String getID()return this.ID;public String toString() return 姓名:++ +身份证号:+this.ID;public int hashcode()return .hashCode()+this.ID.hashCode();public boolean equals(Object o)if(this=null)return false;else if(!(o instanceof Person)return false;elsePerson p=(Person)o;if(this.getID().compareTo(p.getID()0)return true;else if(this.getID().compareTo(p.getID()=0&this.getID().compareTo(p.getID()b)return 1;else if(a=b)return 0;elsereturn -1;Student类:public class Student extends Persondouble chinese;double math;double english;public Student(String name,String ID,double chinses,double math,double english) / TODO Auto-generated constructor =name;this.ID=ID;this.chinese=chinses;this.math=math;this.english=english;public double getchinses()return this.chinese;public double getmath()return this.math;public double getenglish()return this.english;public String toString()return 姓名:++ +身份证号:+this.ID+ 语文成绩:+this.chinese+ 英语成绩:+this.english+ 数学成绩:+this.math;Teacher类:public class Teacher extends Persondouble salar;public Teacher(String name, String ID, double salar) / TODO Auto-generated constructor stub =name; this.ID=ID; this.salar=salar;public double getsalar()return this.salar;public String toString()return 姓名:++ +省份证号:+this.ID+ +工资:+this.salar;List类:import java.util.ArrayList;public class List public List() / TODO Auto-gene

温馨提示

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

评论

0/150

提交评论