In a car-rental showroom, every car is identified by a car-ID. There are two categories of cars in the showroom and they are labelled XX and YY. The mileage of car XX is 15 kmpl and YY is 20 kmpl. Write a Java program that obtains the monthly fuel consumption for a specified number of cars (user-defined) and determines which car has travelled the longest distance. Create a class Car and perform the above task using array of objects.Input form :Enter number of cars: 3Enter car-ID: 121Enter car label: XXEnter car’s monthly fuel consumption in litres: 10Enter car-ID: 122Enter car label: YYEnter car’s monthly fuel consumption in litres: 10Enter car-ID: 123Enter car label: YYEnter car’s monthly fuel consumption in litres: 5Output form :The car-ID of the car that has travelled longest distance is: 122The car label of the car that has travelled longest distance is YYDistance travelled: 200
Question
In a car-rental showroom, every car is identified by a car-ID. There are two categories of cars in the showroom and they are labelled XX and YY. The mileage of car XX is 15 kmpl and YY is 20 kmpl. Write a Java program that obtains the monthly fuel consumption for a specified number of cars (user-defined) and determines which car has travelled the longest distance. Create a class Car and perform the above task using array of objects.Input form :Enter number of cars: 3Enter car-ID: 121Enter car label: XXEnter car’s monthly fuel consumption in litres: 10Enter car-ID: 122Enter car label: YYEnter car’s monthly fuel consumption in litres: 10Enter car-ID: 123Enter car label: YYEnter car’s monthly fuel consumption in litres: 5Output form :The car-ID of the car that has travelled longest distance is: 122The car label of the car that has travelled longest distance is YYDistance travelled: 200
Solution
Here is the Java program that performs the task:
import java.util.Scanner;
class Car {
int carID;
String carLabel;
int monthlyFuelConsumption;
int distanceTravelled;
Car(int carID, String carLabel, int monthlyFuelConsumption) {
this.carID = carID;
this.carLabel = carLabel;
this.monthlyFuelConsumption = monthlyFuelConsumption;
if (carLabel.equals("XX")) {
this.distanceTravelled = monthlyFuelConsumption * 15;
} else if (carLabel.equals("YY")) {
this.distanceTravelled = monthlyFuelConsumption * 20;
}
}
}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter number of cars: ");
int n = scanner.nextInt();
Car[] cars = new Car[n];
for (int i = 0; i < n; i++) {
System.out.println("Enter car-ID: ");
int carID = scanner.nextInt();
System.out.println("Enter car label: ");
String carLabel = scanner.next();
System.out.println("Enter car’s monthly fuel consumption in litres: ");
int monthlyFuelConsumption = scanner.nextInt();
cars[i] = new Car(carID, carLabel, monthlyFuelConsumption);
}
int maxDistance = 0;
int maxCarID = 0;
String maxCarLabel = "";
for (int i = 0; i < n; i++) {
if (cars[i].distanceTravelled > maxDistance) {
maxDistance = cars[i].distanceTravelled;
maxCarID = cars[i].carID;
maxCarLabel = cars[i].carLabel;
}
}
System.out.println("The car-ID of the car that has travelled longest distance is: " + maxCarID);
System.out.println("The car label of the car that has travelled longest distance is: " + maxCarLabel);
System.out.println("Distance travelled: " + maxDistance);
}
}
This program first creates a Car class with fields for carID, carLabel, monthlyFuelConsumption, and distanceTravelled. The Car constructor calculates the distanceTravelled based on the carLabel and monthlyFuelConsumption.
In the main method, the program prompts the user to enter the number of cars, and then for each car, it prompts the user to enter the carID, carLabel, and monthlyFuelConsumption. It creates a Car object for each car and stores them in an array.
The program then iterates over the array of Car objects to find the car that has travelled the longest distance. It prints the carID, carLabel, and distanceTravelled of the car that has travelled the longest distance.
Similar Questions
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.Input:CarHRDQ245652WVC12342H2X1234535kph45OutputCarHRDQ245652WVC12342H2X1234535kph45
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. Input 1: Car (VEHICLE TYPE) HRDQ2456 (VEHICLE NUMBER) 52WVC1234 (ENGINE NUMBER) 2H2X1234 (CHASSIS NUMBER) 535kph (SPEED) 4 (No. of PASSENGER) 5 (GEARS) Output1: Car HRDQ2456 52WVC1234 2H2X1234 535kph 4 5
Predict the outputabstract class Vehicle{ abstract void calcPremium(Number N);}interface Insurance{ abstract void calcPremium (Object O);}class Car extends Vehicle implements Insurance{ public void calcPremium (Object O) { System.out.println("Object"); } void calcPremium (Number N) { System.out.println("Number"); } }public class Test{ public static void main(String[] args) { Vehicle a = new Car(); a. calcPremium (new Integer(121)); Insurance b = new Car(); b. calcPremium (new Integer(121)); Car c = new Car(); c. calcPremium (new Integer(121)); }}Select one:a.NumberObjectNumberb.NumberNumberObjectc.Run time errord.Compile time error
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.
Implement two vehicle classes: Car:The constructor for Car must take two arguments. The first of them is its maximum speed, and the second one is a string that denotes the units in which the speed is given: either "km/h" or "mph".The class must be implemented to return a string based on the arguments. For example, if car is an object of class Car with a maximum speed of 120, and the unit is "km/h", then printing car prints the following string: "Car with the maximum speed of 120 km/h", without quotes. If the maximum speed is 94 and the unit is "mph", then printing car prints in the following string: "Car with the maximum speed of 94 mph", without quotes. Boat:The constructor for Boat must take a single argument denoting its maximum speed in knots.The class must be implemented to return a string based on the argument. For example, if boat is an object of class Boat with a maximum speed of 82, then printing boat prints the following string: "Boat with the maximum speed of 82 knots", without quotes. The implementations of the classes will be tested by a provided code stub on several input files. Each input file contains several queries, and each query constructs an object of one of the classes. It then prints the string representation of the object to the standard output. Constraints1 ≤ the number of queries in one test file ≤ 100 Input Format Format for Custom TestingIn the first line, there is a single integer, q, the number of queries.Then, q lines follow. In the ith of them, there are space-separated parameters. The first of them denotes the vehicle type to be constructed, and the remaining parameters denote the values passed for the constructor of the object.Sample Case 0Sample InputSTDIN Function----- -------2 → number of queries, q = 2car 151 km/h → query parameters = ["car 151 km/h", "boat 77"]boat 77Sample OutputCar with the maximum speed of 151 km/hBoat with the maximum speed of 77 knotsExplanationThere are 2 queries. In the first of them, an object of class Car with the maximum speed of 151 in km/h is constructed, and then its string representation is printed to the output. In the second query, an object of class Boat is constructed with the maximum speed of 77 knots, and then its string representation is printed to the output.Sample Case 1Sample InputSTDIN Function----- --------3 → number of queries, q = 2boat 101 → query parameters = ["boat 101", "car 120 mph", "car 251 km/h"]car 120 mphcar 251 km/hSample OutputBoat with the maximum speed of 101 knotsCar with the maximum speed of 120 mphCar with the maximum speed of 251 km/hExplanationThere are 3 queries. In the first of them, an object of class Boat with the maximum speed of 101 knots is constructed, and then its string representation is printed to the output. In the second query, an object of class Car with the maximum speed of 120 in mph is constructed, and then its string representation is printed to the output. In the third query, an object of class Car with the maximum speed of 251 in km/h is constructed, and then its string representation is printed to the output.
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.