NAME - S .
Abhisesha Kumaara
REG NO -RA2211026010487
21CSC203P - ADVANCED PROGRAMMING PRACTICE
WEEK - 6
Object-Oriented Programming Paradigm
1 . Write a Java program to create a class called "Person" with a name and age
attribute. Create two instances of the "Person" class, set their attributes using the
constructor, and print their name and age.
public class Person { private
String name; private int
age;
public Person(String name, int age) {
this.name = name; this.age = age;
public String getName() { return
name;
}
public int getAge() { return
age;
public static void main(String[] args) {
• Create two instances of the Person class and set their attributes
Person person1 = new Person("Alice", 30);
Person person2 = new Person("Bob", 25);
• Print the name and age of the first person
System.out.println("Person 1:");
System.out.println("Name: " + person1.getName());
System.out.println("Age: " + person1.getAge());
• Print the name and age of the second person
System.out.println("\nPerson 2:");
System.out.println("Name: " + person2.getName());
System.out.println("Age: " + person2.getAge());
OUTPUT :
2 . Write a Java program to create a class called "TrafficLight" with attributes for colour
and duration, and methods to change the colour and check for red or green.
public class TrafficLight {
private String color; private
int duration;
public TrafficLight(String initialColor, int initialDuration)
{ color = initialColor; duration = initialDuration;
public void changeColor(String newColor) { color =
newColor;
public boolean isRed() { return
color.equalsIgnoreCase("red");
public boolean isGreen() { return
color.equalsIgnoreCase("green");
}
public static void main(String[] args) {
TrafficLight trafficLight = new TrafficLight("red", 30);
System.out.println("Initial state of the traffic light:");
System.out.println("Color: " + trafficLight.color);
System.out.println("Duration: " + trafficLight.duration + " seconds");
System.out.println("Is it red? " + trafficLight.isRed());
System.out.println("Is it green? " + trafficLight.isGreen());
trafficLight.changeColor("green");
System.out.println("\nAfter changing the color to green:");
System.out.println("Color: " + trafficLight.color);
System.out.println("Is it red? " + trafficLight.isRed());
System.out.println("Is it green? " + trafficLight.isGreen());
OUTPUT :
3 . Write a Java program to perform arithmetic operations using method
overloading.
public class ArithmeticOperations { public
int add(int num1, int num2) { return
num1 + num2;
public double add(double num1, double num2)
{ return num1 + num2;
public int subtract(int num1, int num2) {
return num1 - num2;
public double subtract(double num1, double num2)
{ return num1 - num2;
public int multiply(int num1, int num2) {
return num1 * num2;
public double multiply(double num1, double num2)
{ return num1 * num2;
public int divide(int num1, int num2) {
if (num2 != 0) {
return num1 / num2;
} else {
System.out.println("Cannot divide by zero."); return 0;
}
}
public double divide(double num1, double num2) { if
(num2 != 0) {
return num1 / num2;
} else {
System.out.println("Cannot divide by zero."); return
0.0;
public static void main(String[] args) {
ArithmeticOperations calculator = new ArithmeticOperations();
int sum1 = calculator.add(5, 3); System.out.println("Integer
Addition: " + sum1); double
sum2 = calculator.add(5.5, 3.5);
System.out.println("Double Addition: " + sum2);
int diff1 = calculator.subtract(10, 4);
System.out.println("Integer Subtraction: " + diff1);
double diff2 = calculator.subtract(10.5, 4.2); System.out.println("Double
Subtraction: " + diff2);
int product1 = calculator.multiply(6, 7);
System.out.println("Integer Multiplication: " + product1);
double product2 = calculator.multiply(6.2, 7.5);
System.out.println("Double Multiplication: " + product2);
int quotient1 = calculator.divide(15, 3);
System.out.println("Integer Division: " + quotient1);
double quotient2 = calculator.divide(15.0, 0.0); // Attempting to divide by zero
System.out.println("Double Division: " + quotient2);
}
}
OUTPUT :
4 . Write a Java program to create a class called Employee with methods called
work() and getSalary(). Create a subclass called HRManager that overrides the
work() method and adds a new method called addEmployee().
class Employee { private
String name; private
double salary;
public Employee(String name, double salary) {
this.name = name; this.salary
= salary;
public void work() {
System.out.println(name + " is working.");
public double getSalary() { return
salary;
}
class HRManager extends Employee {
public HRManager(String name, double salary)
{ super(name, salary);
@Override public
void work() {
System.out.println(getName() + " is managing HR tasks.");
public void addEmployee(String employeeName)
{ System.out.println(getName() + " is adding a new employee: " +
employeeName);
public class EmployeeDemo { public static
void main(String[] args) {
Employee emp1 = new Employee("John", 50000.0); HRManager
manager = new HRManager("Alice", 60000.0);
emp1.work();
System.out.println("Employee Salary: $" + emp1.getSalary());
manager.work();
manager.addEmployee("Bob");
System.out.println("HR Manager Salary: $" + manager.getSalary());
OUTPUT :
5 . Write a Java program to create a class called Shape with methods called
getPerimeter() and getArea(). Create a subclass called Circle that overrides the
getPerimeter() and getArea() methods to calculate the area and perimeter of a
circle.
class Shape { public double getPerimeter() { return 0.0; // Default
implementation, to be overridden by subclasses
public double getArea() { return 0.0; // Default implementation, to be
overridden by subclasses
class Circle extends Shape { private
double radius;
public Circle(double radius) { this.radius =
radius;
@Override
public double getPerimeter() { return
2 * Math.PI * radius;
@Override public double getArea() {
return Math.PI * Math.pow(radius, 2);
public class ShapeDemo { public static
void main(String[] args) {
Circle circle = new Circle(5.0);
System.out.println("Circle Perimeter: " + circle.getPerimeter());
System.out.println("Circle Area: " + circle.getArea());
OUTPUT :
6 . Write a Java program to create an interface Sortable with a method sort() that
sorts an array of integers in ascending order. Create two classes BubbleSort and
SelectionSort that implement the Sortable interface and provide their own
implementations of the sort() method.
interface Sortable { void
sort(int[] arr);
class BubbleSort implements Sortable {
@Override
public void sort(int[] arr) { int
n = arr.length;
boolean swapped;
do {
swapped = false;
for (int i = 1; i < n; i++) { if
(arr[i - 1] > arr[i]) {
• Swap arr[i-1] and arr[i] int
temp = arr[i - 1]; arr[i -
1] = arr[i]; arr[i] = temp;
swapped = true;
n--;
} while (swapped);
class SelectionSort implements Sortable {
@Override
public void sort(int[] arr) {
int n = arr.length; for (int i = 0;
i < n - 1; i++) { int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
• Swap arr[i] and arr[minIndex] int
temp = arr[i]; arr[i] = arr[minIndex];
arr[minIndex] = temp;
public class SortingDemo {
public static void main(String[] args) { int[]
arr1 = { 64, 34, 25, 12, 22, 11, 90 }; int[]
arr2 = { 64, 25, 12, 22, 11 };
Sortable bubbleSort = new BubbleSort(); bubbleSort.sort(arr1);
System.out.println("Bubble Sorted Array:"); printArray(arr1);
Sortable selectionSort = new SelectionSort(); selectionSort.sort(arr2);
System.out.println("\nSelection Sorted Array:"); printArray(arr2);
static void printArray(int[] arr) { for
(int num : arr) {
System.out.print(num + " ");
}
System.out.println();
OUTPUT :
7 . Write a Java program to create an interface Resizable with methods
resizeWidth(int width) and resizeHeight(int height) that allow an object to be
resized. Create a class Rectangle that implements the Resizable interface and
implements the resize methods.
interface Resizable { void
resizeWidth(int width); void
resizeHeight(int height);
}
class Rectangle implements Resizable {
private int width; private int height;
public Rectangle(int width, int height) {
this.width = width; this.height =
height;
@Override public void
resizeWidth(int width) { this.width =
width;
@Override public void
resizeHeight(int height) { this.height =
height;
public int getWidth() { return
width;
public int getHeight() { return
height;
public class ResizableDemo { public static
void main(String[] args) {
Rectangle rectangle = new Rectangle(10, 20);
System.out.println("Original Rectangle:");
System.out.println("Width: " + rectangle.getWidth());
System.out.println("Height: " + rectangle.getHeight());
• Resize the rectangle rectangle.resizeWidth(15);
rectangle.resizeHeight(30);
System.out.println("\nResized Rectangle:");
System.out.println("Width: " + rectangle.getWidth());
System.out.println("Height: " + rectangle.getHeight());
OUTPUT :
8 . Write a Java program to create an interface Flyable with a method called
fly_obj(). Create three classes Spacecraft, Airplane, and Helicopter that implement the
Flyable interface. Implement the fly_obj() method for each of the three classes. Hint :-
fly_obj definition – prints the particular object is flying.
• Flyable interface
interface Flyable { void
fly_obj();
• Spacecraft class implementing Flyable class
Spacecraft implements Flyable {
@Override
public void fly_obj() { System.out.println("Spacecraft is flying
in outer space.");
• Airplane class implementing Flyable class
Airplane implements Flyable {
@Override
public void fly_obj() {
System.out.println("Airplane is flying in the sky.");
• Helicopter class implementing Flyable class
Helicopter implements Flyable {
@Override
public void fly_obj() { System.out.println("Helicopter is
flying in the air.");
public class FlyableDemo { public static
void main(String[] args) {
Flyable spacecraft = new Spacecraft();
Flyable airplane = new Airplane();
Flyable helicopter = new Helicopter(); System.out.println("Let's see
what's flying:");
spacecraft.fly_obj(); airplane.fly_obj();
helicopter.fly_obj(); }
OUTPUT :
9 . Write a Java program to have the arithmetic functions defined in different user-
defined packages and incorporate all the packages and perform the function in a
single class.
package addition;
public class Addition { public static int add(int
num1, int num2) { return num1 + num2;
package subtraction;
public class Subtraction { public static int
subtract(int num1, int num2) { return num1 -
num2;
package multiplication;
public class Multiplication { public static int
multiply(int num1, int num2) { return num1 *
num2;
package main; import
addition.Addition; import
subtraction.Subtraction; import
multiplication.Multiplication;
public class Main { public static void
main(String[] args) { int num1 = 10; int
num2 = 5;
int sum = Addition.add(num1, num2); int difference =
Subtraction.subtract(num1, num2); int product =
Multiplication.multiply(num1, num2);
System.out.println("Sum: " + sum);
System.out.println("Difference: " + difference);
System.out.println("Product: " + product);
OUTPUT :
1. . Create two different packages to compute bubble sort and selection sort.
Write a Java program to implement sorting functions in a single class.
package bubblesort;
public class BubbleSort { public
static void sort(int[] arr) { int n =
arr.length;
boolean swapped;
do {
swapped = false;
for (int i = 1; i < n; i++) { if
(arr[i - 1] > arr[i]) {
• Swap arr[i-1] and arr[i] int
temp = arr[i - 1]; arr[i - 1] =
arr[i]; arr[i] = temp; swapped
= true;
n--;
} while (swapped);
package selectionsort;
public class SelectionSort {
public static void sort(int[] arr) {
int n = arr.length; for (int i = 0; i
• n - 1; i++) { int minIndex = i; for
(int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex])
{ minIndex = j;
• Swap arr[i] and arr[minIndex] int
temp = arr[i]; arr[i] =
arr[minIndex]; arr[minIndex] =
temp;
package main; import
java.util.Arrays; import
bubblesort.BubbleSort; import
selectionsort.SelectionSort;
public class SortMain {
public static void main(String[] args) {
int[] arr1 = { 64, 34, 25, 12, 22, 11, 90 }; int[]
arr2 = { 64, 25, 12, 22, 11 };
System.out.println("Original Arrays:");
System.out.println("Array 1: " + Arrays.toString(arr1));
System.out.println("Array 2: " + Arrays.toString(arr2));
BubbleSort.sort(arr1);
SelectionSort.sort(arr2);
System.out.println("\nSorted Arrays:");
System.out.println("Array 1 (Bubble Sort): " + Arrays.toString(arr1));
System.out.println("Array 2 (Selection Sort): " + Arrays.toString(arr2));
OUTPUT :