CMSI 185 Quiz 1 Answers ======================================= 1. k,d,l,o,i,m,a,n,h,j,f,e,b,g,c 2. The variable sum is not initialized. Should be int sum = 0; Also, the loop only goes up to 9999, Should be <= 10000 Also, the println is inside the loop. It should be outside. 3. public class InitialLetterThing { public static void main(String[] args) { for (String s: args) { System.out.print(s.charAt(0)); } System.out.println(); } } 4. public static boolean isLeapYear(int year) { if (year < 1582) { throw new IllegalArgumentException(); } return year % 4 == 0 && year % 100 != 0 || year % 400 == 0; } 5. import javax.swing.JOptionPane; public class FuddifierDialog { public static void main(String[] args) { String s = JOptionPane.showInputDialog("Enter text:"); JOptionPane.showMessageDialog(null, s.replaceAll("[RrLl]", "w")); } } 6. public void paint(Graphics g) { for (int i = 0; i < NUMBER_OF_SQUARES; i++) { int x = randy.nextInt(WIDTH - SIDE_LENGTH); int y = randy.nextInt(HEIGHT - SIDE_LENGTH); g.fillRect(x, y, SIDE_LENGTH, SIDE_LENGTH); } } 7. public static int lengthOf3nPlus1(int n) { if (n <= 0) { throw new IllegalArgumentException(); } int count = 1; while (n != 1) { n = (n % 2 == 1) ? 3 * n + 1 : n / 2; count++; } return count; }