Question

In: Computer Science

Write a program the declares and uses two parallel arrays. One array for storing the names...

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.  

  • If No, then terminate the program
  • If Yes,
    • Call a value returning method, no parameters required:
      • This method prompts the user: What country would you like to look up?
      • The value entered is returned from this method
    • Call a second value returning method, passing it the value returned from the previous method call (ie. The name of the Country being searched for)
      • This method searches the country names array to see if it's there.
      • If it is there (ie it's found), return the population corresponding to the country
      • If it is NOT there, return a -1 which indicates the country was not found in the Country array
    • Back in the main method output the Country name searched for and it's population if found or Not Found when Not Found

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!!");

        }

}

Solutions

Expert Solution

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 :)


Related Solutions

Write a program the declares and uses two parallel arrays. One array for storing the names...
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...
Write C program Multidimensional Arrays Design a program which uses two two-dimensional arrays as follows: an...
Write C program Multidimensional Arrays Design a program which uses two two-dimensional arrays as follows: an array which can store up to 50 student names where a name is up to 25 characters long an array which can store marks for 5 courses for up to 50 students The program should first obtain student names and their corresponding marks for a requested number of students from the user. Please note that the program should reject any number of students that...
Write a program in C that declares the following array: int. array[] = { 1, 2,...
Write a program in C that declares the following array: int. array[] = { 1, 2, 4, 8, 16, 32 } Then write some code that accepts a number between 0 and 5 from the user and stores it in a variable called "index". Write some code that retrieves the item specified by the index, like this: int item = array[index]; Then write code that outputs the corresponding array entry based on the number the user entered. Example output: The...
C Programming Only Write a program that declares a one-dimensional array of integers with 24 elements....
C Programming Only Write a program that declares a one-dimensional array of integers with 24 elements. Fill the array with random integers (use a loop). Neatly output each element in the one-dimensional array. Next convert your one-dimensional array of 24 elements into a two-dimensional array of 6 x 4 elements. Neatly output each element of the two-dimensional array. The values will be identical to the one-dimensional array – you’re just converting from one dimension to two.
Part 1:Write a program in Java that declares an array of 5 elements and displays the...
Part 1:Write a program in Java that declares an array of 5 elements and displays the contents of the array. Your program should attempt to access the 6th element in the array (which does not exist) and using try. catch your program should prevent the run-time error and display your error message to the user. The sample output including the error message is provided below. Part (1) Printing an element out of bounds 5 7 11 3 0 You went...
Write a program in C that does the following: 1. Declares an array called numbers_ary of...
Write a program in C that does the following: 1. Declares an array called numbers_ary of 6 integer numbers. 2. Declares an array called numbers_ary_sq of 6 integer numbers. 3. Reads and sets the values of numbers_ary from the keyboard using a loop. 4. Sets the values of numbers_ary_sq to the square of the values in numbers_ary using a loop. 5. Displays the values of numbers_ary and the values of numbers_ary_sq beside each other using a loop. Example Output Assume...
Write a program that uses two identical arrays of at least 25 integers. It should call...
Write a program that uses two identical arrays of at least 25 integers. It should call a function that uses the bubble sort algorithm to sort one of the arrays in descending order. The function should keep a count of the number of exchanges it makes. The program then should call a function that uses the selection sort algorithm to sort the other array. It should also keep count of the number of exchanges it makes. Display these values on...
sing arrays please write a program to implement the STACK concept after creating the Array, the...
sing arrays please write a program to implement the STACK concept after creating the Array, the user is to be presented with a menu to choose from a number of options such as pop, push, top, etc... elements to be added on the stack are ints between 0 and 99 include a loop to re display the options (menu) and an outer loop to restart the program Write a C++ program to implement the Stacks concept. the stack implementation is...
USING MatLab (Arrays) Set ASIZE to 5. Write a program that creates an array of ASIZE...
USING MatLab (Arrays) Set ASIZE to 5. Write a program that creates an array of ASIZE numeric elements. Prompt the User for ASIZE numbers and store them in the array. After storing the values, calculate the sum of all the values in the array and display the sum. Modify the program you wrote for Problem 5 such that it calculates the product of all the values instead of the sum. Modify the program you wrote in Problem 6 such that...
Write a program that uses an array for time and temperature. The program should contain an...
Write a program that uses an array for time and temperature. The program should contain an array with 24 elements, each of which is holding a temperature for a single hour. Your program should have a function that lists all of the temperatures that are held in the array. Temperatures should be listed to console with one entry per line. Your program should have a function that lists a single temperature entry. You should ask the user for a number...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT