2013-01-20 58 views
4

我想在我的Athlete類,國家和名稱的兩個數組上打印第一個元素。我還需要創建一個模擬運動員三次潛水嘗試的對象(最初設置爲零)。我是OOP的新手,我不知道如何去做這些事情,只要我的主要工作就是這樣。這是我迄今所做的......String Java中的數組對象

這是主要的:

import java.util.Random; 
import java.util.List; 


public class Assignment1 { 
public static void main(String[] args) { 
      Athlete art = new Athlete(name[0], country[0], performance[0]); 
    } 
} 

我真的我不知道該怎麼辦...

這是與類陣列。

import java.util.Random; 
import java.util.List; 

public class Athlete { 

public String[] name = {"Art", "Dan", "Jen"}; 
public String[] country = {"Canada", "Germant", "USA"}; 
    //Here i would like to create something that would be representing 3 dive attemps (that relate to dive and score. eventually.) 

Athlete(String[] name, String[] country, Performance[] performance) { 
    this.name = name; 
    this.country=country; 
    this.performance=performance; 

} 



public Performance Perform(Dive dive){ 
    dive.getDiveName(); 
    return null;   
} 

public String[] getName() { 
    return name; 
} 

public void setName(String[] name) { 
    this.name = name; 
} 

public String[] getCountry() { 
    return country; 
} 

public void setCountry(String[] country) { 
    this.country = country; 
} 

    } 

在此先感謝您的任何幫助和意見! BTW還有其他類也一樣,只是沒有相關的ATM ..

回答

4

首先,如對於您的Athlete類,您可以刪除Getter and Setter方法,因爲您已聲明實例變量的訪問修飾符爲public。您可以通過Class.variable訪問變量。

但是,如果您確實想使用Getter and Setter,請將public修改器改爲private

第二個,對於構造函數,你正在試圖做一個簡單的技巧shadowingShadowing是當你有一個方法具有名稱作爲聲明變量的參數。這是shadowing一個例子:
----------Shadowing sample----------
您有以下類:

public String name; 

public Person(String name){ 
    this.name = name; // This is Shadowing 
} 

在您例如主要方法,你實例化的Person類,如下所示:
Person person = new Person("theolc");

可變name將等於"theolc"
----------End of shadowing----------

讓我們回到你的問題。如果您只想用當前的代碼打印第一個元素,則可以刪除Getter and Setter。刪除您的參數constructor

public class Athlete { 

public String[] name = {"Art", "Dan", "Jen"}; 
public String[] country = {"Canada", "Germany", "USA"}; 

public Athlete() { 

} 

在你的主要方法中,你可以這樣做。

public static void main(String[] args) { 
     Athlete art = new Athlete(); 

     System.out.println(art.name[0]); 
     System.out.println(art.country[0]); 
    } 
} 
+0

但是,我不明白你在潛水時想做什麼。 –

3

當前無法訪問名爲國家陣列,因爲他們是你痤瘡各類類的成員變量。

基於你看起來像試圖這樣做,這是行不通的。

這些數組屬於您的主類。

1

我覺得你有點搞砸了你在做什麼。 運動員是一個對象,運動員有一個名字,我有一個他住的城市。 運動員可以潛水。

public class Athlete { 

private String name; 
private String city; 

public Athlete (String name, String city){ 
this.name = name; 
this.city = city; 
} 
--create method dive, (i am not sure what exactly i has to do) 
public void dive(){} 
} 




public class Main{ 
public static void main (String [] args){ 

String name = in.next(); //enter name from keyboad 
String city = in.next(); //enter city form keybord 

--create a new object athlete and pass paramenters name and city into the object 
Athlete a = new Athlete (name, city); 

} 
} 
0

公共靜態無效的主要(字串[] args){

 public String[] name = {"Art", "Dan", "Jen"}; 
     public String[] country = {"Canada", "Germant", "USA"}; 
     // initialize your performance array here too. 

     //Your constructor takes arrays as an argument so you need to be sure to pass in the arrays and not just objects. 
     Athlete art = new Athlete(name, country, performance); 

} 
3

