Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Write a java program to read the aircraft details from the user, and find the distinct aircraft manufacturers.
Span a thread to find the aircrafts of each manufacturer and display them.
Create a main class Main.java
Create a Aircraft class with the following attributes.
• name – String
• manufacturerName – String
create a constructor for Aircraft class with two arguments name,manufacturerName
Create another class Manufactuer with following attribute
• aircraft -List<aircraft>
• manufacturerAircraftMap - HashMap<String,List<string>>
In the manufacturerAircraftMap hashMap, Key is the manufacturer name and the value is a list of Strings which corresponds to the aircraft names.
The keys should be sorted in ascending order, and the values list also should be sorted in alphabetical order.

Extend Thread class and override the run method.

In the main class read the input from the user and create a thread for manufactuer to find the distinct aircraft manufacturers and display the details
Input and Output Format :

Refer sample input and output for formatting specifications.
[All text in bold corresponds to input and the rest corresponds to output]
Sample Input/Output:
Enter the number of aircrafts
6
Enter the aircraft 1 details
A320
AirAsia
Enter the aircraft 2 details
A318
AirAsia
Enter the aircraft 3 details
A321
AirAsia
Enter the aircraft 4 details
737
Boeing
Enter the aircraft 5 details
777
Boeing
Enter the aircraft 6 details
BD500
Bombardier
Manufacturer Details
AirAsia
A320
A318
A321
Boeing
737
777
Bombardier
BD500


What I have tried:

Java
public class Main {
	public static void main(String[] args)
	{
	Scanner sc = new Scanner(System.in);
	int m = sc.nextInt();
	HashMap<String , List<string>> manufacturerAircraftMap = new HashMap<>();
	sc.nextLine();
	String name , manufacturerName;
	for(int i = 0 ; i<m ;i++)
	{
		System.out.print("Enter the aircraft " + i + "details :");
		name = sc.nextLine();
		manufacturerName = sc.nextLine();
		manufacturerAircraftMap.put(name , (List<string>) new Aircraft( manufacturerName));
	 }
	}

}
 class Aircraft
{
	private String name;
	private String manufacturerName;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getManufacturerName() {
		return manufacturerName;
	}
	public void setManufacturerName(String manufacturerName) {
		this.manufacturerName = manufacturerName;
	}
	public Aircraft(String name , String manufacturerName)
	{
		this.setName(name);
		this.setManufacturerName(manufacturerName);
	}
}
 class Manufacturer extends Thread
{
     private List<aircraft> aircraft;
     
     public void run()
     {
    	 
     }

	public List<aircraft> getAircraft() {
		return aircraft;
	}

	public void setAircraft(List<aircraft> aircraft) {
		this.aircraft = aircraft;
	}
     
     
}
Posted
Updated 7-Jan-19 21:45pm
v2
Comments
Richard MacCutchan 10-Nov-18 9:15am    
What is the problem?
Member 14050314 10-Nov-18 10:42am    
there is a error in this line
manufacturerAircraftMap.put(name , (List<string>) new Aircraft( manufacturerName));
and can u help me to sort on basis of name and manufacturer name both key and values
Richard MacCutchan 10-Nov-18 10:43am    
What error?
Richard MacCutchan 10-Nov-18 10:46am    
You cannot do that. You are trying to tell the compiler that a new object of the Aircraft class is actually a List<string>, which it patently is not.
Patrice T 10-Nov-18 10:00am    
and you have a question ?

1 solution

First Correct the code it is
Java
List<String>
not List<string>
You could have used TreeMap instead of HashMap, if you want the name(key) to be sorted.
Sorting the manufacture name (values) is tricky but not difficult.

If you have googled about how to sort HashMap by keys and values, you would not have to ask here.Please do your homework by yourself.

Since you have asked, I have googled it for you.

I am assuming you have values in
manufacturerAircraftMap

Since, you want both keys and values to be sorted, then refer below articles:

Java 8 - Sorting HashMap by values in ascending and descending order | Java Code Geeks - 2019
Sort a HashMap in Java | Baeldung
How to sort HashMap in Java by Keys and Values | Java Hungry
 
Share this answer
 
Comments
CPallini 8-Jan-19 5:58am    
Have my 5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900