// // Prepare for Test 2 // // Monday, Nov. 14, 2011 import java.util.*; public class ForTestTwo { public static void main(String[] args) { // Q1: if age is 18 or larger, out "Vote"; ... "Wait". Scanner kb = new Scanner(System.in); /* int age; if(age >= 18) System.out.println("Go vote.\n"); else System.out.println("Please wait.\n"); // Q2: convert the following while loop into a for loop. int score, sum=0; count=0; while(count < 3) { score = kb.nextInt(); sum += score; count ++; } for(count=0; count<3; count++) { score = kb.nextInt(); sum += score; } // Q3: Assuming the input: 10 50 70 60 90, what is the result // of sum for the above while loop? And count? sum = 130 count = 3 // Q4: out all odd numbers from 10 to 100? for(int i=11; i<100; i=i+2) System.out.print(i + " "); System.out.println("\n\n"); // Q5: Read 50 natural numbers, out their smallest one? */ int num = kb.nextInt(); int min = num; for(int i=1; i<50; i++) { num = kb.nextInt(); if(num < min) min = num; } System.out.println("Min = " + min); // Q6: Read real numbers until a 0 is read, and // out their average double num, total=0.0, avg; int count = 0; // On Tuesday Nov. 15, 2011, I will be my office // from 10am - 3pm. Please see me if you have // questions for Test 2. My office: Villege II, // Faculty Suite #104, Phone 757-455-5710 // or call 804-712-3099 num = kb.nextDouble(); while(num != 0) { total += num; count ++; num = kb.nextDouble(); } avg = total/count; System.out.println("Avg = " + avg); // Q7: T/F a) A class must be declared as public. T b) A constructor may have a data type. F c) A constructor must be declared as public. T d) A constructor's name must be the same as the class. T e) A method may be declared as private. T f) A Java code must have an import statement. F e) A Java class must have a main method. F g) A value-returning method must have a return statement in the body. T } }