Computing Science 331 – Spring 2017

Midterm Test

 

Name: ________________________________

 

Instructor: Bill Hohman

Time allowed:  85 minutes

Marks available:  57

 

 

1.      Answer the following short questions.

 

a)      Member functions can (usually) directly access the data members of an object.  Why can’t the main method?  (2 marks)

 

 

 

 

b)      How is it possible for an object to have data members that its member function cannot access directly?  (2 marks)

 

 

 

 

c)      Some (Java?) programmers would argue that it is easier to code polymorphic behaviour in Java than in C++.  What reason(s) would they give to “prove” their point?  (2 marks)

 

 

 

 

 

d)     What output does the following program produce?  Explain briefly – with the aid of a sketch of the memory used for the data in this program.  (4 marks)

public class Short1 {

    public static void main(String[] args) {

        String[] names = {"Adam", "Bob", "Chuck"};

        String[] newNames = names;

 

        newNames[0] = new String("SUE");

           

        for (String s : names)

            System.out.print(s + " ");

        System.out.println();

 

        for (String s : newNames)

            System.out.print(s + " ");

        System.out.println();

    }

}

 


e)      What is the effect of declaring an “instance” variable as static?  (2 marks)

 

 

 

 

 

 

 

 

 

f)       Consider the following code (stored in the file B.java) …

class A {

    private int size;

    public A () {

        this(1);

    }

    public A (int size) {

        this.size = size;

        System.out.println("Setting size " + size);

    }

    int getSize() {

        return size;

    }

}

 

public class B extends A {

    private String code;

    public B () {

        this(1, "aa");

    }

    public B (int size, String code) {

        this.code = code;

        System.out.println("Setting code '" + code + "'");

    }

 

    public static void main(String[] args) {

        B yourB = new B();

        B myB = new B(99, "nn");

    }

}

What output will be produced by compiling and running this code.  (3 marks)

 

 

 

 

 

What one line is “obviously missing” from it?  (No it’s not getCode.)   (1 mark)


2.      Assume the existence of the usual Die class:

public class Die {

    private int faceValue;

    public Die() {

        faceValue = 1;

    }

    public void roll() {

        faceValue = randomInt(1, 6);

    }

    private int randomInt( int low, int high) {

        return (int) Math.floor(low + Math.random() * (high-low+1));

    }

    public int getFaceValue() {

        return faceValue;

    }

}

 

Your task is to write the Java class PairOfDice so that the following main method will compile and run.   Note that you will need to write 4 methods.  (12  marks)

    public static void main(String[] args) {

        PairOfDice myDice = new PairOfDice();

        myDice.roll();

        System.out.println("I have rolled " + myDice);

        System.out.println("That's a " + myDice.getTotal());

    }

Note that if this main method is executed it should produce output of the following form:

I have rolled 2 and 5

That’s a 7

public class PairOfDice {

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


3.      Yes I know you were expecting Monopoly but … a chess board consists of 64 squares (eight rows and eight columns) arranged in two alternating colors (light and dark).  The board is always placed so that the rightmost square on the row nearest each player is a "white" square.  The columns (called files) are labelled by the letters a to h from left to right from the white player's point of view, and the rows (called ranks) by the numbers 1 to 8, with 1 being closest to the white player.

Each player starts the game with 16 pieces placed on the board: 8 pawns, 2 rooks, 2 knights, 2 bishops, and a king and queen.  Each piece has its own rules for moving on the board.

 

Black Player’s Side

Solid white.svg

a

b

c

d

e

f

g

h

Solid white.svg

8

 black king

 black king

 black king

 black king

 black king

 black king

 black king

 black king

8

7

 black king

 black king

 black king

 black king

 black king

 black king

 black king

 black king

7

6

 black king

 black king

 black king

 black king

 black king

 black king

 black king

 black king

6

5

 black king

 black king

 black king

 black king

 black king

 black king

 black king

 black king

5

4

 black king

 black king

 black king

 black king

 black king

 black king

 black king

 black king

4

3

 black king

 black king

 black king

 black king

 black king

 black king

 black king

 black king

3

2

 black king

 black king

 black king

 black king

 black king

 black king

 black king

 black king

2

1

 black king

 black king

 black king

 black king

 black king

 black king

 black king

 black king

1

Solid white.svg

a

b

c

d

e

f

g

h

Solid white.svg

White Player’s Side

 

a)      Sketch a class diagram for this problem – showing the “obvious” 3 or 4 classes.  Show associations and some multiplicity.   (5 marks)

 

 

 

 

 

 

 

 

 

 

 

 

 


b)      Write Java declarations for the class Square and the class Piece with the data and behaviour you have described in part (a).   (3 + 3 marks)

 


4.      And now finally some Monopoly!  Working from the Java code supplied on the handout …

a)      Draw a legible sequence diagram for the move method of the MonopolyGame class.   (6 marks)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

b)      Does the coding of the move method in part (a) more represent distributed control or centralized control?  Explain briefly.  (2 marks)

 

 

 

 

 

 

 

 

 

 

c)      Why might a Player object have the Board as an instance variable?  (2 marks)

 

 

 

 

 

 

 

 

 

 


d)     Write the Java code for class Player – at least everything that is known about it from the given code.  (8  marks)

public class Player {