Quiz 05

Quiz 05



Important notes:
  1. Do not start until you are instructed to do so!
  2. We suggest you do not use your browser's zoom feature. Instead...
  3. You will have 20 minutes once the proctor says to start.
  4. You will have brief additional time after we stop to scan and submit your solutions.

  5. Just before the quiz...
    1. Have a fully-charged smartphone and laptop, and still plug both in if possible
    2. Log into Gradescope on your phone
    3. Change the screen timeout setting on your phone to never, so your screen doesn't go black if you don't interact with your screen for a while.
      • iPhones: Settings / Display & Brightness / Auto-Lock / Never
      • Android: Settings / Display / Screen timeout / 10 minutes (or the maximum amount of time)
    4. Turn on Do Not Disturb (or otherwise turn off all notifications).
    5. Position your webcam so we can see:
      • Your desk
      • The paper you are working on
      • Your writing utensil(s)
      • Both of your hands
      • Your phone

  6. During the quiz:
    1. You may not ask questions during the exam.
      • If you are unsure how to interpret a problem, take your best guess.
      • If you feel a question is unfair because you could not ask a question about it, please email the instructor after the quiz and describe your concern. (We are working hard to make the questions extra clear this year, but we want to know when we fail.)
    2. You may not touch your laptop or webcam.
      • This includes muting yourself at any point; the proctors may mute you though.
    3. All of these must be visible at all times:
      • Your desk
      • The paper you are working on
      • Your writing utensil(s)
      • Both of your hands
      • Your phone, with the quiz webpage
    4. For any tech fails (laptop or internet stops working, etc.):
      1. Stop taking the quiz
      2. Email the instructor (rileyrd@cmu.edu) right away
      3. We will reply soon to set up a 1-on-1 oral quiz with the course faculty

  7. After the quiz:
    1. Follow all proctor instructions on how to end the quiz.
    2. If you finish early, wait patiently until the end of the quiz. (Note: Don't use your phone...)
    3. Keep everything in view (as noted above) until the proctor calls "time".
    4. When instructed, use your phone to scan your quiz and submit the PDF to Gradescope.
    5. After submitting to Gradescope, hold your phone up to the webcam to show the receipt.
    6. Even then, remain in quiz mode until the proctor calls "all clear"

1. Short Answer [3 pts]

What is the difference between an abstract data type and a data structure? (Write 1-3 sentences.)


2. True or False [3 pts]

For this question, clearly state whether the following statement is true or false. Then, write one sentence to justify your answer. (Note: Your justification is the most important part of your answer.)

For loops are always O(N).


3. Free Response: Marble [8 pts]

Consider the following class:

public class Marble {
    private ArrayList<String> colors;

    public Marble(String marbleColors) {
        // Your code goes here
    }

    public boolean addColor(String newColor) {
        // Your code goes here
    }

    public String toString() {
        String ret = "Marble ( ";
        for(String s: this.colors) {
            ret += s + " ";
        }
        return ret + ")";
    }
}

Also consider the following testcode:

/* The constructor takes a string
 * of comma separated colors
 */
Marble m = new Marble("red,blue");
System.out.println(m);

/* Add green. Returns true because
 * green is successfully added
 */
boolean res;
res = m.addColor("green");
System.out.println(res);

/* Add red. Returns false because
 * red is already one of the colors
 */
res = m.addColor("red");
System.out.println(res);
System.out.println(m);

With a proper Marble class, this code will output:

Marble ( red blue )
true
false
Marble ( red blue green )
A. The constructor

Write the constructor Marble(String marbleColors) for the Marble class. You do not need to rewrite the class declaration or the method prototype, just the method’s code is fine. The code should function as demonstrated by the testcode.

B. addColor

Write the addColor method of the Marble class. You do not need to rewrite the class declaration or the method prototype, just the method’s code is fine. The code should function as demonstrated by the testcode.


4. Free Response: ConstantMarble [6 pts]

This problem builds on the previous problem. If your Marble solution does not work, that is fine: It is still possible to receive full points on this problem.

A ConstantMarble is-a Marble, but it is one that cannot have its colors modified after it is created.

Consider the following testcode:

/* The constructor here functions the same
 * as a standard Marble
 */
Marble cm = new ConstantMarble("red,blue");
System.out.println(cm);

/* Add green. Returns false because you
 * cannot add a color to a ConstantMarble
 */
boolean res;
res = cm.addColor("green");
System.out.println(res);
System.out.println(cm);

With a proper Marble class and ConstantMarble class, this code will output:

ConstantMarble ( red blue )
false
ConstantMarble ( red blue )

Write the ConstantMarble class. For full credit, you must properly make use of inheritance, including avoiding unnecessary code duplication.