27.01.2015 Views

1 Sample Midterm Test (Your test will have a format similar to the ...

1 Sample Midterm Test (Your test will have a format similar to the ...

1 Sample Midterm Test (Your test will have a format similar to the ...

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Sample</strong> <strong>Midterm</strong> <strong>Test</strong><br />

(<strong>Your</strong> <strong>test</strong> <strong>will</strong> <strong>have</strong> a <strong>format</strong> <strong>similar</strong> <strong>to</strong> <strong>the</strong> sample given below)<br />

60-322<br />

Mock <strong>test</strong> for Fall 2012<br />

Answer all questions<br />

Only an unmarked copy of a textbook on Object Oriented design or on UML,<br />

your printed class notes (unmarked) <strong>will</strong> be allowed. No computers or any<br />

o<strong>the</strong>r electronic devices allowed. Answer both questions. Note that Question 2 has<br />

2 alternatives. You are advised <strong>to</strong> answer Alternative I which carries 30 marks. If<br />

you fail <strong>to</strong> do so, you should try answering Alternative II which carries 20 marks.<br />

This means that, if you answer Alternative II, <strong>the</strong> maximum marks you can get <strong>will</strong><br />

be 40.<br />

1


Question 1) Consider <strong>the</strong> classes given below. Using UML, draw a class diagram<br />

corresponding <strong>to</strong> <strong>the</strong>se classes.<br />

(Full marks 20)<br />

public class Course {<br />

private String courseNumber;<br />

private Professor professor;<br />

private Course preRequisiteCourses[];<br />

private int numberOfPrerequisiteCourses = 0;<br />

private Student studentsRegisteredInCourse[];<br />

private int numStudentsInCourse = 0;<br />

public Course(String courseNumber,<br />

Professor professorOfferingCourse,<br />

int capacityClassRoom){<br />

this.courseNumber = courseNumber;<br />

professor = professorOfferingCourse;<br />

preRequisiteCourses = new Course[2];<br />

studentsRegisteredInCourse = new Student[capacityClassRoom];<br />

}<br />

public void addPrerequisite(Course newPreRequisite){<br />

preRequisiteCourses[numberOfPrerequisiteCourses] =<br />

newPreRequisite;<br />

numberOfPrerequisiteCourses++;<br />

}<br />

public void insertStudent(Student aStudent){<br />

studentsRegisteredInCourse[numStudentsInCourse] = aStudent;<br />

numStudentsInCourse++;<br />

}<br />

public double getFeesFromCourse(){<br />

double <strong>to</strong>talFees = 0.0; //<br />

Student aStudent;<br />

for (int index = 0; index < numStudentsInCourse; index++){<br />

aStudent = studentsRegisteredInCourse[index];<br />

switch (aStudent. getStudentType())<br />

{ case Student. CANADIANSTUDENT: <strong>to</strong>talFees += 300.00;<br />

// Canadian students pay $300 per course<br />

break;<br />

case Student. CANADIANSENIOR: <strong>to</strong>talFees += 10.00;<br />

// Canadian seniors citizens pay $10 per course;<br />

break;<br />

case Student. FOREIGNSTUDENT: <strong>to</strong>talFees += 1000.00;<br />

// Foreign students pay $1000 per course;<br />

break;<br />

}<br />

}<br />

2


}<br />

return <strong>to</strong>talFees;<br />

}<br />

public String <strong>to</strong>String(){<br />

String result;<br />

result = "Course # " + courseNumber +<br />

" is taught by " + professor. <strong>to</strong>String();<br />

for (int index = 0; index < numStudentsInCourse; index++){<br />

result += "\n" + studentsRegisteredInCourse[index];<br />

}<br />

return result;<br />

}<br />

public class Student {<br />

public static final int CANADIANSTUDENT = 1;<br />

public static final int CANADIANSENIOR = 2;<br />

public static final int FOREIGNSTUDENT = 3;<br />

private String studentName;<br />

private String studentNumber;<br />

private int studentType;<br />

public Student(String name, String studentId, int studentType){<br />

studentName = name;<br />

studentNumber = studentId;<br />

this.studentType = studentType;<br />

}<br />

public int getStudentType(){<br />

return studentType;<br />

}<br />

public String <strong>to</strong>String(){<br />

String result;<br />

result = "Name is " + studentName + "; Id is " + studentNumber;<br />

switch (studentType)<br />

{ case CANADIANSTUDENT: return result + " - a Canadian";<br />

case CANADIANSENIOR: return result + " - a Canadian Senior";<br />

case FOREIGNSTUDENT: return result + " - a Foreign Student";<br />

}<br />

return result;<br />

}<br />

}<br />

public interface Sortable {<br />

public boolean lessThan(Sortable x);<br />

}<br />

3


}<br />

public abstract class Professor implements Sortable{<br />

private String professorName;<br />

public Professor(String professorName){<br />

this.professorName = professorName;<br />

}<br />

public String getName(){<br />

return professorName;<br />

}<br />

public boolean lessThan(Sortable x){<br />

if (this instanceof PartTimeFaculty)return true;<br />

else return false;<br />

}<br />

public class PartTimeFaculty extends Professor {<br />

private String phoneNumber;<br />

public PartTimeFaculty(String phoneNumber, String name){<br />

super(name);<br />

this.phoneNumber = phoneNumber;<br />

}<br />

public String <strong>to</strong>String(){<br />

return super.getName() + "; Phone is " + phoneNumber;<br />

}<br />

}<br />

public class FullTimeFaculty extends Professor {<br />

private String specializationArea;<br />

public FullTimeFaculty(String specializationArea, String name){<br />

super(name);<br />

this.specializationArea = specializationArea;<br />

}<br />

public String <strong>to</strong>String(){<br />

return super.getName() + "; specializes in " + specializationArea;<br />

}<br />

}<br />

Question 2Alternative a) Using Abstract Fac<strong>to</strong>ry Pattern show how you can<br />

solve <strong>the</strong> following problem. Note that you <strong>will</strong> not get any credit if you do not use<br />

<strong>the</strong> Abstract Fac<strong>to</strong>ry pattern in solving <strong>the</strong> problem.<br />

Problem <strong>to</strong> be solved<br />

All animals move in a certain way at a specified speed and eat certain food. For<br />

instance fishes swim at 8 km/hour, dogs run at 10 km/hour, horses gallop at 20<br />

km/hour and birds fly at 10 km/hour. Dogs eat meat, horses eat grass and birds eat<br />

worms.<br />

4


Our client wants <strong>to</strong> write his application which <strong>will</strong> work on objects of classes such<br />

as Fish, Dog, Horse, Bird. His applications <strong>will</strong> create a fac<strong>to</strong>ry <strong>to</strong> generate <strong>the</strong><br />

animal s/he wants. Then his/her application <strong>will</strong> call methods with <strong>the</strong> following<br />

method headers:<br />

i) public String travel(String fromPlace, String <strong>to</strong>Place, double distance)<br />

