实验二面向对象编程实验报告材料.docx
《实验二面向对象编程实验报告材料.docx》由会员分享,可在线阅读,更多相关《实验二面向对象编程实验报告材料.docx(18页珍藏版)》请在冰豆网上搜索。
实验二面向对象编程实验报告材料
实验二面向对象编程
1.实验目的
(1)掌握类与对象基本知识;
(2)Java中的继承机制及包(package)、接口(interface)等的设计方法;
(3)掌握static、this、super等关键字的使用;
(4)掌握Java中两种比较器的用法。
2.实验内容
实验题1定义一个类Book,包含两个属性:
一个是private的String类型的属性title、一个是private的float类型的属性listPrice,封装这两个属性的四个方法setTitle()和getTitle()、setListPrice()和geListPrice()。
基本要求:
(1)设计类Book及类BookManagement,在类BookManagement中输出Book类的对象的两个private属性;
(2)重写父类的toString()方法,实现Book类实例的两个属性的输出。
实验结果:
实验过程:
首先建立一个Books工程,再建立俩个类Book和Bookmanagement。
在Book类中,有俩个属性:
一个是private的String类型的属性title、一个是private的float类型的属性listPrice,使用getter和setter生成四个方法setTitle()和getTitle()、setListPrice()和geListPrice()。
最后重写Tostring函数。
在Bookmanagement中有主函数,使用JOptionPane.showMessageDialog(null,book)函数使之用对话框形式输出
代码如下:
packagebook;
publicclassBook{
privateStringtitle;
privatefloatlistPrice;
publicBook(Stringtitle,floatlistPrice){
super();
this.title=title;
this.listPrice=listPrice;
}
publicStringgetTitle(){
returntitle;
}
publicvoidsetTitle(Stringtitle){
this.title=title;
}
publicfloatgetListPrice(){
returnlistPrice;
}
publicvoidsetListPrice(floatlistPrice){
this.listPrice=listPrice;
}
@Override
publicStringtoString(){
return"title:
"+getTitle()+",Price:
"+getListPrice();
}
}
packagebook;
importjavax.swing.JOptionPane;
publicclassBookmanagement
{
/*publicstaticvoidoutput1(Bookbook)
{
System.out.println("请输出book的title:
"+book.getTitle());
System.out.println("请输出book的listPrice:
"+book.getListPrice());
}
publicstaticvoidoutput2(Bookbook)
{
System.out.println(book.toString());
}
*/
publicstaticvoidmain(String[]args){
Bookbook=newBook("HowtoprograminginJava",89.9f);
JOptionPane.showMessageDialog(null,book);
//output1(book);
//output2(book);
}
}
实验题2有两个类:
MobileManagement和Mobile,分别描述如图3.4所示两部手机名称及价格,类MobileManagement在包cn.edu.nwsuaf.jp.p3中,而Mobile在包cn.edu.nwsuaf.jp.p3.data中。
基本要求:
设计相关类,使程序能够显示两部手机的价格和数量,运行结果如图3.5。
E365,1780RMBM330,1450RMB
图3.4手机及价格图
图3.5运行结果
实验结果:
实验过程:
首先建立一个Mobiles工程,再建立俩个类Mobile和MobileManagement。
在Mobile类中,有俩个属性:
一个是private的String类型的属性name、一个是private的float类型的属性price,使用getter和setter生成四个方法setName()和getName()、setPrice()和gePrice()。
最后重写Tostring函数。
在MobileManagement中有主函数,使用JOptionPane.showMessageDialog(null,phone1+"\n"+phone2+"\n"+"Thereare"+count+"mobilephones.")函数使之用对话框形式输出
代码如下:
packagecn.edu.nwsuaf.jp.p3.data;
publicclassMobile{
privateStringname;
privateintprice;
publicMobile(Stringname,intprice){
super();
this.name=name;
this.price=price;
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicintgetPrice(){
returnprice;
}
publicvoidsetPrice(intprice){
this.price=price;
}
@Override
publicStringtoString(){
returngetName()+""+getPrice()+"RMB";
}
}
packagecn.edu.nwsuaf.jp.p3;
importjavax.swing.JOptionPane;
importcn.edu.nwsuaf.jp.p3.data.Mobile;
publicclassMobileManagement
{
staticintcount=0;
publicstaticvoidoutput(Mobilephone)
{
System.out.println(phone.toString());
}
publicstaticvoidmain(String[]args){
Mobilephone1=newMobile("E365",1780);
count++;
Mobilephone2=newMobile("M330",1450);
count++;
System.out.println();
JOptionPane.showMessageDialog(null,phone1+"\n"+phone2+"\n"+"Thereare"+count+"mobilephones.");
}
}
实验题3有四个类,主类Store在包cn.edu.nwsuaf.jp.p4中,Mobile、Mp3Player、Product在包cn.edu.nwsuaf.jp.p4.data中,Mobile、Mp3Player是Product的子类,Product类为抽象类。
基本要求:
设计类Mobile和类MP3Player,使它们和类Product、Store组成一个完整的程序,且运行结果如图3.6所示。
图3.6运行结果
实验结果:
实验过程:
首先在建立一个store的工程,在包cn.edu.nwsuaf.jp.p4.data中建立是那个类Mobile、Mp3Player、Product。
在包cn.edu.nwsuaf.jp.p4中建立类Store。
各个累的结构如下:
再在主函数中实例化对象,在用对话框形式输出。
代码如下:
packagecn.edu.nwsuaf.jp.p4.data;
publicabstractclassProduct{
privateStringName;
privatedoubleprice;
publicProduct(Stringname,doubleprice){
super();
Name=name;
this.price=price;
}
publicStringgetName(){
returnName;
}
publicvoidsetName(Stringname){
Name=name;
}
publicdoublegetPrice(){
returnprice;
}
publicvoidsetPrice(doubleprice){
this.price=price;
}
@Override
publicStringtoString(){
returngetName()+","+getPrice()+"RMB";
}
publicabstractvoidoutput(Productp);
}
packagecn.edu.nwsuaf.jp.p4.data;
publicclassMp3PlayerextendsProduct
{
publicMp3Player(Stringname,doubleprice){
super(name,price);
}
@Override
publicvoidoutput(Productp){
//TODOAuto-generatedmethodstub
System.out.println(Mp3Player.class.toString());
}
}
packagecn.edu.nwsuaf.jp.p4.data;
publicclassMobileextendsProduct
{
publicMobile(Stringname,doubled){
super(name,d);
}
@Override
publicvoidoutput(Productp){
System.out.println(Mobile.class.toString());
}
}
importjavax.swing.JOptionPane;
importcn.edu.nwsuaf.jp.p4.data.Mobile;
importcn.edu.nwsuaf.jp.p4.data.Mp3Player;
importcn.edu.nwsuaf.jp.p4.data.Product;
publicclassStore{
/**
*@paramargs
*/
publicstaticintcount=0;
publicstaticvoidmain(String[]args){
Mp3Playerp1=newMp3Player("MeizoX3(256MB)",399.0);
Mp3Playerp2=newMp3Player("MeizoE5(512MB)",580.0);
Mp3Playerp3=newMp3Player("XliveXMMp3Play(256MB)",930.0);
Mobilem1=newMobile("E365onChinaMobile",1780.0);
Mobilem2=newMobile("E3330onChinaMobile",1450.0);
Product[]products={p1,p2,p3,m1,m2};
Stringtext="";
for(inti=0;i{
text+=products[i]+"\n";
count++;
}
JOptionPane.showMessageDialog(null,"Theproductsare:
\n\n"+text+"\nThereare"+count+"products.");
}
}
实验题4有四个类,主类Store在包cn.edu.nwsuaf.jp.p4中,Mobile、Mp3Player、Product在包cn.edu.nwsuaf.jp.p4.data中,Mobile、Mp3Player是Product的子类,Product类实现Comparable接口,请重写Comparable接口中方法compareTo,实现product对象按照价格排序,运行结果如图3.8所示。
图3.7运行结果
实验结果:
实验过程:
类似于实验3,各个结构如下:
代码如下:
packagecn.edu.nwsuaf.jp.p4.data;
publicabstractclassProductimplementsComparable{
privateStringName;
privateFloatprice;
publicProduct(Stringname,Floatprice){
super();
Name=name;
this.price=price;
}
publicStringgetName(){
returnName;
}
publicvoidsetName(Stringname){
Name=name;
}
publicFloatgetPrice(){
returnprice;
}
publicvoidsetPrice(Floatprice){
this.price=price;
}
@Override
publicStringtoString(){
returngetName()+","+getPrice()+"RMB";
}
publicabstractvoidoutput(Productp);
@Override
publicintcompareTo(Objecto){
//TODOAuto-generatedmethodstub
Productproduct=(Product)o;
returnnewFloat(getPrice()).compareTo(product.getPrice());
}
}
packagecn.edu.nwsuaf.jp.p4.data;
publicclassMobileextendsProduct
{
publicMobile(Stringname,Floatprice){
super(name,price);
}
@Override
publicvoidoutput(Productp){
System.out.println(Mobile.class.toString());
}
}
packagecn.edu.nwsuaf.jp.p4.data;
publicclassMp3PlayerextendsProduct
{
publicMp3Player(Stringname,Floatprice){
super(name,price);
}
@Override
publicvoidoutput(Productp){
System.out.println(Mp3Player.class.toString());
}
}
packagecn.edu.nwsuaf.jp.p4;
importjava.util.Arrays;
importjavax.swing.JOptionPane;
importcn.edu.nwsuaf.jp.p4.data.Mobile;
importcn.edu.nwsuaf.jp.p4.data.Mp3Player;
importcn.edu.nwsuaf.jp.p4.data.Product;
publicclassStore{
/**
*@paramargs
*/
publicstaticintcount=0;
publicstaticvoidmain(String[]args){
Mp3Playerp1=newMp3Player("MeizoX3(256MB)",399.0f);
Mp3Playerp2=newMp3Player("MeizoE5(512MB)",580.0f);
Mp3Playerp3=newMp3Player("XliveXMMp3Play(256MB)",930.0f);
Mobilem1=newMobile("E365onChinaMobile",1780.0f);
Mobilem2=newMobile("E3330onChinaMobile",1450.0f);
Product[]products={p1,p2,p3,m1,m2};
Arrays.sort(products);
Stringtext="";
for(intindex=0;index{
text+=products[index]+"\n";
count++;
}
JOptionPane.showMessageDialog(null,"Theproductsare:
\n\n"+text+"\nThereare"+count+"products.");
}
}
三、实验总结:
1、通过本次实习基本了解如何让使用eclipse编写程序。
2、学会使用对话框输出执行结果。
3、体会到重写toString函数的妙处。