Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
CS46A Exam 2 Exam Rules • You may use any books, notes, files, web sites of your choice, except as noted below. • It is a good idea to compile and run your programs if you have the time to do it. • You may NOT communicate with anyone during the exam. • You may NOT use the work of anyone else without attribution. • You may NOT open any programs or web sites that allow communication with someone else. No messenger, chat, email, discussion forum, etc. • The proctor will randomly inspect laptops. If your laptop is inspected, you need take the hands off the keyboard immediately. • Save your solutions into Java source files (extension .java) or plain text files (extension .txt). Be sure to save your work occasionally. Submit your solution to the Desire2Learn drop box. You may submit as many versions as you like—the last one will be graded. 1. [10 points] Consider the someMethod below. public int someMethod(String str1, String str2) { String temp; int position = -1; boolean found = false; for (int i=0; i < str1.length() - str2.length(); i++) { temp = str1.substring(i, (i + str2.length())); if(temp.equals(str2) && !found) { found = true; position = i; } } return position; } a) Complete the table below for tracing the execution of the method call someMethod("bananas", "ana"). position found i temp =========================================== b) Write a javadoc comment that explains the purpose, parameters, and return value of this method. You must describe a plausible purpose. You will receive no credit for an answer that merely describes the implementation. You need to tell which parameter values you consider valid. (For example, you will want to exclude those that you found in part b.) Submit a file problem1.txt 2. [10 points] Complete the Ex2Component class below to draw ten circles that have radius 10, 20, ..., 100, touch the line x = 0 at the left and the line y = 200 at the bottom. You must use a loop in your solution. You will receive no credit for a solution that draws ten separate circles. import import import import javax.swing.JComponent; java.awt.Graphics; java.awt.Graphics2D; java.awt.geom.Ellipse2D; /** Draws ten circles. */ public class Ex2Component extends JComponent { public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; // Any work you want to do before the loop // Some loop of your choice { // Complete this loop body double x = ...; double y = ...; double width = ...; double height = ...; Ellipse2D.Double circle = new Ellipse2D.Double(x, y, width, height); g2.draw(circle); } } } A viewer class has been provided for your convenience. import javax.swing.JFrame; public class Ex2Viewer { public static void main(String[] args) { JFrame frame = new JFrame(); final int FRAME_WIDTH = 500; final int FRAME_HEIGHT = 500; frame.setSize(500, 500); frame.setTitle("Ex2Viewer"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Ex2Component component = new Ex2Component(); frame.add(component); frame.setVisible(true); } } 3. [15 points] Complete the methods in the ArrayMethodsTester class below. Each method receives an array list of Color objects . Your task is to find the largest amount of green in any of the Color elements and the average amount of green in all the Color elements. To find the amount of green of a color, call int green = color.getGreen(); // Returns a value between 0 and 255 import java.util.ArrayList; import java.awt.Color; public class ArrayMethodTester { public static void main(String[] args) { ArrayList<Color> crayons = new ArrayList<Color>(); crayons.add(Color.YELLOW); crayons.add(Color.PINK); crayons.add(Color.GRAY); crayons.add(Color.DARK_GRAY); for (int i = 0; i < crayons.size(); i ++) { System.out.println(crayons.get(i)); } int largest = largestGreen(crayons); System.out.println(largest); double average = averageGreen(crayons); System.out.println("The average is: " + average); crayons.remove(0); average = averageGreen(crayons); System.out.println("Now the average is: " + average); } /* finds the largest green value in the Color ArrayList and returns it */ private static int largestGreen(ArrayList<Color> colors) { //your code here return 0; } /* finds the average amount of green in the Color ArrayList and returns it */ private static double averageGreen(ArrayList<Color> colors) { //your code here return 0; } } 4. [10 points] Consider the CSV and CSVTester classes below. You do not need to understand the code. This question tests whether you can use the debugger. Using the BlueJ debugger, set a breakpoint on the following line of CSV.java, inside the getPositionOfCommaAfter method: int j = getPositionOfQuoteAfter(i + 1) + 1; // Set breakpoint here Run the program until it reaches the breakpoint. a) What is the value of i? b) Then step inside the getPositionOfQuoteAfter method. What is the value of i when you reach the first line inside the method? c) Continue stepping through this method. How many times is the while loop traversed? d) What is the value of pos after exiting the loop? e) Take a screen capture of the debugger when you are at the last line of getPositionOfQuoteAfter, i.e. return k; // Take screen capture here Submit a file problem4.txt and a file containing your screen capture. /** A data set that is stored as comma-separated values. */ public class CSV { private String data; /** Constructs an empty data set. */ public CSV() { data = null; } /** Adds an item to the data. @param item the item to add */ public void add(String item) { String toAdd = item.replace("\"", "\"\""); if (item.contains(",")) { toAdd = "\"" + toAdd + "\""; } if (data == null) data = toAdd; else data = data + "," + toAdd; } /** Gets an item. @param n the index of the item to get @return the nth item, or null if there is no such item */ public String get(int n) { if (n < 0) return null; if (data == null || data.length() == 0) return null; int start = -1; for (int k = 0; k < n; k++) { start = getPositionOfCommaAfter(start); if (start == -1) return null; } int end = getPositionOfCommaAfter(start); String item; if (end == -1) item = data.substring(start + 1); else item = data.substring(start + 1, end); if (item.startsWith("\"")) return item.substring(1, item.length() - 1).replace("\"\"", "\""); else return item; } /** Searches for a comma after a given position. @param i the position after which to search. Should be -1 (start of data) or the position of a comma. @return the position of the next comma after i, or -1 if none */ private int getPositionOfCommaAfter(int i) { if (i >= data.length() - 1) return -1; if (data.charAt(i + 1) == '"') { int j = getPositionOfQuoteAfter(i + 1) + 1; // Set breakpoint here if (j == data.length()) return -1; else return j; } else return data.indexOf(",", i + 1); } /** Searches for a quotation mark after a given position. @param i the position after which to search. @return the position of the next quote after i, or -1 if none */ private int getPositionOfQuoteAfter(int i) { boolean done = false; int start = i + 1; int k = -1; while (!done) { k = data.indexOf("\"", start); if (k == data.length() - 1 || k == -1 || data.charAt(k + 1) != '"') done = true; else start = k + 2; } return k; // Take screen capture here } /** Gets the data. @return the comma-separated items */ public String getData() { return data; } } public class CSVTester { public static void main(String[] args) { CSV myData = new CSV(); myData.add("Diana"); myData.add("Diana's dog"); myData.add("Diana's \"cheerful\" hamster"); // Note that the \" is a " inside a quoted string myData.add("Diana's \"smart, cute, and adorable\" bunny"); String item0 = myData.get(0); System.out.println(item0); System.out.println("Expected: Diana"); String item1 = myData.get(1); System.out.println(item1); System.out.println("Expected: Diana's dog"); String item2 = myData.get(2); System.out.println(item2); System.out.println("Expected: Diana's \"cheerful\" hamster"); String item3 = myData.get(3); System.out.println(item3); System.out.println("Expected: Diana's \"smart, cute, and adorable\" bunny"); } }