设计一个矩形类rectangle(设计一个矩形类,按以下要求编写程序 求解 求正确答案完整答案 这是Java的题目)

本文目录
- 设计一个矩形类,按以下要求编写程序 求解 求正确答案完整答案 这是Java的题目
- java编程:定义一个矩形类Rectangle
- 如何设计一个名为Rectangle的矩形类
- c++设计并测试一个矩形类(Rectangle)
- 最近在学C++,设计一个矩形类Rectangle,求大神指点
- 设计一个矩形类rectangle要求如下所述 : 1.该类中的私有成员变量存放矩
设计一个矩形类,按以下要求编写程序 求解 求正确答案完整答案 这是Java的题目
public class Rectangle
{
private double width, height;
public Rectangle()
{
this(0, 0);
}
public Rectangle(double width, double height)
{
this.width = width;
this.height = height;
}
public double getCurve()
{
return 2.0f * (width + height);
}
public double getSurface()
{
return width * height;
}
}
public class Test
{
public static void main(String args)
{
Rectangle rect1 = new Rectangle(10, 20);
System.out.println("面积:" + rect1.getSurface());
System.out.println("周长:" + rect1.getCurve());
}
}
java编程:定义一个矩形类Rectangle
public class Rectangle{
private int width;
private int height;
public Rectangle(){
this.width = 10;
this.height = 10;
}
public Rectangle(int width, int height){
this.width = width;
this.height = height;
}
public int area(){
return width * height;
}
//省略getter/setter
}
如何设计一个名为Rectangle的矩形类
#include《iostream》
#include《cmath》
classpoint{doublex;doubley;public: point(doublea=0,doubleb=0){ x=a;y=b;}
voidset(doublea,doubleb){x=a;y=b;}
doublegetX(){returnx;}doublegetY(){returny;}};
classRectangle{
private:
pointlpoint,rpoint;
public:
Rectangle(pointl,pointr)
{
lpoint=l;
rpoint=r;
}
doubleArea()
{
returngetlength()*getwidth();
}
doublegetlength()
{
***隐藏网址***
}
doublegetwidth()
{
***隐藏网址***
}
}
intmain()
{
pointlt,rb;
doublea,b;
cout《《"输入左上角坐标: ";
cin》》a》》b;
lt.set(a,b);
cout《《"输入右下角坐标: ";
cin》》a》》b;
rb.set(a,b);
Rectangle rt(lt,rb);
cout《《"矩形的面积等于"《《rt.Area()《《endl;
return0;
}
c++设计并测试一个矩形类(Rectangle)
#include 《iostream》
using namespace std;
class Point{
private:
int x;
int y;
public:
Point(int x1,int y1):x(x1),y(y1){}
Point(){}
void setX(int x){ this-》x=x;}
void setY(int y) { this-》y=y;}
int getX(){ return x;}
int getY() {return y;}
};
class Rectangle{
private:
Point left;
Point right;
public:
Rectangle(Point left,Point right){
this-》left=left;
this-》right=right;
}
void print(){
cout《《"left-》x:"《《left.getX()《《",left-》y:"《《left.getY()《《endl;
cout《《"right-》x:"《《right.getX()《《",right-》y:"《《right.getY()《《endl;
}
int getLength(){
int l=right.getX()-left.getX();
int w=right.getY()-left.getY();
l》0 ? l : -l;
w》0 ? w : -w;
return 2*(l+w);
}
int getArea(){
int l=right.getX()-left.getX();
int w=right.getY()-left.getY();
l》0 ? l : -l;
w》0 ? w : -w;
return l*w;
}
};
int main(){
Point p1(1,2);
Point p2(4,5);
Rectangle rect(p1,p2);
rect.print();
cout《《rect.getLength()《《endl;
cout《《rect.getArea()《《endl;
}
最近在学C++,设计一个矩形类Rectangle,求大神指点
这里有几个get函数,实现获取不同的值
所以必须从函数名上区分。
class Rectangle
{
double Width,Height;
public:
Rectangle ():Width(0),Height(0)
{}
Rectangle (double w, double h):Width(w),Height(h)
{}
double getWidth(){return Width;}
double getHeight(){return Height;}
void setRect(double w, double h)
{
Width=w;
Height=h;
}
void getC(){return 2*(Width+Height);}
void getS(){return Width*Height;}
};
主函数你自己加一下就好。
设计一个矩形类rectangle要求如下所述 : 1.该类中的私有成员变量存放矩
#include《iostream》
using namespace std;
class Rect
{
private:
int length;
int width;
int perim;
public:
Rect()
{
length =1;
width =1;
}
Rect(int length,int width)
{
this-》length = length;
this-》width = width;
}
void setLength(int length)
{
if(length 》0 && length 《50)
{
this-》length = length;
}
else
{
cout《《"INPUT ERROR!"《《endl;
}
}
void setWidth(int width)
{
if(width 》0 && width 《50)
{
this-》width = width;
}
else
{
cout《《"INPUT ERROR!"《《endl;
}
}
float permi()
{
return length*width;
}
};
int main()
{
Rect a;
a.setLength(20);
a.setWidth(20);
cout《《a.permi()《《endl;
cin.get();
return 0;
}

更多文章:
animate快捷键大全(adobe animate新建空白关键帧和切换下一帧快捷键是什么)
2026年10月1日 23:40
lpcwstr与char类型不匹配(到 LPCWSTR 的类型不兼容,这是怎么回事啊)
2026年10月1日 22:30
设计一个矩形类rectangle(设计一个矩形类,按以下要求编写程序 求解 求正确答案完整答案 这是Java的题目)
2026年10月1日 22:20
int和integer(int 和 Integer 有什么区别)
2026年10月1日 21:30
method怎么记忆(怎么区分way,means,method)
2026年10月1日 17:40
领域驱动设计 软件核心复杂性应对之道(领域模型驱动设计(DDD)之模型提炼)
2026年10月1日 17:00





