首页 > 编程语言 > 详细

第四周课程总结与实验报告(Java简单类与对象)

时间:2019-09-17 19:10:53      阅读:184      评论:0      收藏:0      [点我收藏+]

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

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

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

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

实验代码

public class Rectangle {
  private double height;
  private double width;
  private String color;
public double getHeight(){
    return height;
}
public void setHeight(double height){
    this.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 Rectangle(double height,double width,String color ){
    this.setHeight(height);
    this.setWidth(width);
    this.setColor(color);   
}
public void getArea(){
    double area = 0;
 area = this.height*this.width;
 System.out.println("矩形的面积" + area);
}
public void getGirth(){
    double girth = 0;
  girth = (this.height + this.width)*2;
 System.out.println("矩形的周长" + girth);
}
public String toString(){
    String recStr = "矩形的高度:" + this.getHeight() + "矩形的高度:" + this.getWidth() + "颜色:" + this.getColor();
    return recStr;
}
public static void main(String args[]){
    Rectangle rec = new Rectangle(8,9,"黑色");
    rec.getArea();
    rec.getGirth();
 System.out.println(rec.toString());
}
}

技术分享图片

第四周课程总结与实验报告(Java简单类与对象)

原文:https://www.cnblogs.com/buxiu888/p/11536002.html

(1)
(1)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!