Java

OOP GUIDE / WORK

 

RECTANGLE CLASS 1

 

Topics

  • Code analysis, instance variables, instance methods

 

 

TASK – CODE ANALYSIS QUESTIONS

 

Consider the following class and answer the questions at the bottom:

 


public
class Rectangle

{

    public double x, y;     //x and y of top left corner

    public double w, h;     //width and height

    public String colour;   //ie: "pink" or "blue"

    

    public String toString()

    {

     String s = "Top left corner: (" + x + "," + y + "), ";

     s = s + "Width: " + w + ", Height: " + h + ", ";

     s = s + "Colour: " + colour;

     return s;

    }

    

    public double area()

    {

     return w * h;

    }

}

 

 

 

a)    What is the name of the class?

b)    What is the name of the file that this code gets stored into?

c)     How many instance variables do this class have?

d)    How many datafields do this class have?

e)    How many constructors does this class have?

f)      How many instance methods does this class have?

g)    What type of data does the toString() method return?

h)    Which instance variables does the toString() method use?

i)       What type of data does the area() method return?

j)       Which instance variables does the area() method use?

k)     Write the code needed to create a rectangle that has (x,y) = (4,9), a width of 6, a height of 3 and a “red” colour.

l)       Where would your place the above code to construct the rectangle?

m)   What is the statement needed to output to screen the area of the rectangle that you created above?