CPSC150 EH07 CPSC150 EH07 Q1: Which of the following is a double-selection control statement? a.do…while. b.for. c.if…else. d.if. ANS: c. if…else. Q2: Which of the following is not a Java keyword? a.do b.next c.while d.for ANS: b. next Q3: What is output by the following Java code segment? int temp; temp = 200; if ( temp > 90 ) System.out.println( "This porridge is too hot." ); if ( temp < 70 ) System.out.println( "This porridge is too cold." ); if ( temp == 80 ) System.out.println( "This porridge is just right!" ); a.This porridge is too hot. b.This porridge is too cold. c.This porridge is just right! d.None of the above. ANS: a. This porridge is too hot. Q4: Which of the following is not true about the conditional operator ( ?: )? a.The conditional operator is a ternary operator, meaning that it takes three operands. b.The first operand is a boolean expression. c.The second operand is the result value if the condition evaluates to false. d.The second operand is the result value if the condition evaluates to true. ANS: c. The second operand is the result value if the condition evaluates to false. 1 CPSC150 EH07 Q5: What is output by the following Java code segment? int temp; temp = 180; if ( temp > 90 ) { System.out.println( "This porridge is too hot." ); // cool down temp = temp – ( temp > 150 ? 100 : 20 ); } // end if else { if ( temp < 70 ) { System.out.println("This porridge is too cold."); // warm up temp = temp + (temp < 50 ? 30 : 20); } // end if } // end else if ( temp == 80 ) System.out.println( "This porridge is just right!" ); a.This porridge is too hot. b.This porridge is too cold. This porridge is just right! c.This porridge is just right! d.None of the above. ANS: d. None of the above. (The output will be This porridge is too hot. This porridge is just right!) 2 CPSC150 EH07 3 Q6: Drivers are concerned with the mileage their automobiles get. One driver has kept track of several tankfuls of gasoline by recording the miles driven and gallons used for each tankful. Develop a Java application that will input the miles driven and gallons used (both as integers) for each tankful. The program should calculate and display the miles per gallon obtained for each tankful and print the combined miles per gallon obtained for all tankfuls up to this point. All averaging calculations should produce floating-point results. Use class Scanner and sentinel-controlled repetition to obtain the data from the user. Please fill in the missing parts of the program. Make the program run and submit your running result. ANS: // e07_q06.java // Program calculates average mpg import java.util.Scanner; public class e07_q06 { // perform miles per gallon calculations public static void main( String args[] ) { Scanner input = new Scanner( System.in ); int miles; // miles for one tankful int gallons; // gallons for one tankful int totalMiles = 0; // total mailes for trip int totalGallons = 0; // total gallons for trip double milesPerGallon; // miles per gallon for tankful double totalMilesPerGallon; // miles per gallon for trip // prompt user for miles and obtain the input from user System.out.print( "Enter miles (-1 to quit): " ); miles = input.nextInt(); // exit if the input is -1 otherwise, proceed with the program while ( miles != -1 ) { // prompt user for gallons and obtain the input from user System.out.print( "Enter gallons: " ); gallons = input.nextInt(); // add gallons and miles for this tank to total totalMiles += miles; totalGallons += gallons; //use if statement to check if gallons is 0. // caluclate miles per gallon for the current tank // Print out the milesPerGallon // Missing Part A // end of Missing Part A //use if statement to check if totalGallons is 0. // calculate miles per gallon for the total trip. CPSC150 EH07 4 // Print out the totalMilesPerGallon // Missing Part B // End of Missing Part B // prompt user for new value for miles System.out.print( "Enter miles (-1 to quit): " ); miles = input.nextInt(); } // end while loop } // end main method } // end class e07_q06 Q7 The following program already can print out the difference between conditional OR(||) operator and unconditional OR (|) operators Modify the following program . Modify the program and let the program can print out the difference between conditional AND(&&) operator and unconditional AND (&) operators. Submit your running results. // e07_q07.java // Program can print out the difference between conditional OR(||) operator and unconditional OR (|) operators. // Program can print out the difference between conditional AND(&&) operator and unconditional AND (&) operators. import java.util.Scanner; public class e07_q07 { // perform miles per gallon calculations public static void main( String args[] ) { Scanner input = new Scanner( System.in ); int x; // for test operators boolean y; // for run boolean expressions int z; // to control program stop or not // prompt user for miles and obtain the input from user System.out.print( "Enter any integer number to run the program (-1 to quit): " ); z = input.nextInt(); // exit if the input is -1 otherwise, proceed with the program while ( z != -1 ) { // Program can print out the difference between conditional OR(||) operator and unconditional OR (|) operators. System.out.println( " difference between conditional OR(||)operator and unconditional OR(|)operators\n" ); System.out.println( " unconditional OR (|) operators: (1 == x) | (10 > x++)" ); x=1; System.out.printf( "%s %d \n", " Before (1 == x) | (10 > x++), x = ", x); y = (1 == x) | (10 > x++); System.out.printf( "%s %d \n\n", "After (1 == x) | (10 > x++), x = ", x); x=1; CPSC150 EH07 5 System.out.println( " Conditional OR (||) operators: (1 == x) || (10 > x++) " ); System.out.printf( "%s %d \n ", " Before (1 == x) | |(10 > x++), x = ", x); y = (1 == x) || (10 > x++); System.out.printf( "%s %d \n \n", "After (1 == x) | |(10 > x++), x = ", x); // print out the difference between conditional AND(&&) operator and unconditional AND ($) operators. System.out.println( "difference between conditional AND($$) operator and unconditional AND (&) operators\n" ); // Missing Part // End of Missing Part // prompt user for new try System.out.print( "Enter any integer number to run the program (-1 to quit): " ); z = input.nextInt(); } // end while loop } // end main method } // end class e07_q07 Q8: The US federal personal income tax is calculated based on the filing status and taxable income. There are four filing statuses: single filers, married filing jointly, married filing separately, and head of household. The tax rates for 2002 are shown in Table 3.1 Please read the following program and fill in “left for Exercise” parts. Make you program run and submit your running result. // e07_q08.java: Computer Tax with Selection Statement import javax.swing.JOptionPane; public class e07_q08 { public static void main(String[] args) { // Prompt the user to enter filing status String statusString = JOptionPane.showInputDialog( "Enter the filing status:\n" + "(0-single filer, 1-married jointly,\n" + "2-married separately, 3-head of household)"); int status = Integer.parseInt(statusString); CPSC150 EH07 6 // Prompt the user to enter taxable income String incomeString = JOptionPane.showInputDialog( "Enter the taxable income:"); double income = Double.parseDouble(incomeString); // Compute tax double tax = 0; if (status == 0) { // Compute tax for single filers if (income <= 6000) tax = income * 0.10; else if (income <= 27950) tax = 6000 * 0.10 + (income - 6000) * 0.15; else if (income <= 67700) tax = 6000 * 0.10 + (27950 - 6000) * 0.15 + (income - 27950) * 0.27; else if (income <= 141250) tax = 6000 * 0.10 + (27950 - 6000) * 0.15 + (67700 - 27950) * 0.27 + (income - 67700) * 0.30; else if (income <= 307050) tax = 6000 * 0.10 + (27950 - 6000) * 0.15 + (67700 - 27950) * 0.27 + (141250 - 67700) * 0.30 + (income - 141250) * 0.35; else tax = 6000 * 0.10 + (27950 - 6000) * 0.15 + (67700 - 27950) * 0.27 + (141250 - 67700) * 0.30 + (307050 - 141250) * 0.35 + (income - 307050) * 0.386; } else if (status == 1) { // Compute tax for married file jointly // Left as exercise } else if (status == 2) { // Compute tax for married separately // Left as exercise } else if (status == 3) { // Compute tax for head of household // Left as exercise } else { System.out.println("Error: invalid status"); System.exit(0); } // Display the result JOptionPane.showMessageDialog(null, "Tax is " + (int)(tax * 100) / 100.0); } } Q9: Using switch statements to replace if- else statements in Question 8. Make the program run and submit your running result. CPSC150 EH07 7