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

Solutions

Expert Solution

Implementation in JAVA:

code:

import java.util.Scanner;

public class Country_Population {

   public static void main(String[] args) {
      
//       create scanner class object
       Scanner s= new Scanner(System.in);
      
//       declare and initialize country and population array of type long and String
       String country[]= {"China","India","United_States","Indonesia","Brazil","Pakistan","Nigeria","Bangladesh","Russia","Japan"};
      
       long pop[]= {1367960000,1262670000,319111000,252164800,203462000,188172000,178517000,157339000,146149200,127090000};
      
       System.out.print("\nDo you want to search population for country : ");
      
//       user ask for continue
       String ans=s.next();
       s.nextLine();
      
//       if invalid inout
       if(!(ans.equals("Yes") || ans.equals("No"))){
          
           System.out.print("Invalid Input Re-enter Please - ");
           ans=s.next();
          
               }
      
//       loop untill user enter yes for continuition
       while(ans.equals("Yes")) {
          
          System.out.print("Enter country name : ");
         
//          ask user for country anme
          String con=s.next();
          s.nextLine();
         
//          get population
          long popu= getpop(country,pop,con);
         
//          print population
          if(popu!=-1) {
             
              System.out.println("Population in "+con+" is : "+popu);
             
          }
         
          else {
              System.out.println("Sorry! Country not found!!!");
          }
         
          System.out.print("\nDo you want to continue : ");
          
//          again ask for continuition
          ans=s.next();
          s.nextLine();
         
          if(ans.equals("No")) {
              System.out.println("Program Terminated \nThank you!!!");
              return;
          }
         
//          for invalid input
          if(!(ans.equals("Yes") || ans.equals("No"))){
              
               System.out.print("Invalid Input Re-enter Please - ");
               ans=s.next();
              
                   }
         
       }
      
   }
  
//   method will return population if found for cname'th index in pop array
//   else return -1 if not found
   public static long getpop(String[] country, long pop[],String cname ) {
      
       for(int i=0;i<country.length;i++) {
          
           if(cname.equals(country[i])) {
              
               return pop[i];
           }
          
       }
      
       return -1;
      
   }

}

SAMPLE OUTPUT:

// PLEASE THUMBS-UP AND RATE POSITIVELY
If you have any doubt regarding this question please ask me in commnets
// 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...
C program: 1, Make two parallel arrays, one to keep track of food names and another...
C program: 1, Make two parallel arrays, one to keep track of food names and another to keep track of the food prices. There will be a total of 10 items, with a total of about 100 characters for each food name title (i think we can use malloc for the strings and allocate it accordingly) Basically it will be something like a menu: 1, we will enter the food information 2, it will list all the food names and...
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.
2. Write a program in C++ that: a) Declares a 1D array A with 30 elements...
2. Write a program in C++ that: a) Declares a 1D array A with 30 elements b) Inputs an integer n from 1-30 from the keyboard. If n < 1 set n = 1. If n > 30 set n = 30. the program should keep asking the user the input n one by one, followed by printing of the value of n (n=n if bigger than 1 and smaller than 30, 1 if smaller than 1 and 30 if...
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...
Read and print parallel array - How can this be made to read parallel arrays and...
Read and print parallel array - How can this be made to read parallel arrays and then print them? The program presented here is intended to read from the text file and build parallel arrays. Then it will print as shown. The first line will not be stored in the array. The second line will be stored in firstArray[] and the third line will then be stored in secondArray[] and the arrays will repeat until the file is read. begin...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT