[go: up one dir, main page]

0% found this document useful (0 votes)
23 views2 pages

Transport Class Implementation

The document describes a Java program that implements a Transport class with attributes for TransportID, Colour, and Price, along with methods for data input, display, and setting values. It creates two Transport objects, gathers user input for their details, and displays the details of the more expensive vehicle. The program demonstrates basic object-oriented principles in Java, including encapsulation and method usage.

Uploaded by

girishmannick
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)
23 views2 pages

Transport Class Implementation

The document describes a Java program that implements a Transport class with attributes for TransportID, Colour, and Price, along with methods for data input, display, and setting values. It creates two Transport objects, gathers user input for their details, and displays the details of the more expensive vehicle. The program demonstrates basic object-oriented principles in Java, including encapsulation and method usage.

Uploaded by

girishmannick
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/ 2

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();
}
}
}

You might also like