
CMSC 131 Quiz 4 Worksheet
The third Quiz of the course will be on Monday, Oct 18 during your lab session (either at
10 am or 11 am). The following list provides more information about the quiz:
• You will have 20 minutes to complete the quiz.
• It will be a written quiz (not using any computer).
• It will be closed-book, closed-notes, and no calculator is allowed.
• Answers must be neat and legible. We recommend that you use pencil and eraser.
• The quiz will be based on the exercises you will find below. The quiz will ask
you to write a class for a particular application.
The following exercises cover the material to be covered in Quiz #4. Solutions to these
exercises will not be provided, but you are welcome to discuss your solutions with TAs
and instructors during office hours.
Problem 1
For this problem you will write a complete Java class called Triangle, which represents a
mathematical triangle. The class has the following private instance variables:
base - double value representing the base of a triangle
height – double value representing the height of the rectangle
All the following methods are non-static except the method named area.
1. Default Constructor – Initializes a triangle with base and height values equal to 1.
2. Constructor – This constructor takes two formal parameters representing the base and
height respectively. The parameters are used to initialize the corresponding instance
variables.
3. getBase – This method returns the base value.
4. getHeight – This method returns the height value.
5. setBase – This method updates the base value with the single parameter.
6. setHeight – This method updates the height value with the single parameter.
7. toString – Returns a String with the string “Triangle:” followed by base and height of the
triangle (separated by a blank space).
8. equals – Boolean method that tests whether the current object has the same base and
height as another Triangle, which is given as the single parameter.
9. tripleBase – Returns a reference to a new Triangle whose base is three times the base of
the current object. The height of the new object will be the current object’s height. The
current object will not be modified.
10. area – This is a static method that takes as formal parameter a Triangle reference and
returns a double representing the area of the triangle.