Midterm Exam Answers -- Winter 14 Part A. 1. False. You specify the command line arguments in Eclipse with Run >> Run configurations... >> Arguments Tab. Type them into Program arguments. 2. True. char c = 'B'; int n = (int) c; The ASCII code of c is assigned to n, which is 66. 3. False. The possible return values are 3 and 4. Math.random cannot actually return 1. Note: rather than using Math.random, it is usually more convenient to create a Random object and call its nextInt(n) method to get a random int in the range 0 to n-1. 4. True Pass the Scanner object a URL object to read from the web. 5. True Use StringBuilder objects if you want mutable (changeable objects). 6. False A constructor does not have a return type. 7. False The Windows end of line terminator is \r\n, which in hex is 0d0a. 8. False Use a PrintWriter object. 9. True 10. True This is the definition of an instance method. Part B. Not discussed this quarter. Part C. 1. public static boolean isSorted(double[ ] arr) { return arr[0] <= arr[1] && arr[1] <= arr[2]; } public static void main(String[ ] args) { double[ ] a1 = {3.4565, 7.5454, 9.3291}; double[ ] a2 = {7.5454, 3.4565, 9.3291}; System.out.println(isSorted(a1) + " " +isSorted(a2)); } // Output: true false 2. public static int countWords(String input) { return input.split(" ").length; } public static void main(String[ ] args) { String s = "This is a test."; System.out.println(countWords(s)); } Part C: 1. public class CashRegister { public double _totalAmountOfSale; <-- Should be private public int _totalItems; <-- Should be private public void CashRegister( ) { <-- void not allowed for constructor this.clear( ); } public double getTotalAmountOfSale( ) <-- Add { at end of line. return _totalAmountOfSale; } public int getTotalItems( ) { return totalItems; <-- Add _ at beginning of totalItems. } public void enterItem(double amount) { _totalAmountOfSale += amount; _totalItems += 1; System.out.print('Price of item: %$6.2f/n' <-- Need double quotes (2 errors). <-- Change /n to \n. <-- Swap % and $. amount); } public clear( ) { <-- Need public void clear. _totalAmountOfSale = 0.0; _totalItems = 0 } <-- Missing } 2. public static void main(String[ ] args) { CashRegister cr = new CashRegister( ); cr.enterItem(34.54); System.out.println(cr.getTotalItems( )); System.out.println(cr.getTotalAmountOfSale( )); cr.clear( ); System.out.println(cr.getTotalItems( )); System.out.println(cr.getTotalAmountOfSale( )); } Part D: 1. import java.awt.print.Paper; public class Main { public static void main(String[] args) { Paper p = new Paper( ); System.out.println(p.getHeight()/72.0 + " " + p.getWidth()/72.0); System.out.println(p.getImageableHeight()/72.0 + " " + p.getImageableWidth()/72.0); } } // Output: 8.5 11 6.5 9