Answer to Question #307988 in Java | JSP | JSF for Manoj

Question #307988

Design a Car model class under package packages with the following attributes:



Member Field Name



licenceNumber



Type



String



String



Double



Model



currentMileage engine Size



Integer



Mark all the attributes as private & Create default & parameterized constructors



Design another class as Main under package packages, where you need to implement logic as follows



Declare an array as Car with size N where N is to be accepted from user Take N Car's information from user and store them in specified array Call sortCarlist method from Main class to get all cars information sorted based on Model name and display then within Main class after returning back



from the specified method From Main class car array need to pass within sortCarLast method which return sorted car array and then display all car information on console.



Design sortCarlist method in Car clast, as follows: will take a car array and retam car array



it will sort array based on model value given in each car as its information

1
Expert's answer
2022-03-10T07:15:22-0500


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[]) {


		for (int i = 0; i < cars.length; i++) {
			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");
		}
	}


	private static Car[] sortCarlist(Car cars[]) {
		for (int i = 0; i < cars.length; i++) {
			for (int j = i + 1; j < cars.length; j++) {
				if (cars[j].getModel().compareToIgnoreCase(cars[i].getModel()) < 0) {
					Car temp = cars[i];
					cars[i] = cars[j];
					cars[j] = temp;
				}
			}
		}
		return cars;
	}


	public static void main(String[] args) {
		Scanner keyboard = new Scanner(System.in);
		String licenceNumber;
		String model;
		double currentMileage;
		int engineSize;
		System.out.print("Enter size N: ");
		int N = keyboard.nextInt();
		Car cars[] = new Car[N];
		keyboard.nextLine();
		for (int i = 0; i < N; 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);
		}
		cars = sortCarlist(cars);
		findCarList(cars);


		keyboard.close();
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS