public class RectangleDemo {
public static void main(String[] args) {
RectangleDemo demo = new RectangleDemo(12, 32)
System.out.println(demo.getPerimeter())
System.out.println(demo.getArea())
demo = new RectangleDemo()
System.out.println(demo.getArea())
System.out.println(demo.getPerimeter())
demo.setHeight(50)
demo.setWidth(30)
System.out.println(demo.getArea())
System.out.println(demo.getPerimeter())
}
// 求周
public double getPerimeter() {
return (height + width) * 2
}
// 求面积
public double getArea() {
return height * width
}
public RectangleDemo(double height, double width) {
this.height = height
this.width = width
}
public RectangleDemo() {
this.height = 10
this.width = 10
}
private double height// 高纯盯度
private double width// 宽度
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
}
}
编写矩做拦和形类RectangleJava程序矩形类两数据员别rLength宽rWidth通getLength()、getWidth()、getArea()别查看矩形、宽面积通setLength()setWidth()重新设置矩形宽衡丛
public class RectangleDemo {private static int M = 20
private static int N = 18
private int m// 长度
private int n// 宽度
RectangleDemo(int m, int n) {
if (m >0) this.m = m
else this.m = M
if (n >0) this.n = n
else this.n = N
}
public void print() {
for (int i = 0i <ni++) {
if (i == 0 || i == n - 1) {
for (int j = 0j <mj++) {
if (j == 0 || j == m - 1)
System.out.print("|")
else
System.out.print("-")
}
} else {
for (int j = 0j <mj++) {
if (j == 0 || j == m - 1)
System.out.print("|")
else
System.out.print(" ")
}
}
System.out.println()
}
}
public static void main(String[] a) {
RectangleDemo re = new RectangleDemo(20, 8)
re.print()
}
}
结果
要想者颂以‘*’为边 把袭游输出改成 * 就可以拍嫌销了
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)