CMSC 131 Quiz 1 Practice, Quizzes of Computer Science

Quiz 1 for intro to object-oriented programming class practice quiz

Typology: Quizzes

2021/2022

Uploaded on 02/12/2023

Bethel567
Bethel567 🇺🇸

5 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CMSC133, Fall 2021, QUIZ #1 (DURATION: 20 MINUTES)
FIRSTNAME, LASTNAME (PRINT IN UPPERCASE):
STUDENT ID (e.g. 123456789):
INSTRUCTIONS:
Provide answers in the rectangular areas provided.
You will be penalized if your code is not efficient.
On the reverse side you will see a driver and expected output that relies on the class you are expected to implement. You can ignore it
if you know what to implement. The driver relies on a method (getCount()) that you don’t need to implement.
Define a class called Room based on the specifications below. The class defines the following private variables:
private String id; /* Represents the room’s id */
private int beds; /* Specifies the number of beds in the room */
private double cost; /* Daily cost of the room in dollars */
private String guests; /* String with the names of guests */
private static int count; /* Keeps track of the number of Room objects created */
1. Constructor - It has three parameters (id, beds, and cost). The constructor will initialize the corresponding instance variables and
update the count static variable so we can keep track of how many Room objects have been created. Also the method will
initialize the guests instance variable to the empty string. You can assume the parameters are valid (e.g., no null references and
numeric values are greater than or equal to 0).
2. Default constructor - It initializes the object with id, beds, and cost values that correspond to “NONE”, 2, and 50.25,
respectively. You can implement this method using any approach. Remember to increase the count variable.
3. Copy constructor - Creates a duplicate of the parameter in such a way that changes to the parameter will not affect the new
object. You can implement this method using any approach. Remember to increase the count variable.
ADDITIONAL QUESTIONS ON THE REVERSE SIDE.
pf2

Partial preview of the text

Download CMSC 131 Quiz 1 Practice and more Quizzes Computer Science in PDF only on Docsity!

CMSC133, Fall 2021, QUIZ #1 (DURATION: 20 MINUTES)

FIRSTNAME, LASTNAME (PRINT IN UPPERCASE):

STUDENT ID (e.g. 123456789):

INSTRUCTIONS:

 Provide answers in the rectangular areas provided.  You will be penalized if your code is not efficient.  On the reverse side you will see a driver and expected output that relies on the class you are expected to implement. You can ignore it if you know what to implement. The driver relies on a method (getCount()) that you don’t need to implement.

Define a class called Room based on the specifications below. The class defines the following private variables:

private String id; /* Represents the room ’s id / private int beds; / Specifies the number of beds in the room / private double cost; / Daily cost of the room in dollars / private String guests; / String with the names of guests / private static int count; / Keeps track of the number of Room objects created */

  1. Constructor - It has three parameters (id, beds, and cost). The constructor will initialize the corresponding instance variables and update the count static variable so we can keep track of how many Room objects have been created. Also the method will initialize the guests instance variable to the empty string. You can assume the parameters are valid (e.g., no null references and numeric values are greater than or equal to 0).
  2. Default constructor - It initializes the object with id, beds, and cost values that correspond to “NONE”, 2, and 50.25, respectively. You can implement this method using any approach. Remember to increase the count variable.
  3. Copy constructor - Creates a duplicate of the parameter in such a way that changes to the parameter will not affect the new object. You can implement this method using any approach. Remember to increase the count variable.

ADDITIONAL QUESTIONS ON THE REVERSE SIDE.

  1. toString - Define a toString() method that returns a string with the id, beds, cost, and guests values separated by commas. For the guests value, add the string “Guests: “ before the guests. Take a look at the sample driver we provided for an example of the expected output format.
  2. addGuest - Takes a string as parameter and appends the parameter to the guests instance variable. It returns a reference to the current object. You can assume the parameter will be valid (not null and not the empty string).

Driver / Expected Output (Feel free to ignore)

Driver

String id = "Room1"; int beds = 2; double cost = 50.75;

Room room = new Room(id, beds, cost); Room copy = new Room(room); copy.addGuest("Mary").addGuest("Peter");

System.out.println("***** Original Room *****"); System.out.println(room);

System.out.println("***** Room copy info *****"); System.out.println(copy);

System.out.println("***** Last room *****"); System.out.println(new Room());

System.out.println("Total Number of Rooms: " + Room.getCount());

Output

***** Original Room ***** Room1, 2, 50.75, Guests: ***** Room copy info ***** Room1, 2, 50.75, Guests: MaryPeter ***** Last room ***** NONE, 2, 50.25, Guests: Total Number of Rooms: 3