Com S 207: Programming Assignment 3

200 Points

In this assignment you will learn about loops and methods. This assignment requires you to write

a class named CowsAndBulls. This class must be in a package named pa3.

Note that the description of a programming assignment is not a linear narrative and often

requires multiple readings before things start to click. You are encouraged to consult instruc-

tors/Teaching Assistants for any questions/clarications regarding programming assignments.

**1 Cows and Bulls**

You might have played the game of Cows and Bulls as a kid. This game consists of two players,

lets call them \Alice” and \Bob”. The game starts by Alice picking a secret 4 digit number so that

all digits of the number are distinct. For example, Alice can pick 1024 but is not allowed to pick

1231. Bob’s goal is to nd the secret number that Alice has picked. The game proceeds in rounds.

In each round Bob guesses a four digit number and Alice gives a clue about how close the guess is

to the secret number. Bob’s guess must also have all distinct digits. Based on these clues, in the

next round, Bob guesses another number. The game proceeds till Bob nds the secret number.

Alice gives clue by specifying how many \cows” and \bulls’ are present in Bob’s guess. This is

best illustrated by an example. Suppose Alice’s secret number is 1723 and Bob’s rst guess is 2893.

The digit 2 is present in both the numbers. However, it is the second digit of Alice’s number and

is the 4th digit of Bob’s number (we are counting from right to left). Thus 2 is considered a \cow”.

The number 3 is also present in both the numbers and it is the rst digit in both the numbers.

So 3 is a \bull’. Now, Alice’s tells Bob that there is 1 cow and 1 bull in his guess. Based on this

information, suppose Bob guessed 8923. This guess has 2 bulls (2 and 3) and 0 cows. So Alice tells

Bob that there are 0 cows and 2 bulls in his guess. Suppose that Bob’s next guess is 7112. Since

7112 does not have distinct digits, Alice responds by telling that it is not a valid guess. Suppose

Bob’s next guess is 7123. This guess has 2 cows ( 7 and 1) and 2 bulls ( 2 and 3). So Alice tells

Bob that there are 2 cows and 2 bulls. Suppose Bob’s next guess is 1723. This guess has 4 bulls,

and thus exactly matches the secret number, and the game is Over.

**2 Your Task**

Your task is to write a program to play this game. The program takes the role of \Alice” and the

user takes the role of \Bob”. When you run the program, it will pick a secret 4-digit number (with

distinct digits) and interacts with the user. For each guess the user types, the program prints the

number of cows and bulls. The program ends, when the user nds the secret number.

Your program must behave as follows: At the beginning, it picks a random 4 digit number (with

all distinct digits) and interacts with the user. It prompts the user to enter a valid guess. A guess is

valid if all the digits of the guessed number are distinct. For each valid guess the user entered, the

1program outputs the number of cows and bulls. If the guess is not valid, then the program outputs

a message. The program repeatedly prompts the user until the user nds the secret number.

Hear is a sample run of the program: Boldface letters indicate user input:

Welcome to Cows and Bulls Game. I picked a random 4-digit number with distinct integers,

try finding it. Type your guess, must be a 4-digit number with distinct digits.

1780

Cows: 0 Bulls: 2

Type your guess, must be a 4-digit number with distinct digits

1084

Cows: 1 Bulls: 1

Type your guess, must be a 4-digit number with distinct digits

2316

Cows: 2 Bulls: 0

Type your guess, must be a 4-digit number with distinct digits

2216

Invalid guess. Type your guess, must be a 4-digit number with distinct digits

1680

Cows: 0 Bulls: 3

Type your guess, must be a 4-digit number with distinct digits

6180

Cows: 2 Bulls: 1

Type your guess, must be a 4-digit number with distinct digits

1170

Invalid guess. Type your guess, must be a 4-digit number with distinct digits

1670

Cows: 0 Bulls: 3

Type your guess, must be a 4-digit number with distinct digits

1650

Congratulations, You found the number, you took 9 guesses

We index digits from right to left (not left to right) and indices start from 1 (not from 0). For

example, the rst digit of 123 is 3. The digit 1 appears at index 3 in 123.

You will write your program by writing several methods. Your class CowsAndBulls must have

the following methods. You are NOT allowed to use any of the String methods in your code.

numDigits(int number). Takes a positive integer number as a parameter and returns the

number of digits in number. You may assume that number is always a positive integer, and you do

not have to write code to check this.

getDigit(int number, int i). Takes a positive integer number and an index i as parameters

and returns the ith digit (from right) of number. If number does not have ith digit, then it returns

1. This method must make call(s) to numDigits. It may make call(s) to other methods.You may

2assume that number is always a positive integer, and you do not have to write code to check this.

hasDistinctDigits(int number). Takes a positive integer number as a parameter and re-

turns true if all the digits of number are distinct, otherwise returns false. This method must make

call(s) to getDigit. It may make call(s) to other methods. You may assume that number is always

a positive integer, and you do not have to write code to check this.

indexOf(int number, int digit). Takes a positive integer number and a digit as parame-

ters and returns the position (from right) at which digit appears in number, if number has distinct

digits. If number does not have distinct digits, then this method returns