Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit


COMP125 Fundamentals of Computer Science

Module Exam

Classes and Objects

QUESTION SET 9

 

Instructions

● Write your answer neatly in space provided after each question, request blank pages if you need more space. Answers must be clear enough to read in order to be marked

● Any attempt of plagiarizing will result in an automatic zero.


Questions

Question 1   (40 points)

Write a definition for a class that encapsulates a book represented by its title and price. The class definiton should have,

(a)  (10 points)  data members, with correct data types

(b)  (10 points)  getters and setters for each data members

(c)  (10 points)  default constructor, setting the title to ”tba”and price to 0.

(d)  (10 points)  parameterized constructor

 

Question 2   (60 points)


Consider the following definition for class Rectangle,

class  Rectangle   {

private  double  width ,  height ;

public  void  setWidth (double  w)   {

width  =  Math . abs (w);

}

public  double  getWidth ()   {

return  width ;

}

//assume  similar  getter  and  setter  for  height

public  Rectangle (double  w ,  double  h)   {

setWidth (w);

setHeight (h);

}

public  boolean  isSquare ()   {

return  width  ==  height ;

}

public  double  area ()   {

return  width  *  height ;

}

}

Perform the following tasks,

(a)  (15 points)  Declare and instantiate an object myRectangle of class Rectangle that represents a rectangle

with 2.5 width and 5.6 height.

(b)  (15 points)  Add a method perimeter in class Rectangle that returns the perimeter of the rectangle repre-sented by the object on which the method is called. Perimeter of a rectangle is defined as the sum of the four sides

(c)  (15 points)  Complete the method equals in class Rectangle that returns true if the rectangle represented by the calling object equals the rectangle passed as the parameter based on areas, and false otherwise. Note the method area() has been defined in the class.


public  boolean  equals ( Rectangle  other )   {

 

 

 

 

 

 

 

}