0% found this document useful (0 votes)
3 views

practice cs

The document contains a quiz for a CSCI-142 recitation focused on classes in Java. It includes questions about program output, UML class diagrams, Java terms definitions, and an explanation of a syntax error in an equals method. The quiz is ungraded and aims to reinforce material from the last lecture.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

practice cs

The document contains a quiz for a CSCI-142 recitation focused on classes in Java. It includes questions about program output, UML class diagrams, Java terms definitions, and an explanation of a syntax error in an equals method. The quiz is ungraded and aims to reinforce material from the last lecture.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

CSCI-142 Recitation Initial Quiz Week 02: Classes

You have five minutes to answer the following questions regarding material from the last lecture.
This quiz is not collected and is not graded.

1. What is the output of the following program?


1 public c l a s s S t a t i c {
2 private s t a t i c i n t f i r s t = 1 ;
3 private i n t s e c o n d ;
4
5 public S t a t i c ( ) {
6 this . second = 1 ;
7 }
8
9 public void change ( ) {
10 ++f i r s t ;
11 ++t h i s . s e c o n d ;
12 }
13
14 public i n t g e t S e c o n d ( ) {
15 return t h i s . s e c o n d ;
16 }
17
18 public s t a t i c void main ( S t r i n g [ ] a r g s ) {
19 S t a t i c a = new S t a t i c ( ) ;
20 S t a t i c b = new S t a t i c ( ) ;
21
22 a . change ( ) ;
23 b . change ( ) ;
24 a . change ( ) ;
25
26 System . out . p r i n t l n ( ” S t a t i c ’ s f i r s t : ” + S t a t i c . first );
27 System . out . p r i n t l n ( ” a ’ s s e c o n d : ” + a . g e t S e c o n d ());
28 System . out . p r i n t l n ( ” S t a t i c ’ s f i r s t : ” + S t a t i c . first );
29 System . out . p r i n t l n ( ”b ’ s s e c o n d : ” + b . g e t S e c o n d ());
30 }
31 }

1
2. Draw the UML class diagram for the following class, Mario.
1 public c l a s s Mario {
2 public f i n a l s t a t i c i n t MAX HEALTH = 8 ;
3 private i n t h e a l t h ;
4
5 public Mario ( ) {
6 t h i s . h e a l t h = MAX HEALTH;
7 }
8
9 public boolean isDead ( ) {
10 return t h i s . h e a l t h <= 0 ;
11 }
12
13 public void takeDamage ( i n t dmg) {
14 i f ( isDead ( ) ) {
15 this . health = 0 ;
16 } else {
17 t h i s . h e a l t h −= dmg ;
18 }
19 }
20
21 public void grabCoin ( ) {
22 i f ( h e a l t h >= MAX HEALTH) {
23 h e a l t h = MAX HEALTH;
24 } else {
25 ++h e a l t h ;
26 }
27 }
28 }

3. You have a class House whose sole constructor requires an integer containing the number of
square feet the house has, and a string containing the house’s address. Write a statement
that declares and initializes a House variable, castle, that is to refer to a 1,666 square-foot
house located at 1600 Transylvania Avenue.

2
4. Match each Java term:

• final

• null

• Object

• private

• public

• static

• this

with the best definition:

A) used in a class to refer to the instance variable of the current class.


B) a member of a class that is not associated with an instance of the class, i.e. class
level.
C) used to indicate a reference does not refer to an object, i.e. similar to Python’s None.
D) a member that is visible to all classes.
E) a member declared this way is only visible within the class, not directly from any other
class.
F) if a primitive variable is declared this way, it cannot be re-assigned to after and is
essentially a constant.
G) the class from which all other classes inherit some functionality.

3
5. This equals method for comparing two Marker’s has a syntax error. The compiler is com-
plaining that it cannot find these symbols for other: type, color and inkLevel.
1 @Override
2 public boolean e q u a l s ( Obj ect o t h e r ) {
3 boolean r e s u l t = f a l s e ;
4 i f ( o t h e r instanceof Marker ) {
5 r e s u l t = t h i s . type == o t h e r . type &&
6 t h i s . c o l o r . e q u a l s ( o t h e r . c o l o r ) &&
7 t h i s . i n k L e v e l == o t h e r . i n k L e v e l ;
8 }
9 return r e s u l t ;
10 }
11
12 // . . .
13 Marker m1 = new Marker ( Marker . Type .PERMANENT, ”RED” ) ;
14 Marker m2 = new Marker ( Marker . Type . DRY ERASE, ”RED” ) ;
15 m1 . e q u a l s (m2 ) ; // e x p e c t : f a l s e ;

Explain what is wrong and how to fix it.

Since an object of one type can be compared to an object of any other type, other comes
in to the equals method as an Object. The code first checks that other is an instance of
Marker, but it does not cast it into a variable reference of type Marker. Therefore, when
using other it is only aware of the methods that the Object class provides.

The fix is the new line 5 using typecasting. And then the new reference, m, is used on
line 6,7 and 8 in the place of other:
1 @Override
2 public boolean e q u a l s ( Obj ect o t h e r ) {
3 boolean r e s u l t = f a l s e ;
4 i f ( o t h e r instanceof Marker ) {
5 Marker m = ( Marker ) o t h e r ;
6 r e s u l t = t h i s . type == m. type &&
7 t h i s . c o l o r . e q u a l s (m. c o l o r ) &&
8 t h i s . i n k L e v e l == m. i n k L e v e l ;
9 }
10 return r e s u l t ;
11 }

You might also like