Please help? I was instructed to complete the following and follow the direction
ID: 3826013 • Letter: P
Question
Please help? I was instructed to complete the following and follow the directions.
Lab Country
In Continent.java
Declare a public enum called Continent. It includes a constant for each of the 7 continents
Remember: According to the Java naming convention enum constants should be capitalized Continents that consist of two words should use an underscore like NORTH_AMERICA Example: public enum Direction { UP, DOWN, LEFT, RIGHT }
. . . Compile and run
In Country.java
In the fields section Create the following two fields:
A private final field of type String called name
A private final field of type Continent called continent
In the constructor section: Create one constructor that takes two parameters. Initialize the private fields with the values passed in the parameters
In the methods section: Add the following two getters above the toString method that is already declared:
getName returns the value of the field name
getContinent returns the value of the field continent
In method toString delete the statement return null; and replace it with the following statement:
return String.format("%s (%s)", name, continent);
. . . Compile and run
In CountryTest.java Un-comment the three print statements in the main method. At the beginning of the main method before the print statements do the following:
Create an instance of type Continent called myContinent. Initialize it with NORTH_AMERICA
Create an instance of type Country called country1. Initialize it with the name USA and the variable myContinent
Create an instance of type Country called country2. Initialize it with the name Austria and the Continent EUROPE
Code to print country1 and country 2 is already provided
. . . Compile and run
Explanation / Answer
Find the below code and output. I mentioned clearly.
InContinent.java
package com.abc.continent;
public class InContinent {
public enum Continent { NORTH_AMERICA, SOUTH_AMERICA, ANTARCTICA, EUROPE, ASIA, AFRICA, AUSTRALIA }
}
Country.java
package com.abc.continent;
import com.abc.continent.InContinent.Continent;
public class Country {
private final String name;
private final Continent continent;
public Country(String name, Continent continent){
this.name =name;
this.continent = continent;
}
public String getName() {
return name;
}
public Continent getContinent() {
return continent;
}
@Override
public String toString() {
return "Country [name=" + name + ", continent=" + continent + "]";
}
}
InCountryTest.java
package com.abc.continent;
import com.abc.continent.InContinent.Continent;
public class InCountryTest {
public static void main(String[] args){
Continent myContinent = Continent.NORTH_AMERICA;
Country country1 = new Country("USA", myContinent);
Country country2 = new Country("Austria", Continent.EUROPE);
System.out.println("Country1: " + country1.toString());
System.out.println("Country2: " + country2.toString());
}
}
OUTPUT:
Country1: Country [name=USA, continent=NORTH_AMERICA]
Country2: Country [name=Austria, continent=EUROPE]