2 question
Class Degree {
Void getDegree() {
System.out.println(“I got a degree”);
Class Undergraduate extends Degree {
Void getDegree() {
System.out.println(“I am an Undergraduate”);
Class Postgraduate extends Degree {
Void getDegree() {
System.out.println(“I am a Postgraduate”);
Public class Main {
Public static void main(String[] args) {
Degree degree = new Degree();
Degree.getDegree();
Undergraduate undergrad = new Undergraduate();
Undergrad.getDegree();
Postgraduate postgrad = new Postgraduate();
Postgrad.getDegree();
3rd question
Class Computer {
// Data members
Int wordSize;
Int memorySize;
Int storageSize;
Int speed;
// Default constructor
Public Computer() {
// Default values
wordSize = 32;
memorySize = 512;
storageSize = 256;
speed = 2000;
// Parameterized constructor
Public Computer(int wordSize, int memorySize, int storageSize, int speed) {
This.wordSize = wordSize;
This.memorySize = memorySize;
This.storageSize = storageSize;
This.speed = speed;
}
// Display function
Public void display() {
System.out.println(“Word Size: “ + wordSize + “ bits”);
System.out.println(“Memory Size: “ + memorySize + “ megabytes”);
System.out.println(“Storage Size: “ + storageSize + “ megabytes”);
System.out.println(“Speed: “ + speed + “ MHz”);
Class Laptop extends Computer {
// Additional data members
Int length;
Int width;
Int height;
Int weight;
// Default constructor
Public Laptop() {
Super(); // Call base class default constructor
// Default values for Laptop
Length = 13;
Width = 8;
Height = 0;
Weight = 2;
// Parameterized constructor
Public Laptop(int wordSize, int memorySize, int storageSize, int speed,
Int length, int width, int height, int weight) {
Super(wordSize, memorySize, storageSize, speed); // Call base class constructor
This.length = length;
This.width = width;
This.height = height;
This.weight = weight;
// Override display function to include Laptop specific data
@Override
Public void display() {
Super.display(); // Call base class display function
System.out.println(“Length: “ + length + “ inches”);
System.out.println(“Width: “ + width + “ inches”);
System.out.println(“Height: “ + height + “ inches”);
System.out.println(“Weight: “ + weight + “ pounds”);
// Example usage
Public class Main {
Public static void main(String[] args) {
Computer desktop = new Computer();
Desktop.display();
System.out.println();
Laptop myLaptop = new Laptop(64, 1024, 512, 3000, 15, 10, 1, 4);
myLaptop.display();
4th question
Class PrintIntegerAndCharacter {
// Method with parameters (int n, char c)
Public void print(int n, char c) {
System.out.println(“Integer: “ + n);
System.out.println(“Character: “ + c);
// Method with parameters (char c, int n)
Public void print(char c, int n) {
System.out.println(“Character: “ + c);
System.out.println(“Integer: “ + n);
// Example usage
Public class Main {
Public static void main(String[] args) {
PrintIntegerAndCharacter printer = new PrintIntegerAndCharacter();
// Call the first method
Printer.print(5, ‘A’);
System.out.println(); // Adding a line break for clarity
// Call the second method
Printer.print(‘B’, 10);
5th question
Class Student {
// Data members
Private String name;
Private int age;
Private String address;
// Constructor with default values
Public Student() {
This.name = “unknown”;
This.age = 0;
This.address = “not available”;
// Method to set name and age
Public void setInfo(String name, int age) {
This.name = name;
This.age = age;
}
// Method to set name, age, and address
Public void setInfo(String name, int age, String address) {
This.name = name;
This.age = age;
This.address = address;
// Method to display student information
Public void displayInfo() {
System.out.println(“Name: “ + name);
System.out.println(“Age: “ + age);
System.out.println(“Address: “ + address);
System.out.println();
// Example usage
Public class Main {
Public static void main(String[] args) {
Student[] students = new Student[10];
// Creating 10 students with default values
For (int I = 0; I < 10; i++) {
Students[i] = new Student();
// Setting information for each student using both methods
For (int I = 0; I < 10; i++) {
If (I % 2 == 0) {
Students[i].setInfo(“Student” + (I + 1), 20 + i);
} else {
Students[i].setInfo(“Student” + (I + 1), 20 + I, “Address” + (I + 1));
// Displaying information for each student
For (int I = 0; I < 10; i++) {
Students[i].displayInfo();
6th question
Class Vehicle {
// Properties
String make;
String model;
Int year;
String fuelType;
// Constructor
Public Vehicle(String make, String model, int year, String fuelType) {
This.make = make;
This.model = model;
This.year = year;
This.fuelType = fuelType;
}
// Method to calculate fuel efficiency (dummy implementation)
Public double calculateFuelEfficiency() {
// Dummy calculation for illustration purposes
Return 20.0;
// Method to calculate distance traveled (dummy implementation)
Public double calculateDistanceTraveled(double fuelConsumed) {
// Dummy calculation for illustration purposes
Return fuelConsumed * calculateFuelEfficiency();
// Method to get maximum speed (dummy implementation)
Public int getMaxSpeed() {
// Dummy value for illustration purposes
Return 120;
Class Truck extends Vehicle {
// Additional properties specific to Truck
// Constructor
Public Truck(String make, String model, int year, String fuelType) {
Super(make, model, year, fuelType);
// Additional initialization specific to Truck
}
Class Car extends Vehicle {
// Additional properties specific to Car
// Constructor
Public Car(String make, String model, int year, String fuelType) {
Super(make, model, year, fuelType);
// Additional initialization specific to Car
Class Motorcycle extends Vehicle {
// Additional properties specific to Motorcycle
// Constructor
Public Motorcycle(String make, String model, int year, String fuelType) {
Super(make, model, year, fuelType);
// Additional initialization specific to Motorcycle
// Example usage
Public class Main {
Public static void main(String[] args) {
// Create instances of each vehicle type
Truck myTruck = new Truck(“Ford”, “F-150”, 2022, “Gasoline”);
Car myCar = new Car(“Toyota”, “Camry”, 2023, “Hybrid”);
Motorcycle myMotorcycle = new Motorcycle(“Harley-Davidson”, “Sportster”, 2021, “Gasoline”);
// Demonstrate method calls
System.out.println(“Truck Fuel Efficiency: “ + myTruck.calculateFuelEfficiency() + “ mpg”);
System.out.println(“Car Distance Traveled: “ + myCar.calculateDistanceTraveled(10) + “ miles”);
System.out.println(“Motorcycle Max Speed: “ + myMotorcycle.getMaxSpeed() + “ mph”);