java 上机试验:类与对象的试验
定义一个名为MyRectangle的矩形,类中有四个私有整形域,分别是矩形的左上角坐标(xUp,yUp)和右下角坐标(xDowm,yDown);类中定义没有参数的构造方法...
定义一个名为MyRectangle的矩形,类中有四个私有整形域,分别是矩形的左上角坐标(xUp,yUp)和右下角坐标(xDowm,yDown);类中定义没有参数的构造方法和有四个int参数的构造方法,用来初始化类对象。类中还有以下方法:getW()——计算矩形的宽度;getH()——计算矩形的高度;area()——计算矩形的面积;toString()——把矩形的宽高面积等信息作为字符串返回。编写程序使用MyRectangle类。
展开
3个回答
展开全部
public class MyRectangle {
//四个私有变量
private int xUp, yUp;
private int xDown, yDown;
//空构造函数
public MyRectangle() {
}
//带有四个参数的构造函数,并初始化
public MyRectangle(int xup, int yup, int xdown, int ydown) {
this.xUp = xup;
this.yUp = yup;
this.xDown = xdown;
this.yDown = ydown;
}
//获取宽度
public int getW() {
return this.xDown - this.xUp;
}
//获取高度
public int getH() {
return this.yUp - this.yDown;
}
//计算面积
public int area() {
return this.getW() * this.getH();
}
//重写toString返回宽度、高度、面积
public String toString() {
return ("width=: " + this.getW() + "\nheight=: " + this.getH() + "\narea=: " + this.area() + "");
}
public static void main(String[] args) {
//实例化对象
MyRectangle myrectangle = new MyRectangle(10, 30, 20, 10);
//打印输出
System.out.println(myrectangle);
}
}
测试结果:
width=: 10
height=: 20
area=: 200
//四个私有变量
private int xUp, yUp;
private int xDown, yDown;
//空构造函数
public MyRectangle() {
}
//带有四个参数的构造函数,并初始化
public MyRectangle(int xup, int yup, int xdown, int ydown) {
this.xUp = xup;
this.yUp = yup;
this.xDown = xdown;
this.yDown = ydown;
}
//获取宽度
public int getW() {
return this.xDown - this.xUp;
}
//获取高度
public int getH() {
return this.yUp - this.yDown;
}
//计算面积
public int area() {
return this.getW() * this.getH();
}
//重写toString返回宽度、高度、面积
public String toString() {
return ("width=: " + this.getW() + "\nheight=: " + this.getH() + "\narea=: " + this.area() + "");
}
public static void main(String[] args) {
//实例化对象
MyRectangle myrectangle = new MyRectangle(10, 30, 20, 10);
//打印输出
System.out.println(myrectangle);
}
}
测试结果:
width=: 10
height=: 20
area=: 200
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public class MyRectangle {
private Integer xUp;
private Integer yUp;
private Integer xDown;
private Integer yDown;
public MyRectangle(){
}
public MyRectangle(Integer x1,Integer y1,Integer x2,Integer y2){
this.xUp = x1;
this.yUp = y1;
this.xDown = x2;
this.yDown = y2;
}
public Integer getW(){
return xDown - xUp;
}
public Integer getH(){
return yDown - yUp;
}
public Integer area(){
return this.getW() * this.getH();
}
public String toString(){
return "宽度为:"+this.getW()+"\n高度为:"+this.getH()+"\n面积为:"+this.area();
}
public static void main(String[] args) {
MyRectangle myRectangle = new MyRectangle(0,0,15,20);
System.out.println(myRectangle.toString());
}
}
private Integer xUp;
private Integer yUp;
private Integer xDown;
private Integer yDown;
public MyRectangle(){
}
public MyRectangle(Integer x1,Integer y1,Integer x2,Integer y2){
this.xUp = x1;
this.yUp = y1;
this.xDown = x2;
this.yDown = y2;
}
public Integer getW(){
return xDown - xUp;
}
public Integer getH(){
return yDown - yUp;
}
public Integer area(){
return this.getW() * this.getH();
}
public String toString(){
return "宽度为:"+this.getW()+"\n高度为:"+this.getH()+"\n面积为:"+this.area();
}
public static void main(String[] args) {
MyRectangle myRectangle = new MyRectangle(0,0,15,20);
System.out.println(myRectangle.toString());
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2012-03-30
展开全部
自己动手丰衣足食
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询