Develop a Java Application with ‘Vehicle’ Class with vehicle type, vehicle number, engine number, chassis number, top speed, number of passengers, number of gears. Generate a report for the vehicles. Implement the concept using Interfaces.
Question
Develop a Java Application with ‘Vehicle’ Class with vehicle type, vehicle number, engine number, chassis number, top speed, number of passengers, number of gears. Generate a report for the vehicles. Implement the concept using Interfaces.
Solution
Sure, here is a simple implementation of a Java application with a Vehicle class and an interface to generate a report for the vehicles.
First, let's define an interface VehicleReport:
public interface VehicleReport {
void generateReport();
}
Next, we'll create the Vehicle class that implements the VehicleReport interface:
public class Vehicle implements VehicleReport {
private String vehicleType;
private String vehicleNumber;
private String engineNumber;
private String chassisNumber;
private int topSpeed;
private int numberOfPassengers;
private int numberOfGears;
public Vehicle(String vehicleType, String vehicleNumber, String engineNumber, String chassisNumber, int topSpeed, int numberOfPassengers, int numberOfGears) {
this.vehicleType = vehicleType;
this.vehicleNumber = vehicleNumber;
this.engineNumber = engineNumber;
this.chassisNumber = chassisNumber;
this.topSpeed = topSpeed;
this.numberOfPassengers = numberOfPassengers;
this.numberOfGears = numberOfGears;
}
@Override
public void generateReport() {
System.out.println("Vehicle Type: " + vehicleType);
System.out.println("Vehicle Number: " + vehicleNumber);
System.out.println("Engine Number: " + engineNumber);
System.out.println("Chassis Number: " + chassisNumber);
System.out.println("Top Speed: " + topSpeed);
System.out.println("Number of Passengers: " + numberOfPassengers);
System.out.println("Number of Gears: " + numberOfGears);
}
}
Finally, let's create a Main class to test our application:
public class Main {
public static void main(String[] args) {
Vehicle car = new Vehicle("Car", "1234", "5678", "9012", 200, 5, 6);
car.generateReport();
}
}
In this application, we first define an interface VehicleReport with a method generateReport(). Then we create a Vehicle class that implements this interface. The Vehicle class has several fields to store information about a vehicle, and it provides an implementation for the generateReport() method to print out these details. In the Main class, we create a new Vehicle object and call its generateReport() method to generate a report for this vehicle.
Similar Questions
Develop a Java Application with ‘Vehicle’ Class with vehicle type, vehicle number, as member. Inherit the classes bicycle, car, bike, and lorry from vehicle class. Consider car and bike only as passenger vehicle. Get inputs engine number, chassis number, top speed, number of passengers, number of gears for Passenger vehicle, and if is not a passenger vehicle, get inputs total amount of load to carry for the respective inherited classes. Generate a report for the vehicles. Implement the concept using inheritance and method overriding.
evelop a Java Application with ‘Vehicle’ Class with vehicle type, vehicle number, engine number, chassis number, top speed, number of passengers, number of gears. Generate a report for the vehicles. Implement the concept using Interfaces.
This assignment will assess your skills and knowledge on implementing interfaces to define contracts and enforce common behavior across multiple classes.Context: You are tasked with developing a software application for a car rental agency. The application needs to handle different types of vehicles, including cars, motorcycles, and trucks. To enforce a common behavior and ensure consistency among these vehicle types, you decide to utilize interfaces. Additionally, you want to incorporate various small modules into the question to make it engaging and interactive for the developers.For this assignment, Compose a Java programming question that incorporates the following modules:Design and implement an interface named "Vehicle" that includes methods for retrieving the vehicle's make, model, and year of manufacture.Develop a class named "Car" that implements the Vehicle and "CarVehicle" interfaces. This interface includes additional methods for setting and retrieving the number of doors and the fuel type (petrol, diesel, or electric).Construct a class named "Motorcycle" that also implements Vehicle and the "MotorVehicle" interfaces. This interface should have methods for setting and retrieving the number of wheels and the type of motorcycle (sport, cruiser, or off-road).Generate a class named "Truck" that implements the Vehicle and "TruckVehicle" interfaces. This interface should include methods for setting and retrieving the cargo capacity (in tons) and the transmission type (manual or automatic).Integrate all the classes into a main program that allows the user to create objects of different vehicle types, provide relevant information, and display the details of each vehicle.You will be accessed based on the following criteria:The interface design in the Vehicle Information System provides a contract specifying the methods for retrieving and setting vehicle details, ensuring a consistent structure for different vehicle types.The class implementation in the Vehicle Information System translates the interface specifications into concrete implementations for different vehicle types, enabling the storage and retrieval of specific attributes and behaviors.The main program in the Vehicle Information System allows users to interactively create, input, and display details of various vehicle objects, providing a comprehensive interface for managing and accessing vehicle information.The code quality in the Vehicle Information System emphasizes readability, maintainability, and adherence to best practices, ensuring a clean and well-structured codebase that promotes efficient development and future scalability.The error handling mechanism in the Vehicle Information System effectively manages and gracefully handles potential exceptions or invalid user inputs, ensuring a robust and stable application experience.The documentation in the Vehicle Information System provides clear and comprehensive explanations of classes, methods, and their functionalities, aiding developers in understanding and maintaining the codebase effectively.Remember to use appropriate variable names and follow coding best practices.
វគ្គសិក្សានេះណែនាំសិស្សឱ្យស្គាល់កម្មវិធី Java Object-Oriented Programming (OOP) និងណែនាំពួកគេក្នុងការអភិវឌ្ឍន៍ប្រព័ន្ធកក់រថយន្ត។ សិស្សនឹងរៀនពីគោលគំនិត OOP សំខាន់ៗដូចជា inheritance, polymorphism, encapsulation, objects, classes, interface, and more។ សិស្សនឹងមាន project ចុងក្រោយសង់ Car Booking System!
Problem StatementKaniska is tasked with creating a program that allows users to input information about different types of vehicles and calculate the time it takes for these vehicles to travel a given distance at their maximum speeds. The program will support three types of vehicles: Cars, Bicycles, and Boats.Your task is to implement the Vehicle class as the base class for these three vehicle types. The Vehicle class should have the following properties and functionalities:Properties:numOfWheels (integer): representing the number of wheels of the vehicle.maxSpeed (floating-point): representing the maximum speed of the vehicle.Functionalities:A default constructor to initialize numOfWheels and maxSpeed to 0.A virtual function setNumOfWheels(int wheels) to set the number of wheels of the vehicle.A virtual function setMaxSpeed(float speed) to set the maximum speed of the vehicle.A virtual function getNumOfWheels() to get the number of wheels on the vehicle.A virtual function getMaxSpeed() to get the maximum speed of the vehicle.A virtual function timeToTravel(float distance) that calculates and returns the time it takes for the vehicle to travel the given distance at its maximum speed.Implement three derived classes, Car, Bicycle, and Boat, which inherit from the Vehicle class. Each derived class should override the timeToTravel(float distance) function with the appropriate calculation for the specific vehicle type. The Car and Bicycle classes should calculate the time based on their maximum speed in mph, while the Boat class should consider the maximum speed in knots and convert it to mph using the formula 1 knot = 1.151 mph.In the main function, create instances of each vehicle type (Car, Bicycle, and Boat) and take user input for the number of wheels and maximum speed for each vehicle. Then, the user will input a distance, and the program will display the details of each vehicle and the time it takes for each vehicle to travel the given distance at its maximum speed.Note: This kind of question will help in clearing Capgemini recruitment.Input format :The first line consists of the number of wheels and the maximum speed of the car, separated by a single space.The third line consists of the number of wheels and the maximum speed of the bicycle, separated by a single space.The fifth line consists of the maximum speed of the boat (in knots).The sixth line consists of the distance to travel (in miles).Output format :The program outputs the details of the car, bicycle, and boat, including the number of wheels and the maximum speed for each vehicle, as well as the time it would take to travel the specified distance at maximum speed.The output includes the vehicle type, number of wheels, maximum speed, and time taken to travel the distance.The time taken is displayed in hours.Refer to the sample input and output for format specifications.Code constraints :The number of wheels (numOfWheels) for each vehicle is a positive integer.The maximum speed (maxSpeed) for each vehicle is a positive floating-point number.The distance to travel is a positive floating-point number.The maximum speed of the Boat (in knots) should be converted to mph using the formula: 1 knot = 1.151 mph.Sample test cases :Input 1 :4 602 153050Output 1 :Vehicle Details:Car has 4 wheels and can go up to 60 mph. It would take 0.833333 hours to travel 50 miles at maximum speed.Bicycle has 2 wheels and can go up to 15 mph. It would take 3.33333 hours to travel 50 miles at maximum speed.Boat has 0 wheels and can go up to 30 knots. It would take 1.44802 hours to travel 50 miles at maximum speed.Input 2 :4 902 203575Output 2 :Vehicle Details:Car has 4 wheels and can go up to 90 mph. It would take 0.833333 hours to travel 75 miles at maximum speed.Bicycle has 2 wheels and can go up to 20 mph. It would take 3.75 hours to travel 75 miles at maximum speed.Boat has 0 wheels and can go up to 35 knots. It would take 1.86174 hours to travel 75 miles at maximum speed.Note :The program will be evaluated only after the “Submit Code” is clicked.Extra spaces and new line characters in the program output will result in the failure of the test case.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.