Question #124434

Class name Country

Parameters are:

Private String name;

Private String language;

Private double area;


Write a method that returns an array of type "country". The method asks the user to enter the number of the country. Then the information of each country.


Please help

Expert's answer

package com.sadkoala.test;

import java.util.Scanner;

public class Main
{

    static class Country {

        private String name;
        private String language;
        private double area;

        public Country(String name, String language, double area) {
            this.name = name;
            this.language = language;
            this.area = area;
        }

        public String getName() {
            return name;
        }

        public String getLanguage() {
            return language;
        }

        public double getArea() {
            return area;
        }

    }

    public static void main(String [] args)
    {
        Country[] countries = fillCountry();
    }

    private static Country[] fillCountry() {
        System.out.println("Input countries count");
        Scanner in = new Scanner(System.in);
        int countriesCnt = in.nextInt();
        in.nextLine();
        if (countriesCnt > 0) {
            Country[] countries = new Country[countriesCnt];
            for (int i = 0; i < countriesCnt; i++) {
                System.out.println("Input country name");
                String name = in.nextLine();
                System.out.println("Input country language");
                String language = in.nextLine();
                System.out.println("Input country area");
                double area = in.nextDouble();
                in.nextLine();
                countries[i] = new Country(name, language, area);
            }
            return countries;
        } else {
            return new Country[] {};
        }
    }

}
LATEST TUTORIALS
APPROVED BY CLIENTS