Cmsc150 Fundamentals of Computing I

CmSc150 Fundamentals of Computing I

Homework 08 due 11/05 by 5 pm. name______

Part 1: Problems

1)  The behavior of an object is defined by the object’s

a)  instance data

b)  constructor

c)  visibility modifiers

d)  methods

e)  all of the above

2)  Which of the following reserved words in Java is used to create an instance of a class?

a)  class

b)  public

c)  public or private, either could be used

d)  import

e)  new

3)  Consider a sequence of method invocations as follows: main calls m1, m1 calls m2, m2 calls m3 and then m2 calls m4, m3 calls m5. If m4 has just terminated, what method will resume execution?

a)  m1

b)  m2

c)  m3

d)  m5

e)  main

4)  A variable whose scope is restricted to the method where it was declared is known as

a)  parameter

b)  global variable

c)  local variable

d)  public instance data

e)  private instance data

5)  A class’ constructor usually defines

a)  how an object is initialized

b)  how an object is interfaced

c)  the number of instance data in the class

d)  the number of methods in the class

e)  if the instance data are accessible outside of the object directly

Consider a Coin class that consists of a constructor, and methods flip, getFace and toString. The method getFace returns an int, 0 if the last flip was a Heads, and 1 if the last flip was a Tails. The toString method returns a String equal to “Heads” or “Tails” depending on the result of the last flip. Using this information, answer questions 6 – 8

6)  A set of code has already instantiated c to be a Coin and has input a String guess, from the user asking whether the user guesses that a coin flip will result in “Heads” or “Tails”. Which of the following sets of code will perform the coin flip and see if the user’s guess was right or wrong?

a)  c.flip( );

if (c.getFace( ).equals(guess))

System.out.println("User is correct");

b)  if (c.flip( ).equals(guess))

System.out.println("User is correct");

c)  if (c.getFace( ).equals(guess))

System.out.println("User is correct");

d)  c.flip( );

if (c.toString( ).equals(guess))

System.out.println("User is correct");

e)  c.flip( ).toString( );

if (c.equals(guess)) System.out.println("User is correct");

7)  What does the following code compute?

int num = 0;

for (int j = 0; j < 1000; j++)

{

c.flip( );

if (c.getFace( ) == 0) num++;

}

double value = (double) num / 1000;

a)  the number of Heads flipped out of 1000 flips

b)  the number of Heads flipped in a row out of 1000 flips

c)  the number of Heads flipped out of 1000 flips and the percentage of heads flipped out of 1000 flips

d)  the percentage of times neither Heads nor Tails were flipped out of 1000 flips

e)  nothing at all

8)  What value does total represent (compute)?

int num = 0, total = 0, last = -1;

while (num < 3)

{

c.flip( );

total++;

if (num==0 & c.getFace( ) == 0) num = 1;

else

if (c.getFace( ) == 0 & last == 0) num++;

else num = 0;

last = c.getFace( );

}

a)  The number of flips it takes to get 3 Heads flipped

b)  The number of flips it takes to get 3 Heads flipped in a row

c)  The number of flips it takes to get 2 Heads flipped in a row 3 times

d)  The number of flips it takes to get 3 Heads flipped in a row twice

e)  The number of flips it takes to get 9 Heads flipped

Part 2: Programming assignment

For both projects: do not forget to write the title comments, your name, and inline comments accompanying the code.

1. Arrays

Write a program that reverses an array of randomly generated integers.

For example, given an array of 5 elements: 3, 5, 1, 6, 7 its reversed content will be: 7, 6, 1, 5, 3.

1. Functional specifications:

The program should do the following:

a. Prompts the user to enter the size of the array and reads the size.

b. Prompts the user to enter the upper limit for the generated numbers and reads the limit.

c. Creates an array with the given size of randomly generated integers less than the specified limit.

d. Prints the array 10 elements per row

e. Reverses the array

f. Prints the reversed array 10 elements per row.

g. Asks the user if the program is to be run again and if the answer is yes, repeats steps a) through g).

The output of the program should look like this (user input is underlined):

Please enter size of array: 5

Please enter upper limit: 50

Elements of the array are:

4 25 1 38 48 27

Elements of the reversed array are:

27 48 38 1 25 4

Another run (y/n) y

Please enter size of array: 6

Please enter upper limit: 50

Elements of the array are:

8 23 5 34 29 15

Elements of the reversed array are:

15 29 34 5 23 8

Another run (y/n) y

Please enter size of array: 25

Please enter upper limit: 100

Elements of the array are:

55 43 79 62 30 66 0 7 67 90

1 0 38 51 65 72 56 75 81 15

65 48 90 80 94

Elements of the reversed array are:

94 80 90 48 65 15 81 75 56 72

65 51 38 0 1 90 67 7 0 66

30 62 79 43 55

Another run (y/n) n

End of program

2. Implementation requirements:

The operations for creating the array, printing the array and reversing the array should be implemented as static methods with the following specifications:

a) creating the array:

public static int[] createArray(int size, int upper)

This method takes as input (through its parameters) the size of the array and the upper limit. It has to:

- declare an array of the specified size

- fill the array with random numbers less than the upper limit

- return the array

b) printing the array

public static void printArray(int[] array, int perRow)

This method takes as input (through its parameters) an array of integers and the number of elements to be printed per row, and then prints the array accordingly.

Note: when this method is called from the main program the second argument in the method call will be 10.

c) reversing the array

public static void reverseArray(int[] array)

This method reverses the array given as a parameter.

You have to come up with an algorithm for reversing the array.

2. Dice Game

Modify Lab08 in the following way:

Design and implement a class GameOneDie – a game with two players that roll one die. The rules of the game stay the same as in Lab08. In Lab08, the rules were implemented within the main program. In this assignment they will be implemented in the class GameOneDie.

Hints:

The private variables of the class might be the two players and the number of runs.

The constructor will initialize the two players and the number of runs.

The rules of the game and all necessary input and output might be implemented in one method only, e.g.

public void playGame( )

The main program should create an object of type GameOneDie and then invoke the playGame method for that object. It will look like this:

public class GameDriver

{

public static void main()

{

GameOneDie game = new GameOneDie();

game.playGame();

}

}

Turn in the answers to Part 1 in a Word file. You may send the file separately, or included in the Java project folder. You may develop the two programs in one project, you may also create two separate projects. Zip the project folder(s), change the extension, and then send everything attached.

3