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

Info iconThis 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 :
Background image of page 1

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

View Full DocumentRight 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 } /** * Returns a random number between 1 and max. * * @return int A number between 1 and max
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/24/2010 for the course SYSC 2004 taught by Professor L.marshall during the Fall '10 term at Carleton.

Page1 / 8

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

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

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