Answer to Question #309713 in Java | JSP | JSF for Sowmya

Question #309713

Design a Car model class under package :package5 with attributes:


Member Field Name


Type


licenceNumber


String


Model


String


currentMileage


Double


engineSize


Integer


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


Design another class as Main under package :package5, 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 sortCarList method which return sorted car array and then display all car information on console.


Design sortCarList method in Car class as follows:


• it will take a car array ,return car array


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

1
Expert's answer
2022-03-12T04:55:48-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 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);
		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");
		}


		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