ANSWERS!!
4.1.6 Using the Rectangle Class correct answers 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 answers 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;
}
public String toString()
{
return "Rectangle with width: " + width + " and height: " + height;
}
}
4.1.8 Using the Point Class correct answers PointTester.java:
, public class PointTester extends ConsoleProgram
{
public void run()
{
Point p = new Point(10, 5);
System.out.println(p);
p.move(3, 4);
System.out.println(p);
// Make a new point here at position (2, 4)
Point a = new Point(2, 4);
// Then print it out
System.out.println(a);
}
}
Point.java:
public class Point
{
private int x;
private int y;
public Point(int xCoord, int yCoord)
{
x = xCoord;
y = yCoord;
}
public void move(int dx, int dy)
{
x += dx;
y += dy;
}
public String toString()
{
return x + ", " + y;
}
}
4.1.9 Using the Student Class correct answers StudentTester.java:
public class StudentTester extends ConsoleProgram
{
public void run()
{
Student alan = new Student("Alan", "Turing", 11);
Student ada = new Student("Ada", "Lovelace", 12);
Student luke = new Student("Luke", "Drelick", 12);