Get Instant Help From 5000+ Experts For
question

Writing: Get your essay and assignment written from scratch by PhD expert

Rewriting: Paraphrase or rewrite your friend's essay with similar meaning at reduced cost

Editing:Proofread your work by experts and improve grade at Lowest cost

And Improve Your Grades
myassignmenthelp.com
loader
Phone no. Missing!

Enter phone no. to receive critical updates and urgent messages !

Attach file

Error goes here

Files Missing!

Please upload all relevant files for quick & complete assistance.

Guaranteed Higher Grade!
Free Quote
wave
Java Programming Assignment Instructions

Warm-up Questions

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:

??????????

?              ?

??????????

?              ?

??????????

?              ?

Part 1: Warm-up

??????????

?              ?

??????????

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?

Part 2: Main Questions

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,

  • contains(x, -3) returns false
  • contains(y, 2) returns true
  • contains(z, 5) returns false

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:

  • 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}

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:

  • 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(guessThree, guessFour) raises an exception with a meaningful message (e.g. no repeat digit).

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:

  • 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(guessThree, guessFour) raises an exception with a meaningful message (e.g. no repeat digit).

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:

  1. Generate a random secret number to be guessed using generateSecretDigits() and the integer received as input.
  1. Display a welcome message to the player. It is up to you to decide how to welcome the users of your program.
  1. Display a message encouraging the player to make a guess. The message should also display the guess attempt.
  1. 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 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.
  1. 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.
  1. 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.
  1.  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 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:

  • Your method could ask for the name of the player.
  • Your method could display difffferent messages depending on the number guess attempts.
  • Your method could use a difffferent 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.lang 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 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).

support
close