Preview only show first 10 pages with watermark. For full document please download

Null 10319289

   EMBED


Share

Transcript

Family Name: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Other Names: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ID Number: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Signature . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Model Solutions COMP 102: Test 2 11 May, 2012 Instructions • Time allowed: 45 minutes • There are 45 marks in total. • Answer all the questions. • Write your answers in the boxes in this test paper and hand in all sheets. You may ask for additional paper if you need it. • If you think some question is unclear, ask for clarification. • Brief Java documentation will be supplied with the test. • This test will contribute 15% of your final grade, (But your mark will be boosted up to your exam mark if that is higher.) • You may use paper translation dictionaries, and calculators without a full set of alphabet keys. • You may write notes and working on this paper, but make sure it is clear where your answers are. Questions Marks 1. Understanding Java [17] 2. Event Driven Input [10] 3. Defining Classes [10] 4. File Processing [8] TOTAL: SPARE PAGE FOR EXTRA ANSWERS Cross out rough working that you do not want marked. Specify the question number for work that you do want marked. COMP 102 (Terms Test 2) 2 continued... Question 1. Understanding Java [17 marks] (a) [4 marks] Consider the following counting method: public void counting(int num){ while (num > 0){ if ( num <= 5 ) { UI. println ("then: " + num∗3); } else { UI. println ("now: " + num); } num = num − 2; } UI. println ("finally: " + num); } What will be printed if counting(9) is called? now: 9 now: 7 then: 15 then: 9 then: 3 finally: -1 (b) [3 marks] Consider the following choose method: public void choose(int a, int b, int c){ if ( a > c ) { UI. println ("March"); } else if ( b > c && a < c ) { UI. println ("April"); } else if ( c == b || c == a ) { UI. println ("May"); } else { UI. println ("June"); } } What will be printed if choose(20, 15,10) is called? March What will be printed if choose(15, 20, 15) is called? May What will be printed if choose(10, 10, 20) is called? June COMP 102 (Terms Test 2) 3 continued... SPARE PAGE FOR EXTRA ANSWERS Cross out rough working that you do not want marked. Specify the question number for work that you do want marked. Spare copy of grid: 100 200 300 400 500 100 200 300 400 COMP 102 (Terms Test 2) 4 continued... (Question 1 continued) (c) [4 marks] Suppose the file called SomeText.txt contains the following: Hello 300 100 GoodBye 100 300 What will the following fileDraw method draw on the graphics pane? (Answer on the grid below.) Hint: keep track of the values of all the variables public void fileDraw(){ try{ Scanner scan = new Scanner (new File("SomeText.txt")); int x = 100; int y = 100; while ( scan.hasNext() ) { String token = scan.next (); UI.drawString(token, x, y ); int u = scan.nextInt (); int v = scan.nextInt (); UI.drawLine(x, y, u, v ); x = u; y = v; } scan.close(); } catch(IOException e){UI.println("Fail: " + e);} } 100 100 Hello 200 300 400 x: y: u: v: 500 GoodBye 200 300 400 (Question 1 continued on next page) COMP 102 (Terms Test 2) 5 continued... (Question 1 continued) (d) [6 marks] Consider the LibraryBook class on the facing page. What will the following fragment of code print out? (Note the variables carefully.) LibraryBook b1 = new LibraryBook("Jingo"); b1.lendBook(); b1.returnBook(5); LibraryBook b2 = new LibraryBook("Thud"); b1.lendBook(); b2.lendBook(); b1.returnBook(15); b2.returnBook(8); b1.retireBook (); loans of Jingo : 1 usage of Jingo : 5 loans of Jingo : 2 loans of Thud : 1 usage of Jingo : 20 usage of Thud : 8 retired Jingo : 20/2 (Question 1 continued on next page) COMP 102 (Terms Test 2) 6 continued... (Question 1 continued) public class LibraryBook{ private String title ; private int usage = 0; private int times = 0; public LibraryBook(String ttl ){ this. title = ttl ; } public void lendBook(){ this.times = this.times + 1; UI. println ("loans of " + this.title + " : " + this.times); } public void returnBook(int days){ this.usage = this.usage + days; UI. println ("usage of " + this.title + " : " + this.usage); } public void retireBook(){ UI. printf ("retired %s : %d/%d \n", this.title, this.usage, this.times); } } COMP 102 (Terms Test 2) 7 continued... Question 2. Event-Driven Input [10 marks] The TargetPractice program on the facing page should allow the user to draw red or blue target shapes on the graphics pane, as shown below. If the user presses the mouse at one point and releases it at a second point, then the program should draw a target shape centered at the first point, and a radius equal to the horizontal distance between the two points. (as shown in the left hand target below). The program should have two buttons (labeled Red and Blue) that allow the user to change the color for future targets. The program has three fields; it needs a constructor and methods to respond to the buttons and the mouse. You are to complete the program. Note that there is a drawTarget method already written for you, which requires the center and radius of the target. Red Blue 1 2 radius (Question 2 continued on next page) COMP 102 (Terms Test 2) 8 continued... (Question 2 continued) public class TargetPractice implements UIButtonListener, UIMouseListener{ private Color targetColor = Color.red; // The current color private double centerX = 0; // position the mouse was pressed private double centerY = 0; public TargetPractice(){ UI.setMouseListener(this); UI.addButton("Red", this); UI.addButton("Blue", this); } public void mousePerformed(String action, double x, double y) { if (action .equals("pressed")) { this.centerX = x; this.centerY = y; } else if (action .equals("released")) { double rad = Math.abs(x − this.centerX); this.drawTarget(this.centerX, this.centerY, rad, this.targetColor ); } public void buttonPerformed(String button){ if (button.equals("Red")){ targetColor = Color.red; } else if (button.equals("Blue")){ targetColor = Color.blue; } } /∗∗ Draws target shape , centered at (x, y) with given radius and color ∗/ public void drawTarget(double x, double y, double radius, Color color){ UI.setColor(color ); double rad = radius; while (rad > 5){ UI. fillOval (x−rad, y−rad, rad∗2, rad∗2); UI.eraseOval(x−(rad−10), y−(rad−10), (rad−10)∗2, (rad−10)∗2); rad = rad − 20; } } } COMP 102 (Terms Test 2) 9 continued... Question 3. Defining Classes [10 marks] For this question, you are to complete part of a program that displays the state of a rowing race which has several boats, each in their own lane. For example, the figure shows a race with four boats. lane 1 canty 3 * laneSep units lane 2 otago lane 3 lane 4 vuw auck 165 units The RowBoat class on the facing page represents individual boats. Each boat has a team name, a lane number (1, 2, 3,. . . ), and the distance it has travelled so far along the lane. The draw method will display the boat as a rectangle containing the team name. The boat is displayed on the appropriate lane, and the horizontal position is the distance it has travelled. In the example below, the boat for the “VUW” is in lane 3 and is 165 meters along the course. Every boat starts at a distance of 0 meters along the course. The program can update the position of the boat (though the lane never changes). You are to complete the RowBoat class: • Define three fields to store the name of the team, the lane, and the position of the boat. • Complete the constructor, which should have two parameters specifying the name and the lane. It should also draw the boat on the graphics pane by calling the draw method. • Complete the restart method, which has no parameters. It should reset the distance along the course to 0 and re-draw the boat. It does not need to erase the boat from its old position. • Complete the moveForward method, which should have one parameter—the distance to move forward. It should change the position of the boat by the value of the parameter, and then redraw the boat. It does not need to erase the boat from its old position. • Complete the draw method, which has no parameters. It should draw the boat on the graphics pane. The boat size should be 30 units wide and 15 units high. The constant laneSep specifies the vertical separation between lanes. Hint: use the drawRect and drawString methods. As an example of using the RowBoat class, the testBoat method below creates a boat, and moves it to the right and then restarts it. public void testBoat(){ RowBoat boat = new RowBoat("VUW", 3); UI.clearGraphics(); boat.moveForward(30); UI.sleep(1000); UI.clearGraphics(); boat. restart (); } COMP 102 (Terms Test 2) 10 continued... (Question 3 continued) public class RowBoat{ private static final double laneSep = 50; private String teamName; private int lane; private double distance = 0; public RowBoat(String name, int ln){ this.teamName = name; this.lane = ln ; this.draw(); } public void restart (){ this.distance = 0; this.draw(); } public void moveForward(double move){ this.distance = this.distance + move; this.draw(); } public void draw(){ double y = laneSep ∗ lane; UI.drawRect(this.distance, y−7, 30, 15); UI.drawString(this.teamName, this.distance, y+7); } } COMP 102 (Terms Test 2) 11 continued... Question 4. File Processing [8 marks] Suppose the file boat-list.txt contains information about boats owned by a sailing club. Each line contains the boat ID, the number of crew, the year of purchase, and the boat type For example, part of the file might be: A1 B15 A43 A33 A27 A35 3 2 1 4 1 4 1963 2006 2010 2003 1988 2009 Flying Ant Heron Laser Coronado Laser Coronado The chooseBoats method on the facing page has one parameter — a year. Complete the chooseBoats method so that it reads the “boat-list.txt” file, prints out the boat ID and the boat type of each boat that was purchased in or after the given year, and finally prints out the total number of crew these boats can hold. For example, if the file contained just the data above, then calling chooseBoats(2006); should print out Boats since 2006: B15 Heron A43 Laser A35 Coronado Total Crew: 7 (Question 4 continued on next page) COMP 102 (Terms Test 2) 12 continued... (Question 4 continued) public void chooseBoats(int year){ UI. println ("Boats since " + year); try{ Scanner scan = new Scanner(new File("boat-list.txt")); int totCrew = 0; while (scan.hasNext()){ String num = scan.next(); int crew = scan.nextInt (); int yr = scan.nextInt (); String type = scan.nextLine(); if (yr >= year){ UI. println (" " + num + " " + type); totCrew = totCrew + crew; } } UI. println ("\nTotal Crew: "+ totCrew); scan.close(); }catch(IOException e){UI.println("File Failure: " + e);} } ******************************** COMP 102 (Terms Test 2) 13 SPARE PAGE FOR EXTRA ANSWERS Cross out rough working that you do not want marked. Specify the question number for work that you do want marked. COMP 102 (Terms Test 2) 14 continued...