Answer to Question #124434 in Java | JSP | JSF for John

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
1
Expert's answer
2020-06-30T07:55:36-0400
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[] {};
        }
    }

}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS