Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

QSTN 1

public class Shape {

public double getArea() {

return 0;

public class Rectangle extends Shape {

private double length;

private double width;

public Rectangle(double length, double width) {

this.length = length;

this.width = width; }

public double getArea() {

return length * width;

public class Main {

public static void main(String[] args) {

Rectangle rectangle = new Rectangle(2,8);

double area = rectangle.getArea();

System.out.println("The area of the rectangle is: " + area);

QSTN 2

class Vehicle {

public void drive() {

System.out.println("Drive a car");
class Car extends Vehicle {

public void drive() {

System.out.println("Repairing a car");

public class Main {

public static void main(String[] args) {

Vehicle vehicle = new Vehicle();

Car car = new Car();

vehicle.drive();

car.drive();

QSTN 3
class Vehicle {

protected String make;

protected String model;

protected int year;

protected String fuelType;

public Vehicle(String make, String model, int year, String fuelType) {

this.make = make;

this.model = model;

this.year = year;

this.fuelType = fuelType;

public double calculateFuelEfficiency(double fuelConsumption) {

return 100 / fuelConsumption; }

public double calculateDistance(double fuelConsumed) {


return fuelConsumed * calculateFuelEfficiency(fuelConsumed);

public int getMaxSpeed() {

return 0;

class Truck extends Vehicle {

private int cargoCapacity; // in tons

public Truck(String make, String model, int year, String fuelType, int cargoCapacity) {

super(make, model, year, fuelType);

this.cargoCapacity = cargoCapacity;

public int getMaxSpeed() {

return 120; }

class Car extends Vehicle {

private int numberOfSeats;

public Car(String make, String model, int year, String fuelType, int numberOfSeats) {

super(make, model, year, fuelType);

this.numberOfSeats = numberOfSeats;

public int getMaxSpeed() {

return 180; }

}
class Motorcycle extends Vehicle {

private boolean hasSideCar;

public Motorcycle(String make, String model, int year, String fuelType, boolean hasSideCar) {

super(make, model, year, fuelType);

this.hasSideCar = hasSideCar;

public int getMaxSpeed() {

return 200;

public class Main {

public static void main(String[] args) {

Truck truck = new Truck("Ford", "F-150", 2022, "Diesel", 2);

Car car = new Car("Toyota", "Camry", 2023, "Petrol", 5);

Motorcycle motorcycle = new Motorcycle("Honda", "CBR600RR", 2024, "Petrol", false);

System.out.println("Truck Max Speed: " + truck.getMaxSpeed() + " km/h");

System.out.println("Car Max Speed: " + car.getMaxSpeed() + " km/h");

System.out.println("Motorcycle Max Speed: " + motorcycle.getMaxSpeed() + " km/h");

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

public double getPerimeter() {

return 2 * Math.PI * radius;

public double getArea() {

return Math.PI * radius * radius;

public class Main {

public static void main(String[] args) {

Circle circle = new Circle(5);

System.out.println("Perimeter of the circle: " + circle.getPerimeter());

System.out.println("Area of the circle: " + circle.getArea());

You might also like