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

Tut 7 Sol Objects and Classes

This document contains a tutorial on objects and classes in Java. It includes examples of: 1. Identifying syntax errors in variable declarations and method headers. 2. Writing declarations for instance variables with different access modifiers and initial values. 3. Identifying an error in a class definition. 4. Explaining the output of code that creates objects and calls methods to increment, decrement, and get/display counter values. 5. Analyzing whether certain statements using created objects are legal or illegal. 6. Showing the output of a program that demonstrates passing objects by value vs reference.

Uploaded by

David Koh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Tut 7 Sol Objects and Classes

This document contains a tutorial on objects and classes in Java. It includes examples of: 1. Identifying syntax errors in variable declarations and method headers. 2. Writing declarations for instance variables with different access modifiers and initial values. 3. Identifying an error in a class definition. 4. Explaining the output of code that creates objects and calls methods to increment, decrement, and get/display counter values. 5. Analyzing whether certain statements using created objects are legal or illegal. 6. Showing the output of a program that demonstrates passing objects by value vs reference.

Uploaded by

David Koh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

SCE/CE9001/CM101/Tutorial7

Tutorial 7
Chapter 8: Objects and Classes
1. Identify the syntax error (if any) in each aria!le declaration or "ethod header.
a. public Boolean isEven;
b. Private boolean isEven;
c. string S;
d. private String s = helloWorld;
e. public String boolean( )
f. private myethod()
#ns$
a. %u!lic &oolean isEen'
(o error) &oolean here is a class) si"ilar to Inte*er class.
!. +riate !oolean isEen'
+riate ,- %riate
c. strin* S'
strin* ,- Strin*) and aria!le should starts .ith a lo.ercase character
d. %riate Strin* s / hello0orld'
hello.0orld ,- 1hello0orld2
3r hello0orld is another %redefined Strin* aria!le
e. &oolean is a 4ey.ord) cannot !e used as an identifier
f. %riate "yMethod()
(eed %roide return ty%e or use oid if nothin* needs to !e returned.
5. 0rite declaritions for each of the follo.in* instance aria!les.
a. # %riate !oolean aria!le na"ed !ool .ith an initial alue of true.
!. # %u!lic Strin* aria!le na"ed str .ith an initial alue of 1hello2.
c. # %riate int aria!le na"ed nE"%loyees .ithout initial alue.
#ns$
a. %riate !oolean !ool / true'
!. %u!lic Strin* str/1hello2'
c. %riate int nE"%loyees'
6. 0hat is .ron* .ith the follo.in* code7
class !est "
public static void main(String#$ args)"
% a = ne& %('() *();
a.print+();
,
,

-lass %"
int .;

%(int ne&+)"
1
SCE/CE9001/CM101/Tutorial7
. = ne&+;
,

public void print() "
System.out.print(.);
,
,
Ans:
class !/0 "
public static void main(String#$ args)"
% a = ne& %('() *(); 11
a. print+();11
,
,

-lass %" //Class should be class
int .;

%(int ne&+)"
. = ne&+;
,

public void print() "
System.out.print(.);
,
,
8. # class definition is *ien !elo.$
public class - "
private int counter;
public -() "
counter = (;
,
public -(int n) "
counter = n;
,
public void increment() "
counter = counter2';
,
public void decrement() "
5
SCE/CE9001/CM101/Tutorial7
counter = counter 3';
,
public int get-ount() "
return counter;
,
public void display() "
System.out.println(get-ount());
,
,
(a) 0hat9s the out%ut of the follo.in* code se*"ent7
- c' = ne& -();
c'.increment();
- c* = ne& -();
c*.increment();
c'.increment();
c*.decrement();
c*.increment();
int counter = (;
c'.display();
c*.display();
(a) :un %ro*ra"$
5
1
(!) #ssu"e the follo.in* code is defined in a class other than C. #re the
state"ents le*al or ille*al7 Ex%lain.
- c' = ne& -();
- c* = ne& -(c'.get-ount());
- c0 = ne& -(4);
c'.increment(4);
c*.decrement() = 4;
c'.counter = 4;
- c5 = ne& -(c*.counter);
(!)
The follo.in* state"ents are le*al$
C c1 / ne. C()'
;ses the default constructor to create a C o!<ect) .ith counter initiali=ed to 0.
C c5 / ne. C(c1.*etCount())'
;ses the second constructor to create a C o!<ect) .ith counter initiali=ed to
the counter of the o!<ect referenced !y c1.
C c6 / ne. C(>)'
;ses the second constructor to create a C o!<ect) .ith counter initiali=ed to >.
6
SCE/CE9001/CM101/Tutorial7
The follo.in* state"ents are ille*al$
c1.incre"ent(>)'
#tte"%ts to %ass an actual %ara"eter to incre"ent()) .hich has no for"al
%ara"eters.
c5.decre"ent() / >'
#tte"%ts to assi*n a alue > to the "ethod inocation c5.decre"ent()) .hich
is not a aria!le.
c1.counter / >'
#tte"%ts to assi*n a alue to the %riate instance aria!le counter.
C c8 / ne. C(c5.counter)'
#tte"%ts to access the %riate instance aria!le counter.
>. Sho. the out%ut of the follo.in* %ro*ra"$
public class -hec6Point "
public static void main (String#$ args)"
7emo d' = ne& 7emo('();
7emo d* = ne& 7emo();
copy'(d') d*);
System.out.println(8%fter copy'9 d'= 82 d'.d 2 8
d*= 82 d*.d);
copy*(d') d*);
System.out.println(8%fter copy*9 d'= 82 d'.d 2 8
d*= 82 d*.d);
,
public static void copy' (7emo .) 7emo y)"
7emo temp = .;
. = y;
y = temp;
,
public static void copy* (7emo .) 7emo y)"
int temp = ..d;
..d = y.d;
y.d = temp;
,
,
class 7emo "
int d;
7emo(int ne&7)"
d = ne&7;
,
7emo()"
,
8
SCE/CE9001/CM101/Tutorial7
,
>

You might also like