




已阅读5页,还剩10页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
实验三 面向对象程序设计计C104 李云霄一、类的定义及使用修饰符 class 类名 extends 父类名 implements 接口列表 / 类体 说明: 1)类修饰符分类: 访问修饰符:缺省(默认方式)、public;非访问修饰符:abstract、final ,不能同时修饰一个类 2) extends : 实现继承3) implements : 实现接口class Rectangle1 private int width; private int length; Rectangle1( int i ,int j ) length=30;width=20; void Rectangle(int l,int w) length=l;width=w; Rectangle1(Rectangle1 r) width=r.getWidth(); length=r.getLength();int getWidth() return width; int getLength() return length; class CRctngle public static void main(String args) Rectangle1 x1=new Rectangle1(0,0); Rectangle1 x2=new Rectangle1(50,40); Rectangle1 x3=new Rectangle1(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(); 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 public static void main(String args)System.out.println(静态变量x=+StaticDemo.getX();System.out.println(实例变量y=+new StaticDemo().getY(); StaticDemo a=new StaticDemo(); StaticDemo b=new StaticDemo(); a.setX(1); a.setY(2); b.setX(3); b.setY(4); System.out.println(静态变量a.x=+a.getX(); System.out.println(实例变量a.y=+a.getY(); System.out.println(静态变量b.x=+b.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; SubClss() 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 SuperDemo public static void main (String args) SubClss s=new SubClss(); 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); 三、接口的定义和使用1、接口的实现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 double r; Circle(double r) this.r=r; public double calculate_area() return PI*r*r; 2、 包的定义和使用(1)在包中创建类import java.util.Calendar;/引用 java.util 包public class SY5_1_YMD private int year,month,day;public static void main(String arg3)public SY5_1_YMD(int y,int m,int d) year = y;month = (m=1) & (m=1) & (d=31) ? d : 1);public SY5_1_YMD() this(0,0,0);public static int thisyear() return Calendar.getInstance().get(Calendar.YEAR);/返回当年的年份public int year() return year;/返回年份public String toString()return year+-+month+-+day;/返回转化为字符串的年-月-日(2)编写使用包Mypackage中Test_YMD类的程序 import Mypackage.SY5_1_YMD; /引用Mypackage包中的SY5_1_YMD类public class SY5_2private String name;private SY5_1_YMD birth;public static void main(String args)SY5_2 a= new SY5_2(李云霄,1991,1,1);a.output();public SY5_2(String n1,SY5_1_YMD d1)name = n1;birth = d1;public SY5_2(String n1,int y,int m,int d)this(n1,new SY5_1_YMD(y,m,d);/初始化变量与对象public int age()/计算年龄return SY5_1_YMD.thisyear() - birth.year(); /返回当前年与出生年的差即年龄public void output()System.out.println(姓名: +name);System.out.println(出生日期: +birth.toString();System.out.println(今年年龄: +age();编程题,编写程序并写出运行结果1、创建一个桌子Table类,该类中有桌子名称、重量、桌面宽度、长度及桌子高度属性。其中有:(1)构造函数初始化所有数据成员;(2)Area() :计算桌面的面积;(3)Display(): 在屏幕上输出所有数据成员的值;(4)ChangeWeight(int ):改变桌子重量的函数;(5)在main()中实现创建一个桌子对象,计算桌面的面积,改变桌子重量,并在屏幕上输出所有桌子数据成员的值。package com.kaylves;public class Table private String tableName;/名称 private int weight; /重量 private int length;/长度 private int height;/高度 private int width; /宽度 public Table() tableName=;public Table(String tableName,int weight,int length,int height,int width)this.tableName=tableName; this.weight=weight; this.length=length; this.height=height; this.width=width;public int getArea() return length*width;public void display() System.out.println(桌名称:+this.tableName);System.out.println(重量:+this.weight);System.out.println(长度:+this.length);System.out.println(高度:+this.height); System.out.println(宽度:+this.width); System.out.println(面积:+this.getArea();public void changeWeight(int w)this.weight=w;public static void main(String args) Table table = new Table(小桌子,20,50,20,20); int area=table.getArea(); table.changeWeight(30); table.display();2、创建一个名称为Pay的类,该类包括工作小时、每小时工资、扣缴率、应得工资总额和实付工资等5个双精度型的成员变量。创建3个重载的应得工资computeNetPay()方法。应得工资是工时乘以每小时工资的计算结果。当computeNetPay()接收代表小时、扣缴率和工资率的数值时,计算出应得工资=工作小时*每小时工资*(1扣缴率)*(1工资率)。当computeNetPay()接收两个参数时,扣缴率假定为15%,计算出应得工资=工作小时*每小时工资*(10.15)*(1工资率)当computeNetPay()接收一个参数时,扣缴率假定为15%,每小时工资率为4.65%。同时编写一个测试类,该测试类的main方法测试所有3个重载的方法。public 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 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;public 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 10% and deduct_rate is 18%); puteNetPay(8.0,0.1,0.18); pay.display();3、商店销售某一件商品,商店每天公布统一的折扣(discount)。同时允许销售人员在销售时灵活掌握价格(price),在统一折扣的基础上,对一次购入10件以上者,还可以销售9.5折优惠。现已知当天5名售货员的销售情况为:售货员编号(num) 销售件数(quantity) 销售单价(price)101 3 126.8221 8 125.6325 10 124.8108 45 123.4901 100 121.5package sale;/我把售货员当作一个个类来继承。 class sale String num; int quantity; double price; public sale(String num, int quantity, double price) this.num=num;this.quantity=quantity;this.price=price;public double amount() return quantity*price; class one extends salepublic one(String num,int quantity,double price) super(num, quantity, price);public double amount() return super.amount();class two extends salepublic two(String num,int quantity,double price)super(num,quantity,price);public double amount() return super.amount();class three extends salepublic three(String num,int quantity,double price)super(num,quantity,price);public double amount() return super.amount();class four extends salepublic four(String num,int quantity,double price)super(num,quantity,price);public double amount() return super.amount();class five extends salepublic five(String num,int quantity,double price)super(num,quantity,price);public double amount() return super.amount();public class testpublic static void main(String args)System.out.println(*商品销售情况*);one one=new one(101, 3, 126.8);one.amount();two two=new two(221, 8, 125.6);two.amount();three three=new three(325, 10, 124.8*0.95);three.amount();four four=new four(108, 45, 124.3*0.95);four.amount(); five five=new five(901, 100, 121.5*0.95);five.amount();double mm=one.price*one.quantity+two.price*two.quantity+three.price*three.quantity+four.price*four.quantity+five.price*five.quantity; System.out.println(当天该商品的销售总额是: +mm); double nn=(one.price*one.quantity+two.price*two.quantity+three.price*three.quantity+four.price*four.quantity+five.price*five.quantity)/(one.quantity+two.quantity+three.quantity+four.quantity+five.quantity); System.out.println(当天该商品的平均售价是: +nn);编写销售商品类Sale和含有main方法的公共类Test,计算当天此商品的总销售额sum,以及每件商品的平均售价,并在显示器上显示。(3)定义接口Shape及其抽象方法getArea()和getPerimeter()用于计算图形和面积和周长。定义类Rectangle(矩形)、类Circle(圆形)、类Triangle(三角形),要求这些类继承点类Coordinates()并实现接口的抽象方法。package jiekou2; interface shape public double getarea();public double getperimter(); class coordinates double x; double y;public coordinates(double x,double y)this.x=x;this.y=y;class rectangle extends coordinates implements shapepublic rectangle(double x,double y)super(x, y);Overridepublic double getarea() return x*y; Overridepublic double getperimter() return x+x+y+y;class circle extends coordinates implements shapedouble pi= 3.14; public circle(double x,double y) super(x,y); Overridepublic double getarea() return pi*x*x;Overridepublic double getperimter() return 2*pi*x;class triangle extends coordinates implements shape publi
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 家纺公司售后管理制度
- 员工晋升淘汰管理制度
- 家庭花园日常管理制度
- 2025企业依法终止劳动合同
- 2025建筑工程设计合同详细版模板
- 核心素养导向的幸福力课程建设与实践
- 艺术家与藏家关系构建考核试卷
- 艺人国际市场拓展与推广执行策略实施考核试卷
- 大型租赁仓库管理制度
- 境外当地员工管理制度
- 2025年湖北省新高考信息卷(三)物理试题及答题
- 2024年山东省初中学业水平考试语文试题(文字版-含答案)
- 2025-2030年力控玩具项目投资价值分析报告
- 基于学校区域文化优势背景下的小学水墨画教学研究
- 设备欠款协议书范本
- 机柜租赁合同协议
- 2025年2月22日四川省公务员面试真题及答案解析(行政执法岗)
- 造价项目时效管理制度
- 腹腔镜手术术后腹胀护理
- 泥水平衡-沉井-顶管及沉井施工方案
- 影视剪辑直播测试题及答案
评论
0/150
提交评论