小弟只懂得C++和少量汇编,对Java不了解.受人之托 请教高人定义一个矩形类(Rectangle),类包括width,height和color三种属性,其中color的缺省颜色为白色。建立一个无参数构造器建立缺省的矩形,一个有参数构造器(传入width和height参数),编写与三个属性相关的get和set方法,和getArea和getPerimeter方法分别求面积和周长。
最后编写main方法建立矩形类对象,调用对象的方法计算面积和周长,并打印出来.
最后编写main方法建立矩形类对象,调用对象的方法计算面积和周长,并打印出来.
解决方案 »
- 通常这种问题,非牛人解答不可!
- 呼唤牛人!!急!!!!重定向后,cookie丢失怎么办?
- 如何调用函数打开一个pdf文件呢??
- 朋友求助的一个JAVA题目,偶也是不懂的,只能拿来请教各位了,先在此谢过. ^_^
- 怎样改变按钮上字体的大小和颜色???
- 应当简单吧?这些包在哪个文件夹下?
- 请问用sun.jdbc.odbc.JdbcOdbcDriver 连oracle数据库,url 怎样写?
- 我的文件该放到哪里,系统才能找到
- [散分100] 哪里有j2sdee的license.jar?
- 程序中报错时如何打印出当前源文件名和行号?
- JAVA初学者提问,请帮帮解释下一些问题!
- 看这段代码,为什么输入exit还不退出。
private int width = 0;
private int height = 0;
private int color = WRITE; Rectangle() {
}
Rectangle(int width, int height) {
this.width = width;
this.height = height;
}
int getWidth() { return width; }
void setWidth(int width) { this.width = width; } int getHeight() { return height; }
void setHeight(int height) { this.height = height; } int getColor() { return color; }
void setColor(int color) { this.color = color; }
}
应该修改为:
private Color color = Color.WHITE;
private int width = 0;
private int height = 0;
private Color color = Color.WHITE; Rectangle() { } Rectangle(int width, int height) {
this.width = width;
this.height = height;
} int getWidth() { return width; }
void setWidth(int width) { this.width = width; } int getHeight() { return height; }
void setHeight(int height) { this.height = height; } Color getColor() { return color; }
void setColor(Color color) { this.color = color; }}
这是全的,呵呵。根据上边的两个大哥说的该的。希望你能用上。
double getArea()
{
return this.width*this.height;
}
double getPerimeter()
{
return (this.width+this.height)*2;
}
void print()
{
System.out.println("面积是:"+this.getArea()+",周长是:"+this.getPerimeter())
}
public static void main(String args[])
{
Rectangle rec1=new Rectangle();
Rectangle rec2=new Rectangle(2.5,3);
rec1.print();
rec2.print();}
class Rectangle
{
private int height,width;
private Color color = Color.WHITE;
Rectangle(int width, int height)
{
this.width = width;
this.height = height;
}
int getPerimeter()
{
return 2*(height+width);
}
int getArea()
{
return height*width;
}
//照搬楼上的(不好意思)
int getWidth() { return width; }
void setWidth(int width) { this.width = width; } int getHeight() { return height; }
void setHeight(int height) { this.height = height; } int getColor() { return color; }
void setColor(int color) { this.color = color; } public static void main(String[] args)
{
Rectangle rect1=new Rectangle(30,40); //以宽30,长40为例 System.out.println("perimeter of Rectangle="+rect1.getPerimeter());
System.out.println("area of Rectangle="+rect1.getArea());
}
}
import java.util.Scanner;
public class Rectangle
{
private String color;
private Double width ;
private Double height ; //无参缺省的矩形
public Rectangle(){
System.out.println("无参缺省的矩形");
color = "WHITE";
width = 0.00;
height= 0.00;
} //有参的矩形
public Rectangle(Double w, Double h){
color = "WHITE";
width = w;
height= h;
}
public String getColor(){
return color;
} public Double getWidth(){
return width;
} public Double getHeight(){
return height;
} public void setColor(String str){
color = str ;
} public void setWidth(Double str){
width = str ;
} public void setHeight(Double str){
height = str ;
} public Double getArea(){
Double area ;
area = width * height;
return area;
} public Double getPerimeter(){
Double perimeter ;
perimeter = (width + height)*2;
return perimeter;
}
public static void main(String[] args)
{
Rectangle rectange = new Rectangle();
System.out.println("新建一个无参Rectangle");
System.out.println("the Area is :"+rectange.getArea());
System.out.println("the Perimeter is :"+rectange.getPerimeter()); Double a, b;
System.out.println("请输入矩形的宽和高:");
Scanner sc = new Scanner(System.in);
a = sc.nextDouble();
b = sc.nextDouble();
System.out.println("a:"+a+" b:"+b);
Rectangle rectange2 = new Rectangle(a,b);
System.out.println("新建一个有参Rectangle");
System.out.println("the Area is :"+rectange2.getArea());
System.out.println("the Perimeter is :"+rectange2.getPerimeter());
}
}