0

Is it possible to do such a thing? Say I wanted to add the values I gave values to in CountriesTest and add them to the ArrayList in Countries. Also how could I reference aCountries to print for option 2, seeing that I created it inside option 1 I can't access it anywhere else.

Here is my interface

public interface CountriesInterface
{
    public String largestPop();
    public String largestArea();
    public String popDensity();
}

Here is the Countries class

import java.util.*;
public class Countries implements CountriesInterface
{
    private final List<CountriesInterface> theCountries = new ArrayList<>();
    private String cName;
    private String finalPopName;
    private String finalAreaName;
    private String finalDensityName;
    private int cPop = 0;
    private int cArea = 0;
    private int popDensity = 0;
    private int popCounter = 0;
    private int areaCounter = 0;
    private int densityCounter = 0;
    public Countries(String cName, int cPop, int cArea, int popDensity)
    {
        this.cName = cName;
        this.cPop = cPop;
        this.cArea = cArea;
        this.popDensity = popDensity;
    }

    public String largestPop()
    {
        for(int i = 0; i < theCountries.size(); i++)
        {
            if(cPop > popCounter)
            {
                popCounter = cPop;
                finalPopName = cName;
            }    
        }
        return finalPopName;
    }

    public String largestArea()
    {
        for(int i = 0; i < theCountries.size(); i++)
        {
            if(cArea > areaCounter)
            {
                areaCounter = cArea;
                finalAreaName = cName;
            }    
        }
        return finalAreaName;
    } 

    public String popDensity()
    {
        for(int i = 0; i < theCountries.size(); i++)
        {
            if(popDensity > densityCounter)
            {
                densityCounter = popDensity;
                finalDensityName = cName;
            }    
        }
        return finalDensityName;
    } 

}

Here is the CountriesTest class

import java.util.*;
public class CountriesTest
{
    public static void main(String[] args)
        {             
            int population = 0;
            int area = 0;
            int density = 0;
            Scanner myScanner = new Scanner(System.in);            
            boolean done = false;
            do
            {
                System.out.println("1.  Enter a country \n2.  Print countries with the largest population, area, and population density \n3.  Exit");
                int choice = Integer.parseInt(myScanner.nextLine());
                if (choice == 1)
                {
                    System.out.print("Enter name of country: ");
                    String input1 = myScanner.nextLine();
                    System.out.print("Enter area of country in square kilometers: ");
                    String input2 = myScanner.nextLine();
                    population = Integer.parseInt(input2);
                    System.out.print("Enter population of country: ");
                    String input3 = myScanner.nextLine();
                    area = Integer.parseInt(input3);
                    density = population/area;
                    Countries aCountries = new Countries(input1, population, area, density);
                }
                else if(choice == 2)
                {
                    System.out.println("The country with the largest population: " );
                    System.out.println("The country with the largest area: "  );
                    System.out.println("The country with the largest population density is: "  );
                }
                else if(choice == 3)
                {
                     done = true;
                }              
                else
                    System.out.println("Invalid Choice");     
            }
            while (!done);
           System.exit(0);
        }
}
6
  • declare aCountries outside loop. Commented Oct 6, 2014 at 12:57
  • Several things aren't OK with your code. First theCountries should be static or inside your main method. Then you don't add your "aCountries" object to "theCountries". And you're using instance methods as class methods (largestPop for instance). Commented Oct 6, 2014 at 13:01
  • But if I did that it would ask for values which I haven't instantiated yet Commented Oct 6, 2014 at 13:01
  • Your loops also don't make sense (for(int i = 0; i < theCountries.size(); i++) since I don't see "i" being used anywhere (and your could use the java 5 "for" format) Commented Oct 6, 2014 at 13:04
  • int i is a counter for the for loop to make sure it runs through all the variables in the ArrayList Commented Oct 6, 2014 at 13:06

1 Answer 1

1

OK, there's some mix-up in your code.

  • You are using the "Countries" class at the same time for an individual Country AND the list of countries. I won't recommand it, but at least you should make "static" members and methods which are for the list of countries. Or you could declare List theCountries = new ArrayList<>(); inside the main method instead.

  • You are never adding the new "Countries" object to the list of countries. So, if you've declared theCountries in the main method, just uste "theCountries.add(aCountries)" right after the "new Countries(...)".

  • your seach methods (like largestPop) won't work because they are never searching through the content of the "theCountries" ArrayList. (the "i" variable is just iterating through the indices, but never actually used to get a countent from this ArrayList).

  • and btw, System.exit(0) is not needed (it's implied)

Sign up to request clarification or add additional context in comments.

4 Comments

Ok then how could I reference the array in the largestPop method, for example, if I put it in the main method
If it was my code, I would create two separate classes : Country and Countries, the second one inheriting from ArrayList<Country>. And I would move the largetsPop method() for example into this one.
Ok I see what you're saying. So I could ditch the interface and just have the test class extends Countries
Not exactly. Your interface is not useful here, so yes you could ditch it. But you should have 3 classes: Country, Countries and CountriesTest. In CountriesTest main method, you start by declaring something like Countries countries=new Countries();. And each time you add a country, you write "Country country=new Country(...); countries.add(country);"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.