Answers
4.1.6 Using the Rectangle Class ✔️Correct Ans-RectangleTester.java:
public class RectangleTester extends ConsoleProgram
{
public void run()
{
// Create a rectangle with width 5 and height 12
Rectangle room = new Rectangle(5,12);
// Then print it out
System.out.println(room);
}
}
Rectangle.java:
public class Rectangle
{
private int width;
private int height;
public Rectangle(int rectWidth, int rectHeight)
{
,width = rectWidth;
height = rectHeight;
}
public int getArea()
{
return width * height;
}
public int getHeight()
{
return height;
}
public int getWidth()
{
return width;
}
public String toString()
{
return "Rectangle with width: " + width + " and height: " + height;
}
,}
4.1.7 Calling A Method ✔️Correct Ans-RectangleTester.java:
public class RectangleTester extends ConsoleProgram
{
public void run()
{
/*
* Rectangle is the name of the class. Every Rectangle
* has a width and a height. But the specific instances
* have their own dimensions.
*/
Rectangle r1 = new Rectangle(7, 14);
System.out.println(r1.getHeight());
System.out.println(r1.getWidth());
System.out.println(r1.getArea());
}
}
Rectangle.java:
public class Rectangle
{
, private int width;
private int height;
public Rectangle(int rectWidth, int rectHeight)
{
width = rectWidth;
height = rectHeight;
}
public int getArea()
{
return width * height;
}
public int getHeight()
{
return height;
}
public int getWidth()
{
return width;
}