CSC108H-d06-part1 - UNIVERSITY OF TORONTO 6‘ Faculty of...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 4
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 6
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 8
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 10
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: UNIVERSITY OF TORONTO 6‘ Faculty of Arts and Science DECEMBER EXAMINATIONS / CSC 108H|F ¢ Duration — 3 hours Examination Aids: None Student Number: I I I I I I I I I I Last (Family) Name(s): First (Given) Name(s): Instructor: K I (circle one) ra Campbell Sills Do not turn this page until you have received the signal to start. (In the meantime, please fill out the identification section above, and read the instructions below carefully.) This final examination consists of 8 questions on 18 pages (in- cluding this one). When you receive the signal to start, please make sure that your copy of the examination is complete. Comments are not necessary except where we ask for them. Write your student number in the space provided at the bottom of both sides of every page of this exam. Good Luck! Page 1 Student#:IIIIIIIIII MARKING GUIDE #=L #:2: #:3: I% 4: #:5: #:6: #:7: #28: TOTAL: ______/ 7 / 13 / 10 __.._/ 10 _/ 8 _/ 12 ___/ 20 / 20 /100 CONT’D. .. CSC 108H FINAL EXAMINATION DECEMBER 2006 Question 1. [7 MARKS] Part (a) [2 MARKS] Rewrite the code below using a single return statement: if (a&&b II !c) { return false; } else { return true; } Part (b) [5 MARKS] public void mystery() { String s = "Happy holidays to all!"; int num = s.index0f(“ ", 6); String 52 = s.substring(0, num); int num2 = s.index0f(s2); // Show variable values here. for(int i = 0; i < s.length(); i++) { int x s.index0f(“o", num2); System.out.print1n(s.substring(num2, x)); num2 = x + 1; i = num2; } else { System.out.print1n("all"); I I I I I I I I I if (x >= 0) { I I I I I I i = s.length(); I I I I What is printed by the method mystery? Show What values the variables have as the code executes. StIIdentquézI . . . , , . l I , Page20f18 CONT’D... DECEMBER 2006 FINAL EXAMINATION CSC 108H Question 2. [13 MARKS] Part (a) [8 MARKS] Complete the following method: public class Exam { /** Given an unsorted array of integers that may contain duplicate values, * return the integer that appears in the array the greatest number of times. * You may assume that there is at least one element in the array and that * no two values will be in the array the same number of times. */ public static int getMostFrequent(int a) { } } Part (b) [5 MARKS] Complete the JUnit test method below, by writing three meaningful assertEquals calls to test getMostFrequent (int [3 ). public class ExamTester extends TestCase { public void testGetMostFrequentO { Student #:. . . , . , . . , . Page3of18 CONT’D... CSC 108H FINAL EXAMINATION DECEMBER 2006 Question 3. [10 MARKS] Complete the following method: /** * Return the average of the student grades in studentList. * * studentList is a comma—separated string of student information in the form: * studentNamezstudentGrade a: * Here is an example: * "Lisa Wang:87.8,Terry Liu:65.4,A1i Aziz:73" * * You may assume that there will be at least one student in studentList. */ public double getAverage (String studentList) { Student#:. . . . . . . . . . Page4of18 CONT’D... DECEMBER 2006 FINAL EXAMINATION CSC 108H Question 4. [10 MARKS] Complete the following methods (all three methods are in the same class). You will be marked on correct- ness,and defign. Part (a) [5 MARKS] /** Sum the contents of each row of table, and return the sum of each row from table * in a 1D array of integers. The sum of row 0 should be in element 0 of the * result, the sum of row 1 in element 1 of the result, and so on.*/ public int rowSums( int[J table ) { } Part (b) [3 MARKS] /** Return true if all elements of list have the same value, and return false * otherwise. */ public boolean allEqual( int list ) { } Part (c) [2 MARKS] /** Return true if all rows of table have the same sum, and return false * otherwise. */ public boolean sameRowSums ( int[J table ) { Student#:. . . I . , . , I , Page50f18 CONT’D... CSC 108H FINAL EXAMINATION DECEMBER 2006 Question 5. [8 MARKS] Write a set of test cases for the sameRowSums method from Question 4, part c. Provide at least 4 arrays that you would use. Draw the test arrays. With each test array write a brief explanation of what the array is testing. Here is one example test with an explanation: Explanation: sameRowSums returns true. Each row has different values, but all row sums are equal. Student #:. . . . , . . . . , Page6of18 CONT’D... DECEMBER 2006 FINAL EXAMINATION CSC 108H Question 6. [12 MARKS] Complete the following methods. You may assume that all necessary import statements have been made. Part (a) [6 MARKS] /** Given a filename f and a string s, return true if s is a * line of the file, and return false otherwise. * * @param f The name of the file to look in. * @param s The line to look for in the file. */ public boolean matchesLine(String f, String s) throws IOException { Part (b) [6 MARKS] /** Write the contents of string 3 to the new file “repeat.txt” n times. * * @param s The text to write to the file. * @param n The number of times to write the text to the file. */ public void createRepeat(String 8, int n) throws IOException { Student #:, . , . I I . . I . Page7of18 CONT’D... CSC 108H FINAL EXAMINATION DECEMBER 2006 Question 7. [20 MARKS] Part (a) [8 MARKS] Complete the following code: /** A book in a library with a title, an author, and a status indicating * whether or not it has been loaned out. */ public class LibraryBook { // Declare any necessary instance/static variables here. /** A new book record with title t, author a, and loan status 1. */ public LibraryBook ( String t, String a, boolean 1 ) { } /** Get the title of this book. */ public String getTitleO { } /** Get the author of this book. */ public String getAuthorO { } /** Get whether or not this book is currently loaned. */ public boolean isLoaned() { Student-fie:l , . . I , . . . . Page80f18 CONT’D... DECEMBER 2006 FINAL EXAMINATION CSC 108H /** Loan 3. copy of this book, if it is currently unloaned. Return * true if the book is succesfully loaned, and return false otherwise. */ public boolean loanBook() { } /** Return true if b represents the same book as this book, and false * otherwise. Books are considered the same if they have the same title * and author. */ public boolean equals( LibraryBook b ) { } } Part (b) [12 MARKS] This question continues from Part (a). Complete the following code. You will be marked on both design and correctness. Keep in mind that some of the methods can and should be called by other methods as helper methods. /** A library with a catalogue of books available. */ public class Library { // Declare any necessary instance/static variables here /** A new library that can hold a maximum of 11 books */ public Library( int n ) { Student #:I I I I I I I I I I Page90f18 CONT’D... CSC 108H FINAL EXAMINATION DECEMBER 2006 /** Add a book to the library’s catalogue. If the library is full * or if a book with that title is already in the library, * then it should not be added. Return true if the book is added, and * false otherwise. */ public boolean addBook( LibraryBook b ) { } /** Loan a copy of book b. A book cannot be loaned out if it is not * in the library’s catalogue or if it is currently loaned out. * Return true if the book is successfully loaned, and false otherwise. */ public boolean loanBook( LibraryBook b ) { Student #:. . . . . . . , . , Page 100f18 CONT’D... ...
View Full Document

Page1 / 10

CSC108H-d06-part1 - UNIVERSITY OF TORONTO 6‘ Faculty of...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online