1、河北工业大学java实验三报告实验三 面向对象程序设计实验目的:通过编程和上机实验理解 Java 语言是如何体现面向对象编程基本思想,熟悉类的封装方法以及如何创建类和对象,熟悉成员变量和成员方法的特性,熟悉类的继承性和多态性的作用,熟悉包、接口的使用方法,掌握OOP方式进行程序设计的方法。实验要求:1、编写程序实现类的定义和使用。2、编写不同成员和不同成员方法修饰方法的程序。3、编写体现类的继承性(成员变量、成员方法、成员变量隐藏)的程序和多态性(成员方法重载、构造方法重载)的程序。实验内容:1、下面给出了一个矩形类Rectangle,其中含有多个构造方法。上机编译并运行此程序,观察运行结果,
2、体会其中不同构造方法的设计和使用。 class Rectangle private int width; private int length; Rectangle() length=30; width=20; Rectangle(int l,int w) length=l; width=w; Rectangle(Rectangle r) width=r.getWidth(); length=r.getLength(); int getWidth() return width; int getLength() return length; public class CRctngle publi
3、c static void main(String arg) Rectangle x1=new Rectangle(); Rectangle x2=new Rectangle(50,40); Rectangle x3=new Rectangle(x1); System.out.println(x1.getLength(); System.out.println(x1.getWidth(); System.out.println(x2.getLength(); System.out.println(x2.getWidth(); System.out.println(x3.getLength();
4、 System.out.println(x3.getWidth(); 2、程序功能:通过两个类StaticDemo、TestDemo说明静态变量/方法与实例变量/方法的区别,程序源代码如下。class StaticDemo static int x; int y; public static int getX() return x; public static void setX(int newX) x=newX; public int getY() return y; public void setY(int newY) y=newY; public class TestDemo publi
5、c static void main (String args) StaticDemo a=new StaticDemo(); StaticDemo b=new StaticDemo(); System.out.println(静态变量x=+StaticDemo.getX(); System.out.println(实例变量x=+b.getY(); a.setX(1); a.setY(2); b.setX(3); b.setY(4); System.out.println(静态变量=a.x+StaticDemo.getX(); System.out.println(实例变量=a.y+a.get
6、Y(); System.out.println(静态变量=b.x+StaticDemo.getX(); System.out.println(实例变量=b.y+b.getY(); 二、继承和多态的作用1、编译并运行下面的程序,观察分析运行结果,体会程序中super和this的用法,进一步理解变量隐藏和方法重写的概念。class SuperClss int x; SuperClss() x=10; void doClss() System.out.println(SuperClss.do Class(); class SubClss extends SuperClss int x; SubCls
7、s() super(); x=100; void doClss() System.out.println(subClss.doCLss(); void doDemo() int x; x=1000; super.doClss(); doClss(); System.out.println(super.x=+super.x); System.out.println(this.x=+this.x); System.out.println(x=+x); public class Super public static void main(String arg) SubClss s=new SubCl
8、ss(); s.doDemo(); 2、编译并运行下面的程序,分析运行结果,体会其中方法重载的用法,进一步理解方法重载的概念。 class Father void speak() System.out.println(I am Father!); void speak(String s) System.out.println(I like+s+.);public class OverLoadingDemo public static void main(String args) Father x=new Father(); x.speak(); x.speak(music); 三、接口的定义和
9、使用某个类为接口中的抽象方法书写语句并定义实在的方法体,称为该类实现可这个接口。示例如下:public class InterfaceTest public static void main(String args) double x; Circle y=new Circle(2.0); x=y.calculate_area(); System.out.println(nthe area is:+x+n); interface Cal_area double PI=3.14; double calculate_area();class Circle implements Cal_area do
10、uble r; Circle(double r) this.r=r; public double calculate_area() return PI*r*r; 四、包的定义和使用创建自定义包Mypackage 在包中创建类编写使用包Mypackage中Test_YMD类的程序 运行结果:五、编程题,编写程序并写出运行结果1、创建一个桌子Table类,该类中有桌子名称、重量、桌面宽度、长度及桌子高度属性。其中有:(1)构造函数初始化所有数据成员;(2)Area() :计算桌面的面积;(3)Display(): 在屏幕上输出所有数据成员的值;(4)ChangeWeight(int ):改变桌子重
11、量的函数;(5)在main()中实现创建一个桌子对象,计算桌面的面积,改变桌子重量,并在屏幕上输出所有桌子数据成员的值。class Table public String name; public int weight,X,Y,high; public Table(String nwname,int nwweight,int nwX,int nwY,int nwhigh) this.name=nwname; this.weight=nwweight; this.X=nwX; this.Y=nwY; this.high=nwhigh; public int Area(int X,int Y) i
12、nt Area=X*Y; return Area; public void ChangeWeight(int NewWeight) weight=NewWeight; public void Display() System.out.println(The Tables Name: Name= +name); System.out.println(The Tables Size: X= +X+ ,Y= +Y); System.out.println(The Tables Area: Area= +Area(X,Y); System.out.println(The Tables Weight:
13、Weight= +weight); System.out.println(The Tables High: High= +high); public static void main(String arg) System.out.println(Now Display MyTables Information:); Table tb=new Table(Mytable,170,10,15,75); tb.Display(); System.out.println(Now Change MyTables Weight to 200!); tb.ChangeWeight(200); System.
14、out.println(Now Display MyTables New Information:); tb.Display(); 结果:2、创建一个名称为Pay的类,该类包括工作小时、每小时工资、扣缴率、应得工资总额和实付工资等5个双精度型的成员变量。创建3个重载的应得工资computeNetPay()方法。应得工资是工时乘以每小时工资的计算结果。当computeNetPay()接收代表小时、扣缴率和工资率的数值时,计算出应得工资=工作小时*每小时工资*(1扣缴率)*(1工资率)。当computeNetPay()接收两个参数时,扣缴率假定为15%,计算出应得工资=工作小时*每小时工资*(10
15、.15)*(1工资率)当computeNetPay()接收一个参数时,扣缴率假定为15%,每小时工资率为4.65%。同时编写一个测试类,该测试类的main方法测试所有3个重载的方法。 class Pay double work_hour,price=100,deduct_rate; double should_pay,real_pay=0; public double computeNetPay(double work_hour) should_pay=work_hour*this.price*(1-0.15)*(1-0.0465); return should_pay; public dou
16、ble computeNetPay(double work_hour,double price_rate) should_pay=work_hour*this.price*(1-0.15)*(1-price_rate); return should_pay; public double computeNetPay(double work_hour,double price_rate,double deduct_rate) should_pay=work_hour*this.price*(1-deduct_rate)*(1-price_rate); return should_pay; publ
17、ic void display() System.out.println(The employee should be pay $ +should_pay); public static void main(String args) Pay pay=new Pay(); System.out.println(The first employee worked 8hour); puteNetPay(8.0); pay.display(); System.out.println(The first employee worked 8hour and price_rate 8%); puteNetP
18、ay(8.0,0.08); pay.display(); System.out.println(The first employee worked 8hour and price_rate 10% and deduct_rate is 18%); puteNetPay(8.0,0.1,0.18); pay.display(); 结果:3、商店销售某一件商品,商店每天公布统一的折扣(discount)。同时允许销售人员在销售时灵活掌握价格(price),在统一折扣的基础上,对一次购入10件以上者,还可以销售9.5折优惠。现已知当天5名售货员的销售情况为:售货员编号(num) 销售件数(quant
19、ity) 销售单价(price)101 3 126.8221 8 125.6325 10 124.8108 45 123.4901 100 121.5编写销售商品类Sale和含有main方法的公共类Test,计算当天此商品的总销售额sum,以及每件商品的平均售价,并在显示器上显示。(3)定义接口Shape及其抽象方法getArea()和getPerimeter()用于计算图形和面积和周长。定义类Rectangle(矩形)、类Circle(圆形)、类Triangle(三角形),要求这些类继承点类Coordinates()并实现接口的抽象方法。import java.io.*;class Sale
20、 double discount,price; int sale_sum=0; double sum=0.0,price_avg=0.0; public void change_discount(double discount) this.discount=discount; public void SaleCount(int person_sale_num,double price) if(person_sale_num10) sum+=price*discount*person_sale_num; else sum+=0.95*price*discount*person_sale_num;
21、 sale_sum+=person_sale_num; price_avg=sum/sale_sum; /return sale_sum; public void display() System.out.println(The total sale namber is: +sale_sum); System.out.println(The total sale money is: +sum); System.out.println(The avg of the price is: +price_avg); class Test public static void main(String a
22、rgs)throws IOException Sale sale=new Sale(); System.out.println(Press the discount :); BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in); double disc=Double.parseDouble(stdin.readLine(); sale.change_discount(disc); System.out.println(The saler 101 saled 3 goods at price 126.
23、8;); sale.SaleCount(3,126.8); System.out.println(The saler 221 saled 8 goods at price 125.6;); sale.SaleCount(8,125.6); System.out.println(The saler 325 saled 10 goods at price 124.8;); sale.SaleCount(10,124.8); System.out.println(The saler 108 saled 45 goods at price 123.4;); sale.SaleCount(45,123.4); System.out.println(The saler 901 saled 100 goods at price 121.5;); sale.SaleCount(100,121.5); sale.display(); 结果:
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1