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);
}
}