ANSWERS FOR MIDTERM C 1a. x y x > 0 Output: ----+-----+------ 3 107 0 true 21 1 true 4 2 true 0 3 false 1b. int y = 0; for(int x = 107; x > 0; x /= 5) y++; System.out.println(y); 1c. Then there would be no body to the while top, the condition would never change and the while loop would be true and repeat forever. 2. x y a x + y > 0 x + y > 50 -----+-----+----------+----------+---------- 2 7 abc true false 13 34 34abc true false 73 175 17334abc false true 17534abc73 false true Output: 17534abc73 false true 3. public static void main(String[] args) { double input; DecimalFormat df = new DecimalFormat("#0.00"); input = Double.parseDouble( JOptionPane.showInputDialog("Enter amount in Francs.")); System.out.println(input + " Francs gives you " + df.format(2.1 * output / 6.5) + " Marks."); } 3. public static void main(String[] args) { double hours, wage, totalWages = 0.0; DecimalFormat df = new DecimalFormat("#0.00"); hours = Double.parseDouble( JOptionPane.showInputDialog("Enter hours.")); wage = Double.parseDouble( JOptionPane.showInputDialog("Enter hours.")); if (hours > 40) totalWages = 40.0 * wage + (40.0 - hours) * 1.5 * wage; else totalWages = wage * hours; System.out.println("Total wages are " + totalWages); } 4. public static void main(String[] args) { int number, maxEven = 0, maxOdd = 0; boolean existsAnEven = false, existsAnOdd = false; number = Integer.parseInt( JOptionPane.showInputDialog("Enter an int.")); while(number >= 0) { if (number % 2 == 0) { maxEven = Math.max(maxEven, number); existsAnEven = true; } else { maxOdd = Math.max(maxOdd, number); existsAOdd = true; } number = Integer.parseInt( JOptionPane.showInputDialog("Enter an int.")); } if (existsAnEven) System.out.println("The maximum even integer is " + maxEven + "."); else System.out.println("No even integers input."); if (existsAnOdd) System.out.println("The maximum odd integer is " + maxOdd + "."); else System.out.println("No odd integers input."); } 6. public void paint(Graphics g) { int i, x, y; for(i = 0; i <= 10000; i++) { x = (int) (50 * Math.random() + 50); y = (int) (50 * Math.random() + 50); g.fillOval(x, y, 1, 1); x = (int) (50 * Math.random() + 150); y = (int) (50 * Math.random() + 150); g.fillOval(x, y, 1, 1); } }