Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

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

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). Also in your Main method, include code that allows the user to enter country names. The program should find that country name in the ArrayList 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

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions