In a program you need to store the populations of 12 countries. Define two array
ID: 3767691 • Letter: I
Question
In a program you need to store the populations of 12 countries.
Define two arrays that may be used in parallel to store the names of the countries and their populations.
Write a loop that uses these arrays to print each country's name and its population.
Add a loop in your main method which asks the user running it if they would like to look up a given country's population. When they indicate “No” the program terminates.
Prompt user: What country would you like to look up?
Upon user entering something the program calls the countryLookup method.
countryLookup method accepts an argument (parameter) containing what the user entered for the country to look up. The method searches country name array for the name and upon finding it returns the corresponding population from the population array. If it doesn't find the country simply return -1.
China 1367960000 India 1262670000 United States 319111000 Indonesia 252164800 Brazil 203462000 Pakistan 188172000 Nigeria 178517000 Bangladesh 157339000 Russia 146149200 Japan 127090000Explanation / Answer
Solution:
package com.chegg.nancy.solutions;
import java.util.Scanner;
public class Population {
static String[] country = { "China", "India", "United States", "Indonesia", "Brazil", "Pakistan", "Nigeria",
"Bangladesh", "Russia", "Japan" };
static int[] population = { 1367960000, 1262670000, 319111000, 252164800, 203462000, 188172000, 178517000,
157339000, 146149200, 127090000 };
static boolean flag = false;
public static void main(String[] args) {
// Displaying country and corresponding population.
System.out.println("Country : Population");
System.out.println("--------------------");
for (int i = 0; i < 10; i++) {
System.out.println(country[i] + " : " + population[i]);
}
System.out.println("Do you want to look up for the population of perticular country(yes/no?)");
Scanner scan = new Scanner(System.in);
String input = scan.next();
if (input.equalsIgnoreCase("no")) {
System.out.println("Program exited.");
System.exit(0);
} else {
System.out.println("what country you want to look up for : ");
String countryName = scan.next();
countryLookup(countryName);
}
}
static int countryLookup(String countryName) {
for (int i = 0; i < 10; i++) {
if (country[i].equalsIgnoreCase(countryName)) {
System.out.println(country[i] + " : " + population[i]);
flag = true;
}
}
if (flag == false) {
System.out.println("-1");
return -1;
}
return 0;
}
}
Output:
Country : Population
--------------------
China : 1367960000
India : 1262670000
United States : 319111000
Indonesia : 252164800
Brazil : 203462000
Pakistan : 188172000
Nigeria : 178517000
Bangladesh : 157339000
Russia : 146149200
Japan : 127090000
Do you want to look up for the population of perticular country(yes/no?) yes
what country you want to look up for : nigeria
Nigeria : 178517000
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.