Académique Documents
Professionnel Documents
Culture Documents
Programme
Programme
A class structure
Class definition specifies data members each instance (object) of the class will have
methods, including instance methods that are called on
instances/ objects and class methods or static methods that
are called on the class directly without invoking objects.
1
2
S t r i n g g r e e t i n g = H e l l o , World ! ;
System . o u t . p r i n t l n ( g r e e t i n g . l e n g t h ( ) ) ;
1
2
S t r i n g l e c t u r e r 1 = Gaurav ;
S t r i n g l e c t u r e r 2 = new S t r i n g ( S c o t t ) ;
The two variables are different instances of the String class.
First is known as lazy instantiation, just for Strings.
Second (using new) is generally how we create instances.
More information about this will come a bit later in this
lecture ...
Defining classes
Defining a class
Each class is defined in a separate file with the same name
and ending with . java
All Java class definitions are separate files in the same folder
(for now).
Adding instance variables
Instance variables can be declared as in the following two
examples. Note the public modifier (for now):
1
2
public int
public String
instanceVar1 ;
instanceVar2 ;
Defining methods
p u b l i c double a r e a ( ) {
return width height ;
}
5
6
7
8
p u b l i c double p e r i m e t e r ( ) {
return 2( width + h e i g h t ) ;
}
9
10
11
12
v EXERCISE TIME! w
Define a class for a Circle that is represented by its radius.
ClassName c l a s s V a r ; // d e c l a r a t i o n
R e c t a n g l e r e c t ; // e x a m p l e
Declaration
c l a s s V a r = new ClassName ( ) ; // i n s t a n t i a t i o n
r e c t = new R e c t a n g l e ( ) ; // e x a m p l e
3
4
R e c t a n g l e r e c t = new R e c t a n g l e ( ) ; // e x a m p l e
v EXERCISE TIME! w
Declare and instantiate an object myCircle of class Circle
that has a radius of 2.4
R e c t a n g l e m y R e c t a n g l e = new R e c t a n g l e ( ) ;
// e x a m p l e
myRectangle . width = 5 ;
1
2
3
4
R e c t a n g l e m y R e c t a n g l e = new R e c t a n g l e ( ) ;
myRectangle . width = 5 ;
myRectangle . h e i g h t = 8 ;
System . o u t . p r i n t l n ( m y R e c t a n g l e . a r e a ( ) ) ;
v EXERCISE TIME! w
Write a piece of code that sits outside the class definition and
displays the radius of the object myCircle and also its area.
p u b l i c double a r e a ( ) {
return width height ;
}
5
6
7
8
p u b l i c boolean i s S q u a r e ( ) {
r e t u r n ( w i d t h == h e i g h t ) ;
}
9
10
11
12
Now, the instance variables width and height are visible only
within the class definition.
1
2
3
4
5
6
7
8
9
You can see that we validated the passed values before assigning
to the instance variable as width = Math.abs(w). This is a
typical case and setters are in charge of validating data before
assigning it to the instance variables.
v EXERCISE TIME! w
Add getters and setters to class Circle. The setter should
result in radius becoming zero if the parameter passed is not
positive.
v EXERCISE TIME! w
Write a client (code sitting outside Circle.java, for
example, in the main method of another class) that performs
the following operations,
Declare and instantiate object myCircle of class Circle
that has a radius of 1.8
Display radius of myCircle.
Increase radius of myCircle by 1.4
R e c t a n g l e r e c t = new R e c t a n g l e ( ) ;
r e c t . width = 5;
rect . height = 8;
However, it would be really nice if one could pass the values for the
data members in the instantiation statement itself, as,
1
R e c t a n g l e r e c t = new R e c t a n g l e ( 5 , 8 ) ;
Constructors
Constructor - example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Constructor - example
Default constructor
R e c t a n g e r = new R e c t a n g l e ( ) ;
public Rectangle () {
setLength (1) ;
setBreadth (1) ;
}
v EXERCISE TIME! w
Add two constructors to the class Circle.
1
No parameters passed (default constructor): Assigns the
value 0.3183099 to radius ... through the setter (who can
tell me why this value?)
2
Parameter passed for radius (parameterized constructor):
Assigns the passed value to radius through the setter.
Displaying objects
R e c t a g l e r e c t = new R e c t a n g l e ( 5 , 8 ) ;
System . o u t . p r i n t l n ( r e c t . w i d t h + by +r e c t
. height ) ;
Displaying objects
public void d i s p l a y ( ) {
System . o u t . p r i n t l n ( w i d t h + by +
height ) ;
}
R e c t a g l e r e c t = new R e c t a n g l e ( 5 , 8 ) ;
rect . display () ;
But this would only let us display the object details, and not send
to a file, or concatenate with any other output.
Java provides a standard way to return the String description of an
object using the toString() method (with return type String).
R e c t a n g l e m y R e c t a n g l e = new R e c t a n g l e ( 1 , 3 )
;
System . o u t . p r i n t l n ( m y R e c t a n g l e ) ; //
something l i k e [ I@70dea4e
Java saw that you want to display a rectangle object and replaced
it by the toString() method operating on that object as,
1
2
3
System . o u t . p r i n t l n ( m y R e c t a n g l e ) ;
// became
System . o u t . p r i n t l n ( m y R e c t a n g l e . t o S t r i n g ( ) ) ;
R e c t a g l e m y R e c t a n g l e = new R e c t a n g l e ( 5 , 8 ) ;
System . o u t . p r i n t l n ( m y R e c t a n g l e ) ;
// a u t o m a t i c a l l y i n v o k e s m y R e c t a n g l e .
t o S t r i n g ( ) and d i s p l a y s t h e v a l u e
returned
v EXERCISE TIME! w
Define the toString method in the Circle class such that it
displays the object details in the following format Circle radius: <radius>, area: <area>
In a separate client, create a Circle object with radius 1.6 and
display it on the console.