In: Computer Science
Write a program the declares and uses two parallel arrays. One array for storing the names of countries and a second array for storing the populations of those countries. As you can see per the following the Country name and it's corresponding Population are stored at the same element index in each array.
China | 1367960000 |
India | 1262670000 |
United States | 319111000 |
Indonesia | 252164800 |
Brazil | 203462000 |
Pakistan | 188172000 |
Nigeria | 178517000 |
Bangladesh | 157339000 |
Russia | 146149200 |
Japan | 127090000 |
In the main method write a loop which asks the user if they would like to look up a given country's population.
i.e,
public class InClassModule_9 { public static String[] country = {"China", "India", "United States", "Indonesia", "Brazil", "Pakistan", "Nigeria", "Bangladesh", "Russia", "Japan"}; public static int[] population = {1367960000, 1262670000, 319111000, 252164800, 203462000, 188172000, 178517000, 157339000, 146149200, 127090000}; public static void main(String[] args) { System.out.println("Output/display contents from both arrays"); for(int x=0; x < country.length; x++) { System.out.println(x + ") Country: " + country[x] + "\tPopulation: " + population[x]); } System.out.println("\nThe End!!"); } }
Here is your solution
I create two methods
public String askUser() //this is use for ask country name to user
public int findPop(String n) //this is use to find population on given country name
Code:
import java.util.*;
public class InClassModule_9 {
//use for taking input from user
static Scanner sc = new Scanner(System.in);
public static String[] country = {"China", "India", "UnitedStates", "Indonesia", "Brazil",
"Pakistan", "Nigeria", "Bangladesh", "Russia", "Japan"};
public static int[] population = {1367960000, 1262670000, 319111000, 252164800, 203462000,
188172000, 178517000, 157339000, 146149200, 127090000};
//method with no parameters and return value(country name)
public String askUser(){
System.out.print("What country would you like to look up? : ");
//scan and return
return sc.next();
}
//method with parameters and return value(population)
public int findPop(String n){
for(int x=0; x < country.length; x++) {
//if country name match then it will return
if(country[x].toLowerCase().equals(n.toLowerCase())){
//return population of the country
return population[x];
}
}
//return if country not found
return -1;
}
public static void main(String[] args) {
//flag for user choice
String flag;
//display all countries and population
for(int x=0; x < country.length; x++) {
System.out.println(x + ") Country: " + country[x] + "\tPopulation: " + population[x]);
}
//creating object for calling methods
InClassModule_9 obj = new InClassModule_9();
//ask user
System.out.print("\nwould you like to look up a given country's population? (Yes/No) : ");
flag = sc.next();
/*here i convert user input into lowercase and then match with "yes"
"yes" & "YES" both are same*/
while(flag.toLowerCase().equals("yes")){
//first method call
String c = obj.askUser();
//second method call
int p = obj.findPop(c);
//check return output and print according to that
if( p != -1){
System.out.println(c+" : "+p);
}else{
System.out.println("Not Found");
}
//ask user
System.out.print("\nwould you like to look up a given country's population? : ");
flag = sc.next();
}
System.out.println("\nThe End!!");
}
}
Output:
I hope this solution will help you.
If you have any doubts about this, then do comment below.
Do you feel it is helpful to you?
Then please up vote me.
Thank You :)