第二次实验报告及学习总结

时间:2019-09-18
本文章向大家介绍第二次实验报告及学习总结,主要包括第二次实验报告及学习总结使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1. 写一个名为Rectangle的类表示矩形。其属性包括宽width、高height和颜色color,width和height都是double型的,而color则是String类型的。要求该类具有:

(1) 使用构造函数完成各属性的初始赋值

(2) 使用get…()和set…()的形式完成属性的访问及修改

(3) 提供计算面积的getArea()方法和计算周长的getLength()方法

实验代码:

   package xingqisan;

   public class Rectangle {                    //定义Rectangle类
        private double width;                       
        private double height;
        private String color;                //定义属性
        public double getHeight() {
            return height;                  //取得height属性
        }
        public void setHeight(double height) {
            this.height = height;          //设置height属性下同
        }
        public double getWidth() {
            return width;
        }
        public void setWidth(double width) {
            this.width = width;
        }
        public String getColor() {
            return color;
        }
        public void setColor(String color) {
            this.color = color;
        }
        
        public void getArea() {
            double area=0;
            area=this.height*this.width;
            System.out.println("面积为"+area);
        }
        public void getLength() {
            double length=0;
            length=(this.height+this.width)*2;
            System.out.println("周长为"+length);
        }
        public static void main(String args[]) {
            Rectangle rec=new Rectangle();
            rec.setWidth(3);
            rec.setHeight(4);
            rec.setColor("黄色");
            rec.getArea();
            rec.getLength();
            System.out.println("长:"+rec.getWidth()+",高:"+rec.getHeight()+",颜色:"+rec.getColor());
        }
   }

实验结果截图:

原文地址:https://www.cnblogs.com/fengmixinluo/p/11541228.html