Labsheet 2
1.
// Shape.java
abstract class Shape {
abstract double area();
abstract double perimeter();
}
// Circle.java
class Circle extends Shape {
private double radius;
Circle(double radius) {
this.radius = radius;
}
@Override
double area() {
return Math.PI * radius * radius;
}
@Override
double perimeter() {
return 2 * Math.PI * radius;
}
}
// Rectangle.java
class Rectangle extends Shape {
private double width, height;
Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
@Override
double area() {
return width * height;
}
@Override
double perimeter() {
return 2 * (width + height);
}
}
// Triangle.java
class Triangle extends Shape {
private double a, b, c; // sides of the triangle
Triangle(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
}
@Override
double area() {
double s = (a + b + c) / 2; // semi-perimeter
return Math.sqrt(s * (s - a) * (s - b) * (s - c)); // Heron's formula
}
@Override
double perimeter() {
return a + b + c;
}
}
// Main.java
public class Main {
public static void main(String[] args) {
Shape circle = new Circle(5);
Shape rectangle = new Rectangle(4, 6);
Shape triangle = new Triangle(3, 4, 5);
System.out.println("Circle Area: " + circle.area());
System.out.println("Circle Perimeter: " + circle.perimeter());
System.out.println("Rectangle Area: " + rectangle.area());
System.out.println("Rectangle Perimeter: " + rectangle.perimeter());
System.out.println("Triangle Area: " + triangle.area());
System.out.println("Triangle Perimeter: " + triangle.perimeter());
}
}
2.
// Vehicle.java
class Vehicle {
protected String make;
protected String model;
Vehicle(String make, String model) {
this.make = make;
this.model = model;
}
void displayInfo() {
System.out.println("Make: " + make + ", Model: " + model);
}
}
// Car.java
class Car extends Vehicle {
private int year;
Car(String make, String model, int year) {
super(make, model);
this.year = year;
}
void displayCarInfo() {
displayInfo();
System.out.println("Year: " + year);
}
}
// Main.java
public class Main {
public static void main(String[] args) {
Car car = new Car("Toyota", "Camry", 2020);
car.displayCarInfo();
}
}
3.
// Bank.java
interface Bank {
void deposit(double amount);
void withdraw(double amount);
}
// SavingsAccount.java
class SavingsAccount implements Bank {
private double balance;
@Override
public void deposit(double amount) {
balance += amount;
System.out.println("Deposited: " + amount + ", New Balance: " + balance);
}
@Override
public void withdraw(double amount) {
if (amount <= balance) {
balance -= amount;
System.out.println("Withdrawn: " + amount + ", New Balance: " + balance);
} else {
System.out.println("Insufficient funds.");
}
}
}
// CurrentAccount.java
class CurrentAccount implements Bank {
private double balance;
@Override
public void deposit(double amount) {
balance += amount;
System.out.println("Deposited: " + amount + ", New Balance: " + balance);
}
@Override
public void withdraw(double amount) {
balance -= amount;
System.out.println("Withdrawn: " + amount + ", New Balance: " + balance);
}
}
// Main.java
public class Main {
public static void main(String[] args) {
SavingsAccount savings = new SavingsAccount();
savings.deposit(500);
savings.withdraw(200);
CurrentAccount current = new CurrentAccount();
current.deposit(1000);
current.withdraw(1200);
}
}
4.
interface A
{
void myMethod();
}
class B
{
public void myMethod()
{
System.out.println("My Method");
}}
class C extends B implements A
{
}
class MainClass
{
public static void main(String[] args)
{
A a = new C();
a.myMethod();
}
5.
// Pen.java
abstract class Pen {
abstract void write();
abstract void refill();
}
// FountainPen.java
class FountainPen extends Pen {
@Override
void write() {
System.out.println("Writing with a fountain pen.");
}
@Override
void refill() {
System.out.println("Refilling the fountain pen.");
}
}
// Main.java
public class Main {
public static void main(String[] args) {
Pen pen = new FountainPen();
pen.write();
pen.refill();
}
}
6.
// BaseClass.java
class BaseClass {
static final String STATIC_VAR = "Static Variable";
void display() {
System.out.println("BaseClass display method.");
}
}
// SubClass.java
class SubClass extends BaseClass {
String instanceVar;
SubClass(String instanceVar) {
this.instanceVar = instanceVar;
}
void display() {
super.display(); // Call the method from BaseClass
System.out.println("SubClass display method. Instance Variable: " + this.instanceVar);
}
static void staticMethod() {
System.out.println(STATIC_VAR); // Accessing static variable from BaseClass
}
}
// Main.java
public class Main {
public static void main(String[] args) {
SubClass obj = new SubClass("Instance Variable Value");
obj.display();
SubClass.staticMethod();
}
}