java labbb
java labbb
java labbb
*;
import java.util.*;
abstract class Shape {
public abstract double calculateArea();
public abstract double calculatePerimeter();
}
class Circle extends Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public double calculateArea() {
return Math.PI * radius * radius;
}
@Override
public double calculatePerimeter() {
return 2 * Math.PI * radius;
}
}
class Triangle extends Shape {
private double side1, side2, side3;
public Triangle(double side1, double side2, double side3) {
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
@Override
public double calculateArea() {
// Heron's formula for area of a triangle
double s = (side1 + side2 + side3) / 2;
return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
}
@Override
public double calculatePerimeter() {
return side1 + side2 + side3;
}
}
public class Main {
public static void main(String[] args) {
Circle circle = new Circle(5);
Triangle triangle = new Triangle(3, 4, 5);
System.out.println("Circle - Area: " + circle.calculateArea() + ", Perimeter: " + circle.calculatePerimeter());
// Displaying area and perimeter for Triangle
System.out.println("Triangle - Area: " + triangle.calculateArea() + ", Perimeter: " +
triangle.calculatePerimeter());
}
}
7) import java.io.*;
import java.util.*;
interface Resizable {
void resizeWidth(int width);
void resizeHeight(int height);
}
class Rectangle implements Resizable {
private int width;
private int height;
// Constructor for Rectangle
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 void display() {
System.out.println("Rectangle - Width: " + width + ", Height: " + height);
}
}
public class ResizableDemo {
public static void main(String[] args) {
Rectangle rectangle = new Rectangle(10, 5);
System.out.println("Original Rectangle:");
rectangle.display();
rectangle.resizeWidth(15);
rectangle.resizeHeight(8);
System.out.println("\nResized Rectangle:");
rectangle.display();
}
}
8) import java.io.*;
import java.util.*;
class OuterClass {
public void display() {
System.out.println("OuterClass display function");
}
class InnerClass {
public void display() {
System.out.println("InnerClass display function");
}
}
}
public class Main {
public static void main(String[] args) {
OuterClass outerObject = new OuterClass();
outerObject.display();
OuterClass.InnerClass innerObject = outerObject.new InnerClass();
innerObject.display();
}}
9) import java.io.*;
import java.util.*;
class DivisionByZeroException extends Exception {
public DivisionByZeroException(String message) {
super(message);
}
}
public class Main {
public static void main(String[] args) {
try {
divideNumbers(10, 0);
} catch (DivisionByZeroException e) {
System.out.println("Error: " + e.getMessage());
} finally {
System.out.println("Finally block executed");
}
}
private static void divideNumbers(int numerator, int denominator) throws DivisionByZeroException {
try {
int result = numerator / denominator;
System.out.println("Result of division: " + result);
} catch (ArithmeticException e) {
// Catch the ArithmeticException (division by zero) and throw a custom exception
throw new DivisionByZeroException("Cannot divide by zero");
}
}
}
10.1)
package mypack;
public class MyPackageClass {
public void displayMessage() {
System.out.println("This is a message from MyPackageClass in the mypack package.");
}
}
10.2)
import mypack.MyPackageClass;
public class MyMainClass {
public static void main(String[] args) {
MyPackageClass myPackageObject = new MyPackageClass();
myPackageObject.displayMessage();
}
}
11) class MyRunnable implements Runnable {
private String threadName;
public MyRunnable(String name) {
this.threadName = name;
}
public void run() {
try {
for (int i = 0; i < 5; i++) {
System.out.println(threadName + ": Count " + i);
Thread.sleep(500);
}
} catch (InterruptedException e) {
System.out.println(threadName + " interrupted.");
}
System.out.println(threadName + " exiting."); } }
public class RunnableThreadExample {
public static void main(String[] args) {
Thread thread1 = new Thread(new MyRunnable("Thread 1"));
Thread thread2 = new Thread(new MyRunnable("Thread 2"));
thread1.start();
thread2.start();
}}
12) class MyThread extends Thread {
public MyThread(String threadName) {
super(threadName);
start();
}
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + ": Count " + i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
System.out.println(Thread.currentThread().getName() + " interrupted.");
}
}
System.out.println(Thread.currentThread().getName() + " exiting.");
}
}
public class Main {
public static void main(String[] args) {
System.out.println("Main thread started.");