「java编写矩形包含」编写矩形类

博主:adminadmin 2022-11-22 18:45:05 73

本篇文章给大家谈谈java编写矩形包含,以及编写矩形类对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

用java编写矩形类

class Rectangle{

double x1, y1, x2, y2;

Rectangle(){

this(0,0,0,0);

}

Rectangle(double x1, double y1, double x2, double y2){

this.x1 = x1;

this.y1 = y1;

this.x2 = x2;

this.y2 = y2;

}

double getArea(){

return Math.abs((x1-x2)*(y1-y2))/2;

}

double getPemi(){

return Math.abs((x1-x2)+(y1-y2))*2;

}

}

public class Test{

public static void main(String[] args) {

Rectangle a = new Rectangle();

a.x1=2.1;

a.y1=3.2;

a.x2=5.2;

a.y2=6.3;

System.out.println("面积是"+a.getArea()+" 周长是"+a.getPemi());

Rectangle b = new Rectangle(1, 2, 6.8, 10.5);

System.out.println("面积是"+b.getArea()+" 周长是"+b.getPemi());

}

}

Java编写一个矩形类,至少包含以下方法:

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;

}

}

麻烦采纳呦~~~亲

Java编写一个矩形类,并计算面积和周长?

class Rectangle{

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());

}

}

java编写矩形包含的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于编写矩形类、java编写矩形包含的信息别忘了在本站进行查找喔。

The End

发布于:2022-11-22,除非注明,否则均为首码项目网原创文章,转载请注明出处。