CSE1030 Test 1R

Thu Oct 10
1:00PM - 2:20PM

Introduction

This test consists of a short programming question and some short answer written questions. I recommend budgeting 50 minutes for the programming question and 30 minutes for the written questions.

Please follow the instructions given in the test.

Setting Up

In eclipse:

  1. Create a new Java Project (perhaps called test1)
  2. In your project, create a new Package called cse1030.test1
  3. In the package you just created, create an empty text file named answers.txt for your answers to the written questions:
    1. select the File menu
    2. select New
    3. select File
    4. type answers.txt for the file name and press Finish

Programming Question

It is expected that your program compiles, meets the specifications given in the API, and follows the style guidelines for CSE1030. Javadoc comments are not required.


The game rock-paper-scissors is played by two people who at the same time form one of three values using their hand. The three values are:

  1. rock
  2. paper
  3. scissors

The rules to determine the winner are as follows:

If both players choose the same value then there is no winner.

Complete the immutable class Rps that represents the value chosen by a rock-paper-scissors player. Your class must implement the following API:

You should use a String to represent the value chosen by the player.

You must create and complete the following fields, constructors, and methods:

With the exception of equals, all of your constructors and methods should be very short (4 lines of code or less).

A small test program is available to help you debug your program:

The test program generates the following output for a correct program:

default constructor: rock

constructor:  paper
constructor:  scissors
constructor:  exception was thrown correctly

copy constructor: rock!
copy constructor: paper!
copy constructor: scissors!

compare:  rock beats scissors ? 1
compare:  scissors beats paper? 1
compare:  paper beats rock    ? 1

compare:  scissors beats rock ? -1
compare:  paper beats scissors? -1
compare:  rock beats paper    ? -1

defeats:  what beats "rock"     ? paper!
defeats:  what beats "scissors" ? rock!

equals:   rock equals rock    ? true
equals:   scissors equals rock? false
equals:   paper equals paper  ? true
equals:   paper equals null   ? false

Written Questions

Type the answers to the written questions into the file answers.txt that you were instructed to create earlier. Make sure that you number your answers to match the questions.

1. Consider the following Java class where all of its fields are shown:

import cse1030.test1;
import java.util.Date;

public class Y {

  public static final int C = 299792458;                         // speed of light in vacuum
  public static final Rps ROCK = new Rps("rock");                // the rock-paper-scissors hand rock
  public static final Date TEST1 = new Date(1381424400216L);     // Oct 10, 2013 1PM

  private Date now;

  public Y() {
    this.now = new Date();
  }

  public static void main(String[] args) {
    Y thisIs = new Y();
    Y silly = new Y();     // ???
    
    // more code here not shown
  }
}

(a) [2 marks] In general, what are public static final fields (written in all capital letters) supposed to represent?

Constant values (i.e., constant primitive values or constant immutable reference types).

(b) [2 marks] Given your answer to (a), list all of the public static final fields in Y that are acceptable; explain your answer.

C is acceptable because it is a primitive type.
ROCK is acceptable because it is an immutable reference type.

(c) [2 marks] Given your answer to (a), list all of the public static final fields in Y that are not acceptable; explain your answer.

TEST1 is not acceptable because it is a mutable reference type.

(d) [2 marks] After the line marked ??? in the main method runs, how many Date objects are in memory? Explain your answer.

There are 2 Y objects and each Y object has 1 Date object. There is also one static Date object that belongs to the Y class. In total, there are 3 Date objects.



2. Suppose that you made the String that stores the value of a rock-paper-scissors hand public in your implementation of the Rps class. We say that such an implementation has poor encapsulation.

(a) [2 marks] In one or two sentences, explain what you think the term poor encapsulation means.

Poor encapsulation means that the implementation details of the class are visible to clients.

(b) [2 marks] Would a client be able to change the value of the hand if the String that stores the value of a rock-paper-scissors hand was public? Explain your answer.

No, because String is immutable and that field should be final because Rps is also immutable. The fact that the field should also be final is important; if it is not final then the following is possible:

public final class Rps {
  // the hand; NOT final
  public String hand;

  public Rps(String theHand) {
    // code that validates theHand here

    this.hand = theHand;
  }

  // ...
}

// client code
Rps rock = new Rps("rock");
rock.hand = "scissors";

(c) [2 marks] State one negative consequence of making the String that stores the value of a rock-paper-scissors hand public in the Rps class. (Note: stating that the client can change the value of the hand is not an acceptable answer because I already asked you about this in Question 2b).

Clients can see how the hand is implemented.
The field cannot be made private without breaking all existing client code that currently uses the public field.



3. [4 marks] The method compare in Rps strongly resembles the compareTo method from the Comparable interface; however, the Rps class does not implement the Comparable interface (in fact, it cannot correctly implement the Comparable interface). There is a good reason why Rps does not implement the Comparable interface: explain what the reason is.

The Comparable interface requires that there be a natural ordering for Rps objects. There is no natural ordering for Rps objects because every hand loses to and defeats some other hand.



4. [2 marks] The method defeats looks like a constructor in that it returns a reference to a new Rps object. Why is it not a constructor, and what is the name given to such a method?

This question should not have been asked because it was not covered in the lectures being tested.

Making it a constructor would require the signature Rps(Rps) which already exists in the class. Methods such as defeats are called static factory methods.


Submit

Submit your answers to the written questions and your Java program when you are finished:

submit 1030 Test1 Rps.java answers.txt