「java中rect」java中rectangle函数怎么用

博主:adminadmin 2022-11-25 19:14:09 258

本篇文章给大家谈谈java中rect,以及java中rectangle函数怎么用对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java Rect.java输出问题

使用Java,double 进行运算时,经常出现精度丢失的问题,总是在一个正确的结果左右偏0.0000**1。 特别在实际项目中,通过一个公式校验该值是否大于0,如果大于0我们会做一件事情,小于0我们又处理其他事情。 这样的情况通过double计算出来的结果去和0比较大小,尤其是有小数点的时候,经常会因为精度丢失而导致程序处理流程出错。

这里有篇文章,你可以去看看怎么处理

java 编写一个矩形类 rect 要求如下:

public class Rect{

    private int length;

    private int width;

    private int startX;

    private int startY

    

    public Rect(){}

    

    public Rect(int length,int width){

            this.length = length;

            this.width = width;

    }

    

    public Rect(int length,int width,int startX,int startY){

            this.length = length;

            this.width = width;

            this.startX = startX;

            this.startY = startY;

    }

    

    //不知道你要什么成员方法,我随便点....

    

    

    public void louzhuhao(){

            System.out.println("楼主好....");

    }

public int getLength() {

    return length;

}

public void setLength(int length) {    

    this.length = length;

}

public int getWidth() {

    return width;

}

public void setWidth(int width) {

    this.width = width;

}

public int getStartX() {

    return startX;

}

public void setStartX(int startX) {

    this.startX = startX;

}

public int getStartY() {

    return startY;

}

public void setStartY(int startY) {

this.startY = startY;

}

}

Java编写一个Rect类

public class Rect{

private int length;

private int width;

public Rect(int length, int width) {

this.length = length;

this.width = width;

}

public int getLength() {

return length;

}

public void setLength(int length) {

this.length = length;

}

public int getWidth() {

return width;

}

public void setWidth(int width) {

this.width = width;

}

public void countArea(){

int area = this.length * this.width;

System.out.println("矩形的面积为 : "+area);

}

public void countGirth(){

int girth = (this.length + this.width) * 2;

System.out.println("矩形的周长为 : "+girth);

}

public static void main(String[] args) {

Rect rect = new Rect(20, 30);

rect.countArea();

rect.countGirth();

}

}

用java创建一个矩形类Rectangle,其属性为各点的坐标。

class Point{

protected:

double x;

double y;

public:

Point(double a,double b){x=a;y=b;}

Point(){x=0.0;y=0.0;}

double GetX(){return x;}

double GetY(){return y;}

};

class Rect{

protected:

Point p1;

Point p2;//p1是左下角,p2是右上角

public:

Rect(double i,double j,double,k,double l):p1(i,j),p2(k,l){}

double Area()

{return ((p2.GetX()-p1.GetX())*(p1.GetY()-p2.GetY()));}

};

//用于测试的主函数

void main()

{

Rect rect1(2,3,4,5);

coutrect1.Area()endl;

}

用JAVA编写程序,编写一个Rect类 ,要求,(1)Rect类的私有成员变量有:a:double型,代表长 b: doubl

class Rect{

private double a ;

private double b;

Rect(){

this.a = 0;

this.b = 0;

}

Rect(double len,double width){

this.a = len;

this.b = width;

}

public Double area(){

return this.a* this.b;

}

public void display(){

System.out.println(this.area());

}

}

public class userect {

public static void main(String[] args){

Rect rect1 = new Rect();

rect1.display();

Rect rect2 = new Rect(10,10);

rect2.display();

}

}

Java 编写一个矩形类Rect

public class Rect {

private double length;//矩形的长

private double width;//矩形的宽

public Rect() {}//默认构造器

public Rect(double length,double width) {

this.length = length;

this.width = width;

}

/**

* 取得矩形的面积

* */

public double getArea(){

return this.getLength() * this.getWidth();

}

/**

* 取得矩形的周长

* */

public double getPerimeter(){

return (this.getLength() + this.getWidth()) * 2;

}

/**

* 取得矩形的面积,需传入矩形长与宽

* */

public double getArea(double length,double width){

return length * width;

}

/**

* 取得矩形的周长,需传入矩形长与宽

* */

public double getPerimeter(double length,double width){

return (length + width) * 2;

}

public double getLength() {

return length;

}

public void setLength(double length) {

this.length = length;

}

public double getWidth() {

return width;

}

public void setWidth(double width) {

this.width = width;

}

}

java中rect的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java中rectangle函数怎么用、java中rect的信息别忘了在本站进行查找喔。

The End

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