public class Rectangle { private double len; private double wid; private double
ID: 3693610 • Letter: P
Question
public class Rectangle
{
private double len;
private double wid;
private double x;
private double y;
public Rectangle()
{ len = 1; wid = 1; x = 0; y = 0; }
public Rectangle(double l, double w)
{ len = l; wid = w; x = 0; y = 0; }
public Rectangle(double l, double w, double x1, double y1)
{ len = l; wid = w; x = x1; y = y1; }
public double disparea()
{ return len*wid; }
public void scale(double factor)
{
len = len*factor; wid *= factor; }
public void movehoriz(double x1)
{ x += x1; }
//needing to set up three shapes 2x3 at origin, 4x4 at (1,1), and 5x1 at (-1,-2); double the size of the 4x4, move the 2x3 down by 2; and display the final size and locations
Explanation / Answer
//I took the liberty of adding some more methods to make the class complete.
public class Rectangle
{
private double len;
private double wid;
private double x;
private double y;
public Rectangle()
{ len = 1; wid = 1; x = 0; y = 0; }
public Rectangle(double l, double w)
{ len = l; wid = w; x = 0; y = 0; }
public Rectangle(double l, double w, double x1, double y1)
{ len = l; wid = w; x = x1; y = y1; }
public double disparea()
{ return len*wid; }
public void scale(double factor)
{
len = len*factor; wid *= factor; }
public void movehoriz(double x1)
{ x += x1; }
public void moveverti(double y1)
{ y += y1; }
public double getlen()
{
return len;
}
public double getwid()
{
return wid;
}
public double getx()
{
return x;
}
public double gety()
{
return y;
}
}
public class TestRectangle {
public static void main(String[] args)
{
Rectangle r1 = new Rectangle(2, 3, 0, 0);
Rectangle r2 = new Rectangle(4, 4, 1, 1);
Rectangle r3 = new Rectangle(5, 1, -1, -2);
r2.scale(2);
r1.moveverti(-2);
System.out.println("The size of first rectangle is "+r1.getlen()+" X "+r1.getwid());
System.out.println("The location of first rectangle is ("+r1.getx()+","+r1.gety()+")");
System.out.println("The size of second rectangle is "+r2.getlen()+" X "+r2.getwid());
System.out.println("The location of second rectangle is ("+r2.getx()+","+r2.gety()+")");
System.out.println("The size of third rectangle is "+r3.getlen()+" X "+r3.getwid());
System.out.println("The location of third rectangle is ("+r3.getx()+","+r3.gety()+")");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.