[go: up one dir, main page]

0% found this document useful (0 votes)
3 views4 pages

Labtask 03

The document contains Java code defining a vehicle hierarchy with performance metrics for cars and trucks, including methods for calculating fuel efficiency and displaying performance ratings. It also includes a communication interface with basic, voice, and text communication implementations. The main classes demonstrate the creation of vehicle objects and communication instances, showcasing their functionalities.

Uploaded by

alliyafatima78
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

Labtask 03

The document contains Java code defining a vehicle hierarchy with performance metrics for cars and trucks, including methods for calculating fuel efficiency and displaying performance ratings. It also includes a communication interface with basic, voice, and text communication implementations. The main classes demonstrate the creation of vehicle objects and communication instances, showcasing their functionalities.

Uploaded by

alliyafatima78
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

package yum;

import java.util.*;

interface PerformanceMetrics {
double calculateFuelEfficiency();
String displayPerformanceRating();
}

class Vehicle {
protected String make;
protected String model;
protected int year;

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


this.make = make;
this.model = model;
this.year = year;
}

public void displayDetails() {


System.out.println("Vehicle: " + year + " " + make + " " + model);
}
}

class Car extends Vehicle implements PerformanceMetrics {


private double distanceTraveled;
private double fuelConsumed;

public Car(String make, String model, int year, double distanceTraveled,


double fuelConsumed) {
super(make, model, year);
this.distanceTraveled = distanceTraveled;
this.fuelConsumed = fuelConsumed;
}

public double calculateFuelEfficiency() {


return distanceTraveled / fuelConsumed;
}

public String displayPerformanceRating() {


double efficiency = calculateFuelEfficiency();
if (efficiency >= 15) {
return "Excellent";
} else if (efficiency >= 10) {
return "Good";
} else {
return "Poor";
}
}
}

class Truck extends Vehicle implements PerformanceMetrics {


private double distanceTraveled;
private double fuelConsumed;
private double cargoWeight;

public Truck(String make, String model, int year, double


distanceTraveled, double fuelConsumed, double cargoWeight) {
super(make, model, year);
this.distanceTraveled = distanceTraveled;
this.fuelConsumed = fuelConsumed;
this.cargoWeight = cargoWeight;
}

public double calculateFuelEfficiency() {


double weightFactor = 1 + (cargoWeight / 1000);
return (distanceTraveled / fuelConsumed) / weightFactor;
}

public String displayPerformanceRating() {


double efficiency = calculateFuelEfficiency();
if (efficiency >= 8) {
return "Excellent";
} else if (efficiency >= 5) {
return "Average";
} else {
return "Poor";
}
}
}

public class Main {


public static void main(String[] args) {
List<Vehicle> vehicles = new ArrayList<>();
vehicles.add(new Car("Toyota", "Corolla", 2020, 600, 40));
vehicles.add(new Car("Honda", "Civic", 2018, 400, 35));
vehicles.add(new Truck("Ford", "F-150", 2019, 500, 60, 1500));
vehicles.add(new Truck("RAM", "1500", 2021, 450, 70, 1000));

for (Vehicle v : vehicles) {


v.displayDetails();
if (v instanceof PerformanceMetrics) {
PerformanceMetrics pm = (PerformanceMetrics) v;
System.out.printf("Fuel Efficiency: %.2f km/l\n",
pm.calculateFuelEfficiency());
System.out.println("Performance Rating: " +
pm.displayPerformanceRating());
}
}
}
}

OUTPUT

Q2
package yum;
interface Communicator {
void sendMessage(String message);
}

interface VoiceCommunicator extends Communicator {


void call(String phoneNumber);
}

interface TextCommunicator extends Communicator {


void sendText(String phoneNumber);
}

class BasicCommunicator implements Communicator {


public void sendMessage(String message) {
System.out.println("Basic message sent: " + message);
}
}

class AdvancedVoiceCommunicator implements VoiceCommunicator {

public void sendMessage(String message) {


System.out.println("VoiceCommunicator sending message: " +
message);
}
public void call(String phoneNumber) {
System.out.println("Calling " + phoneNumber + " via voice call...");
}
}

class AdvancedTextCommunicator implements TextCommunicator {


public void sendMessage(String message) {
System.out.println("TextCommunicator sending message: " +
message);
}
public void sendText(String phoneNumber) {
System.out.println("Sending text message to " + phoneNumber +
"...");
}
}

public class Main{


public static void main(String[] args) {
Communicator basic = new BasicCommunicator();
Communicator voice = new AdvancedVoiceCommunicator();
Communicator text = new AdvancedTextCommunicator();

basic.sendMessage("Hello from BasicCommunicator!");

voice.sendMessage("Hello from AdvancedVoiceCommunicator!");


if (voice instanceof VoiceCommunicator) {
((VoiceCommunicator) voice).call("+1234567890");
}

text.sendMessage("Hello from AdvancedTextCommunicator!");


if (text instanceof TextCommunicator) {
((TextCommunicator) text).sendText("+9876543210");
}
}
}

output

You might also like