Description
In this assignment you will learn about loops and methods. This assignment requires you to writea class namedCowsAndBulls. This class must be in a package namedpa3.Note that the description of a programming assignment is not a linear narrative and oftenrequires multiple readings before things start to click. You are encouraged to consult instruc-tors/Teaching Assistants for any questions/clarifications regarding programming assignments.1 Cows and BullsYou 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 thatall digits of the number are distinct. For example, Alice can pick 1024 but is not allowed to pick1231. Bob’s goal is to find 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 isto the secret number. Bob’s guess must also have all distinct digits. Based on these clues, in thenext round, Bob guesses another number. The game proceeds till Bob finds the secret number.Alice gives clue by specifying how many “cows” and “bulls’ are present in Bob’s guess. This isbest illustrated by an example. Suppose Alice’s secret number is 1723 and Bob’s first guess is 2893.The digit 2 is present in both the numbers. However, it is the second digit of Alice’s number andis 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 first 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 thisinformation, suppose Bob guessed 8923. This guess has 2 bulls (2 and 3) and 0 cows. So Alice tellsBob that there are 0 cows and 2 bulls in his guess. Suppose that Bob’s next guess is 7112. Since7112 does not have distinct digits, Alice responds by telling that it is not a valid guess. SupposeBob’s next guess is 7123. This guess has 2 cows ( 7 and 1) and 2 bulls ( 2 and 3). So Alice tellsBob 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 TaskYour task is to write a program to play this game. The program takes the role of “Alice” and theuser takes the role of “Bob”. When you run the program, it will pick a secret 4-digit number (withdistinct digits) and interacts with the user. For each guess the user types, the program prints thenumber of cows and bulls. The program ends, when the user finds the secret number.Your program must behave as follows: At the beginning, it picks a random 4 digit number (withall distinct digits) and interacts with the user. It prompts the user to enter a valid guess. A guess isvalid if all the digits of the guessed number are distinct. For each valid guess the user entered, the program outputs the number of cows and bulls. If the guess is not valid, then the program outputsa message. The program repeatedly prompts the user until the user finds 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.1780Cows:0 Bulls:2Type your guess, must be a 4-digit number with distinct digits1084Cows:1 Bulls:1Type your guess, must be a 4-digit number with distinct digits2316Cows:2 Bulls:0Type your guess, must be a 4-digit number with distinct digits2216Invalid guess.Type your guess, must be a 4-digit number with distinct digits1680Cows:0 Bulls:3Type your guess, must be a 4-digit number with distinct digits6180Cows:2 Bulls:1Type your guess, must be a 4-digit number with distinct digits1170Invalid guess.Type your guess, must be a 4-digit number with distinct digits1670Cows:0 Bulls:3Type your guess, must be a 4-digit number with distinct digits1650Congratulations, You found the number, you took 9 guessesWe index digits from right to left (not left to right) and indices start from 1 (not from 0). Forexample, the first digit of 123 is 3. The digit 1 appears at index 3 in 123.You will write your program by writing several methods. Your classCowsAndBullsmust havethe following methods. You are NOT allowed to use any of theStringmethods in your code.numDigits(int number).Takes a positive integernumberas a parameter and returns thenumber of digits innumber. You may assume thatnumberis always a positive integer, and you donot have to write code to check this.getDigit(int number, int i).Takes a positive integernumberand an indexias parametersand returns theith digit (from right) ofnumber. Ifnumberdoes not haveith digit, then it returns-1. This method must make call(s) tonumDigits. It may make call(s) to other methods