对象的应用编程.docx_第1页
对象的应用编程.docx_第2页
对象的应用编程.docx_第3页
对象的应用编程.docx_第4页
对象的应用编程.docx_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

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

文档简介

对象的应用编程实验目的:1 掌握对象的生成机制2.了解构造函数的作用实验内容1. 对象创建和使用。2. 使用关联类进行属性的定义。3. 访问控制修饰符的作用。4. 掌握静态属性、方法和初始化器的特点。5. 包的应用实验步骤1. 写一个名为Rectangle的类表示矩形。其属性包括宽width、高height和颜色color,width和height都是double型的,而color则是String类型的。要求该类提供计算面积的方法getArea()方法,以及修改width和height的值及获得width和height当前值的方法。要求:(1) 使用构造函数完成各属性的初始赋值(2) 使用getter和sr的形式完成属性的访问及修改public class Rectangle private double width;private double height;private String color; public Rectangle(double width, double height, String color) this.width = width;this.height = height;this.color = color;public double getWidth() return width;public void setWidth(double width) this.width = width;public double getHeight() return height;public void setHeight(double height) this.height = height;public double getArea()double area;return area=width*height;public static void main(String args) Rectangle a=new Rectangle(2.0,3.0,red);System.out.println(a.getArea();一个Fan有自己的品牌和型号,其功能有start,stop,speed,start和stop可以改变Fan的状态status(on/off),speed可以调整Fan的速度(档级在0、1、2、3),请分析并提供Fan类的定义。并编写程序对其进行测试。Public class Fan private String band; private String type;private String status ;private int speed;public Fan(String band,String type,String status,int speed)this.band=band;this.type=type;this.status=status;this.speed=speed;public String start()return status;public String stop()return status;public void speed(int a)this.speed=a;public static void main(Stringargs)Fan t=new Fan(ss,dd,off,1);System.out.printf(%s,%s,%b,%d,t.band,t.type,t.status,t.speed); 解释下面的程序运行结果输出为什么是null public class My String s; public void My() s = Constructor; public void go() System.out.println(s); public static void main(String args) My m = new My(); m.go(); 调用者没有传递值2.写出程序运行结果class StaticDemo static int x;int y; static x=10;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 static void main(String args) System.out.println(静态变量x=+StaticDemo.getX();System.out.println(实例变量y=+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();静态变量x=10静态变量a.x=3实例变量a.y=2静态变量b.x=3实例变量b.y=42.一个计算机商店销售很多品牌的计算机,每台计算机都应该记录其配置信息,包括处理器、主板、显示器、内存、硬盘等基本设备,每个设备都有自己的品牌、价格、型号信息,请你尝试构造合适的类并利用组合的方法来表示计算机,并为该计算机类添加计算价格(各设备价格之和)、打印配置信息等方法。要求:将定义的类都放在一个包product内。Cpu.javapackage product;public class cpuprivate String brand;private double value;private String type;public cpu(String brand,double value,String type)this.brand=brand;this.value=value;this.type=type;public String getbrand()return brand;public String gettype()return type;public double getvalue()return value;public void print()System.out.println(brand:+getbrand()+t+value:+getvalue()+t+type:+gettype();/System.out.println(brand:);Hardware.javapackage product;public class hardwareprivate String brand;private double value;private String type;public hardware(String brand,double value,String type)this.brand=brand;this.value=value;this.type=type;public String getbrand()return brand;public String gettype()return type;public double getvalue()return value;public void print()System.out.println(brand:+getbrand()+t+value:+getvalue()+t+type:+gettype();/System.out.println(brand:);Mainboard .javapackage product;public class mainboard private String brand;private double value;private String type;public mainboard (String brand,double value,String type)this.brand=brand;this.value=value;this.type=type;public String getbrand()return brand;public String gettype()return type;public double getvalue()return value;public void print()System.out.println(brand:+getbrand()+t +value:+getvalue()+t+type:+gettype();/System.out.println(brand:);Monitor .javapackage product;public class monitorprivate String brand;private double value;private String type;public monitor(String brand,double value,String type)this.brand=brand;this.value=value;this.type=type;public String getbrand()return brand; public String gettype()return type;public double getvalue()return value; public void print()System.out.println(brand:+getbrand()+t+value:+getvalue()+t+type:+gettype();/System.out.println(brand:);Ram.javapackage product;public class ramprivate String brand;private double value;private String type;public ram(String brand,double value,String type)this.brand=brand;this.value=value;this.type=type;public String getbrand()return brand;public String gettype()return type;public double getvalue()return value;public void print()System.out.println(brand:+getbrand()+t+value:+getvalue()+t+type:+gettype();/System.out.println(brand:);computer.javapackage product;public class computerprivate cpu cp;private hardware hard;private mainboard main;private monitor mon;private ram ra;public computer()this.cp=new cpu(atm,232.2d,xora878);this.hard=new hardware(westdigital,300.2d,pc222);this.main=new mainboard(aim,321.2d,x-32);this.mon=new monitor(voa,900.2d,w-ss);this.ra=new ram(kingsdon,127.2d,s-24);public void kprint()cp.print();hard.print();main

温馨提示

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

评论

0/150

提交评论