0% found this document useful (0 votes)
18 views3 pages

Experiment No 3

The document outlines an experiment aimed at implementing basic object-oriented concepts such as class design, encapsulation, inheritance, and polymorphism to enhance software modularity and maintainability. It provides practical programming tasks and questions related to Java, focusing on creating classes and objects, and includes quizzes to reinforce understanding of key OOP principles. Suggested references for further learning are also provided.

Uploaded by

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

Experiment No 3

The document outlines an experiment aimed at implementing basic object-oriented concepts such as class design, encapsulation, inheritance, and polymorphism to enhance software modularity and maintainability. It provides practical programming tasks and questions related to Java, focusing on creating classes and objects, and includes quizzes to reinforce understanding of key OOP principles. Suggested references for further learning are also provided.

Uploaded by

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

Experiment No: 3

AIM: To implement basic object-oriented concepts.

Date:

CO mapped: CO-2

Objectives:

a) To apply fundamental object-oriented principles, such as class design, encapsulation,


inheritance, and polymorphism, to improve software modularity, code organization,
and maintainability.

b) Implementing these basic object-oriented concepts in your software development


practices will help you create more structured, maintainable, and reusable code, which
is essential for building robust and scalable software systems.

Background:
Object means a real-world entity such as a pen, chair, table, computer, watch, etc. Object-
Oriented Programming is a methodology or paradigm to design a program using classes and
objects. It simplifies software development and maintenance by providing some concepts:

● Object: Any entity that has a state and behavior is known as an object. For example, a
chair, pen, table, keyboard, bike, etc. It can be physical or logical. An Object can be
defined as an instance of a class. An object contains an address and takes up some
space in memory. Objects can communicate without knowing the details of each
other's data or code. The only necessary thing is the type of message accepted and the
type of response returned by the objects.
● Class: Collection of objects is called class. It is a logical entity. A class can also be
defined as a blueprint from which you can create an individual object. Class doesn't
consume any space.
● Inheritance: When one object acquires all the properties and behaviors of a parent
object, it is known as inheritance. It provides code reusability. It is used to achieve
runtime polymorphism.
● Polymorphism: If one task is performed in different ways, it is known as
polymorphism. For example: to convince the customer differently, to draw something,
for example, shape, triangle, rectangle, etc. In Java, we use method overloading and
method overriding to achieve polymorphism. Another example can be to speak
something; for example, a cat speaks meow, dog barks woof, etc.
● Abstraction: Hiding internal details and showing functionality is known as
abstraction. For example, phone call, we don't know the internal processing. In Java,
we use abstract class and interface to achieve abstraction.
● Encapsulation: Binding (or wrapping) code and data together into a single unit are
known as encapsulation. For example, a capsule, it is wrapped with different
medicines. A java class is an example of encapsulation. Java bean is the fully
encapsulated class because all the data members are private here.
Practical questions:
1. Write a Java application which takes several command line arguments, which are
supposed to be names of students and prints output as given below: (Suppose we enter
3 names then output should be as follows):
Number of arguments = 3
1: First Student Name is =Tom
2: Second Student Name is =Dick
3: Third Student Name is =Harry (Hint: An array may be used for converting
from numeric values from 1 to 20 into String.)

2. Design a class named Rectangle to represent a rectangle. The class contains: Two
double data fields named width and height that specify the width and height of the
rectangle. The default values are 1 for both width and height.

A no-arg constructor that creates a default rectangle.


A constructor that creates a rectangle with the specified width and height.
A method named getArea() that returns the area of this rectangle.
A method named getPerimeter() that returns the perimeter.
Write a test program that creates two Rectangle objects—one with width 4 and height
40 and the other with width 3.5 and height 35.9. Display the width, height, area, and
perimeter of each rectangle in this order.

3. Define a class called Cartesian Point, which has two instance variables, x and y.
Provide the methods get X() and get Y() to return the values of the x and y values
respectively, a method called move() which would take two integers as parameters
and change the values of x and y respectively, a method called display() which would
display the current values of x and y. Now overload the method move() to work with
single parameter, which would set both x and y to the same values,provide
constructors with two parameters and overload to work with one parameter as well.
Now define a class called Test Cartesian Point, with the main method to test the
various methods in the Cartesian Point class.

4. Create a class Employee which has two private data members name and salary and it
has two public member functions named as getData() and putData() where getData()
gets name and salary from the user putData() displays name and salary for any user.

5. Define a class Time with hours and minutes astwo data members, add necessary
member functions to initialize and display data of class. Do not use constructors in a
class. Define a member function sum () which adds two Time objects. (Use the
statements like T3.sum (T1, T2)).

6. Define Class named Point which represents 2-D Point, i.e P (x, y). Define
Defaultconstructor to initialize both data member value 5, Parameterized constructor
to initialize member according to value supplied by user and Copy Constructor.
Define Necessary Function and Write a program to test class Point.

7. Create a class Account. It has three data member account id, name and balance.
Define function to assign value and display value. Define function that search account
number given by the user. If account number exists, print detail of that account. Write
a program using array of object. Declare at least 5 account and print details.
Observations: Put Output of the program

Conclusion:

Quiz:

1. Explain the concept of encapsulation and why it is important in OOP.


2. What is a class, and how does it relate to objects in OOP?
3. Define and explain static and dynamic binding.
4. Explain the concept of method overloading and method overriding in OOP.

Suggested Reference:

1. https://www.tutorialspoint.com/java/
2. https://www.geeksforgeeks.org/
3. https://www.w3schools.com/java/
4. https://www.javatpoint.com/

You might also like