Click here to Skip to main content
13,830,471 members
Rate this:
 
Please Sign up or sign in to vote.
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:

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 22: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 ?
OriginalGriff 10-Nov-18 10:00am
   
And?
What does it do that you didn't expect, or not do that you did?
Where are you stuck?
What help do you need?
Member 14050314 10-Nov-18 10:39am
   
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 manufaturer name both key and values
OriginalGriff 10-Nov-18 10:49am
   
What error?
When?
What do you do to get the error?
Are there any error messages?
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

First Correct the code it is
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
   
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web03 | 2.8.190114.1 | Last Updated 8 Jan 2019
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100