Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Part 2 The questions in this part of the assignment will be graded. Throughout this assignment you are allowed to use everything we have learned

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 transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Part 2 The questions in this part of the assignment will be graded. 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 (60 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 progr with the secret number. If the matching digits are in the correct positions, they are called "bulls", if they are in different positions, they are called "cows". After each guess, the program reveals how many bulls and cows the player's guess contains. am reveals the number of digits that match 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. 1a) 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, 7; int[] y - [9, 0, 2, 6-; int[] z = {}; Then, . contains (x, -3) returns false contains(y, 2) returns true contains(z, 5) returns false 1b) 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 unl 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] 1c) Method to extract the digits from a given number Write a method called extractDigits) that takes as input an integer and returns an array containing all the digits of the given number. Note that if the number contains less than 4 digits, then the method returns an array with 4 elements where the missing digits are replaced by Os and placed at the beginning of the array For example, extractDigits(1234) returns the array 11, 2, 3, 4) extractDigits(75) returns the array [0, 0, 7, 5} extractDigits (593823) returns the array f5, 9, 3, 8, 2, 3] extractDigits(-326) returns the array 10, 3, 2, 6) 1d) 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: intlI secret - t2, 0. 6, int[J guessOne - [9, 5, 6, 2J; int[] guessTwo = {2, 0, 6, 2); int[J guessThree -11, 2, 3, 4, 5, 6; int[J guessFour - l1, 3, 4, 4, 0, 5]; Then: getNumOfBulls (secret, guessOne) returns 1. getNumOfBulls (secret, guessTwo) returns 3. getNumOfBulls (secret, guessThree) raises an exception. getNumOfBulls (guessThree, guessFour) returns 2. 1e) 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, int[J guessOne - [9, 5, 6, 2J; int[J guessTwo - [2, 0, 6, 2J; int[J guessThree -11, 2, 3, 4, 5, 6; int[J guessFour - l1, 3, 4, 4, 0, 5]; Then: getNumOfCows (secret, guessOne) returns 2. Page 5 getNumOfCows (secret, guessTwo) returns 0 getNumOfCows (secret, guessThree) raises an exception. getNumOf Cows (guessThree, guessFour) returns 3 1f) Method to simulate a game Write a method playBullsAndCows ) which takes an integer as input 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 2. Display a welcome message to the player. It is up to you to decide how to welcome the users of 3. Display a message encouraging the player to make a guess. The message should also display the 4. The method retrieves the guess as an integer. Here you can assume that the player will enter arn received as input your program. guess attempt. input of the correct type. If the player inputs a negative integer or an integer with more than 5 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 thenm 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 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. Your method could use a different layout 2a. A method called onCurve Write a method called onCurve) that takes 3 inputs: an integer array representing the coordinates of a point on the plane, a double array representing the coefficients of the polynomial function you want to draw, and a double representing the desired thickness of the curve. The method determines whether or not the given point should be considered to be part of the specified curve given the desired thickness. In general, given the coefficients of a polynomial function {arn, an-1,.. . ,a1, ao], a specific point (ro, yo,) is considered to be part of the curve if the following equation holds Since our drawing system is not continuous (i.e. it is a 20 by 20 grid), you need to relax the equality in the equation. Instead of checking if the equality is satisfied, you should check if the following inequalities are satisfied where t represents the thickness of the curve being drawn. Thus, to recap, the method receives as input the coordinates of a point (Izo, Vo)), the coef ficients of a curve (Ian,an-1,. .., a1,ao]), and a desired thickness (t). The method returns true if the above inequalities are satisfied, false otherwise Consider the following variables double[] line = {1.0, 2); // = x + 2 double[] parabola = {0.1, 1, -8); // y = 0. 1x"2 -x-8 int[J pointOne -[0, 2; int[J pointTwo - t0, -8; int[I pointThree- [-5, -1); int[J pointFour [3, 5J; double lineThickness -1; double parabolaThickness - 1.05; Page 9 Then, onCurve(pointOne, line, lineThickness) returns true onCurve (pointTwo, line, lineThickness) returns false

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

Students also viewed these Databases questions