Please read the entire PDF before starting. You must do this assignment individually. It is very important that you follow the directions as closely as possible. The directions, while perhaps tedious, are designed to make it as easy as possible for the TAs to mark the assignments by letting them run your assignment, in some cases through automated tests. While these tests will never be used to determine your entire grade, they speed up the process signifificantly, which allows the TAs to provide better feedback and not waste time on administrative details.
Up to 30% can be removed for bad indentation of your code as well as omitting comments, or poor coding structure.
1. 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.
2. Make sure that your code compiles
â Non-compiling code will receive a 0.
3. Write your name and student ID as a comment in all .java fifiles you hand in
Â
4. Indent your code properly
Â
5. Name your variables appropriately
â The purpose of each variable should be obvious from the name
6. Comment your work
â A comment every line is not needed, but there should be enough comments to fully understand your program
Do Not submit this part, as it will not be graded. However, doing these exercises might help you to do the second part of the assignment, which will be graded. If you have diffiffifficulties with the questions of Part 1, then we suggest that you consult the TAs during their offiffiffice hours; they can help you and work with you through the warm-up questions. You are responsible for knowing all of the material in these questions.
Warm-up Question 1 (0 points)
Create a method to print the the following image. This method must have one parameter, which is the length of the sides in number of *âs. This method should use only two for loops, and use if statements within the for loops to decide whether to draw a space or a star. You can assume the parameter is an odd number.
Draw the outline of a square as follows:
??????????
?       ?
??????????
?       ?
??????????
?       ?
??????????
?       ?
??????????
N.B. It is normal that the square does not appear to be a perfect square on screen as the width and the length of the characters are not equal.
Change the method you just created to have two parameters, so that you can create rectangles. The fifirst parameter will be the height of the rectangle, and the second parameter will be the width of the rectangle.
Write a program to display the (x,y) coordinates up to (9,9) of the upper right quadrant of a Cartesian plane. As in the previous warm-up question, your solution should use two nested for loops. Your program should also display the axes, by checking to see if the x-coordinate is zero or if the y-coordinate is zero. Note that when both the x and y coordinates are zero, you should print a + character.
For example, the output of your code should look like:
|(1,9)(2,9)(3,9)(4,9)(5,9)(6,9)(7,9)(8,9)(9,9)
|(1,8)(2,8)(3,8)(4,8)(5,8)(6,8)(7,8)(8,8)(9,8)
|(1,7)(2,7)(3,7)(4,7)(5,7)(6,7)(7,7)(8,7)(9,7)
|(1,6)(2,6)(3,6)(4,6)(5,6)(6,6)(7,6)(8,6)(9,6)
|(1,5)(2,5)(3,5)(4,5)(5,5)(6,5)(7,5)(8,5)(9,5)
|(1,4)(2,4)(3,4)(4,4)(5,4)(6,4)(7,4)(8,4)(9,4)
|(1,3)(2,3)(3,3)(4,3)(5,3)(6,3)(7,3)(8,3)(9,3)
|(1,2)(2,2)(3,2)(4,2)(5,2)(6,2)(7,2)(8,2)(9,2)
|(1,1)(2,1)(3,1)(4,1)(5,1)(6,1)(7,1)(8,1)(9,1)
+--------------------------------------------->
Note that in the above image, all of the coordinates containing 0âs are not displayed, since we are printing axes instead.
Write a method generateRandomArray() that takes as input an integer n and returns an array of size Page 2n. The elements of the array should be random doubles between 0 (included) and 5 (excluded). You can use Math.random() to do this.
Write a method getLargestElement() that takes an array of integers as input and returns the largest element inside the array. For example, if the input is:
int[] arr= {1, 5, -3, 15, 4};
then getLargestElement(arr) should return 15.
Write a method getSum() that takes an array of integers as input and returns the sum of all the elements inside the array. For example, if the input is:
int[] arr= {1, 5,-3, 15, 4};
then getSum(arr) should return 22.
Write a method countNegatives() that takes an array of integers as input and returns the number of negative numbers inside the array. For example, if the input is:
int[] arr= {1,5,-3, 15, -13};
then countNegative(arr) should return 2.
Write a method getVowels() that takes a String as input and returns an array characters containing all the vowels from the given string. For example, if the input is:
String s = "kangaroo";
then getVowels(s) should return {âaâ, âaâ, âoâ, âoâ}.
Create a fifile called Counting.java, and in this fifile, declare a class called Counting. This class should ask the user when the computer should stop counting. When should I stop counting to?
10 <---- User typed this
I am counting until 10: 1 2 3 4 5 6 7 8 9 10
For this question you have to generalize the last question. The user will give you the number they want the computer to count up to and the step by which it will do so. When should I stop counting to?
25 <----
Which step should I use?
3 <----
I am counting to 25 with a step of 3:
1 4 7 10 13 16 19 22 25
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!).
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â, if they are in difffferent 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 fifile named BullsAndCows.java.
Write a method called contains() that takes as input an array of integers and a specifific integer. The method returns true if the specifified 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,
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 fifile). 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. his allows us to generate an array containing four digits that are all difffferent from one another. Make sure to initialize the elements of the array from the fifirst one to the last one.
For example:
Write a method called getNumOfBulls() that takes as input two integer arrays. The fifirst 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 defifinition of bull in this context). If the input arrays have a difffferent 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:
Write a method called getNumOfCows() that takes as input two integer arrays. The fifirst 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 defifinition of cow in this context). If the input arrays have a difffferent 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};
Page 5Then:
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 fifile). The method should then do the following:
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 difffferent 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 fifix a specifific 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:
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 fifind 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).