Java Program: Transport Class Implementation
Problem Statement
Write a class Transport with three data members TransportID, Colour and Price. It also
contains the following functions:
• The getData() function is used to input values
• The showData() function is used to display the values
• The setData() function is used to set the values of the data members using parameters
• The getPrice() is used to return the value of Price
The program should create two objects of this class and input values for these objects. The
program should as well display the details of the most expensive vehicle. [5 Marks]
Java Code Implementation
import java.util.Scanner;
class Transport {
private String transportID;
private String colour;
private double price;
public void getData() {
Scanner input = new Scanner(System.in);
System.out.print("Enter Transport ID: ");
transportID = input.nextLine();
System.out.print("Enter Colour: ");
colour = input.nextLine();
System.out.print("Enter Price: ");
price = input.nextDouble();
}
public void showData() {
System.out.println("Transport ID: " + transportID);
System.out.println("Colour: " + colour);
System.out.println("Price: " + price);
}
public void setData(String id, String col, double pr) {
transportID = id;
colour = col;
price = pr;
}
public double getPrice() {
return price;
}
}
public class TestTransport {
public static void main(String[] args) {
Transport t1 = new Transport();
Transport t2 = new Transport();
System.out.println("Enter details for Transport 1:");
t1.getData();
System.out.println("\nEnter details for Transport 2:");
t2.getData();
System.out.println("\nDetails of the most expensive vehicle:");
if (t1.getPrice() > t2.getPrice()) {
t1.showData();
} else {
t2.showData();
}
}
}