Escolar Documentos
Profissional Documentos
Cultura Documentos
Faculte de genie
Faculty of engineering
Identification
Last name: First name:
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
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 ;
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
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 ;
}
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 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
p u b l i c c l a s s ScoredGame e x t e n d s Game {
}
February 2016 ITI 1121 Page 5 of 13
(a) Line 3, type of the returned value: public static int factorial
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 ;
}
private E 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 ;
}
(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
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 {
// ...
/ / 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
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.
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);
import j a v a . u t i l . Random ;
/ / MODEL
p r i v a t e i n t [ ] numbers ;
publ ic i n t g e t ( i n t pos ) {
r e t u r n numbers [ p o s ] ;
}
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 ] ;
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 ( ) {
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 ;