Design a Car model class under package :package6 with the following attributes:
Member Field Name
Type
licenceNumber
String
Model
String
currentMileage
Double
engineSize
Integer
Mark at the attributes as private & create appropriate Getters & Setters
Design another class as Main under package :package6, where you need to implement logic as follows:
Declare an array as Car with size 10.
Take 10 Car's information from user and store them in specified array.
Call findCarList method from Main class to get all cars information related to Current Mileage engine Size values should to findCarList method as argument well as Car array (with size 10).
findCarList method Car class follows:
will take current Mileage engine Size as (with size 10)
displays List of than current Mileage and engine Size (both) for the given current Mileage engine values.
there are cars, then shows cars found".
import java.util.*;
class Car {
private String licenceNumber;
private String model;
private double currentMileage;
private int engineSize;
public Car() {
}
/***
* Constructor
*
* @param licenceNumber
* @param model
* @param currentMileage
* @param engineSize
*/
public Car(String licenceNumber, String model, double currentMileage, int engineSize) {
this.licenceNumber = licenceNumber;
this.model = model;
this.currentMileage = currentMileage;
this.engineSize = engineSize;
}
/**
* @return the licenceNumber
*/
public String getLicenceNumber() {
return licenceNumber;
}
/**
* @param licenceNumber the licenceNumber to set
*/
public void setLicenceNumber(String licenceNumber) {
this.licenceNumber = licenceNumber;
}
/**
* @return the model
*/
public String getModel() {
return model;
}
/**
* @param model the model to set
*/
public void setModel(String model) {
this.model = model;
}
/**
* @return the currentMileage
*/
public double getCurrentMileage() {
return currentMileage;
}
/**
* @param currentMileage the currentMileage to set
*/
public void setCurrentMileage(double currentMileage) {
this.currentMileage = currentMileage;
}
/**
* @return the engineSize
*/
public int getEngineSize() {
return engineSize;
}
/**
* @param engineSize the engineSize to set
*/
public void setEngineSize(int engineSize) {
this.engineSize = engineSize;
}
}
class App {
private static void findCarList(Car cars[], String carModel) {
int counter = 0;
for (int i = 0; i < 10; i++) {
if (carModel.compareToIgnoreCase(cars[i].getModel()) == 0) {
System.out.println("The car licence number " + (i + 1) + ": " + cars[i].getLicenceNumber());
System.out.println("The car model " + (i + 1) + ": " + cars[i].getModel());
System.out.println("The car current mileage " + (i + 1) + ": " + cars[i].getCurrentMileage());
System.out.println("The car engine size " + (i + 1) + ": " + cars[i].getEngineSize() + "\n");
counter++;
}
}
if (counter == 0) {
System.out.println("No cars found");
}
}
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
Car cars[] = new Car[10];
String licenceNumber;
String model;
double currentMileage;
int engineSize;
for (int i = 0; i < 10; i++) {
System.out.print("Enter car licence number " + (i + 1) + ": ");
licenceNumber = keyboard.nextLine();
System.out.print("Enter car model " + (i + 1) + ": ");
model = keyboard.nextLine();
System.out.print("Enter car current mileage " + (i + 1) + ": ");
currentMileage = keyboard.nextDouble();
System.out.print("Enter car engine size " + (i + 1) + ": ");
engineSize = keyboard.nextInt();
keyboard.nextLine();
cars[i] = new Car(licenceNumber, model, currentMileage, engineSize);
}
System.out.print("Enter the car model to display: ");
model = keyboard.nextLine();
findCarList(cars, model);
keyboard.close();
}
}
Comments
Leave a comment