You are on page 1of 16

ADVANCED PROGRAMMING PRACTICE

Week 6
SUBMITTED BY: Joyjit Banerjee

RA NO: RA2211003012007
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 theirname and age.

class Person { private


String name;
private int yrs;

public Person(String name, int age) { this.name


= name;
this.yrs = age;
}

public String Name() { return


name;
}

public int Age() { return


yrs;
}
}

public class Person1 { public static void


main(String[] args) {
Person p1 = new Person("Alice", 25);
Person p2 = new Person("Bob", 30);

System.out.println("Person 1:");
System.out.println("Name: " + p1.Name());
System.out.println("Age: " + p1.Age());

System.out.println();

System.out.println("Person 2:");
System.out.println("Name: " + p2.Name());
System.out.println("Age: " + p2.Age());
}
}
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.

class Light { private


String color;
private int time;

public Light(String col, int dur) {


color = col; time = dur;
}

public void change(String newCol, int newDur) {


color = newCol; time = newDur;
}

public String getColor() { return


color;
}

public int getTime() { return


time;
}
}

public class TLExample { public static


void main(String[] args) { Light l1 =
new Light("red", 30);

System.out.println("Initial Light:");
System.out.println("Color: " + l1.getColor());
System.out.println("Time: " + l1.getTime() + " seconds");

System.out.println(); tl1.change("green", 45);

System.out.println("Updated Light:");
System.out.println("Color: " + l1.getColor());
System.out.println("Time: " + tl1.getTime() + " seconds");

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

class Calculator { public int


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

public int subtract(int a, int b) { return


a - b;
}

public int multiply(int a, int b) { return


a * b;
}

public int divide(int a, int b) {


if (b != 0) { return a / b;
} else { throw new ArithmeticException("Division by
zero");
}
}
}

public class CalculatorExample { public


static void main(String[] args) {
Calculator calculator = new Calculator();

int sum = calculator.add(5, 3); int


difference = calculator.subtract(10, 4); int
product = calculator.multiply(6, 7); int
quotient = calculator.divide(15, 3);

System.out.println("Sum: " + sum);


System.out.println("Difference: " + difference);
System.out.println("Product: " + product);
System.out.println("Quotient: " + quotient);
}
}
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 Emp { private
double pay;

public Emp(double pay) { this.pay


= pay;
}

public void performWork() {


System.out.println("Employee is working.");
}

public double getPay() { return


pay;
}
}

class Manager extends Emp {


public Manager(double pay) {
super(pay);
}

@Override
public void performWork() {
System.out.println("Manager is managing.");
}

public void recruitEmployee() {


System.out.println("Manager is recruiting.");
}
}

public class Organization { public static


void main(String[] args) {
Emp employee = new Emp(50000.0);
Manager manager = new Manager(70000.0);

System.out.println("Employee's Pay: Rs." + employee.getPay());


employee.performWork();

System.out.println("\nManager's Pay: Rs." + manager.getPay());


manager.performWork(); manager.recruitEmployee();
}
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 {
}

class CustomCircle extends Shape { private


double radius;

public CustomCircle(double rad) { radius


= rad;
}

public double getCustomPerimeter() { return


2 * Math.PI * radius;
}

public double getCustomArea() { return


Math.PI * radius * radius;
}
}

public class ShapeExample { public


static void main(String[] args) {
CustomCircle customCircle = new CustomCircle(5.0);

double customPerimeter = customCircle.getCustomPerimeter();


double customArea = customCircle.getCustomArea();

System.out.println("Circle's Custom Perimeter: " + customPerimeter);


System.out.println("Circle's Custom Area: " + customArea);
}
}
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 Sorter {
void sort(int[] arr);
}

class BubbleSort implements Sorter {

public void sort(int[] arr) { int n =


arr.length; for (int i = 0; i < n - 1;
i++) { for (int j = 0; j < n - i - 1;
j++) { if (arr[j] > arr[j + 1]) { int
temp = arr[j]; arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
}

class SelectionSort implements Sorter {

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;
}
}
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
}
7.
public class Main { public static void
main(String[] args) { int[] array1 = {5,
2, 9, 1, 5, 6}; int[] array2 = {8, 3, 7, 2,
10, 4};

Sorter bubbleSorter = new


BubbleSort(); bubbleSorter.sort(array1);
for (int num : array1) {
System.out.print(num + " ");
}
System.out.println();

Sorter selectionSorter = new


SelectionSort(); selectionSorter.sort(array2);
for (int num : array2) {
System.out.print(num + " ");
}
System.out.println();
}
}
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 w); void
resizeHeight(int h);
}

class Rectangle implements Resizable { private


int width;
private int height;

public Rectangle(int w, int h) { width


= w;
height = h;
}

public void resizeWidth(int w) { width


= w;
}

public void resizeHeight(int h) { height


= h;
}

public void display() {


System.out.println("Rectangle: Width = " + width + ", Height = " + height);
}
}

public class Main { public static void


main(String[] args) {
Rectangle rect = new Rectangle(15, 25);
System.out.println("Original Rectangle:");
rect.display();

rect.resizeWidth(35);
System.out.println("Resized Width:");
rect.display();
8.

rect.resizeHeight(50);
System.out.println("Resized
Height:");
rect.display();
}
}
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.

interface F {
void fly();
}

class Space implements F {


public void fly() {
System.out.println("Spacecraft is flying.");
}
}

class Plane implements F {


public void fly() {
System.out.println("Airplane is flying.");
}
}

class Heli implements F {


public void fly() {
System.out.println("Helicopter is flying.");
}
}

public class Main { public static void


main(String[] args) { F s = new
Space();
s.fly();

F a = new Plane();
a.fly();

F h = new Heli(); h.fly();


}
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.
1. `Addition.java` (inside the `addition` package):

java
package com.addition;

public class Addition { public static


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

2. `Subtraction.java` (inside the `subtraction` package):

java
package com.subtraction;

public class Subtraction { public static


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

3. `Multiplication.java` (inside the `multiplication` package):

java
package com.multiplication;

public class Multiplication { public


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

4. `Division.java` (inside the `division` package):

java
package com.division;

public class Division { public static double divide(int a, int b) { if (b == 0) {


throw new IllegalArgumentException("Division by zero is not allowed"); }
return (double) a / b;
}
}

5. `Main.java` (inside the `com` package): Java.package com;

import com.addition.Addition; import


com.subtraction.Subtraction; import
com.multiplication.Multiplication; import
com.division.Division;

public class Main { public static void


main(String[] args) { int a = 10; int b
= 5;

System.out.println("Addition: " + Addition.add(a, b));


System.out.println("Subtraction: " + Subtraction.subtract(a, b));
System.out.println("Multiplication: " + Multiplication.multiply(a, b));
System.out.println("Division: " + Division.divide(a, b)); }
}

10. Create two different packages to compute bubblesort and selection


sort. Write a Java program to implement sorting functions in a
single class.

1. `BubbleSort.java` (inside the `bubblesort` package):

```java
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;
}
}
} while (swapped);
}
}
```

2. `SelectionSort.java` (inside the `selectionsort` package):

```java
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;
} } if (minIndex
!= i) {
// Swap arr[i] and
arr[minIndex] int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}
}
}
```

3. `Main.java` (inside the default package):


```java import
bubblesort.BubbleSort; import
selectionsort.SelectionSort;

public class Main {


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

System.out.println("Original array:"); printArray(arr);

// Perform bubble sort


BubbleSort.sort(arr);
System.out.println("\nSorted using Bubble Sort:");
printArray(arr);

// Reset the array arr = new int[]{64, 34,


25, 12, 22, 11, 90}; // Perform selection
sort
SelectionSort.sort(arr);
System.out.println("\nSorted using Selection Sort:");
printArray(arr);
}

public static void printArray(int[] arr) {


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

You might also like