***JAVA*** Create a Country class with the following: — instance variables name,
ID: 3721650 • Letter: #
Question
***JAVA***
Create a Country class with the following:
— instance variables name, continent, capital (all String), and population (long)
— a Country constructor with four parameters. There can be NO negative value for the population instance variable. Use 0 as the default value if the population parameter is invalid.
— get and set (accessor and mutator) methods for each instance variable. There can be NO negative value for the population instance variable so be sure to validate the parameter in the setPopulation method. Do NOT update the instance variable if the parameter is invalid (negative). Do NOT use exception handling.
— a toString method that returns a String with the country’s name, continent, capital, and population, ALL clearly labeled. Use the String.format method. Format the population to use commas for numbers greater than 999.
Create a CountryTest program with the following.
— an array (data type Country) named countries that has size (length) 6
— 5 Country objects must be created with existing data (see links below) and placed in the array. You MUST choose one country from each continent: Asia, Americas, Europe, Africa, and Oceania. The sixth Country object must be created with user input (any continent). Place it last in the array.
Use these sites for country info:
https://en.wikipedia.org/wiki/List_of_countries_by_population_(United_Nations) (Links to an external site.)Links to an external site.
https://www.thoughtco.com/capitals-of-every-independent-country-1434452 (Links to an external site.)Links to an external site.
— use an enhanced for loop to display the info of each Country object in the array, clearly labeled. Do this by calling the toString method.
— use the command prompt window (terminal) for input and output
— Do NOT use the countries shown in the sample program run below.
Sample program run:
Explanation / Answer
ScreenShot
------------------------------------------------------------------------------------
Program
/* Import packages for I/O and formatting */
import java.text.NumberFormat;
import java.util.*;
/* Country class for country name,continent capital and population
* set and get
* display the details
*/
class country{
//Member variables
String name;
String continent;
String capital;
long population;
//Constructor
public country(){
name="";
continent="";
capital="";
population=0;
}
//Mutators
public void setName(String name) {
this.name=name;
}
public void setContinent(String continent) {
this.continent=continent;
}
public void setCapital(String capital) {
this.capital=capital;
}
public void setPopulation(long population) {
if(population>0) {
this.population=population;
}
}
//accessors
public String getName() {
return name;
}
public String getContinent() {
return continent;
}
public String getCapital() {
return capital;
}
public long getPopulation() {
return population;
}
//Tostring method
public String toString() {
return "Country:"+name+" Continent:"+continent+" Capital:"+capital+" Population:"+NumberFormat.getInstance().format(population);
}
}
//Main class
public class countryArray {
//main method
public static void main(String[] args) {
//Scanner to read user input;
Scanner sc=new Scanner(System.in);
//Variables to read data
String name,continent,capital;
long population;
//Create an array for country
country[] countries=new country[6];
//Sixth object creation prompt
System.out.println("Enter country name:");
name=sc.nextLine();
System.out.println("Enter continent:");
continent=sc.nextLine();
System.out.println("Enter capital:");
capital=sc.nextLine();
System.out.println("Enter population:");
population=sc.nextLong();
//first object creation
countries[0]=new country();
countries[0].setName("Brazil");
countries[0].setContinent("Americas");
countries[0].setCapital("Brasilia");
countries[0].setPopulation(209288278);
System.out.println(countries[0]);
System.out.println();
//second object creation
countries[1]=new country();
countries[1].setName("Netherlands");
countries[1].setContinent("Europe");
countries[1].setCapital("Amsterdam; The Hague (seat of government)");
countries[1].setPopulation(17035938);
System.out.println(countries[1]);
System.out.println();
//third object creation
countries[2]=new country();
countries[2].setName("Taiwan");
countries[2].setContinent("Africa");
countries[2].setCapital("Taipei");
countries[2].setPopulation(23626456);
System.out.println(countries[2]);
System.out.println();
//fourth object creation
countries[3]=new country();
countries[3].setName("Swaziland");
countries[3].setContinent("Africa");
countries[3].setCapital("Mbabane");
countries[3].setPopulation(1367254);
System.out.println(countries[3]);
System.out.println();
//fifth object creation
countries[4]=new country();
countries[4].setName("Fiji");
countries[4].setContinent("Oceania");
countries[4].setCapital("Suva");
countries[4].setPopulation(905502);
System.out.println(countries[4]);
System.out.println();
//sixth object
countries[5]=new country();
countries[5].setName(name);
countries[5].setContinent(continent);
countries[5].setCapital(capital);
countries[5].setPopulation(population);
System.out.println(countries[5]);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.