ChallengeChallenge

Task

Your task is to create a Country class. It should have fields: name, population, and continent on which the country is located. All fields in this class should have a private access modifier. You should also write getters and setters for each field in this class. The main method is already prepared, your task is to implement the Country class:

Note

This is a complex and challenging task, so if you haven't made progress in solving it within ≈15 minutes, feel free to click on the Hint button or analyze the Solution!

java

Main.java

  1. 1. Fill in the blanks by writing the private access modifier and then the data type;
    1. 2. Also, fill in the blanks in the getters and setters;
    2. 3. The data type in the getter and setter should match the data type of the field for which the getter/setter is being written;
    3. 4. In the getter, the return type should be the name of the field being returned;
    4. 5. In the setter, the type of the parameter should be the same as the field's parameter;
    5. 6. Inside the body of the setter method, use the syntax this.field = field;.

package com.example;

class Country {
    private String name;
    private int population;
    private String continent;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getPopulation() {
        return population;
    }

    public void setPopulation(int population) {
        this.population = population;
    }

    public String getContinent() {
        return continent;
    }

    public void setContinent(String continent) {
        this.continent = continent;
    }
}

public class Main {
    public static void main(String[] args) {
        Country france = new Country();
        france.setName("France");
        france.setPopulation(10000000);
        france.setContinent("Eurasia");
        Country canada = new Country();
        canada.setName("Canada");
        canada.setPopulation(15000000);
        canada.setContinent("North America");
        System.out.println("There's two countries: " + System.lineSeparator()
                + france.getName() + " with population: "
                + france.getPopulation() + ". It is located on continent named "
                + france.getContinent() + ";" + System.lineSeparator()
                + canada.getName() + " with population: "
                + canada.getPopulation() + ". It is located on continent named "
                + canada.getContinent() + ".");

    }
}
      

Everything was clear?

Section 5. Chapter 6