Question 1

advertisement
Question 1.
Bulb.java
class Bulb
{
boolean state;
static final boolean ON = true, OFF = false;
Bulb()
{
state = OFF;
}
Bulb(boolean state)
{
this.state = state;
}
boolean isOn()
{
return state;
}
boolean isOff()
{
return !state;
}
boolean getState()
{
return state;
}
void change()
{
state = !state;
}
String stateToString()
{
if(state == ON) return "On";
else return "Off";
}
}
TestBulb.java
class TestBulb{
public static void main(String[] args){
Bulb b = new Bulb();
System.out.println("Bulb is now " + b.stateToString() + ".");
System.out.println("Change the state ....");
b.change();
System.out.println("Bulb is now " + b.stateToString() + ".");
System.out.println("\nCreating a new bulb...");
Bulb bTwo = new Bulb();
if(bTwo.getState() == Bulb.ON)
System.out.println("The bulb is on.");
else
System.out.println("The bulb is off.");
}
}
We declared ON and OFF static because we can access it even though no object bulb
object is declared. These are class wide variables and common for all the objects. Final is
due to fact that these are not more than informative variables. We make the true or false
to the more readable form that is convenient to our situation like the states of bulb. So
these are declared once and no need to modify these variables.
Question 2.
PTrafficLight.java
class PTrafficLight
{
private Bulb red, green;
PTrafficLight()
{
red = new Bulb(Bulb.ON);
green = new Bulb();
}
void change()
{
red.change();
green.change();
}
Bulb getGreen()
{
return green;
}
Bulb getRed()
{
return red;
}
}
TestPTrafficLight.java
class TestPTrafficLight
{
public static void main(String str[])
{
PTrafficLight pt = new PTrafficLight();
System.out.println("Red: "+pt.getRed().stateToString());
System.out.println("Green: "+pt.getGreen().stateToString());
pt.change();
System.out.println("\nState Changed");
System.out.println("\nRed: "+pt.getRed().stateToString());
System.out.println("Green: "+pt.getGreen().stateToString());
}
}
Problem 3.
(a)Rectange.java
class Rectangle
{
private static int count = 0;
private int x, y, width, height;
Rectangle(int x, int y, int width, int height)
{
this.x = x;
this.y = y;
this.width = width;
this.height = height;
count++;
}
public void translate(int deltaX, int deltaY)
{
x += deltaX;
y += deltaY;
}
public long area()
{
return (long)width * height;
}
public boolean contains(int x, int y)
{
return ((this.x <= x && this.y <= y) && (this.x + width >= x && this.y +
height >= y));
}
public String toString()
{
return "("+x+", "+y+"), "+width+" X "+height;
}
public static int getCreated()
{
return count;
}
}
(b)TestRectangle.java
class TestRectangle
{
public static void main(String str[])
{
System.out.println("Number of Rectangles: "+Rectangle.getCreated());
Rectangle rec1 = new Rectangle(10, 12, 100, 50);
System.out.println("Rectangle object rec1 created");
System.out.println("Number of Rectangles: "+Rectangle.getCreated());
System.out.println("String value of rec1: "+rec1);
System.out.println("Area of rec1: "+rec1.area());
if(rec1.contains(10, 12))
System.out.println("(10, 12) is in rec1");
else
System.out.println("(10, 12) not in rec1");
if(rec1.contains(110, 62))
System.out.println("(110, 62) is in rec1");
else
System.out.println("(110, 62) not in rec1");
if(rec1.contains(100, 50))
System.out.println("(110, 62) is in rec1");
else
System.out.println("(110, 62) not in rec1");
if(rec1.contains(2, 2))
System.out.println("(2, 2) is in rec1");
else
System.out.println("(2, 2) not in rec1");
if(rec1.contains(112, 65))
System.out.println("(112, 65) is in rec1");
else
System.out.println("(112, 65) not in rec1");
rec1.translate(30, 20);
System.out.println("\n\nrec1 translated by 30 and 20");
System.out.println("String value of rec1: "+rec1);
System.out.println("Area of rec1: "+rec1.area());
if(rec1.contains(10, 12))
System.out.println("(10, 12) is in rec1");
else
System.out.println("(10, 12) not in rec1");
if(rec1.contains(110, 62))
System.out.println("(110, 62) is in rec1");
else
System.out.println("(110, 62) not in rec1");
if(rec1.contains(100, 50))
System.out.println("(110, 62) is in rec1");
else
System.out.println("(110, 62) not in rec1");
if(rec1.contains(2, 2))
System.out.println("(2, 2) is in rec1");
else
System.out.println("(2, 2) not in rec1");
if(rec1.contains(112, 65))
System.out.println("(112, 65) is in rec1");
else
System.out.println("(112, 65) not in rec1");
Rectangle rec2 = new Rectangle(10, 12, 100, 50);
System.out.println("\n\nRectangle object rec2 created");
System.out.println("Number of Rectangles: "+Rectangle.getCreated());
}
}
(c)As the count variable in rectangle class keeps the track f number of instances that have
been created. This is the class wide information and we need that information even
though no instance of Rectangle exists. It’s the common information that is same for all
the objects. All information except count is the property of each object itself. It represents
the state of individual objects. The last method getCreated() gives the count of created
object by accessing the class name.
(d)Nearly all instance fields are declared with private access. As these are the properties
of the object and needed to instantiate the objects. All behaviors like area and contains
depends on the current state information. We really don’t need to play these properties in
outer world. We protect these variables with most protection. No one is able to turn the
object into unwanted behavior. toString method is needed public as Object’s (Super Class
of All classes) toString overloading is not allowed here with less protective access.
(e)I have traversed nearly every path of test here. First it is checked to see number of
created instances. Then rectangle created and then number of instances created checked.
Its string representation checked then area checked and then exhaustive check of contains
method checked. Nearly every point on each side and inside and on the edges checked
here. Afterward it is so translated that some of the points remains inside and some are
shifted outside (Points checked for contains method). All string representation, area and
point containment checked as for the rectangle before translation. Then another rectangle
is created and again there is the check for number of instance created.
Download