Question 1

Suppose you are writing a program that receives a series of integers from the user and reports the running total. The user can enter up to 15 numbers, but when the user enters 0, the program terminates after reporting the total.

// QUESTION: SPECIFY AN IMPORT
public class ComputingTotal {
public static int update(int presentTotal, int newNumber) {
// WRITE YOUR CODE
}

public static void main(String[] args) {
int total = 0. input;
Scanner keyboard = // WRITE THIS PART
System.out.println("Enter up to 15 numbers next. "
+ "One number per line. Terminate with 0.");
for(. . .) { // WRITE A FOR-LOOP
// WRITE A CODE FOR PRINT PROMPT, RECEIVE INPUT,
// PERFORM ACTION USING update()
}
System.out.println("Your final total is " + total);
}
}

Question 2

Write a public static void method nonMultiple that receives three positive int values start, end, and base and prints all the integers between start and end that are not divisible by base. If either (a) any of the three numbers is less than or equal to 0, (b) base is strictly greater than end, or (c) start is strictly greater than end, then method must print "Invalid parameters" and then return with no further action. Here is the output of nonMultiple(10, 20, 3);

10
11
13
14
16
17
19
20

Here is the output of nonMultiple(10, 20, 3);

Invalid parameters

Question 3

Write the main() method of a program that receives two integer parameters, hhh and www, and then prints #'s on the screen so that they take the shape of a parallelogram having hhh and line and width www for each line. For each parameter, the program prompts the user to enter the parameter. If the entered parameter is not positive, the program informs the user about it without producing the shape on the screen. The report can be by way of printing an error message.

Here is a sample execution of the code:

Enter hhh: 6
Enter www: 3
###
###
###
###
###
###

Here is a sample execution of the code:

Enter hhh: 4
Enter www: 6
######
######
######
######

Here is another sample where an error message is shown:

Enter hhh: -5
Enter www: 4
Both parameters must be positive

Question 4

Recall that a Scanner object can be instantiated with a String data to read tokens from the String data. For example, if sc is a Scanner object instantiated with w and w has the value "Computer Science 120", sc has three tokens. It is possible to read the tokens in sc using the successive calls to the methods from the next() family for Scanner.

Write a public static method named thirdToken with a String parameter w that returns a boolean representing if w has at least three tokens and its third token can be interpreted as an int. To execute the test, consider first instantiating a Scanner variable with w. Then you can use hasNext(), hasNextInt(), and next() in some manner to execute the test. Make sure that the method runs without producing a runtime error when w has less than two tokens.

Think of the program as behaving as follows. (a) Instantiate a Scanner object with w. (b) If the scanner does not have a token, return false; otherwise, read the first token off from the scanner. (c) If the scanner does not have a token, return false; otherwise, read off the next token. (d) Return if the scanner has a token and that token is interpretable as int.

Question 5

The following program is expected to receive two real numbers from the user, multiply them together, add a quantity 5.0 to it, and then report the result so that it shows exactly 8 digits after the decimal point. State 10 syntax errors in the following code along with the line numbers in which they occur. Must not state more than one error from the same line. Do not state more than 10 errors (each one after the 10th one will result in point reduction).

01: import java.io.Scanner;
02: public class 5.0_TIMES {
03: public static final int DIFF = 5.0;
04: public static void main(String[]) {
05: Scanner keyboard = Scanner(system.in);
06: double operand1, operand2, res:
07: res = DIFF;
09: System.out.print('Enter inputs: ');
10: operand1 = keyboard.nextdouble();
11: operand2 = keyboard.nextdouble();
12: res = res ++ operand1 * operand2;
13: System.out.printf("The result is %.8f", res);
14: }
15: }}

Question 6

Consider the following code:

int x = 25, y = 9, z, w;
z = x % y; w = x / y; // line 1
z *= z; w--; // line 2

Fill in the blanks below that state the values of z and w

Immediately after line 1, z is equal to _____ and w is to _____.

Immediately after line 2, z is equal to _____ and w is equal to _____.

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.