Part One

Summary: Implement a program that will input "tokens" from the user, determine whether each token can be read as a double, int, or neither, and process each token accordingly.

Your program will print out one prompt. Your program will then read an unknown number of tokens from the user. A token can be a number, a single word, or gibberish. The hasNextDouble() method of Scanner returns true if the next token can be read as a double; if so, you can use the nextDouble() method of Scanner to read it. Similarly, the hasNextInt() method of Scanner returns true if the next token can be read as a int by using the nextInt() method. Otherwise, you will need to use the next() method of Scanner to read the next token as a String.

Your program will keep track of the following:

1. The number of ints.
2. The number of doubles (that are not ints).
3. The number of tokens that are neither ints nor doubles.
4. The sum of all the numbers (both ints and doubles).
5. A String containing all the non-numeric tokens separated by commas (no comma at the end).

Your program will keep reading tokens until one of the counts (items 1, 2 , or 3 above) reaches five. When that happens, your program will print out the above information in coherent sentences and halt. For example, if the user enters:

0.1 0.2 0.3 1 2 3 one two three 4 5

In this example, your program should determine that there are 5 ints, 3 doubles, and 3 other tokens. The program should also obtain a sum of 15.6 (or very very close to 15.6). The other tokens are one,two,three and note that there should not be a comma at the end of the String.

Part Two

Answer the following questions and save your answers in a text file (.txt extension) in your Eclipse project. You can start editing a text file in Eclipse using:

File > New > Untitled Text File

Then do File > Save to start a dialog to save the file in your project.

1. Look in the Java documentation (https://docs.oracle.com/javase/8/docs/api/) for the two replace methods in the String class. Write exactly what is listed for their "Modifier and Type" and their Method and Description. Create an example code segment that uses both methods.

2. What is output by the following code?

for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
System.out.printf("%s ", j);
}
System.out.println();
}

3. Draw a flowchart of the previous code. Submit as a separate image file included in your Eclipse project.

4. Modify the previous double for loop so it prints out:

1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6

5. Modify the following while loop so that it throws two dice until the sum of the dice is 7. Be sure to print the values of both dice after the loop.

int die = 1 + (int) (6 * Math.random());
int count = 1;
while (die != 6) {
die = 1 + (int) (6 * Math.random());
count++;
}
System.out.printf("After %s throws, the die is %sn", count, die);

6. Consider the following static method:

public static int mystery(int a, int b) {
int c = a + b;
while (c >= a || c >= b) {
a += 1;
b += 1;
c -= 1;
}
return c;
}

a) Provide a method call that returns 0.
b) Provide a method call that returns 1.
c) Provide a method call that returns -1.
d) Provide a method call that returns 10.

7. Write a static method named mysterySolved that has two parameters a and c. It should return a value b such that mystery(a,b) as written above returns c. That is, mystery(a, mysterySolved(a, c)) should return c. If there is no such value, mysterySolved should do:

throw new IllegalArgumentException();
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.