# Practical List - Naresh Chandra

Software and s/w Development

Nov 3, 2013 (4 years and 5 months ago)

64 views

OOPS LAB

Program
No.

Lab Assignments

Page
No.

Expected

Date of
Completion

Date

of
Completion

Faculty
Sign.

1

Given a class definition Rectangle below, describe
the structure of any 3 instances of Rectangle class in
Java.

class Rectangle

{

Attributes:

Length

Width

Methods:

getLength() { return length }

getWidth() { return width }

draw() { ……}

}

9
th

feb’10

2

Define using the following structure a class definition for
cars. A car generally has abilities to start, move forward,
and move backward, sto
its relative location. The starting location is a value 0.

class Car {

Attributes:

...

Methods:

...

}

11
th

feb’10

3

Create a class called triangle, which stores the length
of the base and height of a right angle tr
iangle in two
private instance variable. Define three functions for
getting the data, to calculate hypotaneous and to
calculate the area of the triangle.

16
th

feb’10

4

Define a class to represent bank A/c. Include the
following data members (name_ of_d
epositor,
account_no, account_type, balance) member
functions (to assign the initial values, to deposit an
amount, to withdraw an amount after checking the
balance, to display name and balance). Write a main
program to perform all such operations

23
nd

feb
’10

5

Create a class student, includes details such as rno,
name, section, marks_ obtained and total_marks. We
would like to perform operations such as adding a
record, deleting a record and display a record. Show how
these operations are implemented us
ing a
class with

9
th

mar
’10

arrays

as data members.

6

Create a class employee, with data members (name,
age, hra, da, pf, gross_salary, basic_salary) member
functions(getdata(), putdata(), salary()). Show how
these operatins are implemented using
ob
ject arrays

1
1
th

mar’10

7

Create a class complex, with member function
sum(), having
objects as functional arguments,

it
performs the addition of two complex numbers.
Write a program to implement it.

1
1
th

mar’10

8

Declare a class candidate, include

following data
static member
)
and member functions (vote(), initialize(), display()),
assume there are five candidates, accept and count votes

1
6
th

mar’10

9

Declare a base class employee with data members
(empid, bs as private) member function (get_id(),get_bs()
as public). Now create a derived class salary with data
members (hra, da, pf as private and ns as public) member
functions(calculate(), display()

as public). The base class
is inherited as publicly. Calculate() computes the
ns(ns=bs+da+hra
-
pf) and display() prints emp_id, bs and
ns.

16
th

mar’10

10

Implement the function display of class result, so that it
prints following information: Rno, Nam
e, total marks and
result of the student. Assume member functions in each

18
th

mar’10

11

