2014-10-05 34 views
1

正如您在我的CountriesTest類中所看到的,我正在跟蹤最大的人口,地區和密度。然而,我想不出如何引用這些元素所處的位置,並使用國家類中的相應方法在該類別中插入具有最高值的國家的適當名稱。我知道界面似乎對這種情況毫無用處,但分配需要它。我在代碼中包含了筆記,我認爲我錯過的東西會去。從ArrayList調用方法的問題

編輯:這是分配的參數,因爲我的問題是有點混亂:

編寫一個程序,打印:面積最大的國家,人口最多的國家,並與該國人口密度最大。僅使用抽象類和抽象方法創建解決方案。

這裏是我的接口

public interface CountriesInterface 
{ 

    public String largestPop(); 

    public String largestArea(); 

    public String popDensity(); 
} 

下面是訪問接口

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

這裏是我的測試類的類

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

我已經多次閱讀你的問題,但我不明白是什麼讓你煩惱。你能澄清你的問題嗎?「但我想不出如何引用這些元素所在的位置,並使用國家類中的相應方法在所述類別中插入具有最高值的國家的適當名稱」 – 2014-10-05 20:29:13

+0

好的我想要做的是找出哪個國家有最大的人口,例如只打印那個國家的名字 – user3451158 2014-10-05 20:42:29

+0

我認爲首先你應該看看@Hovercraft Full Of Eels的答案,並在照顧這個問題你有 – 2014-10-05 20:43:50

回答

2

我擔心,你不妨先從因爲你的代碼看起來不符合標準。每個國家應該通過接口方法來定義,該方法返回一個名稱String,一個int或long的總體,一個int或long的區域以及一個密度,可能是一個double。每個國家都不需要最大的任何東西,並且不應該有返回largestXXX()的方法,因爲這應該是國家/地區集合的屬性,而不是單個國家/地區的屬性。

爲了簡化,你可能想是這樣的:

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,您的問題將是實現您CountryCollection接口的方法。具體課程可能會持有List<Country>並分配此ArrayList<Country>。然後在getLargestXXX()方法中,它使用for循環,遍歷ArrayList,找到具有最大XXX的國家並返回該國家/地區。例如,如果您正在嘗試查找最多的人口,請在該方法中創建一個int和Country局部變量(稱爲largestPop和selectedCountry),並在每個國家/地區循環調用getPopulation()。如果人口大於當前最大的流量,則將當前國家的人口設置爲最大的流量變量,將當前的國家設置爲所選國家。在方法結束時,返回所選國家。

+0

我不熟悉第二個界面的用法,你能詳細說明它的工作原理嗎? – user3451158 2014-10-05 20:50:29

+0

@ user3451158:考慮首先發布完整的完整作業需求,因爲我不知道您的代碼需要多少接口。 – 2014-10-05 21:00:12

+0

我只是把它作爲編輯 – user3451158 2014-10-05 21:06:04