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

Lab 3 Manual

Uploaded by

bajajram2005
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Lab 3 Manual

Uploaded by

bajajram2005
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Programming with Python and Java

(CS 29008)

School of Electronics Engineering, KIIT DU


Chapter 3

Lab 3: Class, Objects and


Methods in Java

The class is at the core of Java. It forms the basis for object-oriented pro-
gramming in Java. Any concept we wish to implement in a Java program
must be encapsulated within a class.
A class is declared by use of the class keyword. A simplified general form
of a class definition is shown below:
Listing 3.1: class syntax
class classname {
dtype v a r i a b l e 1 ;
dtype v a r i a b l e 2 ;
// . . .
dtype v a r i a b l e N ;

dtype methodname1 ( parameter− l i s t ) {


// body o f method1
}
dtype methodname2 ( parameter− l i s t ) {
// body o f method2
}
// . . .
dtype methodnameN ( parameter− l i s t ) {
// body o f methodN
}
}
The data or variables defined within a class are called instance-variables.
The code is contained within methods. Collectively, the methods and vari-

17
ables defined within a class are called members of the class. As a general
rule, it is the methods that determine how a class data can be used.
Note that the general form of a class does not specify a main() method.
Java classes do not need to have a main() method. We only specify one if
that class is the starting point for the program. Further, some kinds of Java
applications, such as applets, do not require a main() method at all.

3.1 A Simple Class


Let us illustrate the class with a simple example. Here, a class called Box
defines three instance-variables: width, height, and depth. Currently, Box
does not contain any methods (but will be added later).
Listing 3.2: An example class Box
c l a s s Box {
double width ;
double h e i g h t ;
double depth ;
}
A class defines a new type of data. In this case, the new data type is
called Box. We will use this name to declare objects of type Box. It is
important to remember that a class declaration only creates a template; it
does not create an actual object.
To actually create a Box object, we use a statement like the following:
Box mybox = new Box ( ) ; // c r e a t e a Box o b j e c t c a l l e d mybox
mybox is now an instance of class Box. Thus, it will have “physical”
reality. Every Box object will contain its own copies of the instance-variables
width, height, and depth. To access these variables, we use the dot (.)
operator. For example, to assign the width variable of mybox the value
100, we use the following statement:
mybox . width = 1 0 0 ;
Here is a Java program that uses the Box class.
c l a s s Box {
double width ;
double h e i g h t ;
double depth ;
}
c l a s s BoxDemo {

18
public s t a t i c void main ( S t r i n g a r g s [ ] ) {
Box mybox = new Box ( ) ;
double v o l ;

// a s s i g n v a l u e s t o mybox ’ s i n s t a n c e −v a r i a b l e s
mybox . width = 1 0 ;
mybox . h e i g h t = 2 0 ;
mybox . depth = 1 5 ;

// compute volume o f box


v o l = mybox . width ∗ mybox . h e i g h t ∗ mybox . depth ;
System . out . p r i n t l n ( ”Volume : ” + v o l ) ;
}
}
We should compile the file BoxDemo.java because the main() method
is in the BoxDemo class. After compiling BoxDemo.java, Java compiler
will automatically create two .class files- Box.class and BoxDemo.class.
To run the program, we must execute BoxDemo.class which displays out-
put Volume: 3000.0.

3.2 Declaring Objects


When we create a class, we are creating a new data type. We can use this
type to declare objects of that type. To do this, we use new operator. The
new operator dynamically allocates (that is, allocates at run-time) memory
for an object and returns a reference to it. This reference is the address in
memory of the object allocated by new. This reference is then stored in the
variable. Thus, in Java, all class objects must be dynamically allocated.
In the above code, we declare an object of type Box in the following
manner:
Box mybox = new Box ( ) ;
Alternatively, we can write the above statement in the following manner:
Box mybox ; // d e c l a r e r e f e r e n c e t o o b j e c t
mybox = new Box ( ) ; // a l l o c a t e a Box o b j e c t

19
3.3 Methods
Typically, a class consists of two things: instance-variables and methods.
The general form of a method is:
dtype methodname ( parameter− l i s t ) {
// body o f t h e method
}
Here, dtype refers the type of data returned by the method. The return
type can be any valid built-in data type or a user-defined class type.
Listing 3.3: Adding methods to Box class
c l a s s Box {
double width ;
double h e i g h t ;
double depth ;

// compute and r e t u r n volume


double volume ( ) {
return width ∗ h e i g h t ∗ depth ;
}
}
c l a s s BoxDemo {
public s t a t i c void main ( S t r i n g a r g s [ ] ) {
Box mybox = new Box ( ) ;
double v o l ;

// a s s i g n v a l u e s t o mybox ’ s i n s t a n c e −v a r i a b l e s
mybox . width = 1 0 ;
mybox . h e i g h t = 2 0 ;
mybox . depth = 1 5 ;

// g e t volume o f box
v o l = mybox . volume ( ) ;
System . out . p r i n t l n ( ”Volume : ” + v o l ) ;
}
}
Several methods need parameters. The example code shown below illus-
trates the use of parameterized methods.
Listing 3.4: Parameterized methods in class Box
c l a s s Box {

20
double width ;
double h e i g h t ;
double depth ;

// compute and r e t u r n volume


double volume ( ) {
return width ∗ h e i g h t ∗ depth ;
}

// s e t s d i m e n s i o n s o f Box
void setDim ( double w, double h , double d ) {
width = w;
height = h ;
depth = d ;
}
}
c l a s s BoxDemo {
public s t a t i c void main ( S t r i n g a r g s [ ] ) {
Box mybox = new Box ( ) ;
double v o l ;

// i n i t i a l i z e mybox
mybox . setDim ( 1 0 , 2 0 , 1 5 ) ;

// g e t volume o f box
v o l = mybox . volume ( ) ;
System . out . p r i n t l n ( ”Volume : ” + v o l ) ;
}
}

3.4 Nested Class in Java


An example of a nested class in Java is shown.

21
22
3.5 Lab 3 Exercises
Objectives:

• To learn Java programs related to class, objects and methods.

Outcomes:

• After completing this, the students would be able to develop Java pro-
grams using class, objects and methods.

Lab Assignments

1. Modify the Java program BoxDemo.java to do the following:

1. create two objects of class Box

2. assign values to two objects using objects’ instance-variables and get


the volumes of two boxes

3. set values to two objects using parameterized methods and get the
volumes of two boxes

2. Write a Java program to calculate the area and perimeter of a rectangle


using the concepts of class, objects and methods.

3. Create a Java class called uniMember which has instance-variables


name and gender. Within this class, create two more classes, Student
with instance-variable roll number and Faculty with instance-variable em-
ployee id. Write the Java methods to enter the details (name, gender,
roll number, employee id) of a student and a faculty and display the same
on the console.

23

You might also like