class Rectangle{
創(chuàng)新互聯(lián)主營十堰網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,app開發(fā)定制,十堰h5小程序定制開發(fā)搭建,十堰網(wǎng)站營銷推廣歡迎十堰等地區(qū)企業(yè)咨詢
private int width = 2;
private int length = 1;
public int getWidth(){
return this.width;
}
public void setWidth(int w){
this.width = w;
}
public int getLength(){
return this.length;
}
public void setLength(int l){
this.length = l;
}
public int getArea(){
return this.length * this.width;
}
public int getCircumference(){
return (this.length + this.width) * 2;
}
public Rectangle(){}
public Rectangle(int l, int w){
this.length = l;
this.width = w;
}
}
public class demo{
public static void main(String[] args) {
Rectangle rect = new Rectangle(30, 8);
System.out.print("長方形的面積是:");
System.out.println(rect.getArea());
System.out.printf("長方形的周長是:%d\n", rect.getCircumference());
}
}
// 矩形
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程序矩形類兩數(shù)據(jù)員別rLength寬rWidth通getLength()、getWidth()、getArea()別查看矩形、寬面積通setLength()setWidth()重新設(shè)置矩形寬
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("請輸入正方形的大小:");
int line = s.nextInt();; // 正方形的邊長
for (int i = 1; i = line; i++) { // 行
for (int j = 1; j = line; j++) { // 列
if (i == 1 || i == line) { // 如果是第一行或者是最后一行則輸出星號
System.out.print("* ");
} else if (j == 1 || j == line) { // 如果是第一列或者是最后一列則輸出星號
System.out.print("* ");
} else {
System.out.print(" "); // 如果不是第一行、最后一行、第一列、最后一列則輸出空格
}
}
System.out.print("\n"); // 換行
}
}
import?java.awt.Point;
public?class?Rectangle?{
private?int?widthT?=?0;
private?int?heightT?=?0;
Point?point?=?new?Point();
public?Rectangle(int?width,?int?height,?int?centerX,?int?centerY)?{
widthT?=?width;
heightT?=?height;
point.x?=?centerX;
point.y?=?centerY;
}
public?int?width()?{
return?widthT;
}
public?int?height()?{
return?heightT;
}
public?int?centerX()?{
return?point.x;
}
public?int?centerY()?{
return?point.y;
}
}
麻煩采納呦~~~親