CSC108H-a07-part1 - (6‘ UNIVERSITY OF TORONTO Q Faculty...

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: (6‘ UNIVERSITY OF TORONTO Q Faculty of Arts and Science 6!? August 2007 EXAMINATIONS 62‘ ¢ 050 108H1Y V‘? O - <0 Instructor: Scott Lelshman ’4/ Duration — 3 hours Examination Aids: None Student Number: I I I I 1 Last (Family) Name(s): First (Given) Name(s): 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.) MARKING GUIDE # 1: / 20 This final examination consists of 7 questions on 20 pages (including # 2; / 20 this one). When you receive the signal to start, please make sure that your copy of the examination is complete. # 31 _/ 24 . . # 4: _/ 16 Comments are not necessary except where we explicitly ask for them. # 5: / 12 For 2 bonus marks, write your student number in the space provided at the bottom of both sides of every page of this exam. # 63 ____/ 12 # 7: / 20 TOTAL: _/ 124 Good Luck! Student #1 l . Page 1 CONT’D. .. CSC 108H FINAL EXAMINATION Summer 2007 Question 1. [2O MARKS] 1. Given a non-null and non—empty rectangular two-dimensional array called matrix, write a statement that returns the total number of elements in the matrix. [2 marks] 2. Write a statement that declares a constant char variable that represents the key a user types when they want to move left (as part of a game perhaps), and initializes it with character ’a’. You should not be able to change the value assigned to this variable after its initialization, and the variable should be associated with the class it is defined in (as opposed to objects of that class). [2 marks] 3. Assume you are given an int array age that contains at least 3 elements, and Whose length is an odd number. Write an expression to evaluate the sum of the first element, the middle element, and the last element in the array age. [2 marks] 4. Given an int variable called num that is assumed to contain a positive integer less than 10, use a single while loop to print (to System.out) the value that num represents, exactly num times. Each occurence should appear on the same line. Valid examples include: 1, 333, and 55555 [3 marks] 5. Assume you have a class Train. Declare a subclass called VIARailEast that customizes the Train class and contains a String array instance variable called destination Whose cities are "Toronto" , "Ottawa" , and "Montreal". Don’t implement any constructors or methods. [2 marks] Student #2 , I I , l I , Page 2 of 20 CONT’D. .. Summer 2007 FINAL EXAMINATION CSC 108H 6. In a leap year, February has 29 days instead of 28. A year is a leap year if and only if the year is divisible by 4 and not divisible by 100, or the year is divisible by 400. Given an int variable year that contains the value of the current year, use the ternary operator : ? to assign the correct number of days to an already declared int variable called febDays. [2 marks] 7. Assume that a class named Rectangle with a JFrame instance variable frame, and a static double variable totalArea has been declared. Write a constructor for Rectangle which takes two int parameters named width and height, and uses them to initialize the Frame instance variable with those dimensions. When a new Rectangle object is created, its area should be added to totalArea. [3 marks] 8. In a test designed to evaluate the performance of a group of video game players playing a shooting game, the test results containing the response time and accuracy for each player are recorded in the following format: "a, 1.2,0.7#b,2.1,0.86#c,1.5,0.9". "a, 1.2,0.7" means player a took 1.2 seconds and achieved 70% accuracy (# is used to separate individual records). Write a method called averageTime, that takes a single String parameter called results as input, and returns the average player response time measured as a double. The average response time is calculated by adding each player’s response time, then dividing by the number of players. Assume you have at your disposal an already implemented helper method getTime(String s) that can extract the reponse time (as a double) from an input string of the form "<player>,<time>,<accuracy>" (like "a, 1 . 2,0.7" for example). [4 marks] Student #: , , , , . Page 3 of 20 CONT’D. .. CSC 108H FINAL EXAMINATION Summer 2007 Question 2. [2O MARKS] Complete the 3—column table in each question below. For each question: 0 If the code fragment has a compile error, then place a checkmark J in the first column. 0 If the code fragment compiles, but has a run-time error, then place a checkmark / in the second column. 0 If there is no error, then in the third column write down the result of the specified variable after executing the code fragment. If there is an error then do not write in the last column. If you know there is no error, but you are unsure of the result, then place a checkmark / in the last column for part marks. Note: Each table has one and only one column that you need to fill; filling more than one column will get a mark of 0. Each question is worth 2 marks. You should assume the following: o All necessary import statements have been declared. o All code fragments are independent of each other. "test"; a. substring<0 , a. length()) 1. String a String b 2. intx inty intz z=++x y?x-1:z; Compile error_____ ll VHHH z’s value: _______________ __ Run-time error _______ __ 3. int a = {5,4,3,2,1}; int 1: = a[a[a[0] % a-length] ‘ 1]; Compile error_________ 1: ’5 value : _________________- Run—time error _______ __ 4. intx=(6+7)%5/2+5; String s = "August"; 5 = s.substring(s.index0f(’A’), x + 1); Compile error_________ 5 ’s value: ____________-____ Run-time error _______ __ Student #: , , , , Page 4 of 20 CONT’D. .. Summer 2007 FINAL EXAMINATION CSC 108H 5. int x = 0; int y = 0; while (y < 3) ++y; x += y; x’s value:____-___________ Compile error__ _____ __ Run-time error_________ 6. double a = 8.2; int b = 2; int x; x = b / (a * 2); Compile error _______ __ x’s value: _______________ __ Run—time error _______ __ 7. int x = 555; int y = 640; String d = "."; d = x + d + y + d + (7900 + 5); Compile error _______ __ Run-time error _______ __ d’s value: ________________ __ 8. int [1 a = {5, 2, 9, 7, —1}; int x = 0; if (a[0] > 0 ll a[a.length] < O) { x = a[0] - a[a.1ength — 1]; } Compile error _______ __ x’s value: ________________ __ Run-time error _____ ___ 9. String [J a = {{"a", "b"}, {"c", "d"}, {"e", "f"}}; String t = ""; for (int i = O; i < a[0].length; i++) { for (int j = 0; j < a.length; j++) { t = t + aEiJEj]; } } Compile error-________ Run—time error _______ __ t’s value: ________________ __ 10. String x = new String [4]; int y = x[0] .lengthO; Compile error _______ __ Run-time error _______ __ y’s value: ________________ __ Student #: . , , . l , Page 5 of 20 CONT’D. .. CSC 108H FINAL EXAMINATION Summer 2007 Question 3. [24 MARKS] Part (a) [6 MARKS] Complete the following code. /** * The Allele class represents some portion of an organisms DNA, encoding a particular * trait of a gene (like brown or blue for the gene responsible for eye colour). * Each allele has a particular trait value, can be classified as either dominant * or recessive, and is associated with a particular gene. */ public class Allele { // Declare any necessary instance/static variables here /** * Create a new Allele instance based on the parameters passed. * @param g The name of the gene this allele represents ex. "eye colour" * @param v The trait value of this particular allele ex. "blue" * Qparam d Whether this allele is considered dominant (true means it is, and * false means it is recessive */ public Allele(String g, String v, boolean d) { } /** * Return whether or not this allele represents a dominant trait. * ©return boolean with value true if dominant or false if recessive */ public boolean isDominant() { } /** * Get the gene this allele is associated with. * @return A String specifying the name of the gene this allele is associated with */ public String getGene() { Student #: l , . Page 6 of 20 CONT’D. .. Summer 2007 FINAL EXAMINATION CSC 108H /** * Get the trait value that this allele encodes. * @return A String specifying the trait value ex. "blue" or "tall" etc. */ public String getValue() { Part (b) [18 MARKS] This question continues from Part (a). Complete the following code. You will be marked on both design and correctness. /** This class represents an organisms genome (genetic sequence). The genetic sequence is composed of a finite number of alleles (which are just arbitrary length sequences of DNA that happen to encode gene traits). Some parts of the sequence do not code gene traits. A genome may be monoploid (each gene is determined by a single allele), diploid (gene values are determined by two alleles -- one from each parent in the case of humans), or polyploid (gene values are determined by more than one allele). ******* */ public class Genome { // Declare any necessary instance/static variables here /** * Create a new Genome instance based on the parameters passed. * @param len The length of this genome sequence (in DNA sections). Each DNA section * will either encode an allele, or be non—coding. If a section encodes an allele * it will be represented by an Allele object, if it is non-coding, it should be * represented by null. Initially, each section should be non-coding. */ public Genome(int len) { Student #: , . Page 7 of 20 CONT’D. . . CSC 108H FINAL EXAMINATION Summer 2007 /** * Return the number of alleles in this genome sequence that are associated * with the gene passed. * @param g The name of the gene to count allele occurrences of. * ©return The number of times alleles that represent the gene passed appear :0: in this sequence. */ public int countAlleles(String g) { /** * Attempt to insert the allele given at the sequence position specified. * ©param a The allele object to insert. You may assume a is not null. * @param i The index at which to insert the allele. You may assume that the index * passed will be valid (non-negative and within the range of sequence positions). * @return A boolean specifying true if the allele was successfully inserted, or false if it * could not be inserted (because an existing allele is already in the position specified). */ public boolean insertAllele(Allele a, int i) { 4 Student #: , , , I , Page 8 of 20 CONT’D. .. Summer 2007 FINAL EXAMINATION CSC 108H /** * Recover all of the locations of alleles representing the gene specified. * ©param g The name of the gene to search for alleles of. * @return An array of ints containing the sequence positions of each allele of * that gene. If no such alleles can be found, then null is returned instead. */ public int getGeneLocations(String g) { /** Determine whether the gene passed is diploid recessive in this organisms genome. This can only happen if exactly two alleles of the gene are * 2.: * present, and both of them are recessive. * @param g The name of the gene to check for diploid recessivity. * @return boolean with value true if this gene is diploid recessive, and false otherwise. */ public boolean isDiploidRecessive(String g) { Student #2 I , , I , , Page 9 of 20 CONT’D. .. CSC 108H Question 4. FINAL EXAMINATION [16 MARKS] Summer 2007 Complete the following methods. Assume that all methods belong to the same class. You will be marked based on correctness and design. Part (a) \ * * * ************ [6 MARKS] Compute the inner product (dot product) of the two one—dimensional arrays passed. Recall from your old algebra classes that the inner product is the sum of the product of corresponding elements. Thus, given arrays {1, 5, 2} and {10, 6, 4} for example, the calculation should be: 1 * 10 + 5 * 6 + 2 * 4 = 48. We assume that both arrays are of the same length, and that each contains at least one element. @param a1 The first array to be used in the inner product calculation @param a2 The second array to be used in the inner product calculation @return An int specifying the scalar inner product result / public static int innerProduct(int a1, int a2) { } Part (b) [0 MARK] Note that you don’t need to do anything for this part except use this method in completing the next part. /** * Return the elements of the specified column of the matrix passed. * * @param m The matrix to extract a column from. We assume the matrix is not * null and each row contains at least i+1 columns. * @param i The column index. We assume the value passed is not-negative, * and the indices start at O. * @return An int array whose elements are the same as those of the required * column. */ public static int getColumn(int m, int i) { } Student #2 , I l I int[J res = new int[m.length]; for (int j = 0; j < m.length; j++) { resEj] = mljl [i]; } return res; Page 10 of 20 CONT’D. .. ...
View Full Document

Page1 / 10

CSC108H-a07-part1 - (6‘ UNIVERSITY OF TORONTO Q Faculty...

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