Answer to Question #314743 in Java | JSP | JSF for Nitin

Question #314743

design a car model class under package :package 3 with the following attributes: member field name licence number model current mileage engine size type string string double integer mark all the attributes as private & create appropriate getters & setters design another class as mainunder package :package3, where you need to implement logic as follows: you need to take the number of cars as input and then take each car's necessary information and put them in array. then at end show them in well-designed format.

1
Expert's answer
2022-03-20T06:20:16-0400






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 Mainunder {


	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);
		}
		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