JAVA第四章实验

上传人:m**** 文档编号:548431821 上传时间:2023-09-11 格式:DOC 页数:9 大小:117KB
返回 下载 相关 举报
JAVA第四章实验_第1页
第1页 / 共9页
JAVA第四章实验_第2页
第2页 / 共9页
JAVA第四章实验_第3页
第3页 / 共9页
JAVA第四章实验_第4页
第4页 / 共9页
JAVA第四章实验_第5页
第5页 / 共9页
点击查看更多>>
资源描述

《JAVA第四章实验》由会员分享,可在线阅读,更多相关《JAVA第四章实验(9页珍藏版)》请在金锄头文库上搜索。

1、第四章实验4.1 对象的创建和使用(1) Rectangle是一个矩形类,该类包含两个成员变量width和length分别表示矩形的宽和长。成员方法area()用来计算矩形的面积。试填写下列程序中的空白部分,以输出一个宽为2.5长为8.7的举行的面积。(提示:应首先填写Rectangle的构造函数)。class JLab0401public static void main(String args)Rectangle myRect = new Rectangle(2.5,8.7); double theArea;theArea = myRect.area();System.out.printl

2、n(My rectangle has area + theArea); /end of class JLab0401class Rectangledouble width, length;Rectangle(double widthd,double lengthd)width=widthd;length=lengthd;public double area()double a;a = length * width;return a; /end of class Rectangle(2) 编译并执行你填写完整的程序。(3)在上述程序的JLab0401类中main方法中添加相关语句,使之能够输出矩

3、形的长和宽的值。编译并执行该程序,以验证你更改的正确性。class JLab0401 public static void main(String args) Rectangle myRect = new Rectangle(2.5,8.7);double theArea; theArea = myRect.area(); System.out.println(My rectangles length + myRect.length);System.out.println(My rectangles width + myRect.width);System.out.println(My rec

4、tangle has area + theArea); /end of class JLab0401 class Rectangle double width, length;Rectangle(double widthd,double lengthd)width=widthd;length=lengthd;public double area() double a; a = length * width; return a; /end of class Rectangle 黑体是添加的语句,其运行结果是(4)在(3)成功的基础上,在Rectangle类中添加公共方法setWidth和setL

5、ength方法,这两个方法分别用来设置矩形的长宽值。在JLab0401类的main方法中使用Rectangle的默认午餐书构造函数创建一个新的Rectangle类的实例rect。并使用etWidth和setLength方法将其长宽分别设置为3.6和10.8,并输出其面积。提示:使用默认构造函数创建实例rect的语句为Rectangle rect = new Rectangle( );class JLab0401 public static void main(String args) Rectangle myRect = new Rectangle(2.5,8.7);Rectangle rec

6、t = new Rectangle();Rectangle.setWidth(3.6);Rectangle.setLength(10.8);double theArea; theArea = myRect.area(); System.out.println(My rectangles length + myRect.length);System.out.println(My rectangles width + myRect.width);System.out.println(My rectangle has area + theArea); System.out.println(rect

7、has area + theArea); /end of class JLab0401 class Rectangle static double width;static double length;Rectangle()Rectangle(double widthd,double lengthd)width=widthd;length=lengthd;static boolean setWidth(double newWidth)if(newWidth0)width=newWidth;return true;elseSystem.out.println(非法宽度。);return fals

8、e;static boolean setLength(double newLength)if(newLength0)length=newLength;return true;elseSystem.out.println(非法长度。);return false;public double area() double a; a = length * width; return a; /end of class Rectangle 运行结果是:(5)不改变JLab0401类中main方法,要达到(3)中同样的结果应如何做?修改相关程序,编译并执行以验证你的做法。class JLab0401 publ

9、ic static void main(String args) Rectangle myRect = new Rectangle(2.5,8.7);double theArea; theArea = myRect.area(); System.out.println(My rectangle has area + theArea); /end of class JLab0401 class Rectangle double width, length;Rectangle(double widthd,double lengthd)width=widthd;length=lengthd;publ

10、ic double area() double a; a = length * width; System.out.println(My rectangles length + 8.7);System.out.println(My rectangles width + 2.5);return a; /end of class Rectangle运行结果是:(6)编写一个圆形类Circle,定义其成员变量(半径):double radius该类含有两个构造函数:public Circle(double r) 初始化半径为rpublic Circle() 初始化半径为1该类含有下列方法:publi

11、c double area() 返回圆形的面积public double circumference() 返回圆形的周长public String toString() 返回该类对象的文字描述,如:Circle with radius 2.5public void setRadius(double r) 设置半径为rpublic double getRadius() 返回半径的值。class Circle double radius;public Circle(double r)radius=r;public Circle()public double area() double a; a =

12、 3.14 * radius*radius; return a;public double circumference() double c;c=2*3.14*radius;return c;public String toString() System.out.println(Circle with radius +radius);return null;public boolean setRadius(double r) if(r0)radius=r;return true;elseSystem.out.println(非法半径。);return false;public double g

13、etRadius()return radius;(7)编写一个含有主方法的类JLab0401B,在主方法中用两个构造函数分别创建两个Circle类的实例:myCircle1和myCircle2,并调用相关方法,以验证你的程序。public class JLab0401Bpublic static void main(String args)Circle myCircle1=new Circle();double Circle1Area,Circle1C;Circle1Area=Circle.area();Circle1C=Circle.circumference();System.out.pr

14、intln(Circle1s 面积 is+Circle1Area);System.out.println(Circle1s 周长 is+Circle1C);Circle myCircle2=new Circle(1.0);double Circle2Area,Circle2C;Circle2Area=Circle.area(); Circle2C = Circle.circumference();System.out.println(Circle1s 面积 is+Circle2Area);System.out.println(Circle1s 周长 is+Circle2C);class Circle

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 建筑/环境 > 施工组织

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号