0% found this document useful (0 votes)
12 views16 pages

APP Week - 6 Assignment

The document contains a series of Java programming assignments focused on object-oriented programming concepts. It includes implementations of classes such as Person, TrafficLight, Employee, and Circle, as well as interfaces like Sortable and Resizable, demonstrating method overloading, inheritance, and polymorphism. Each section provides code examples and expected outputs for various programming tasks, showcasing practical applications of Java programming principles.

Uploaded by

kpnx8cpmmk
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)
12 views16 pages

APP Week - 6 Assignment

The document contains a series of Java programming assignments focused on object-oriented programming concepts. It includes implementations of classes such as Person, TrafficLight, Employee, and Circle, as well as interfaces like Sortable and Resizable, demonstrating method overloading, inheritance, and polymorphism. Each section provides code examples and expected outputs for various programming tasks, showcasing practical applications of Java programming principles.

Uploaded by

kpnx8cpmmk
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/ 16

ADVANCED PROGRAMMING

PRACTICE

ASSIGNMENT-6

RA2211026010486
G.Abiram
Z2-cse Aiml

PROGRAMS:

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.

Ans:

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 class PersonTest {


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 their name and age


System.out.println("Person 1 - Name: " + person1.getName() + ", Age: " +
person1.getAge());
System.out.println("Person 2 - Name: " + person2.getName() + ", Age: " +
person2.getAge());
}
}

Output:

Person 1 - Name: Alice, Age: 30


Person 2 - Name: Bob, Age: 25

2.Write a Java program to create class called "TrafficLight" with attributes for color and
duration, and methods to change the color and check for red or green.

Ans:

class TrafficLight {
private String color;
private int duration;

public TrafficLight(String initialColor, int initialDuration) {


color = initialColor;
duration = initialDuration;
}

public void changeColor(String newColor, int newDuration) {


color = newColor;
duration = newDuration;
}

public boolean isRed() {


return color.equalsIgnoreCase("red");
}

public boolean isGreen() {


return color.equalsIgnoreCase("green");
}

public String getColor() {


return color;
}

public int getDuration() {


return duration;
}
}

public class TrafficLightTest {


public static void main(String[] args) {
TrafficLight trafficLight = new TrafficLight("red", 30);

System.out.println("Current Traffic Light Status:");


System.out.println("Color: " + trafficLight.getColor());
System.out.println("Duration: " + trafficLight.getDuration() + " seconds");
System.out.println("Is it Red? " + trafficLight.isRed());
System.out.println("Is it Green? " + trafficLight.isGreen());

trafficLight.changeColor("green", 45);

System.out.println("\nUpdated Traffic Light Status:");


System.out.println("Color: " + trafficLight.getColor());
System.out.println("Duration: " + trafficLight.getDuration() + " seconds");
System.out.println("Is it Red? " + trafficLight.isRed());
System.out.println("Is it Green? " + trafficLight.isGreen());
}
}

Output:

Current Traffic Light Status:


Color: red
Duration: 30 seconds
Is it Red? true
Is it Green? false

Updated Traffic Light Status:


Color: green
Duration: 45 seconds
Is it Red? false
Is it Green? true

3.Write a Java program to perform arithmetic operations using method overloading.

Ans:

public class ArithmeticOperations {


public static void main(String[] args) {
System.out.println("Addition:");
System.out.println("Result (2 + 3): " + add(2, 3));
System.out.println("Result (4 + 6 + 8): " + add(4, 6, 8));
System.out.println("Result (1.5 + 2.5): " + add(1.5, 2.5));

System.out.println("\nSubtraction:");
System.out.println("Result (10 - 3): " + subtract(10, 3));
System.out.println("Result (20 - 5 - 7): " + subtract(20, 5, 7));
System.out.println("Result (5.5 - 2.5): " + subtract(5.5, 2.5));

System.out.println("\nMultiplication:");
System.out.println("Result (4 * 5): " + multiply(4, 5));
System.out.println("Result (2 * 3 * 4): " + multiply(2, 3, 4));
System.out.println("Result (2.5 * 3.5): " + multiply(2.5, 3.5));

System.out.println("\nDivision:");
System.out.println("Result (10 / 2): " + divide(10, 2));
System.out.println("Result (15 / 3 / 5): " + divide(15, 3, 5));
System.out.println("Result (9.0 / 3.0): " + divide(9.0, 3.0));
}

// Method for addition


public static int add(int a, int b) {
return a + b;
}

public static int add(int a, int b, int c) {


return a + b + c;
}

public static double add(double a, double b) {


return a + b;
}

// Method for subtraction


public static int subtract(int a, int b) {
return a - b;
}

public static int subtract(int a, int b, int c) {


return a - b - c;
}

public static double subtract(double a, double b) {


return a - b;
}

// Method for multiplication


public static int multiply(int a, int b) {
return a * b;
}

public static int multiply(int a, int b, int c) {


return a * b * c;
}

public static double multiply(double a, double b) {


return a * b;
}

// Method for division


public static int divide(int a, int b) {
return a / b;
}

public static int divide(int a, int b, int c) {


return a / b / c;
}

public static double divide(double a, double b) {


return a / b;
}
}

Output:

Addition:
Result (2 + 3): 5
Result (4 + 6 + 8): 18
Result (1.5 + 2.5): 4.0

Subtraction:
Result (10 - 3): 7
Result (20 - 5 - 7): 8
Result (5.5 - 2.5): 3.0

Multiplication:
Result (4 * 5): 20
Result (2 * 3 * 4): 24
Result (2.5 * 3.5): 8.75

Division:
Result (10 / 2): 5
Result (15 / 3 / 5): 1
Result (9.0 / 3.0): 3.0

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().

Ans:

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() + " (HR Manager) is managing HR tasks.");
}
public void addEmployee(String employeeName, double employeeSalary) {
System.out.println(getName() + " (HR Manager) added employee: " + employeeName);
}
}

public class EmployeeTest {


public static void main(String[] args) {
Employee employee1 = new Employee("John", 50000);
HRManager hrManager1 = new HRManager("Alice", 60000);

employee1.work();
System.out.println("Employee 1 Salary: $" + employee1.getSalary());

hrManager1.work();
System.out.println("HR Manager 1 Salary: $" + hrManager1.getSalary());

hrManager1.addEmployee("Bob", 55000);
}
}

Output:

John is working.
Employee 1 Salary: $50000.0
Alice (HR Manager) is managing HR tasks.
HR Manager 1 Salary: $60000.0
Alice (HR Manager) added employee: Bob

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.

Ans:

class Shape {
public double getPerimeter() {
return 0.0;
}

public double getArea() {


return 0.0;
}
}
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 * radius * radius;
}
}

public class ShapeTest {


public static void main(String[] args) {
double circleRadius = 5.0;
Circle circle = new Circle(circleRadius);

System.out.println("Circle Radius: " + circleRadius);


System.out.println("Circle Perimeter: " + circle.getPerimeter());
System.out.println("Circle Area: " + circle.getArea());
}
}

Output:

Circle Radius: 5.0


Circle Perimeter: 31.41592653589793
Circle Area: 78.53981633974483

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.

Ans:

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 SortingTest {


public static void main(String[] args) {
int[] arr = {64, 34, 25, 12, 22, 11, 90};

Sortable bubbleSort = new BubbleSort();


bubbleSort.sort(arr);
System.out.println("Sorted using Bubble Sort:");
printArray(arr);

int[] arr2 = {64, 34, 25, 12, 22, 11, 90};

Sortable selectionSort = new SelectionSort();


selectionSort.sort(arr2);
System.out.println("\nSorted using Selection Sort:");
printArray(arr2);
}

public static void printArray(int[] arr) {


for (int value : arr) {
System.out.print(value + " ");
}
System.out.println();
}
}

Output:

Sorted using Bubble Sort:


11 12 22 25 34 64 90

Sorted using Selection Sort:


11 12 22 25 34 64 90

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.

Ans:

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;
}

public int getWidth() {


return width;
}

public int getHeight() {


return height;
}

@Override
public void resizeWidth(int width) {
this.width = width;
}

@Override
public void resizeHeight(int height) {
this.height = height;
}
}

public class ResizableTest {


public static void main(String[] args) {
Rectangle rectangle = new Rectangle(10, 20);

System.out.println("Original Rectangle Size:");


System.out.println("Width: " + rectangle.getWidth());
System.out.println("Height: " + rectangle.getHeight());

// Resize the rectangle


rectangle.resizeWidth(15);
rectangle.resizeHeight(25);

System.out.println("\nResized Rectangle Size:");


System.out.println("Width: " + rectangle.getWidth());
System.out.println("Height: " + rectangle.getHeight());
}
}

Output:

Original Rectangle Size:


Width: 10
Height: 20

Resized Rectangle Size:


Width: 15
Height: 25
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.

Ans:

interface Flyable {
void fly_obj();
}

class Spacecraft implements Flyable {


@Override
public void fly_obj() {
System.out.println("Spacecraft is flying.");
}
}

class Airplane implements Flyable {


@Override
public void fly_obj() {
System.out.println("Airplane is flying.");
}
}

class Helicopter implements Flyable {


@Override
public void fly_obj() {
System.out.println("Helicopter is flying.");
}
}

public class FlyableTest {


public static void main(String[] args) {
Flyable spacecraft = new Spacecraft();
Flyable airplane = new Airplane();
Flyable helicopter = new Helicopter();

System.out.println("Flying Objects:");

spacecraft.fly_obj();
airplane.fly_obj();
helicopter.fly_obj();
}
}

Output:
Flying Objects:
Spacecraft is flying.
Airplane is flying.
Helicopter is flying.

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

Ans:

// Package "addition"
package addition;

public class Addition {


public static int add(int a, int b) {
return a + b;
}
}

// Package "subtraction"
package subtraction;

public class Subtraction {


public static int subtract(int a, int b) {
return a - b;
}
}

// Package "multiplication"
package multiplication;

public class Multiplication {


public static int multiply(int a, int b) {
return a * b;
}
}

// Package "division"
package division;

public class Division {


public static double divide(int a, int b) {
if (b != 0) {
return (double) a / b;
} else {
System.out.println("Error: Division by zero.");
return Double.NaN;
}
}
}

// Main package
import addition.Addition;
import subtraction.Subtraction;
import multiplication.Multiplication;
import division.Division;

public class MainApp {


public static void main(String[] args) {
int num1 = 10;
int num2 = 5;

// Perform arithmetic operations using functions from different packages


int sum = Addition.add(num1, num2);
int difference = Subtraction.subtract(num1, num2);
int product = Multiplication.multiply(num1, num2);
double quotient = Division.divide(num1, num2);

// Display the results


System.out.println("Sum: " + sum);
System.out.println("Difference: " + difference);
System.out.println("Product: " + product);
System.out.println("Quotient: " + quotient);
}
}

Output:

Sum: 15
Difference: 5
Product: 50
Quotient: 2.0

10.Create two different packages to compute bubblesort and selection sort. Write a Java
program to implement sorting functions in a single class

Ans:

// Package "bubblesort"
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"
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;
}
}
}

// Main package
import bubblesort.BubbleSort;
import selectionsort.SelectionSort;

public class SortApp {


public static void main(String[] args) {
int[] arr1 = {64, 34, 25, 12, 22, 11, 90};
int[] arr2 = {64, 34, 25, 12, 22, 11, 90};
// Perform sorting using functions from different packages
BubbleSort.sort(arr1);
SelectionSort.sort(arr2);

// Display the sorted arrays


System.out.println("Sorted using Bubble Sort:");
printArray(arr1);

System.out.println("\nSorted using Selection Sort:");


printArray(arr2);
}

public static void printArray(int[] arr) {


for (int value : arr) {
System.out.print(value + " ");
}
System.out.println();
}
}

Output:

Sorted using Bubble Sort:


11 12 22 25 34 64 90

Sorted using Selection Sort:


11 12 22 25 34 64 90

You might also like