public class Rectangle{ public Rectangle(int width,int height){ System.out.println(width*height); } public Rectangle(int double,int height){ System.out.println(width*height); } public Rectangle(int double,int double){ System.out.println(width*height); } }
public class Rectangle{ public Rectangle(int width,int height){ System.out.println("int,int"); System.out.println(width*height); } public Rectangle(double width,int height){ System.out.println("double,int"); System.out.println(width*height); } public Rectangle(double width,double height){ System.out.println("double,double"); System.out.println(width*height); } public static void main(String[] s){ new Rectangle(10,10); new Rectangle(5.0,10); new Rectangle(2.0,2.0); } }输出结果: int,int 100 double,int 50.0 double,double 4.0
public class Rectangle { private int wid; private int len; public Rectangle() { this(0,0); } public Rectangle(int wid,int len) { this.wid = wid; this.len=len; } //求面积 public long sumArea() { return wid*len; } //求周长 public long sumRound() { return 2*(len+wid); } public static void main(String[]args) { Rectangle r=new Rectangle(10,20); int round=r.sumRound(); System.out.println("周长为:"+round); int area=r.sumArea(); System.out.println("面积为:"+area); } }
这道题目我在JAVA 入门经典里见过。我把代码贴上来。有两个类。 Rectangle 和TestRectangle ``````````````````````````````````````````````````````````````````````````` public class Rectangle { private Point topLeft; private Point bottomRight; // Constructors public Rectangle(double x1, double y1, double x2, double y2) { // Ensure topleft uses the smaller of x1,x2 and y1,y2 // and topright uses the larger of x1,x2 and y1,y2 topLeft = new Point(Math.min(x1, x2), Math.min(y1, y2)); bottomRight = new Point(Math.max(x1, x2), Math.max(y1, y2)); } public Rectangle(Point tl, Point br) { this(tl.getX(), tl.getY(), br.getX(), br.getY()); } public Rectangle(Rectangle rect) { topLeft = new Point(rect.topLeft); bottomRight = new Point(rect.bottomRight); } // Methods to create a rectangle enclosing the current rectangle and the // argument public Rectangle encloses(Rectangle rect) { // Return a new rectangle defined by the minimum x,y for to left and the // and maximum x,y for bottom right return new Rectangle(Math.min(topLeft.getX(), rect.topLeft.getX()), Math.min(topLeft.getY(), rect.topLeft.getY()), Math.max( bottomRight.getX(), rect.bottomRight.getX()), Math.max( bottomRight.getY(), rect.bottomRight.getY())); } public String toString() { return "Rectangle: " + topLeft + " , " + bottomRight; } }··········································public class TestRectangle { public static void main(String args[]) { Rectangle[] rects = new Rectangle[4]; Rectangle enclosing; // Initialize the rectangles as arbitrary sizes and at arbitrary // positions: for (int i = 0; i < rects.length; i++) { rects[i] = new Rectangle(Math.random() * 100, Math.random() * 100, Math.random() * 100, Math.random() * 100); System.out.print("Coords of rectangle " + i + " are: "); System.out.println(rects[i]); } // Initialize the enclosing rectangle to be first rectangle enclosing = rects[0]; // Combine it with each the other rectangles in turn. // This will result in the rectangle that encloses them all. for (int i = 1; i < rects.length; i++) { enclosing = enclosing.encloses(rects[i]); } System.out.println("\nCoords of Enclosing rectangle are "); System.out.println(enclosing); } }
public Rectangle(int width,int height){
System.out.println(width*height);
}
public Rectangle(int double,int height){
System.out.println(width*height);
}
public Rectangle(int double,int double){
System.out.println(width*height);
}
}
private double long;
private double length; Rectangle(){
this(0.0, 0.0);
} Rectangle(double long, double length){
this.long=long;
this.length=length;
} public void showPerimeter(){
double perimeter;
perimeter = 2*(long+length);
System.out.println("周长为:"+ perimeter);
} public void showArea(){
double area;
area = long*length;
System.out.println("周长为:"+ area);
}}
public Rectangle(int width,int height){
System.out.println("int,int");
System.out.println(width*height);
}
public Rectangle(double width,int height){
System.out.println("double,int");
System.out.println(width*height);
}
public Rectangle(double width,double height){
System.out.println("double,double");
System.out.println(width*height);
} public static void main(String[] s){
new Rectangle(10,10);
new Rectangle(5.0,10);
new Rectangle(2.0,2.0); }
}输出结果:
int,int
100
double,int
50.0
double,double
4.0
{ /**
* @param args
*/
public static void main(String[] args)
{
Rectangle r = new Rectangle();
r.showPerimeter();
r.showArea();
Rectangle r1 = new Rectangle(3,5);
r1.showPerimeter();
r1.showArea();
}}class Rectangle
{
private double length; private double width; Rectangle()
{
this(0.0, 0.0);
} Rectangle(double length, double width)
{
this.length = length;
this.width = width;
} public void showPerimeter()
{
double perimeter;
perimeter = 2 * (length + width);
System.out.println("周长为:" + perimeter);
} public void showArea()
{
double area;
area = length * width;
System.out.println("面积为:" + area);
}}
运行结果:
周长为:0.0
面积为:0.0
周长为:16.0
面积为:15.0
大学好好学吧。赶快结帖吧。
{
private int wid;
private int len;
public Rectangle()
{
this(0,0);
}
public Rectangle(int wid,int len)
{
this.wid = wid;
this.len=len;
}
//求面积
public long sumArea()
{
return wid*len;
}
//求周长
public long sumRound()
{
return 2*(len+wid);
}
public static void main(String[]args)
{
Rectangle r=new Rectangle(10,20);
int round=r.sumRound();
System.out.println("周长为:"+round);
int area=r.sumArea();
System.out.println("面积为:"+area);
}
}
不妥吧,建议改为“double round=r.sumRound();”
public class Rectangle {
private Point topLeft; private Point bottomRight; // Constructors
public Rectangle(double x1, double y1, double x2, double y2) {
// Ensure topleft uses the smaller of x1,x2 and y1,y2
// and topright uses the larger of x1,x2 and y1,y2
topLeft = new Point(Math.min(x1, x2), Math.min(y1, y2));
bottomRight = new Point(Math.max(x1, x2), Math.max(y1, y2));
} public Rectangle(Point tl, Point br) {
this(tl.getX(), tl.getY(), br.getX(), br.getY());
} public Rectangle(Rectangle rect) {
topLeft = new Point(rect.topLeft);
bottomRight = new Point(rect.bottomRight);
} // Methods to create a rectangle enclosing the current rectangle and the
// argument
public Rectangle encloses(Rectangle rect) {
// Return a new rectangle defined by the minimum x,y for to left and the
// and maximum x,y for bottom right
return new Rectangle(Math.min(topLeft.getX(), rect.topLeft.getX()),
Math.min(topLeft.getY(), rect.topLeft.getY()), Math.max(
bottomRight.getX(), rect.bottomRight.getX()), Math.max(
bottomRight.getY(), rect.bottomRight.getY())); } public String toString() {
return "Rectangle: " + topLeft + " , " + bottomRight;
}
}··········································public class TestRectangle {
public static void main(String args[]) {
Rectangle[] rects = new Rectangle[4];
Rectangle enclosing; // Initialize the rectangles as arbitrary sizes and at arbitrary
// positions:
for (int i = 0; i < rects.length; i++) {
rects[i] = new Rectangle(Math.random() * 100, Math.random() * 100,
Math.random() * 100, Math.random() * 100);
System.out.print("Coords of rectangle " + i + " are: ");
System.out.println(rects[i]);
} // Initialize the enclosing rectangle to be first rectangle
enclosing = rects[0]; // Combine it with each the other rectangles in turn.
// This will result in the rectangle that encloses them all.
for (int i = 1; i < rects.length; i++) {
enclosing = enclosing.encloses(rects[i]);
} System.out.println("\nCoords of Enclosing rectangle are ");
System.out.println(enclosing);
}
}