Implement the program using for-loop - complete the method


QUESTION 1
1. Complete the following method, which compares two parameters and returns -1, 0, or 1 if the first parameter is less than, equal to, or greater than the second parameter. Note answers are case sensitive.
public int compare(int a, int b) {
result; // declare variable of appropriate type. The variable will get its value then be returned.
if (a = -1; // assignment -1 to which variable?
} else (a b) {// when the two parameters have same value, assign 0 to variable to be returned
= 0;
} else { // a must be great than b in this case
result = 1;
}
result; //one of the branching keywords.
}

QUESTION 2
1. The following switch statement decides the value of g based on the value of s (whose types have been declared elsewhere).
switch (s) {
case 9:
g="A";
;
case 8:
="B";
;
default:
g ="F";
}

Then complete the following code that is equivalent to the above switch statement.
if (s 9) {
g = "";
} else (s 8) {
g = "";
} {
g="F";
}

QUESTION 3
1. Indicate the values printed when the following code executes:
for (int i=1, j=5; i<=j; i++, j--) {
System.out.printf("%d %d %dn", i, j, i+j);
}

QUESTION 4
1. The following flowchart depicts a part of program that calculates 20 years of the annual value of an investment account with an initial balance of v, an annual additional investment a deposited into the account, and an annual rate of return r randomly drawn from a uniform distribution. Implement the program using for-loop.

Here is partially finished code. Fill in the blanks to complete it.
double v=1000, a=600; //initial value $1000; invest $600 at the end of each year
double r; // variable for storing annual rate of return
for (int = ; 20; i) {
r = -3 + 15*Math.random() ; //r is between -3 to 18%
v = v*(1+r/100)+a;
System.out.printf("%5 %5.2 %,10.0", i, r, v); // supply conversion characters
}

QUESTION 5
1. After the following code finishes executing, the value of the result variable is , the value of the i variable is . (Note: use numeric values for your answer)
int i=4, result=1;
while(i>1) {
result *= i;
i--;
}

Solution Preview :

Prepared by a verified Expert
JAVA Programming: Implement the program using for-loop - complete the method
Reference No:- TGS02361195

Now Priced at $10 (50% Discount)

Recommended (93%)

Rated (4.5/5)