UNIT I – OOP in Java: Descriptive Programs with Answers and Explanation
Program 1: Class and Object Example
Statement: Write a program to define a class Student with fields rollNo, name, and
marks. Display the details of the student.
Program:
class Student {
int rollNo;
String name;
float marks;
void insertRecord(int r, String n, float m) {
rollNo = r;
name = n;
marks = m;
}
void displayInfo() {
System.out.println("Roll No: " + rollNo + ", Name: " + name + ", Marks: " +
marks);
}
public static void main(String[] args) {
Student s1 = new Student();
s1.insertRecord(101, "Ravi", 89.5f);
s1.displayInfo();
}
}
Explanation: This program demonstrates how to create a class and object. The object s1
is used to call insertRecord() and displayInfo().
Program 2: Constructor Usage
Statement: Write a program to create a constructor for initializing object data.
Program
class Employee {
int id;
String name;
Employee(int i, String n) {
id = i;
name = n;
}
void display() {
System.out.println("ID: " + id + ", Name: " + name);
}
public static void main(String[] args) {
Employee e1 = new Employee(1001, "Kumar");
e1.display();
}
}
Explanation: Constructor Employee(int i, String n) initializes the values at object
creation.
Program 3: Method Overloading
Statement: Demonstrate method overloading by writing a program that calculates the area
of square and rectangle.
class Area {
void calculate(int side) {
System.out.println("Area of square: " + (side * side));
}
void calculate(int l, int b) {
System.out.println("Area of rectangle: " + (l * b));
}
public static void main(String[] args) {
Area a = new Area();
a.calculate(5);
a.calculate(5, 10);
}
}
Explanation: Same method name calculate() with different parameters showcases method
overloading.
Program 4: Inheritance Example
Statement: Write a program using single inheritance where a class Animal is inherited by
Dog.
class Animal {
void sound() {
System.out.println("Animals make sound");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
}
public static void main(String[] args) {
Dog d = new Dog();
d.sound();
d.bark();
}
}
Explanation: Dog inherits sound() method from Animal.
Program 5: Polymorphism (Method Overriding)
Statement: Demonstrate method overriding using base class and derived class.
class Vehicle {
void run() {
System.out.println("Vehicle is running");
}
}
class Bike extends Vehicle {
void run() {
System.out.println("Bike is running safely");
}
public static void main(String[] args) {
Vehicle v = new Bike();
v.run();
}
}
Explanation: Method run() is overridden in subclass Bike. Dynamic binding occurs at
runtime.
Program 6: Static Keyword Usage
Statement: Write a program to demonstrate static variables and static methods.
class Counter {
static int count = 0;
Counter() {
count++;
System.out.println(count);
}
public static void main(String[] args) {
Counter c1 = new Counter();
Counter c2 = new Counter();
Counter c3 = new Counter();
}
}
Explanation: The static variable count is shared among all objects.
Program 7: Using Packages
Statement: Create a package and access a class from another file.
// File: mypack/Addition.java
package mypack;
public class Addition {
public int add(int a, int b) {
return a + b;
}
}
// Main class
import mypack.Addition;
class Test {
public static void main(String[] args) {
Addition a = new Addition();
System.out.println("Sum = " + a.add(10, 20));
}
}
Explanation: This shows how to define and use a custom package.
Program 8: Type Conversion (Casting)
Statement: Write a program to demonstrate type casting from float to int.
class Casting {
public static void main(String[] args) {
float f = 10.56f;
int x = (int) f;
System.out.println("Float: " + f);
System.out.println("Converted to int: " + x);
}
}
Explanation: Shows explicit type casting from float to int (narrowing conversion).
Program 9: Using Strings
Statement: Write a program to demonstrate basic string operations.
class StringDemo {
public static void main(String[] args) {
String s = "Java Programming";
System.out.println("Length: " + s.length());
System.out.println("Uppercase: " + s.toUpperCase());
System.out.println("Substring: " + s.substring(5));
}
}
Explanation: Demonstrates common string methods like length(), toUpperCase(),
substring().
Program 10: Array Usage
Statement: Write a program to find the sum of elements in an array.
class ArraySum {
public static void main(String[] args) {
int[] arr = {5, 10, 15, 20};
int sum = 0;
for (int num : arr) {
sum += num;
}
System.out.println("Sum = " + sum);
}
}
Explanation: Loops through an array and calculates the sum using enhanced for loop.