0% found this document useful (0 votes)
6 views

Java 41280

Uploaded by

Kunal Dayma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Java 41280

Uploaded by

Kunal Dayma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Project No.

- 4
Objective: Design a code to Develop a java class with a instance variable
CountryMap HashMap (M1).
Code:
import java.util.*;
public class Main
{
HashMap<String, String> M1= new HashMap<>();
void saveCountryCapital(String CountryName, String capital)
{
M1.put(CountryName,capital);
}
String getCapital(String CountryName)
{
String a=null;
if (M1.containsKey(CountryName))
{
a = M1.get(CountryName);
}
return a;
}
String getCountry(String capitalName)
{
String a= null;
if (M1.containsValue(capitalName))
{
a = M1.get(capitalName);
}
return a;
}
HashMap<String, String> copyMap()
{
HashMap<String, String> M2=new HashMap<>();
for (Map.Entry<String, String> en : M1.entrySet())
{
M2.put(en.getValue(), en.getKey());
}
return M2;
}
ArrayList<String> mapArrayList()
{
ArrayList<String> arr= new ArrayList<>();
for (Map.Entry<String, String> en : M1.entrySet())
{
arr.add(en.getKey());
}

HARBANS SINGH 17BCS1280


return arr;
}
public static void main(String[] args)
{
Main C= new Main();
Scanner sc= new Scanner(System.in);
int option, ch;
do
{
System.out.println("*******************");
System.out.println("1. Add counry and capital");
System.out.println("2. get capital");
System.out.println("3. get country");
System.out.println("4. Copy Map");
System.out.println("5. Make Arraylist");
System.out.print("Enter your choice: ");
option=sc.nextInt();
switch(option)
{
case 1:
System.out.println("Enter details: ");
System.out.println("Country Name: ");
String countryname=sc.next();
System.out.println("Capital Name: ");
String capitalname=sc.next();
C.saveCountryCapital(countryname,capitalname);
break;
case 2:
System.out.println("Country Name: ");
String countryname1=sc.next();
System.out.println(countryname1+" has capital "+C.getCapital(countryname1));
break;
case 3:
System.out.println("Capital Name: ");
String capitalname1=sc.next();
System.out.println(capitalname1+" has country "+C.getCapital(capitalname1));
break;
case 4:
HashMap<String, String> M2= C.copyMap();
System.out.println("after copying");
System.out.println(M2);
break;
case 5:
System.out.println("your Arraylist is here:");
System.out.println(C.mapArrayList());
break;
}

HARBANS SINGH 17BCS1280


System.out.println("\nDo you want to continue(0/1):");
ch=sc.nextInt();
}
while(ch!=0);
sc.close();
}
}

Output:

HARBANS SINGH 17BCS1280

You might also like