Implement the function display of class salary, so that it
prints following information: Rno, Name, total marks a
nd
result of the student. Assume member functions in each
class as per your requirement (gross_salary=((bs
+da+hra
-
pf)*12) netsalary_pa=gross_salary
-
tax_to_pay

23
th

mar’10

12

WAP to overload function area, so that it calculates

the
area of circle, triangle and square

25
th

mar
’10

13

The purpose of this lab is to become familiar with
implementing inheritance in Java. You will write a class
named Polygon that is subclassed by a Triangle class.

Write a class called Polygon that

has two
fields: an int for the number of sides and
a double for the area. Add a method
called get
-

NumberOfSides() that prints
out and returns the number of sides.

Override the toString() method in
Polygon to return a nice string
olygon class.

Add a constructor to Polygon that takes
in an int to represent the number of sides
and prints out the message “Inside
Polygon constructor.”

Save and compile the Polygon class.

Write a class called Triangle that extends

fields: one for the
base and one for the height. (Triangles
have a base and a height.)

Add a constructor to Triangle that takes
in two int’s for the base and height. The
constructor needs to use super() to invoke
the constructor in Polygon, passing in 3
for the number of sides. Print out the
message “Inside Triangle constructor.”

Add a toString() method to Triangle that
prints out the triangle’s base and height.

Add a getArea() method to Triangle that
computes and returns the area. The
formula for the a
rea of a triangle is:

area = 1/2 (base * height)

30
th

mar
’10

Save and compile the Triangle class.

Write a program that instantiates at least
one Polygon object and one Triangle
object. Invoke the various methods to
ensure that everything is working.

When you inst
antiate a Triangle object, the output should
display “Inside Polygon constructor” before displaying
“Inside Triangle constructor.

14

This lab is a continuation of Question 17 and
demonstrates overriding methods.

Write a class called RightTr
iangle that
Question 8.

Add a field of type double called
hypotenuse to represent the longest side
of a right triangle.

Add a constructor that takes in two int’s
to represent the base and height. Pass
these two values up
to the Triangle
constructor and then use these two values
in the constructor to compute the
hypotenuse field. The formula is:

hypotenuse = sqrt(base*base +
height*height)

Use the Math.sqrt() function to compute
the square root. Math.sqrt() takes in a
d
ouble and returns a double. Also, print
out a message stating “Inside
RightTriangle constructor.”

RightTriangle. Use super to invoke
toString() in the parent and concatenate
the result with the hypotenuse.

Save and compile th
e RightTriangle
class.

Write a program that instantiates a
RightTriangle object and invokes the
toString(), getArea(), and
getNumberOfSides() methods. Run your
program, and verify that everything is
working correctly.

When you instantiate a RightTriangle

object, the output
of the constructors should be in the following order:
“Inside Polygon constructor,” “Inside Triangle
constructor,” and “Inside RightTriangle constructor.”

1
st

apr
’10

15

The purpose of this lab is to become familiar with using
polym
orphism in Java. You will use polymorphic
parameters to write a class that can “draw” any shape, and
use virtual method invocation so that the appropriate
shape is drawn without requiring any casting of
references. Polymorphism and Abstraction

Write a cla
ss called Shape that only
contains a method named draw(), with no

6
th
apr
’10

parameters and a void return value. The
draw method should print out “Drawing a
Shape”.

Save and compile the Shape class.

Write a class named Rectangle that
extends Shape. Add two int fiel
ds for the
width and height of the rectangle, and
add a constructor with two int parameters
that are used to initialize these fields. Use
encapsulation to ensure that the value of
width and height are between 1 and 15.

In your Rectangle class, override th
e
draw() method in Shape. Using nested for
loops and asterisks (*), print out the
rectangle using its proper width and
height. For example, if width is 7 and
height is 3, the draw() method should
display:

*******

**

*******

Save and compile the Rectangl
e class.

Write a class named RightTriangle that
extends Shape. Add two int fields for the
base and height of the triangle, and add a
constructor with two int parameters that
are used to initialize these fields. Use
encapsulation to ensure that the value
of
base and height are between 1 and 20.

In your RightTriangle class, override the
draw() method in Shape. Using nested for
loops and asterisks, print out the triangle
similarly to the way you printed out the
Rectangle. For example, if the base is 8
and
the height is 4, the output should
look similar to:

*

***

******

********

Save and compile the RightTriangle
class.

Write a class named Artist that contains a
method named draw
-

Shape(). The
method has one parameter of type Shape
and returns void. The d
rawShape()
method should invoke the draw() method
on whatever Shape object is passed in.

Save and compile the Artist class.

Write a class named ArtistDemo that
contains main(). Within main(),
instantiate a Shape, a Rectangle, and a
RightTriangle object.

Within main(), also instantiate an Artist
object. Using your Artist object, invoke
drawShape() three times, once for each of
the three shapes instantiated in the
previous step.

Save, compile, and run the Artist class.

16

The purpose of thi
s lab is to become familiar with
creating a JFrame.

Write a class named Calculator that
extends JFrame.

Within the constructor of the Calculator
class, use panels and layout managers to
create a GUI similar to any Windows
based Calculator. Note that the
class uses
JButton for the buttons and JTextField
for the display above the buttons.

class that instantiates and displays your
Calculator GUI.

Save, compile, and run the Calculator
class.

similar to the one that
Windows Machine has. Do not worry if it is not exactly
the same, especially because we have not discussed the
details of the various components yet.

8
th

apr’10

17

This lab is the beginning of a project that you will work
on th
roughout the remainder of this book: You will write
an instant messaging program. Instant messaging has
become a very popular Internet application because it
enables you to “talk” to someone by sending messages
back and forth that are instantly read by the

project will use Swing, and you will create the GUI
portion of the program first. Write a class named
InstantMessageFrame. Have it extend the JFrame class.

Add a constructor that contains a single
parameter of type String to represent the
title bar of the JFrame. Within the
constructor, pass the String parameter up
to the parent JFrame constructor that
takes in a String.

Within the constructor, use the
setDefaultCloseOperation() method so
that your JFrame will cause the program
to exit whe
n the user closes the JFrame.

Add a main() method within your
InstantMessageFrame class. Within
main(), instantiate a new
InstantMessageFrame object, passing in
“My IM Program” for the title bar.

Within main(), set the size of your
window to be 220 pixel
s wide by 450
pixels high.

Within main(), invoke setVisible() to

13
th

apr’10

make your window visible on the
screen.

Save, compile, and run your
InstantMessageFrame program.

You should see a JFrame that is 220 pixels wide by 450
pixels high. Your program should exi
t when you close the
JFrame.

18

The purpose of this lab is to become familiar with
handling the events of GUI components. You are going to
from Lab 20 works.

Write a class named Calc
ulatorListener
that implements ActionListener. This
class is going to handle the ActionEvent
from all 16 JButtons in your calculator.

Add a field of type JTextField and a
constructor that has a JTextField
parameter. Assign the field to the
parameter withi
n this constructor.

to perform the arithmetic of the
calculator. Use if/else statements to
determine which button was clicked, and
include all the necessary logic for your
CalculatorListener class so that the
calcula
tor functions properly.

You should see a fully functional calculator. Be sure to
perform some quality assurance on your calculator,
testing to make sure everything is working properly.

15
th

apr’10

19

Write a login web page in Servlet that accept user n
ame

from user and check it in
database to validate account.

20
th

apr’10

20

Develop commercial Web Sites in java according to your
choice

22
th

apr’10