本科实验报告课程名称:Java语言程序设计实验项目:实验三Java面向对象程序设计实验地点:致远楼B401专业班级:1125学号:2011005217学生姓名:董文杰指导教师:相洁2013年12月5日实验3Java面向对象程序设计1.实验目的掌握Java面向对象程序设计中类、继承、包和接口的概念与应用,能熟练应用方法、类、成员变量等元素。2.实验内容(1)创建一个复数类complex,以附属进行数学运算、复数具有如下格式:RealPart+ImaginaryRart*I,其中,I为-1的平方根。要求如下:①利用浮点变量表示此类的私有数据。提供两个构造方法,一个用于此类声明的对象的初始化;一个为默认的无参构造方法。②提供两复数加、减、乘的运算方法。③按格式(a,b)打印复数。其中a为实部,b为虚部。(2)创建银行账号Account类,实现存款(balance)的存(deposit)、取(withdraw)和查询(getbalance)等功能。(3)编程定义一个接口,实现三个数中求最小值和最大值的方法,并将程序存放在mymaths包中。(4)编程实现以下简单超市管理,具体要求如下:①实现描述超市的类Suppermacket类,具有货架属性,记录系统中现有商品,定义盘点商品的函数check(盘点各类商品的数量,并输出盘点结果),查询商品的函数query,并显示查询结果;②定义商品类Goods,具有商品名称Name,商品价格Price,商品产地Producer等属性,操作Sale(销售商品,余额不足时给予提示)、Add(商品上架操作)和ShowMe(显示商品信息)。③main函数,测试以上所要求的各种功能,即可以根据菜单命令增加、删除、查询商品、盘点商品,以及商品销售和商品上架的操作提示:①超市货架可以使用商品数组,数组大小100表明存放100个商品。②注意实现商品查询的多态性,要求可以根据商品的名称或产地或价格查询商品的数量。3.实验预习要求复习本章所学内容,思考以下问题:(1)什么是对象?什么是类?(2)面向对象编程的特性有哪三个?它们各自有哪些特性?(3)Java语言在面向对象编程方面有哪些独特的特点?(4)抽象类与接口有何异同?4.实验步骤及结果实验过程中,注意记录以下内容:(1)实验过程中实验步骤及其结果。①packagekk;importjava.util.Scanner;publicclasssy{publicstaticvoidmain(String[]srgs){Complexa=newComplex();Complexb=newComplex();a.Complex1();b.Complex1();Complex.Complexadd(a,b);Complex.Complexjian(a,b);Complex.Complexcheng(a,b);Complex.Complexchu(a,b);}}classComplex{publicfloatrealPart;//实部publicfloatimaginPart;//虚部publicvoidComplex1(){Scannerscan=newScanner(System.in);System.out.println(请输入实部:);floatj=scan.nextFloat();this.realPart=j;Scannerscan1=newScanner(System.in);System.out.println(请输入虚部:);floati=scan.nextFloat();this.imaginPart=i;System.out.print(复数为:+realPart+++imaginPart+i+\n);System.out.print(复数为(+realPart+,+imaginPart+)\n);}publicComplex(){this.realPart=0;this.imaginPart=0;}publicstaticvoidComplexadd(Complexa,Complexb){Complexd=newComplex();d.realPart=a.realPart+b.realPart;d.imaginPart=a.imaginPart+b.imaginPart;System.out.println(a+b=:+d.realPart+++d.imaginPart+i+\t);//System.out.print(\ta+b=(+d.realPart+,+d.imaginPart+)\t);}publicstaticvoidComplexjian(Complexa,Complexb){Complexd=newComplex();d.realPart=a.realPart-b.realPart;d.imaginPart=a.imaginPart-b.imaginPart;System.out.println(\ta-b=:+d.realPart+++d.imaginPart+i+\t);//System.out.print(\ta-b=(+d.realPart+,+d.imaginPart+)\t);}publicstaticvoidComplexcheng(Complexa,Complexb){Complexd=newComplex();d.realPart=a.realPart*b.realPart-a.imaginPart*b.imaginPart;d.imaginPart=a.imaginPart*b.realPart+a.realPart*b.imaginPart;System.out.println(\ta*b=:+d.realPart+++d.imaginPart+i+\t);//System.out.print(\ta*b=(+d.realPart+,+d.imaginPart+)\t);}publicstaticvoidComplexchu(Complexa,Complexb){Complexd=newComplex();d.realPart=(a.realPart*b.realPart+a.imaginPart*b.imaginPart)/(b.realPart*b.realPart+b.imaginPart*b.imaginPart);d.imaginPart=(a.imaginPart*b.realPart-a.realPart*b.imaginPart)/(b.realPart*b.realPart+b.imaginPart*b.imaginPart);System.out.println(\ta/b=:+d.realPart+++d.imaginPart+i+\t);//System.out.print(\ta/b=(+d.realPart+,+d.imaginPart+)\t);}}②packagesun;importjava.util.Scanner;publicclassyh{@SuppressWarnings(resource)publicstaticvoidmain(String[]args){System.out.println(欢迎进入银行系统!存钱请按1,取钱请按2,查询余额请按3,退出请按0.);doublei,j;Accounta=newAccount();for(;;){Scannerscan=newScanner(System.in);i=scan.nextDouble();if(i==1){Scannerscan1=newScanner(System.in);System.out.println(请输入存钱金额:);j=scan1.nextDouble();a.deposit(j);}elseif(i==2){Scannerscan1=newScanner(System.in);System.out.println(请输入取钱金额:);j=scan1.nextDouble();if(j=a.count){a.withdraw(j);}else{System.out.println(账户余额不足,请重新输入!);}}elseif(i==3){System.out.println(查询余额为:);System.out.println(a.getbalance());}elseif(i==4){break;}System.out.println(请输入下一次命令:);}}}classAccount{doublecount;publicvoiddeposit(doublea){this.count+=a;}publicvoidwithdraw(doubleb){this.count-=b;}publicdoublegetbalance(){returncount;}}③主函数:packagekj;importjava.io.IOException;publicclassmn{publicstaticvoidmain(String[]args)throwsIOException{iaio=new最大值最小值();io.IO();}}接口:packagekj;importjava.io.IOException;publicinterfaceia{publicvoidIO()throwsIOException;}继承接口的类:packagekj;importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStreamReader;publicclass最大值最小值implementsia{publicvoidIO()throwsIOException{double[]a=newdouble[3];System.out.println(输入三个数字:);BufferedReaderio=newBufferedReader(newInputStreamReader(System.in));for(inti=0;i3;i++){Strings=io.readLine();a[i]=Double.parseDouble(s);//System.out.println(a+2);}for(inti=0;i1;i++){if(a[i]a[i+1]){if(a[i]a[i+2]){System.out.println(最大值为:+a[i]);if(a[i+1]a[i+2]){System.out.println(最小值为:+a[i+2]);}else{System.out.println(最小值为:+a[i+1]);}}else{System.out.println(最大值为:+a[i+2]);System.out.println(最小值为:+a[i+1]);}}else{if(a[i+1]a[i+2]){System.out.println(最大值为:+a[i+1]);if(a[i]a[i+2]){System.out.println(最小值为:+a[i+2]);}else{System.out.println(最小值为:+a[i]);}}else{System.out.println(最大值为:+a[i+2]);System.out.println(最小值为:+a[i]);}}}}}④packagecs;importjava.io.*;importjava.util.InputMismatchException;importjava.util.Scanner;publicclasscs{publicstaticvoidmain(String[]args){inti;goodsg=newgoods();g.add(0);for(intj=1;;j++){System.out.println(欢迎进入超