Here are the answers for the midterm practice test:
Reading code:
If a=2 and b = 6:
2 loop stop stop 4 loop stop again 4 yes again 5 stop 8 loop stop 11 more
If a = 4 and b = 7:
4 loop stop again 4 again 4 yes again 5 stop 8 loop stop 11 more
Reading code with methods:
if a = 1 and b = 7:
3724 1989 7938 5823 3824
if a = 1 and b = 6:
3624 1934 2933 5823 3824
Application Program solution:
import java.io.*;
public class Junk { public static void main( String args[] ) throws IOException {
DataInputStream input; input = new DataInputStream(System.in);
int score, high, avgEven, count80, ecount, esum; score = high = avgEven = count80 = ecount = esum = 0; // fast way to init String name= "";
System.out.println("Enter score"); // sentinel value score = Integer.parseInt(input.readLine());
while (score != 0) {
if (score%2 == 0) // number is even { System.out.println("score is even"); ecount++; esum = esum + score; }
if (score%2 !=0) // could have used else System.out.println("score is odd");
if (score > 79) { if (score < 90) count80++; }
if (score > high) { System.out.println("highest so far"); System.out.println("What is your name?"); name = input.readLine(); high = score; }
System.out.println("Enter score"); // sentinel value score = Integer.parseInt(input.readLine());
} // end while
System.out.println("average of all evens is " + esum/ecount); System.out.println(name + " had the highest score"); System.out.println("There were " + count80 + " scores in the 80s");
} }
|