你在運動員類企圖似乎對付一羣運動員,這是一個設計錯誤。

定義一個類來表示單個運動員,與代表運動員的屬性的字段:

public class Athlete { 
    private final String name; 
    private final String country; 
    private List<Performance> performances = new ArrayList<Performance>(); 
    // other fields as required 

    public Athlete (String name, String country) { 
     this.name = name; 
     this.country = country; 
    } 
    // getters omitted 

    public List<Performance> getPerformances() { 
     return performances; 
    } 

    public Performance perform(Dive dive) { 
     // not sure what your intention is here, but something like this: 
     Performance p = new Performance(dive, this); 
     // add new performance to list 
     performances.add(p); 
     return p; 
    } 
} 

然後你的主要方法是採用TI這樣的:

public class Assignment1 { 
    public static void main(String[] args) { 
     String[] name = {"Art", "Dan", "Jen"}; 
     String[] country = {"Canada", "Germant", "USA"}; 
     Dive[] dive = new Dive[]{new Dive("somersault"), new Dive("foo"), new Dive("bar")}; 
     for (int i = 0; i < name.length; i++) { 
      Athlete athlete = new Athlete(name[i], country[i]); 
      Performance performance = athlete.perform(dive[i]); 
      // do something with athlete and/or performance 
     } 
    } 
} 
0

首先,數組毫無意義,讓我們擺脫它們:他們所做的只是提供模擬數據的值。你如何構造模擬對象一直是有爭議的事情,但顯然,在單元測試中,創建假運動員的代碼應該是。我會使用Joshua Bloch的靜態構建器來處理Athlete類,但現在只有兩個屬性,所以只需將它們傳遞給構造器即可。應該是這樣的:

class Athlete { 

    private String name; 
    private String country; 

    private List<Dive> dives; 

    public Athlete(String name, String country){ 
     this.name = name; 
     this.country = country; 
    } 

    public String getName(){ 
     return this.name; 
    } 

    public String getCountry(){ 
     return this.country; 
    } 

    public String getDives(){ 
     return this.dives; 
    } 

    public void addDive(Dive dive){ 
     this.dives.add(dive); 
    } 
} 

那麼對於潛水類:

class Dive { 

    private Athlete athlete; 
    private Date date; 
    private double score; 

    public Dive(Athlete athlete, double score){ 
     this.athlete = athlete; 
     this.score = score; 
     this.date = new Date(); 
    } 

    public Athlete getAthlete(){ 
     return this.athlete; 
    } 

    public Athlete getAthlete(){ 
     return this.athlete; 
    } 

    public Athlete getAthlete(){ 
     return this.athlete; 
    } 

} 

然後進行單元測試,只是構建類,操縱它們,確保它們正常工作。現在他們沒有做任何事情,所以你所能做的就是斷言他們保留了你所投入的潛水。例如:

@Test 
public void testThatDivesRetainInformation(){ 
    Athlete art = new Athlete("Art", "Canada"); 
    Dive art1 = new Dive(art, 8.5); 
    Dive art2 = new Dive(art, 8.0); 
    Dive art3 = new Dive(art, 8.8); 
    Dive art4 = new Dive(art, 9.2); 

    assertThat(art.getDives().size(), is(5)); 
    } 

然後,你可以辦理,並添加試驗之類的東西,確保你不能建立一個沒有潛水運動員等

你可以移動的運動員建設納入測試的設置方法,以便您可以在整個地方使用它。大多數IDE都支持重構。

+0

我需要使用陣列 – choloboy

+0

認真?然後把這些值放在數組中,並用for方法迭代,用一個常量來調用構造函數,例如:new Athlete(dataset [i] [name],dataset [i] [country]); – Rob

+0

OP中沒有提到TDD。雖然這是一次非常棒的對話,但這不是對這個問題的回答。 – LocalPCGuy