ii) public String eats()<br />

Draw a class diagram showing all <strong>the</strong> abstract classes (including <strong>the</strong> methods in<br />

<strong>the</strong>se abstract classes) you need for a fac<strong>to</strong>ry <strong>to</strong> create horses and <strong>the</strong> relevant<br />

classes related <strong>to</strong> <strong>the</strong> product (an object of class Horse). Even though you don’t<br />

show it on <strong>the</strong> class diagram, it should be possible <strong>to</strong> create a fac<strong>to</strong>ry for dogs or for<br />

birds, using <strong>the</strong> same abstract classes.<br />

Give <strong>the</strong> Java class definitions of<br />

‣ all <strong>the</strong> abstract classes you need,<br />

‣ <strong>the</strong> fac<strong>to</strong>ry <strong>to</strong> create objects of <strong>the</strong> Horse class<br />

‣ <strong>the</strong> Horse class itself<br />

‣ The application that <strong>the</strong> client <strong>will</strong> use <strong>to</strong> handle <strong>the</strong> following requirements.<br />

Requirements for Application<br />

Consider <strong>the</strong> case of client wanting <strong>to</strong> ride a horse <strong>to</strong> travel from <strong>the</strong> University <strong>to</strong><br />

Devonshire Mall (a distance of 10 km). The client wishes <strong>to</strong> know how <strong>the</strong> horse<br />

<strong>will</strong> travel (“gallop”), <strong>the</strong> time needed (0.5 hours) and what <strong>the</strong> horse <strong>will</strong> eat<br />

(“grass”).<br />

(Full marks 30)<br />

Question 2 alternative II) Show how you can use JUnit <strong>test</strong> <strong>to</strong> ensure that <strong>the</strong><br />

following methods of class Course are working as expected:<br />

‣ <strong>the</strong> construc<strong>to</strong>r,<br />

‣ <strong>the</strong> insertStudent method<br />

‣ <strong>the</strong> getFeesFromCourse method<br />

Assume that all <strong>the</strong> o<strong>the</strong>r class definitions <strong>have</strong> no errors. When <strong>test</strong>ing <strong>the</strong> method<br />

insertStudent, you must also make sure that <strong>the</strong> method throws an exception of an<br />

ArrayIndexOutOfBoundsException class when you attempt <strong>to</strong> insert <strong>to</strong>o many<br />

objects of class Student in<strong>to</strong> <strong>the</strong> array studentsRegisteredInCourse.<br />

(Full marks 20)<br />

Hints: To help you, I am giving you some fragments of code I wrote <strong>to</strong> <strong>test</strong> some of<br />

<strong>the</strong> classes. All <strong>the</strong>se <strong>test</strong>s were successful.<br />

a) When <strong>test</strong>ing Professor, I used <strong>the</strong> following lines of code:<br />

Professor p = new FullTimeFaculty("Data Structures",<br />

"Dr Smith");<br />

assertEquals("Dr Smith; specializes in Data Structures",<br />

5


p.<strong>to</strong>String());<br />

b) When <strong>test</strong>ing Student, I used <strong>the</strong> following lines of code:<br />

Student s1, s2, s3;<br />

s1 = new Student("John", "123", 1);<br />

assertEquals("Name is John; Id is 123 - a Canadian",<br />

s1.<strong>to</strong>String());<br />

s2 = new Student("Sally", "456", 2);<br />

assertEquals("Name is Sally; Id is 456 - a Canadian Senior",<br />

s2.<strong>to</strong>String());<br />

s3 = new Student("Amar", "234", 3);<br />

assertEquals("Name is Amar; Id is 234 - a Foreign Student",<br />

s3.<strong>to</strong>String());<br />

6


1) You must sit at <strong>the</strong> desk allotted <strong>to</strong> you.<br />

Rules and regulations<br />

2) You must not remove <strong>the</strong> staple at <strong>the</strong> bot<strong>to</strong>m right corner before you are authorized <strong>to</strong><br />

remove it and start writing <strong>the</strong> <strong>test</strong>.<br />

3) Normally you <strong>will</strong> not be allowed <strong>to</strong> leave <strong>the</strong> exam hall temporarily. If you <strong>have</strong> <strong>to</strong> leave<br />

<strong>the</strong> exam hall temporarily, you must<br />

a) request permission from a proc<strong>to</strong>r<br />

b) <strong>have</strong> an escort with you while you are out<br />

c) not communicate with anyone, in any way, when you are outside <strong>the</strong> Hall.<br />

4) You must not attempt <strong>to</strong> communicate with any proc<strong>to</strong>r except Dr Subir, <strong>the</strong> instruc<strong>to</strong>r of<br />

<strong>the</strong> course.<br />

5) If you need <strong>to</strong> communicate with Dr Subir, please raise your hand and some proc<strong>to</strong>r <strong>will</strong><br />

request <strong>the</strong> person in charge <strong>to</strong> answer your question.<br />

6) You <strong>will</strong> not be permitted <strong>to</strong> leave before 4:30 PM.<br />

7) No student <strong>will</strong> be allowed in<strong>to</strong> <strong>the</strong> exam Hall after 4:30 PM.<br />

8) During <strong>the</strong> <strong>test</strong> <strong>the</strong> proc<strong>to</strong>rs <strong>will</strong> check your ID. Please <strong>have</strong> your Student card on <strong>the</strong> desk.<br />

9) If you are caught cheating during <strong>the</strong> <strong>test</strong> in any way (e.g., communicating with any one<br />

o<strong>the</strong>r than <strong>the</strong> person in charge of <strong>the</strong> exam, using a computer, using any material not<br />

specified in this question paper), we <strong>will</strong> refer you <strong>to</strong> <strong>the</strong> Dean of Student Affairs for<br />

disciplinary proceedings.<br />

10) The person in charge of <strong>the</strong> exam has <strong>the</strong> authority <strong>to</strong> ask you <strong>to</strong> move <strong>to</strong> ano<strong>the</strong>r seat<br />

without assigning any reason.<br />

11) You must sign <strong>the</strong> exam roster before handing in your <strong>test</strong>.<br />

12) If you finish before 6:45 PM, you <strong>will</strong> be allowed <strong>to</strong> leave <strong>the</strong> Hall after signing <strong>the</strong> exam<br />

roster.<br />

13) If you do not finish by 6:25 PM, you must not attempt <strong>to</strong> leave <strong>the</strong> Hall before <strong>the</strong> exam is<br />

finished at 6:50 PM.<br />

14) When <strong>the</strong> exam is over, please sit quietly at your desk until <strong>the</strong> person in charge of <strong>the</strong><br />

hall gives you permission <strong>to</strong> leave <strong>the</strong> Hall.<br />

7

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!