<pre name="code" class="java">/*
* Copyright (c) 2015, 烟台大学计算机学院
* All rights reserved.
* 文件名称:test.java
* 作 者:王元阳
* 完成日期:2015 年 10 月 20 日
* 版 本 号:v1.0
*问题描述: 构造一个矩形类并将长和宽的初值赋为1,最后输出长、宽、面积。
*/
public class Rectangle {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Rectangle1 rectangle1 = new Rectangle1(4, 40);
System.out.println("The area of the rectangle of width "
+ rectangle1.width + " and height " + rectangle1.height + " is "
+ rectangle1.getArea());
Rectangle1 rectangle2 = new Rectangle1(3.5, 39.5);
System.out.println("The area of the rectangle of width "
+ rectangle2.width + " and height " + rectangle2.height + " is "
+ rectangle2.getArea());
}
}
class Rectangle1 {
double width;
double height;
Rectangle1() {
width = 1.0;
height = 1.0;
}
Rectangle1(double newWidth, double newHeight) {
width = newWidth;
height = newHeight;
}
double getArea() {
return width * height;
}
}
<img src="https://img-blog.csdn.net/20151020193529293?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />