Question
Final Test - Programming Create a class called Country. It contains two private variables for the name of a country and the population of a
Final Test - Programming
Create a class called Country. It contains two private variables for the name of a country and the population of a country. Give it a constructor that sets those variables. Give it get methods for the name and population variables. In your Main method, write code that requests name and population data from the user. Use that data to instantiate Country objects. Store those objects in an ArrayList that has been configured to only contain Country objects. Your code should allow for an indefinite number of Country objects to be created and loaded that way (i.e. all of the above is in a loop). Still in your Main method, but in a second loop, allow the user to enter country names. The program should search the ArrayList for the Country object with that name and display the associated population. If it cannot find a match, display a message to that effect. This code should also be in a loop that runs until the user tells it to stop.
Important safety tip -- I've seen people have problems with this. If you want to test a String for equality, use the 'equals' method, not the "==" operator. So.
String animal = "Lion";
if(animal.equals("Lion"){ System.out.println("This is the right way to compare Strings); }
A run of your program might look like this:
Enter a country name: India Enter a country population: 1000
Do you wish to add another country? (enter y/n): y
Enter a country name: Australia Enter a country population: 100
Do you wish to add another country? (enter y/n): y
Enter a country name: Japan Enter a country population: 250
Do you wish to add another country? (enter y/n): n
Enter a country name to see its population: Japan Japan has a population of 250
Do you wish to see the population of another country? (enter y/n): y
Enter a country name to see its population: France France is not in our list.
Do you wish to see the population of another country? (enter y/n): n
Enter a country name to see its population: India Japan has a population of 1000
Do you wish to see the population of another country? (enter y/n): n
The program has ended.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started