Você está na página 1de 13

Universite dOttawa University of Ottawa

Faculte de genie
Faculty of engineering

Ecole de science informatique School of Electrical Engineering



et de genie
electrique and Computer Science

Introduction to Computing II (ITI 1121)


M IDTERM E XAMINATION : S OLUTIONS
Instructors: Nour El-Kadri, Guy-Vincent Jourdan, and Marcel Turcotte
February 2016, duration: 2 hours

Identification
Last name: First name:

Student #: Seat #: Signature: Section: A or B or C

Instructions Marking scheme


1. This is a closed book examination. Question Maximum Result
2. No calculators, electronic devices or other aids are permit-
ted. 1 10
(a) Any electronic device or tool must be shut off, stored
2 15
and out of reach. 3 20
(b) Anyone who fails to comply with these regulations 4 20
may be charged with academic fraud. Total 65
3. Write your answers in the space provided.
(a) Use the back of pages if necessary.
(b) You may not hand in additional pages.
4. Do not remove pages or the staple holding the examination
pages together.
5. Write comments and assumptions to get partial marks.
6. Beware, poor hand writing can affect grades.
7. Wait for the start of the examination.

All rights reserved. No part of this document may be reproduced, stored in a retrieval system or transmitted in any form or by any
means, electronic, mechanical, photocopying, recording or otherwise without prior written permission from the instructors.
February 2016 ITI 1121 Page 2 of 13

Question 1 (10 marks)


Run Game Team

+ main(args : String[]) : void + getTeam1() : Team + getName() : String
+ getTeam2() : Team

ScoredGame

+ setScore(score1: int, score2 : int) : void
+ getScore1() : int
+ getScore2() : int

For this question, you must implement the classes Game, ScoredGame, and Team, according to the UML
diagram above, and the instructions below. You must also make sure that your implementation is consistant
with the provided test class Run.

A. An object of the class Team stores the name of a sports team. Make sure to include a getter for this
attribute.

B. The class Game describes the characteristics that are common to all games. Namely, a game has two
teams (objects of the class Team). Make sure to include getters for these two attributes.

C. Finally, the class ScoredGame is a specialization of the class Game. An object of the class ScoredGame
stores the score for each of the two teams. Make sure to include the necessary access methods. Consult
the UML diagram above and the class Run below for the signatures of the methods.

p u b l i c c l a s s Run {

p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {

Team s e n a t o r s , c a n a d i e n s ;

s e n a t o r s = new Team ( "Ottawa Senators" ) ;


c a n a d i e n s = new Team ( "Montreal Canadiens" ) ;

ScoredGame s ;

s = new ScoredGame ( s e n a t o r s , c a n a d i e n s ) ;
s . setScore (5 , 0);

}
February 2016 ITI 1121 Page 3 of 13

A. Implement the class Team in the box below.


/ / MODEL:

p u b l i c c l a s s Team {

p r i v a t e S t r i n g name ;

p u b l i c Team ( S t r i n g name ) {
t h i s . name = name ;
}

p u b l i c S t r i n g getName ( ) {
r e t u r n name ;
}

B. Implement the class Game in the box below.


/ / MODEL:

p u b l i c c l a s s Game {

p r i v a t e Team team1 ;
p r i v a t e Team team2 ;

p u b l i c Game ( Team team1 , Team team2 ) {


t h i s . team1 = team1 ;
t h i s . team2 = team2 ;
}

p u b l i c Team getTeam1 ( ) {
r e t u r n team1 ;
}

p u b l i c Team getTeam2 ( ) {
r e t u r n team2 ;
}

}
February 2016 ITI 1121 Page 4 of 13

C. Implement the class ScoredGame in the box below.


/ / MODEL:

p u b l i c c l a s s ScoredGame e x t e n d s Game {

private int score1 ;


private int score2 ;

p u b l i c ScoredGame ( Team team1 , Team team2 ) {


s u p e r ( team1 , team2 ) ;
score1 = score2 = 0;
}

public void s e t S c o r e ( i n t score1 , i n t score2 ) {


this . score1 = score1 ;
this . score2 = score2 ;
}

public int getScore1 ( ) {


return score1 ;
}

public int getScore2 ( ) {


return score2 ;
}

}
February 2016 ITI 1121 Page 5 of 13

Question 2 (15 marks)


A. Fix 5 errors in the following Java program so that it compiles and runs without error. This method
computes the factorial of its argument. You can assume that the value of the parameter will always be
greater than or equals to 0. Add the label of each of the five answers below (a, b, c, d, or e) onto the
code, where the error is found.
1 public class U t i l s {
2
3 public s t a t i c ( a ) f a c t o r i a l ( ( b ) n ) {
4
5 ( c ) r e s u l t = 1;
6
7 f o r ( ( d ) i = 1 ; i <=n ; i ++) {
8 result = i result ;
9 }
10
11 (e) result ;
12 }
13
14 }

(a) Line 3, type of the returned value: public static int factorial

(b) Line 3, type of the parameter: int n

(c) Line 5, type of a local variable: int result

(d) Line 7, type of a local variable: int i=1

(e) Line 11, keyword return: return result;

B. In one or two sentences, explain why the following Java code does not compile. Circle the statement
or statements that are the source of the problem.
Answer ( Model ) :
L i n e s 4 and 5 , t h e c l a s s C h i l d u s e s t h e s y m b o l s name and age ( where ) ,
which h a v e p r i v a t e a c c e s s ( why ) i n t h e c l a s s P e r s o n .

public c l a s s Person {
p r i v a t e S t r i n g name ;
p r i v a t e i n t age ;
}

1 public c l a s s Child extends Person {


2 private int grade ;
3 p u b l i c C h i l d ( S t r i n g name , i n t age , i n t g r a d e ) {
4 t h i s . name = name ;
5 t h i s . age = age ;
6 t h i s . grade = grade ;
7 }
8 }
February 2016 ITI 1121 Page 6 of 13

C. Given the declaration of the class Cell below.


p u b l i c c l a s s C e l l <E> {

private E value ;

public Cell (E value ) {


this . value = value ;
}

p u b l i c b o o l e a n i s E q u a l ( C e l l <E> o t h e r ) {
i f ( o t h e r == n u l l ) {
return f a l s e ;
}

i f ( t h i s . v a l u e == n u l l | | o t h e r . v a l u e == n u l l ) {
r e t u r n t h i s . v a l u e == n u l l && o t h e r . v a l u e == n u l l ;
}

return t h i s . value . equals ( o t h e r . value ) ;


}
}

(a) Declare two reference variables, respectively named s and t, that can be used to designate objects
of the class Cell to store String values.
C e l l <S t r i n g > s ;
C e l l <S t r i n g > t ;

(b) Give the Java statements to create two Cell objects to store String values, save the references
these objects in the reference variables s and t. The first Cell object must be initialized with the
value "Suki", whereas the other must be initialized with "Juliette".
s = new C e l l <S t r i n g >("Suki" ) ;
t = new C e l l <S t r i n g >("Juliette" ) ;

(c) Give the Java expression for calling the method isEqual of the object designated by s passing the
reference of the object designated by t as actual parameter.
s . isEqual ( t )
February 2016 ITI 1121 Page 7 of 13

(d) Declare an interface named Taxable. A Taxable object has a method getTaxes that returns a
value of type double.
/ / MODEL:

public i n t e r f a c e Taxable {
double g e t T a x e s ( ) ;
}
February 2016 ITI 1121 Page 8 of 13

Question 3 (20 marks)


The interface ExtendedStack provides the usual stack methods, as well as the methods count, which returns
the number of elements currently in the stack, and the method reverse, which reverses the elements in the
stack.
The interface definition is provided below.
public interface ExtendedStack {
boolean isEmpty ( ) ;
void push ( S t r i n g element ) ;
S t r i n g pop ( ) ;
S t r i n g peek ( ) ;
int count ( ) ;
void r e v e r s e ( ) ;
}

Someone has partially implemented this interface in the class ExtendedStackImplementation. Specif-
ically, methods isEmtpy, push and pop are already (correctly) implemented. ExtendedStackImplemen-
tation has only one constructor, with no parameter. This implementation can accommodate any number of
elements.
Without making any assumptions on how ExtendedStackImplementation stores the stack (in particular,
you cannot assume that the implementation uses an array) and using only the methods of ExtendedStack that
have already been implemented (isEmtpy, push and pop), provide an implementation of the method peek.
You dont need to handle the error situations, in particular, you can assume that peek will not be called if
the stack is empty.
p u b l i c c l a s s E x t e n d e d S t a c k I m p l e m e n t a t i o n implements E x t e n d e d S t a c k {

// ...

p u b l i c S t r i n g peek ( ) {

/ / MODEL:

String s ;
s = t h i s . pop ( ) ;

push ( s ) ;

return s ;

}
February 2016 ITI 1121 Page 9 of 13

Still using only the methods already implemented, provide an implementation of the method count.
p u b l i c c l a s s E x t e n d e d S t a c k I m p l e m e n t a t i o n implements E x t e n d e d S t a c k {

// ...

public int count (){

/ / MODEL:

int size = 0;

E x t e n d e d S t a c k temp ;
temp = new E x t e n d e d S t a c k I m p l e m e n t a t i o n ( ) ;

while ( ! isEmpty ( ) ) {
temp . p u s h ( pop ( ) ) ;
s i z e ++;
}

w h i l e ( ! temp . i s E m p t y ( ) ) {
p u s h ( temp . pop ( ) ) ;
}

return s i z e ;

}
}
February 2016 ITI 1121 Page 10 of 13

Finally, provide the implementation of the method reverse which reverses the elements in the current
stack (that is, it keeps the same elements, but stacked up in the reverse order).
p u b l i c c l a s s E x t e n d e d S t a c k I m p l e m e n t a t i o n implements E x t e n d e d S t a c k {
...
public void r e v e r s e ( ) {

/ / MODEL:

E x t e n d e d S t a c k temp1 ;
temp1 = new E x t e n d e d S t a c k I m p l e m e n t a t i o n ( ) ;

E x t e n d e d S t a c k temp2 ;
temp2 = new E x t e n d e d S t a c k I m p l e m e n t a t i o n ( ) ;

while ( ! isEmpty ( ) ) {
temp1 . p u s h ( pop ( ) ) ;
}

w h i l e ( ! temp1 . i s E m p t y ( ) ) {
temp2 . p u s h ( temp1 . pop ( ) ) ;
}

w h i l e ( ! temp2 . i s E m p t y ( ) ) {
p u s h ( temp2 . pop ( ) ) ;
}

}
}
February 2016 ITI 1121 Page 11 of 13

Question 4 (20 marks)


Write a class to represent a permutation. Herein, a permutation is a rearrangement of the integers 0 to n 1,
where n is the size of the permutation. The permutation that consists of the integers 0 to n 1 in-order is
called the identity-permutation. For instance, 0, 1, 2, 3, 4 is the identity-permutation for size 5.
Write a class called Permutation. Add all the necessary instance variable(s).

A. Implement the constructor Permutation(int size), which initializes this permutation to the identity-
permutation. The parameter size is the size of the permutation.

B. Implement the method int getSize(), which returns the size of this permutation.

C. Implement the method int get(int pos), which returns the element at position pos of this permutation.
You can assume that pos is not larger than the size of the permutation.

D. Write the method rotate(int n). The method rotates the elements of this permutation by n positions,
shifting the elements to the right (see exemple of a call below).

E. Implement the method shuffle that randomly rearranges the elements of this permutation. Use your
time wisely. You might want to come back to this question once you have completed the rest of the
examination.

Here is a sample use of the class Permutation:


Permutation p ;
p = new P e r m u t a t i o n ( 5 ) ;

f o r ( i n t i = 0 ; i < p . g e t S i z e ( ) ; i ++)
System . o u t . p r i n t ( " p[" + i + "] = "+ p . g e t ( i ) ) ;

p. rotate (2);

System . o u t . p r i n t l n ( "\n after rotation of 2:" ) ;


f o r ( i n t i = 0 ; i < p . g e t S i z e ( ) ; i ++)
System . o u t . p r i n t ( " p[" + i + "] = "+ p . g e t ( i ) ) ;

The code above produces the following output:

p[0] = 0 p[1] = 1 p[2] = 2 p[3] = 3 p[4] = 4


after rotation of 2:
p[0] = 3 p[1] = 4 p[2] = 0 p[3] = 1 p[4] = 2
February 2016 ITI 1121 Page 12 of 13

import j a v a . u t i l . Random ;

/ / MODEL

public class Permutation {

p r i v a t e s t a t i c f i n a l Random g e n e r a t o r = new Random ( ) ;

p r i v a t e i n t [ ] numbers ;

public Permutation ( int size ) {


numbers = new i n t [ s i z e ] ;
f o r ( i n t i = 0 ; i <s i z e ; i ++) {
numbers [ i ] = i ;
}
}

publ ic i n t g e t ( i n t pos ) {
r e t u r n numbers [ p o s ] ;
}

public int getSize (){ (1)


r e t u r n numbers . l e n g t h ;
}

public void r o t a t e ( i n t n ) {
int [] array ;
a r r a y = new i n t [ numbers . l e n g t h ] ;

f o r ( i n t i = 0 ; i <numbers . l e n g t h ; i ++) {
a r r a y [ ( i +n ) % numbers . l e n g t h ] = numbers [ i ] ;
}

numbers = a r r a y ;
}
February 2016 ITI 1121 Page 13 of 13

public void s h u f f l e ( ) {

boolean [ ] s e l e c t e d ;
s e l e c t e d = new b o o l e a n [ numbers . l e n g t h ] ;

int [] array ;
a r r a y = new i n t [ numbers . l e n g t h ] ;

f o r ( i n t p o s = 0 ; p o s < numbers . l e n g t h ; p o s ++) {


b o o l e a n done ;

done = f a l s e ;
w h i l e ( ! done ) {
int index ;
i n d e x = g e n e r a t o r . n e x t I n t ( numbers . l e n g t h ) ;
i f ( ! s e l e c t e d [ index ] ) {
a r r a y [ p o s ] = numbers [ i n d e x ] ;
s e l e c t e d [ index ] = true ;
done = t r u e ;
}
}

numbers = a r r a y ;
}

public void s h u f f l e A l t e r n a t i v e ( ) {

f o r ( i n t i t e r = 0 ; i t e r < 10 numbers . l e n g t h ; i t e r ++) {

i n t pos1 , pos2 , tmp ;

p o s 1 = g e n e r a t o r . n e x t I n t ( numbers . l e n g t h ) ;
p o s 2 = g e n e r a t o r . n e x t I n t ( numbers . l e n g t h ) ;

tmp = numbers [ p o s 1 ] ;
numbers [ p o s 1 ] = numbers [ p o s 2 ] ;
numbers [ p o s 2 ] = tmp ;