Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can someone please help me write the following lab in JAVA? I'm a little confused. Please add // comments as you go along your code

Can someone please help me write the following lab in JAVA? I'm a little confused. Please add // comments as you go along your code so that I can understand what you did.

__________________________________________________________________________________________________________________________________________________

Description:

You will write a program that will allow the user to search countries by Name, Capital, and Population.

Here is the data within countries.txt:

Afghanistan Kabul 35530081 Albania Tirana 2930187 Algeria Algiers 41318142 Andorra Andorra la Vella 76965 Angola Luanda 29784193 Antigua and Barbuda Saint Johns 102012 Argentina Buenos Aires 44271041 Armenia Yerevan 2930450 Australia Canberra 24450561 Austria Vienna 8735453 Azerbaijan Baku 9827589 Bahamas, The Nassau 395361 Bahrain Manama 1492584 Bangladesh Dhaka 164669751 Barbados Bridgetown 285719 Belarus Minsk 9468338 Belgium Brussels 11429336 Belize Belmopan 374681 Benin Porto-Novo 11175692 Bhutan Thimphu 807610 Bolivia La Paz, Sucre 11051600 Bosnia and Herzegovina Sarajevo 3507017 Botswana Gaborone 2291661 Brazil Brasilia 209288278 Brunei Bandar Seri Begawan 428697 Bulgaria Sofia 7084571 Burkina Faso Ouagadougou 19193382 Burundi Bujumbura 10864245 Cambodia Phnom Penh 16005373 Cameroon Yaounde 24053727 Canada Ottawa 36624199 Cape Verde Praia 546388 Central African Republic Bangui 4659080 Chad NDjamena 14899994 Chile Santiago 18336653 China Beijing 1420062022 Colombia Bogota 49849818 Comoros Moroni 850912 Congo, Republic of the Brazzaville 5542197 Congo, Democratic Republic of the Kinshasa 86727573 Costa Rica San Jose 4999384 Cote dIvoire Yamoussoukro 24294750 Croatia Zagreb 4140148 Cuba Havana 11492046 Cyprus Nicosia 1198427 Czech Republic Prague 10630589 Denmark Copenhagen 5775224 Djibouti Djibouti 985690 Dominica Roseau 74679 Dominican Republic Santo Domingo 10996774 East Timor (Timor-Leste) Dili 1352360 Ecuador Quito 17100444 Egypt Cairo 101168745 El Salvador San Salvador 6445405 Equatorial Guinea Malabo 1360104 Eritrea Asmara 5309659 Estonia Tallinn 1303798 Ethiopia Addis Ababa 110135635 Fiji Suva 918757 Finland Helsinki 5561389 France Paris 65480710 Gabon Libreville 2109099 Gambia, The Banjul 2228075 Georgia Tbilisi 3904204 Germany Berlin 82438639 Ghana Accra 30096970 Greece Athens 11124603 Grenada Saint Georges 108825 Guatemala Guatemala City 17577842 Guinea Conakry 13398180 Guinea-Bissau Bissau 1953723 Guyana Georgetown 786508 Haiti Port-au-Prince 11242856 Honduras Tegucigalpa 9568688 Hungary Budapest 9655361 Iceland Reykjavik 340566 India New Delhi 1368737513 Indonesia Jakarta 269536482 Iran Tehran 82820766 Iraq Baghdad 40412299 Ireland Dublin 4847139 Israel Jerusalem 8583916 Italy Rome 59216525 Jamaica Kingston 2906339 Japan Tokyo 126854745 Jordan Amman 10069794 Kazakhstan Astana 18592970 Kenya Nairobi 52214791 Kiribati Tarawa Atoll 120428 Korea, North Pyongyang 25727408 Korea, South Seoul 51339238 Kosovo Pristina 1983105 Kuwait Kuwait City 4248974 Kyrgyzstan Bishkek 6218616 Laos Vientiane 7064242 Latvia Riga 1911108 Lebanon Beirut 6065922 Lesotho Maseru 2298682 Liberia Monrovia 4977720 Libya Tripoli 6569864 Liechtenstein Vaduz 38404 Lithuania Vilnius 2846459 Luxembourg Luxembourg 596992 Macedonia Skopje 2086720 Madagascar Antananarivo 26968642 Malawi Lilongwe 19718743 Malaysia Kuala Lumpur 32454455 Maldives Male 451738 Mali Bamako 19689140 Malta Valletta 433245 Marshall Islands Majuro 53211 Mauritania Nouakchott 4611149 Mauritius Port Louis 1271368 Mexico Mexico City 132328035 Micronesia, Federated States of Palikir 106983 Moldova Chisinau 4029750 Monaco Monaco 39102 Mongolia Ulaanbaatar 3166244 Montenegro Podgorica 629355 Morocco Rabat 36635156 Mozambique Maputo 31408823 Myanmar (Burma) Naypyidaw 54336138 Namibia Windhoek 2641996 Nauru Yaren District 11260 Nepal Kathmandu 29942018 Netherlands Amsterdam 17132908 New Zealand Wellington 4792409 Nicaragua Managua 6351157 Niger Niamey 23176691 Nigeria Abuja 200962417 Norway Oslo 5400916 Oman Muscat 5001875 Pakistan Islamabad 204596442 Palau Melekeok 22206 Panama Panama City 4226197 Papua New Guinea Port Moresby 8586525 Paraguay Asuncion 6981981 Peru Lima 32933835 Philippines Manila 108106310 Poland Warsaw 38028278 Portugal Lisbon 10245666 Qatar Doha 2743901 Romania Bucharest 19483360 Russia Moscow 143895551 Rwanda Kigali 12794412 Saint Kitts and Nevis Basseterre 56345 Saint Lucia Castries 180454 Saint Vincent and the Grenadines Kingstown 110488 Samoa Apia 198909 San Marino San Marino 33683 Sao Tome and Principe Sao Tome 213379 Saudi Arabia Riyadh 34140662 Senegal Dakar 16743859 Serbia Belgrade 8733407 Seychelles Victoria 95702 Sierra Leone Freetown 7883123 Singapore Singapore 5868104 Slovakia Bratislava 5450987 Slovenia Ljubljana 2081900 Solomon Islands Honiara 635254 Somalia Mogadishu 15636170 South Africa Pretoria, Cape Town, Bloemfontein 58068097 South Sudan Juba 13263184 Spain Madrid 46441049 Sri Lanka Colombo, Sri Jayewardenepura Kotte 21018859 Sudan Khartoum 42514094 Suriname Paramaribo 573085 Swaziland Mbabane 1415414 Sweden Stockholm 10053135 Switzerland Bern 8609259 Syria Damascus 18499181 Taiwan Taipei 23626456 Tajikistan Dushanbe 9292000 Tanzania Dodoma 60913557 Thailand Bangkok 69306160 Togo Lome 8186384 Tonga Nukualofa 110041 Trinidad and Tobago Port-of-Spain 1375443 Tunisia Tunis 11783168 Turkey Ankara 82961805 Turkmenistan Ashgabat 5942561 Tuvalu Funafuti 11393 Uganda Kampala 42862958 Ukraine Kyiv 44222947 United Arab Emirates Abu Dhabi 9400145 United Kingdom London 66181585 United States of America Washington D.C. 324459463 Uruguay Montevideo 3456750 Uzbekistan Tashkent 31910641 Vanuatu Port-Vila 276244 Vatican City (Holy See) Vatican City 792 Venezuela Caracas 31977065 Vietnam Hanoi 95540800 Yemen Sanaa 28250420 Zambia Lusaka 17094130 Zimbabwe Harare 16529904

Part One:

Create a class called Country.java.

Data fields include name, capital and population.

Write a 3-arg constructor.

Write accessor methods for each of the data fields.

Write a toString method.

Part Two:

Create a class called CountryFinder.java

Create an array of Country objects, size 196. Fill the array using a file-reading Scanner. Close the scanner after you finish filling the array.

Use a while loop to present a menu of choices:

Search by name

Search by capital

Find country with the largest population

Find country with the smallest population

Find countries with a larger population than a given number

Find countries with a smaller population than a given number

Error check the user input: do not let them enter an invalid choice

Name should be found using a binary search (do this using methods similar to those shown in the slides).

Capital should be found using a linear search.

Here is a sample run:

Choose an option:

1. Search country by name

2. Search country by capital

3. Find country with the largest population

4. Find country with the smallest population

5. List countries with over _____ people

6. List countries with under _____ people

Type 0 to quit

324

Invalid

Choose an option:

1. Search country by name

2. Search country by capital

3. Find country with the largest population

4. Find country with the smallest population

5. List countries with over _____ people

6. List countries with under _____ people

Type 0 to quit

1

Enter country name:

Nigeria

Nigeria, Capital: Abuja, Pop: 200962417

Choose an option:

1. Search country by name

2. Search country by capital

3. Find country with the largest population

4. Find country with the smallest population

5. List countries with over _____ people

6. List countries with under _____ people

Type 0 to quit

1

Enter country name:

sdfgerg

Country not found

Choose an option:

1. Search country by name

2. Search country by capital

3. Find country with the largest population

4. Find country with the smallest population

5. List countries with over _____ people

6. List countries with under _____ people

Type 0 to quit

2

Enter the capital

Kuwait City

Kuwait, Capital: Kuwait City, Pop: 4248974

Choose an option:

1. Search country by name

2. Search country by capital

3. Find country with the largest population

4. Find country with the smallest population

5. List countries with over _____ people

6. List countries with under _____ people

Type 0 to quit

2

Enter the capital

bern

Country not found

Hints:

This program runs on mostly String inputs, some multi-word. Use input.nextLine() to read all String inputs. To read an int input, use Integer.parseInt(input.nextLine()).

Also:

The current program works with exact matches only. Modify it so that it will work with partial spellings and return all countries whose data fields begin with that substring.

So far, I have the following code down.

CountryFinder.java

import java.io.*; import java.util.*; public class CountryFinder { public static void main(String[] args) throws IOException { Scanner input = new Scanner (new FileReader ("countries.txt")); Country[] arr = new Country [196]; int i = 0; while(input.hasNextLine()) { String n = input.nextLine(); String c = input.nextLine(); int p = Integer.parseInt(input.nextLine()); Country next = new Country (n, c, p); arr[i] = next; i++; } input.close();

} }

Country.java

public class Country { private String name, capital; private int population;

public Country(String n, String c, int p) {

name = n; capital = c; population = p; } public void setName(String n) { name = n; } public void setCapital(String c) { capital = c; } public void setPopulation(int p) { population = p; } public String getName() { return name; }

public String getCapital() { return capital; }

public int getPopulation() { return population; } public String toString() { return "Country:" + name + "Capital:" + capital + "Population:" + population; } }

For the linear search, I am supposed to incorporate this code (with a separate method of course):

public static int linSearch (Country[] array, String key) {

for (int i = 0; i

if( /* compare array[i]'s capital to key */)

return i;

return -1;

}

For the menu choices (to be included in main):

Scanner keyboard = new Scanner (System.in);

int choice = 1;

while(choice ! = 0)

{

displayMenu(); //write a method to display the menu choices

choice = Integer.parseInt(keyboard.nextLine());

while (*/error check for incorrect input*/){

//error checking

}

if(choice == 1) //find by name

{

System.out.println("Enter country name:");

String find = keyboard.nextLine();

int loc = binSearch (arr, find);

if (loc == -1)

System.out.print ("Country not found");

else

System.out.println(arr[loc]);

}

//choice 2

//choice 3

//etc

}

System.out.println("Goodbye!");

____________________________________________________________

With all the resources I have given, can someone help fix my code? Also please use only the code I gave when you help.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

Understanding Databases Concepts And Practice

Authors: Suzanne W Dietrich

1st Edition

1119827949, 9781119827948

More Books

Students also viewed these Databases questions

Question

6. List and explain important trends in compensation management.

Answered: 1 week ago

Question

What are our strategic aims?

Answered: 1 week ago