0% found this document useful (0 votes)
4 views

Java q1 A2

The document defines a base class 'Vehicle' with attributes for make, model, year, and rental price, along with getter and setter methods. It also includes two subclasses, 'Car' and 'Truck', which extend 'Vehicle' and add specific attributes like number of seats and cargo capacity, respectively. A main class demonstrates the creation of instances of each class and displays their details.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Java q1 A2

The document defines a base class 'Vehicle' with attributes for make, model, year, and rental price, along with getter and setter methods. It also includes two subclasses, 'Car' and 'Truck', which extend 'Vehicle' and add specific attributes like number of seats and cargo capacity, respectively. A main class demonstrates the creation of instances of each class and displays their details.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

// Base Vehicle Class

Class Vehicle {

Private String make;

Private String model;

Private int year;

Private double rentalPricePerDay;

// Constructor

Public Vehicle(String make, String model, int year, double rentalPricePerDay) {

This.make = make;

This.model = model;

This.year = year;

This.rentalPricePerDay = rentalPricePerDay;

// Getter and Setter Methods

Public String getMake() {

Return make;

Public void setMake(String make) {

This.make = make;

Public String getModel() {

Return model;

Public void setModel(String model) {

This.model = model;

Public int getYear() {


Return year;

Public void setYear(int year) {

This.year = year;

Public double getRentalPricePerDay() {

Return rentalPricePerDay;

Public void setRentalPricePerDay(double rentalPricePerDay) {

This.rentalPricePerDay = rentalPricePerDay;

// Method to Display Vehicle Details

Public void displayDetails() {

System.out.println(“Make: “ + make + “, Model: “ + model + “, Year: “ + year + “, Rental Price/Day: $” + rentalPricePerDay);

// Car Subclass

Class Car extends Vehicle {

Private int numSeats;

// Constructor

Public Car(String make, String model, int year, double rentalPricePerDay, int numSeats) {

Super(make, model, year, rentalPricePerDay);

This.numSeats = numSeats;

// Getter and Setter for numSeats

Public int getNumSeats() {

Return numSeats;
}

Public void setNumSeats(int numSeats) {

This.numSeats = numSeats;

@Override

Public void displayDetails() {

Super.displayDetails();

System.out.println(“Number of Seats: “ + numSeats);

// Truck Subclass

Class Truck extends Vehicle {

Private double cargoCapacity;

// Constructor

Public Truck(String make, String model, int year, double rentalPricePerDay, double cargoCapacity) {

Super(make, model, year, rentalPricePerDay);

This.cargoCapacity = cargoCapacity;

// Getter and Setter for cargoCapacity

Public double getCargoCapacity() {

Return cargoCapacity;

Public void setCargoCapacity(double cargoCapacity) {

This.cargoCapacity = cargoCapacity;

@Override

Public void displayDetails() {


Super.displayDetails();

System.out.println(“Cargo Capacity: “ + cargoCapacity + “ tons”);

// Main Class to Test the Functionality

Public class Main {

Public static void main(String[] args) {

// Create an instance of Vehicle

Vehicle vehicle = new Vehicle(“Toyota”, “Corolla”, 2020, 50.0);

System.out.println(“Vehicle Details:”);

Vehicle.displayDetails();

// Create an instance of Car

Car car = new Car(“Honda”, “Civic”, 2021, 60.0, 5);

System.out.println(“\nCar Details:”);

Car.displayDetails();

// Create an instance of Truck

Truck truck = new Truck(“Ford”, “F-150”, 2019, 80.0, 10.5);

System.out.println(“\nTruck Details:”);

Truck.displayDetails();

You might also like