APP Week - 6 Assignment
APP Week - 6 Assignment
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;
Output:
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;
trafficLight.changeColor("green", 45);
Output:
Ans:
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));
}
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;
@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);
}
}
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;
}
@Override
public double getPerimeter() {
return 2 * Math.PI * radius;
}
@Override
public double getArea() {
return Math.PI * radius * radius;
}
}
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.
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);
}
}
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.
Ans:
interface Resizable {
void resizeWidth(int width);
void resizeHeight(int height);
}
@Override
public void resizeWidth(int width) {
this.width = width;
}
@Override
public void resizeHeight(int height) {
this.height = height;
}
}
Output:
Ans:
interface Flyable {
void fly_obj();
}
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;
// Package "subtraction"
package subtraction;
// Package "multiplication"
package multiplication;
// Package "division"
package division;
// Main package
import addition.Addition;
import subtraction.Subtraction;
import multiplication.Multiplication;
import division.Division;
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;
// Package "selectionsort"
package selectionsort;
// Main package
import bubblesort.BubbleSort;
import selectionsort.SelectionSort;
Output: