ANSWERS TO MIDTERM D 1.a. See Lecture Summary from 6/24/02. 1.b. Although writing your own classes is rather complex, using classes is fairly straightforward for beginning programmers. Most Java textbooks take the "objects early" approach, where objects are used from Day 1, but writing classes is postponed until later (until after the midterm in our case.) Here are some of the classes that we have used before the midterm: System, Coin, String, DecimalFormat, Math, Applet, Color, Graphics. There is also the NumberFormat class discussed in the text that is useful, but which we didn't discuss. 2. x y z a x > 0 && y > 0 x >= y -----+-----+-----+----------+------------------+---------- 60 100 0 true false 20 25 1 1-1 true false 6 6 2 2-1-12 true true 2 1 3 3-2-1-123 true true 0 0 4 4-3-2-1-1234 false Output: 60100 251-120 62-1-126 23-2-1-1231 3. public static void main(String args[]) { String inputString, outputString; int len; inputString = JOptionPane.showInputDialog("Enter a string")); len = inputString.length(); if (inputString.charAt(0) == 'e' || inputString.charAt(len - 1)) outputString = "Yes"; else outputString = "No"; System.out.println(outputString); } 4. public static void main(String args[]) { double hoursWorked, overtimeHours = 0.0; int overtimeCount = 0; hoursWorked = Double.parseDouble( JOptionPane.showInputDialog("Enter hours worked.")); while(hoursWorked >= 0) { if (hoursWorked > 40) { overtimeHours += hoursWorked - 40; overtimeCount++; } hoursWorked = Double.parseDouble( JOptionPane.showInputDialog("Enter hours worked.")); } System.out.println( "Total overtime hours are " + overtimeHours); System.out.println( "Numbers of employees with overtime are " + overtimeCount); } 5. public class MyApplet extends Applet { public void init() { setBackground(Color.white); setSize(150, 150); } public void paint(Graphics g) { for(int x = 0; x <= 150; x += 10) g.drawLine(0, 0, x, 150); } }