Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

To get full marks, you must: . Follow all directions below - In particular, make sure that all classes and method names are spelled and

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed
To get full marks, you must: . Follow all directions below - In particular, make sure that all classes and method names are spelled and capitalized exactly as described in this document. Otherwise, you will receive a 50% penalty. . Make sure that your code compiles - Non-compiling code will receive a 0. . Write your name and student ID as a comment in all .java files you hand in . Indent your code properly . Name your variables appropriately - The purpose of each variable should be obvious from the name . Comment your work - A comment every line is not needed, but there should be enough comments to fully understand your programThen: . getNumOfCows(secret, guessOne) returns 2. getNumOfCows(secret, guessTwo) returns 1. . getNumOfCows(secret, guessThree) raises an exception with a meaningful message (e.g. length error). getNumOfCows(secret, guessFour) raises an exception with a meaningful message (e.g. no repeat digit).To complete the program, simply call playBullsAndCows() from the main method providing an integer of your choice as input. Note that the secret number generated depends on that integer. If you want to test your program with different numbers you should change the integer provided as input. In fact, to play the game you can generate a random integer inside the main method (using Math.random()) and provide such integer as input to playBullsAndCows(). To debug your program, we highly suggest you fix a specific integer so that it will be easier to understand whether your method is behaving as it should. Note that you can add additional features of your choice to the game. For example your game could do the following: . Your method could ask for the name of the player. . Your method could display different messages depending on the number guess attempts. Page 6 . Your method could use a different layout. . Your method could display the number of seconds the game was played for (if you'd like to do that, you can use System.currentTimeMillis() ). . Any other thing you can think of as long as you don't use any another class beside Scanner, Random, and those that belongs to java. long package.Note that none of the above features is needed (you won't receive extra marks for it), but it might be fun to make the game a little more original. :) Below you can find a couple of sample runs of the program. Note that our program displays the number of seconds played, but yours does not have to do that. In all of the examples, the call made from the main method was playBullsAndCows(123) (this means that the secret number to guess is 2069). Examples Welcome to the game ! Have fun! Guess #1, Enter a four-digit number, each digit should be unique : 1234 Bulls: O Cows: 1 Guess #2, Enter a four-digit number, each digit should be unique : 123 You must enter a four-digit number! You just wasted one guess! Guess #3, Enter a four-digit number, each digit should be unique : 12345 You must enter a four-digit number! You just wasted one guess! Guess #4, Enter a four-digit number, each digit should be unique : dis You must enter a four-digit number! You just wasted one guess! Guess #5, Enter a four-digit number, each digit should be unique : 5678 Bulls: 0 Cows : 2 Do you want to quit the game? Answers: y n Guess #6, Enter a four-digit number, each digit should be unique : -34 You must enter a four-digit number ! You just wasted one guess! Do you want to quit the game? Answers : y y You've decided to quit the game, after making 6 attemptsWelcome to the game ! Have fun! Guess #1, Enter a four-digit number, each digit should be unique : 1234 Bulls : 0 Cows : 2 Guess #2, Enter a four-digit number, each digit should be unique : 0198 Bulls: 0 Cows : 2 Guess #3, Enter a four-digit number, each digit should be unique : 1111 You are not allowed to repeat the same number! Guess #4, Enter a four-digit number, each digit should be unique : 1298 Bulls: 0 Cows : 3 Guess #5, Enter a four-digit number, each digit should be unique : 1268 Bulls: 0 Cows : 3 Do you want to quit the game? Answers: y no Guess #6, Enter a four-digit number, each digit should be unique : 1278 Bulls: 0 Cows : 4 Do you want to quit the game? Answers: 1 maybe Guess #7, Enter a four-digit number, each digit should be unique : 2817 Bulls: 4 Cows: 0 Congratulations, you guessed the secret number! It took you 7 attempts.Throughout this assignment you are allowed to use everything we have learned in class up to and including arrays, Scanner, and Random. This does not mean however that you are allowed to change any of the headers of the methods described below. You need to make sure to follow precisely the instructions provided (for example, you'll be using Scanner only in one of the methods for this assignment!). Question 1: BullsAndCows (100 points) Bulls and Cows is an old code-breaking mind game. For this question, you will write a Java program that implements a game of Bulls and Cows in which the player needs to guess a randomly generated secret 4- digits number. When the player takes a guess, the program reveals the number of digits that match with the secret number. If the matching digits are in the correct positions, they are called "bulls", ifthey are in different positions, they are called "cows". After each guess, the program reveals how many bulls and cows the player's guess contains, To complete this task, you will need to implement all the methods listed below. Note that you are free to write more methods if they help the design or readability of your code. All the code for this question must be placed in a file named BullsAndCows.java. la) Method to check if an element is contained in a given array Write a method called contains() that takes as input an array of integers and a specific integer. The method returns true if the specified integer is an element of the given array, false otherwise. For example, let int x = {-2, 71; int y = 19, 0, 2, 6h int z = th; Then, contains(x, -3) returns false contains(y, 2) returns true contains(z, 5) returns false1b) Method to generate the secret number Write a method called generateSecretDigits() that takes as input an integer and returns an array of integers containing the 4 digits that make up the randomly generated secret number. The method uses the input to create an object of type Random with the provided seed (remember that to use Random you should add the appropriate import statement at the beginning of your file). The method then uses such object to generate a random integer between 0 and 9 (both included). If the integer is not already part of the array, then the method uses it to initialize one of the elements of the array, otherwise a new integer is generated. The process continues until all four elements of the array have been generated. This allows us to generate an array containing four digits that are all different from one another. Make sure to initialize the elements of the array from the first one to the last one. For example: . generateSecretDigits(123) returns the array (2, 0, 6, 9} generateSecretDigits(45) returns the array (9, 1, 0, 7} . generateSecretDigits(987) returns the array (5, 8, 9, 7} Page 41c) Method to get the number of bulls in a guess Write a method called getNumOfBulls() that takes as input two integer arrays. The first one represents the digits of the secret number, while the second one represents the digits of the number guessed by the player. The method returns the number of bulls in the guess of the players (see the intro paragraph for the definition of bull in this context). If the input arrays have a different number of elements, then the method should throw an IllegalArgumentException with an appropriate message. For example, consider the following arrays: int secret = {2, 0, 6, 9); int guessOne = {9, 5, 6, 2); int guessTwo = {1, 0, 6, 2); int guessThree = {1, 2, 3, 4, 5, 6); int guessFour = {1, 3, 4, 4, 0, 5}; Then: getNumOfBulls(secret, guessOne) returns 1. getNumOfBulls(secret, guessTwo) returns 2. . getNumOfBulls(secret, guessThree) raises an exception with a meaningful message (e.g. length error). getNumOfBulls(secret, guessFour) raises an exception with a meaningful message (e.g. no repeat digit).1d) Method to get the number of cows in a guess Write a method called getNumOfCows() that takes as input two integer arrays. The first one represents the digits of the secret number, while the second one represents the digits of the number guessed by the player. The method returns the number of cows in the guess of the players (see the intro paragraph for the definition of cow in this context). If the input arrays have a different number of elements, then the method should throw an IllegalArgumentException with an appropriate message. For example, consider the following arrays: int secret = {2, 0, 6, 9); int[ ] guessOne = (9, 5, 6, 2); int guessTwo = {1, 0, 6, 2}; int[ ] guessThree = {1, 2, 3, 4, 5, 6); int[ ] guessFour = {1, 3, 4, 4, 0, 5 );le) Method to simulate a game Write a method playBullsAndCows() which takes an integer as the seed and simulates a game of Bulls and Cows. The method creates one object of type Scanner to retrieve the inputs from the user (remember that to use Scanner you should add the appropriate import statement at the beginning of your file). The method should then do the following: 1. Generate a random secret number to be guessed using generateSecretDigits() and the integer received as input. 2. Display a welcome message to the player. It is up to you to decide how to welcome the users of your program. 3. Display a message encouraging the player to make a guess. The message should also display the guess attempt. 4. The method retrieves the guess as an integer. Here you can assume that the player will enter an input of the correct type . If the player inputs a negative integer or an integer with more than 4 digits, then the method displays a message letting them know they wasted one guess and that they should enter a positive integer with at most 4 digits. Otherwise, the method displays how many bulls and cows the player's guess contains. 5. If the player guessed the secret number, then the method congratulates the player and let them know how many tries were needed to win the game. 6. If the player has not guessed the secret number after 5 guesses, then the method should start to ask the player if they want to quit the game at each incorrect guess. If the player replies with a 'y' (which is retrieved as a String) the method should displays a message acknowledging that the player wanted to quit and letting them know how many times they tried to guess the secret number. 7. If the player did not guess the number nor decided to quit, then the method goes back to step three. integer

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

More Books

Students also viewed these Programming questions