1

As you can see in my CountriesTest class I am keeping track of the largest populations, areas, and densities. However I cannot think of how to reference the positions that those elements are at and use the corresponding method in the Countries class to insert the appropriate name of the country with the highest values in said category. I know the interface seems kind of useless for the situation but the assignment required it. I included notes in the code where I thought the things I was missing would go.

Edit: Here is the parameters of the assignment because my question was a bit confusing:

Write a program that prints: the country with the largest area, the country with the largest population, and the country with the largest population density. Create your solution using only abstract classes and abstract methods.

Here is my interface

public interface CountriesInterface
{

    public String largestPop();

    public String largestArea();

    public String popDensity();
}

Here is the class that access that interface

public class Countries implements CountriesInterface
{
    private String cName;
    private int cPop;
    private int cArea;
    private int popDensity;
    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()
    {
        return cName;
    }  
    public String largestArea()
    {
        return cName;
    } 
    public String popDensity()
    {
        return cName;
    } 
}

Here is my test class

import java.util.*;
public class CountriesTest
{
    public static void main(String[] args)
        {  
            int population = 0;
            int area = 0;
            int density = 0;
            int arbitraryValue = 0;
            int arbitraryValue2 = 0;
            int arbitraryValue3 = 0;
            String outputPop;
            String outputArea;
            String outputDensity;
            ArrayList<String> countryList = new ArrayList<String>();
            ArrayList<Integer> countryPopulation = new ArrayList<Integer>();
            ArrayList<Integer> countryArea = new ArrayList<Integer>();
            ArrayList<Integer> countryPopulationDensity = new ArrayList<Integer>();
            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();
                    countryList.add(input1);
                    System.out.print("Enter area of country in square kilometers: ");
                    String input2 = myScanner.nextLine();
                    population = Integer.parseInt(input2);
                    countryPopulation.add(population);
                    System.out.print("Enter population of country: ");
                    String input3 = myScanner.nextLine();
                    area = Integer.parseInt(input3);
                    countryArea.add(area);
                    density = population/area;
                    countryPopulationDensity.add(density);
                    Countries aCountries = new Countries(input1, population, area, density);
                    if(population > arbitraryValue)
                    {
                        population = arbitraryValue;
                        //I don't know whats supposed to go here
                    }    
                    if(area > arbitraryValue2)
                    {
                        area = arbitraryValue2;
                        //I don't know whats supposed to go here
                    }  
                    if(density > arbitraryValue3)
                    {
                        density = arbitraryValue3;
                        //I don't know whats supposed to go here
                    }  
                }
                else if(choice == 2)
                {
                    System.out.println("The country with the largest population: " + I don't know whats supposed to go here);
                    System.out.println("The country with the largest area: " + I don't know whats supposed to go here);
                    System.out.println("The country with the largest population density is: " + I don't know whats supposed to go here);
                }
                else if(choice == 3)
                {
                     done = true;
                }              
                else
                    System.out.println("Invalid Choice");     
            }
            while (!done);
           System.exit(0);
        }
}
3
  • 2
    I have read your question for several times, but I could not understand what is troubling you. can you elucidate your issue plz?"However I cannot think of how to reference the positions that those elements are at and use the corresponding method in the Countries class to insert the appropriate name of the country with the highest values in said category" Commented Oct 5, 2014 at 20:29
  • Ok what I want to be able to do is figure out which country has the largest population for example and just print the name of that country Commented Oct 5, 2014 at 20:42
  • I think first you should take a look at the answer of @Hovercraft Full Of Eels and after take care of this issue that you have Commented Oct 5, 2014 at 20:43

1 Answer 1

2

I fear that you may wish to start over as your code looks off the mark from the get-go. Each Country should be defined by interface methods that return a name String, a population int or long, an area int or long and a density, which perhaps a double. Each individual Country has no need of a largest anything, and should have no method that returns largestXXX() as that should be a property of a Country collection, not an individual Country.

To simplify, you might want something like:

interface Country {
  String getName();
  int getPopulation();
  // ... etc
}

class DefaultCountry implements Country {
   // implement your methods
}

interface CountryCollection {
  addCountry(Country country);
  removeCountry(Country country);
  Country getLargestPopulation();
  Country getLargestArea();
  ....
}

OK, your problem will be to implement the methods of your CountryCollection interface. The concrete class will likely hold a List<Country> and assign this an ArrayList<Country>. then in the getLargestXXX() method, it use a for loop, iterate through the ArrayList, find the Country with the largest XXX and return that Country. For instance, if you're trying to find the largest population, create an int and Country local variable in the method, say called largestPop and selectedCountry, and inside the loop call getPopulation() on each Country. If the population is greater than the current largestPop, then set the current Country's population to the largestPop variable and the current Country to the selectedCountry. At the end of the method, return the selected Country.

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

3 Comments

I'm not familiar with the usage of the second interface, could you elaborate how it works?
@user3451158: Consider first posting your full complete assignment requirements as I don't know how many interfaces you need for your code.
Awesome I think this is what I was looking for!!

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.