2004w08c.mt1.sampsoln

2004w08c.mt1.sampsoln - Carleton University Department of...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Page 1 of 8 Carleton University Department of Systems and Computer Engineering SYSC 2004 C - Object-Oriented Software Development – Winter 2008 Midterm Exam 1 – February 7 th , 2008 Sample Solutions Please read these instructions before you answer any of the exam questions: 1. You have 75min (1hr15min) for three questions worth 30 marks on 9 pages. 2. The midterm exam is closed book. Calculators are not permitted. 3. Do not talk to any other students from the time the exam begins until your completed exam has been submitted and you have left the exam room. 4. Ask a question only if you believe there is a mistake on the exam . Otherwise, make a reasonable assumption and proceed. 5. The questions on the midterm are based on a Pick-A-Number game. In each round of this game, first the game picks a number between 1 and “max”, and then each player tries to guess the number. The winner of the round is the player that is closest to the original number chosen for that round by the game (don’t worry about how we calculate the round winner if two or more players are equally close). The player that wins the most rounds wins the game (there may be a tie if several players win the same number of rounds). 6. API documentation for ArrayList and Random are provided at the end of this question paper, along with the BlueJ class diagram for Pick-A-Number . You may carefully remove the reference page from the exam (page 9). Question 1 (7 marks) In this question you are to develop a class called “ Pick ” that models a random number generator used to generate integers between 1 and “max”. a) What class from the Java libraries will be helpful in writing class Pick ? ( 1 mark ) Random b) How do we include this Java library class in our code? ( 1 mark ) import java.util.Random; or import java.util.*; c) Given the information below, complete the fields, constructor and method guess :
Image of page 1

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

View Full Document Right Arrow Icon
Page 2 of 8 public class Pick { /** our random number generator */ // 0.5 marks private Random r; // insert your answer to a) here /** we will generate random numbers between 1 and max */ private int max; /** * Constructor for objects of class Pick. The argument * specified must be at least 1, otherwise an * IllegalArgumentException is thrown. * * @param int The largest number we will generate * @throws IllegalArgumentException if argument < 1 */ public Pick(int max) // 2.5 marks total as follows: { if (max<1) throw new IllegalArgumentException ( Pick::Pick – max must be at least 1 ); // 1 mark r = new Random(); // 1 mark this.max = max; // 0.5 marks
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern