JAVA,写一个名为Rectangle的类表示矩形,怎么写?

如题所述

说明
1。颜色题目没初始化,我定义为“black”
2。 测试函数具体没说测试什么,以及长宽没初始化数值,故代码中只调用toString()方法,具体自己改写

public class Test
{
public static void main(String[] args)
{
Rectangle r1 = new Rectangle(2.3,5.2);
System.out.println(r1.toString());

}

}

class Rectangle
{
private double width,height;
private String color = "black";
Rectangle(double width,double height)
{
this.width = width;
this.height = height;
}
public void setWidth(double width)
{
this.width = width;
}
public double getWidth()
{
return width;
}
public void setHeight(double height)
{
this.height = height;
}
public double Height()
{
return height;
}
public double getArea()
{
return height * width;
}
public String toString()
{
return "长方形的宽是" + width + "长方形的高是" + height + "长方形的颜色是" + color;
}

}
温馨提示:内容为网友见解,仅供参考
无其他回答

JAVA,写一个名为Rectangle的类表示矩形,怎么写?
1。颜色题目没初始化,我定义为“black”2。 测试函数具体没说测试什么,以及长宽没初始化数值,故代码中只调用toString()方法,具体自己改写 public class Test { public static void main(String[] args){ Rectangle r1 = new Rectangle(2.3,5.2);System.out.println(r1.toString());} } ...

java作业 设计一个名为rectangle的类
package com.jihe;public class Rectangle {private double width = 1.0;private double height = 1.0;Rectangle() {}Rectangle(double width, double height) {this.width = width;this.height = height;}public double getPerimeter() {return 2 * (width + height);}public double getArea() ...

Java类表示矩形,要求提由四个顶点坐标实例化,或由边长度和高度实例化...
public class TestRectangle { public static void main(String[] args){ Rectangle r=new Rectangle(10,2,20,12.5);System.out.println(r.computerArea());} } class Rectangle{ private double left,top,right,bottom;private double height,width;public Rectangle(){ } \/\/(left,top)矩形左上...

设计并测试一个名为Rectangle的类表示矩形,其属性为矩形的左下角和右...
Rectangle(){} Rectangle(Rectangle &r):p1(r.p1),p2(r.p2){ } Rectangle(double x1,double y1,double x2,double y2):p1(x1,y1),p2(x2,y2){ } double GetLength(){ return fabs(p2.x-p1.x);} double GetArea(){ return fabs((p2.x-p1.x) * (p2.y-p1.y));} private...

设计并测试一个名为Rectangle的矩形类,其属性为矩形的左下角和右上角...
Rectangle(double x1,double y1,double x2,double y2):LeftDown(x1,y1),Rightup(x2,y2){} double Area(){return (Rightup.getX()-Leftdown.getX())*(Rightup.getY()-Leftdown.getY());} };void main(){ Rectangle r(1.0,1.5,2.0,3.3);cout<<"矩形面积是:"<<r.Area()<<...

创建一个简单的表示矩形的Rectangle类,满足以下条件:
构造函数没有给成员变量赋值 public Rectangle(double height,double width){ this.height = height; this.width = width; }还有 public double getArea() { return height * width; }最好这么写,不然成员变量和构造函数就没什么意义了 ...

定义一个rectangle 类,它包含两个数据成员 length 和 width ;以及包含...
class Rectangle { public:double Length;\/\/长度 double Width;\/\/宽度 Rectangle(double length, double width) {\/\/定义一个有两个参数的构造函数,用于设置长方形的宽度和长度 this->Length = length;this->Width = width;} double Area() { \/\/求面积函数 return Width * Length;\/\/返回长度和...

JAVA上课作业:求一份代码
Java程序代码:public class test {public static void main(String[] args) {if(args.length != 2) {System.out.println("参数数量错误!必须是2个参数,以空格隔开...");return;}Rectangle rect = new Rectangle(Double.parseDouble(args[0]), Double.parseDouble(args[1]));System.out....

C++问题:设计一个名为Rectangle的矩形类,其属性为矩形的左下角和右...
Rectangle::Rectangle(int left,int bottom,int right,int top){ itsLeft=left;itsBottom=bottom;itsRight=right;itsTop=top;} int Rectangle::GetArea(){ Width=abs(itsRight-itsLeft);Height=abs(itsTop-itsBottom);cout<<"该矩形的长:"<<Width<<endl;cout<<"该矩形的宽:"<<Height<<endl...

定义一个Point类为一个点,再定义一个Rectangle类表示矩形。
class Rectangle{ Point p;int width;int height;public Rectangle(Point p1, int width, int height){ this.p = p;this.width = width;this.height = height;} main(){ Rectangle r = new Rectangle(new Point(0,0), 6,4);} } class Point{ int x;int y;public Point(int x, int ...

相似回答