Docsity

Quiz 4 Worksheet - Object-Oriented Program I | CMSC 131, Quizzes of Computer Science

Toggle sidebar
bg1
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.
pf3

Partial preview of the text

Download Quiz 4 Worksheet - Object-Oriented Program I | CMSC 131 and more Quizzes Computer Science in PDF only on Docsity!

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.

Problem 2

Define a Student class which represents a college student. A student has a name (String reference), a gpa (double), and a student id (integer). The class has the following methods which you must consider non-static unless indicated otherwise.

  1. Default constructor – Initializes the name, gpa, and id variables with the values “NONAME”, 0.0, and -1 respectively.

2. Constructor – Takes a name, gpa, and id values as parameters and initializes the current

object accordingly.

3. equals – Determines whether the current object and the single parameter value represent

the same student.

4. increaseGPA – Increases the student gpa by the single parameter value.

5. toString – Generates a String with all the values associated with a student object

using the format: “Student: “ followed by the name, gpa and id (each separated by

a blank character).

6. Write the appropriate accessor and mutators methods associated with the class.

For example, getId is the accessor for id and setId the mutator.

7. Write a copy constructor for your Student class.

8. Write a method called notEquals which uses the equals method you defined

above.

9. Write a static method called total that returns the number of Student object

instances that have been created. Feel free to add any instance variables that will

allow you to keep track of the number of Student objects created.

Problem 3

Define a class called Scores. The class keeps track of scores associated with a student. The class has the following instance variables:

  1. name – String representing the name of the student.
  2. sum – double representing the sum of the scores.
  3. numberScores – the current number of scores accumulated in the sum instance variable.

The class has the following non-static methods:

  1. Constructor – Takes as parameter a String reference representing the name of the student. Initializes the sum and numberScores to 0.
  2. getName – Returns the student’s name.
  3. validScore – private method that determines whether a score is valid. A score is considered valid if it is in the range 0 → 100.
  4. addScore – Increases the sum by the single parameter value if the parameter represents a valid score.
  5. numGrade – It has no parameters. It returns the numeric grade of the student. The numeric grade is the average of the scores.