2011-03-19 90 views
0

繼承人我的計劃,我不斷收到此錯誤爪哇 - 獲取的StackOverflowError

異常線程 「main」 java.lang.StackOverflowError的

這是分配

描述
對於此程序,您將創建一個四分衛類。 這是一個簡單的對象, 代表NFL中的四分衛。 您可以創建額外的私人 方法,你覺得有必要或 有助於實現這個類。

package quarterback;  
public class Quarterback 
{ 

    private int attempts; 
    private int completions; 
    private String firstName; 
    private int interceptions; 
    private String lastName; 
    private int touchdowns; 
    private int yards; 
    private int passerRating; 
    //************************************************************ 
    public Quarterback() 
    { 
     new Quarterback(); 
    } 

    //**************************************************************** 

    public Quarterback(String firstName, String lastName, int completions, int attempts, int yards, int interceptions, int touchdowns) 
    { 
     this.firstName = firstName; 
     this.lastName = lastName; 
     this.completions = completions; 
     this.attempts = attempts; 
     this.yards = yards; 
     this.interceptions = interceptions; 
     this.touchdowns = touchdowns; 

    } 

     //***************************************************************** 

    public Quarterback copy() 
    { 
     Quarterback o = new Quarterback(); 
     o.firstName = this.firstName; 
     o.lastName = this.lastName; 
     o.completions = this.completions; 
     o.attempts = this.attempts; 
     o.yards = this.yards; 
     o.interceptions = this.interceptions; 
     o.touchdowns = this.touchdowns; 
     return o; 



    } 

     //****************************************************************** 

    public boolean equals(Object o) 
    { 
     if (((Quarterback) o).getAttempts() == this.attempts) return true; 
     if (((Quarterback) o).getCompletions() == this.completions) return true; 
     if (((Quarterback) o).getFirstName() == this.firstName) return true; 
     if (((Quarterback) o).getInterceptions() == this.interceptions) return true; 
     if (((Quarterback) o).getLastName() == this.getLastName()) return true; 
     if (((Quarterback) o).getTouchdowns() == this.touchdowns) return true; 
     if (((Quarterback) o).getYards() == this.yards) return true; 

     return false; 
    } 



     //********************************************************************* 

    public int getAttempts() 
    { 
     return this.attempts; 
    } 

     //******************************************************************************* 

    public int getCompletions() 
    { 
     return this.completions; 
    } 

     //********************************************************** 

    public String getFirstName() 
    { 
     return this.firstName; 
    } 

     //******************************************************************************* 

    public int getInterceptions() 
    { 
     return this.interceptions; 

    } 

     //**************************************************************************** 

    public String getLastName() 
    { 
     return this.lastName; 
    } 

     //**************************************************************************** 

    public double getRating(int a, int b, int c, int d, int passerRating) 
    { 
     a = (int) (((this.completions/this.attempts)-.3)*5); 
     b = (int) (((this.yards/this.attempts)-3)*.25); 
     c = ((this.touchdowns/this.attempts) *20); 
     d = (int) (((this.interceptions/this.attempts)*25)-2.375); 

     passerRating = (((a + b + c + d)/6)*100); 
     return passerRating; 
    } 

     //**************************************************************************** 

    public int getTouchdowns() 
    { 
     return this.touchdowns; 

    } 

     //***************************************************************************** 

    public int getYards() 
    { 
     return this.yards; 
    } 

      //*************************** 

    public void setAttempts(int attempts) 
    { 
     this.attempts = attempts; 

    } 

     //******************************************************************************* 

    public void setCompletions(int completions) 
    { 
     this.completions = completions; 

    } 

     //******************************************************************************* 

    public void setFirstName(String firstName) 
    { 
     this.firstName = firstName; 

    } 
     //******************************************************************************* 

    public void setInterceptions(int interceptions) 
    { 
     this.interceptions = interceptions; 
    } 
     //***************************************************************************** 

    public void setLastName(String lastName) 
    { 
    this.lastName = lastName; 
    } 
     //***************************************************************************** 

    public void setTouchdowns(int touchdowns) 
    { 
     this.touchdowns = touchdowns; 
    } 
     //***********************************************       

    public void setYards(int yards) 
    { 
     this.yards = yards; 
    } 
      //***************************************************************************** 

    public String toString() 
    { 
     System.out.println(this.firstName + " " + this.lastName + passerRating); 
     return toString(); 
    } 
} 

繼承人的司機

package quarterback; 

import java.util.Scanner; 

public class QBtester { 


    public static void main(String[] args) { 
     // Create a Quarterback 
     Quarterback qb = new Quarterback(); 

     // Test OK quarterback 
     qb.setFirstName("qb"); 
     qb.setLastName("ok"); 
     qb.setAttempts(465); 
     qb.setCompletions(272); 
     qb.setYards(2972); 
     qb.setTouchdowns(18); 
     qb.setInterceptions(12); 
     if ("qb ok 79.6".equals(qb.toString())) { 
      System.out.println("Passed OK QB"); 
     } else { 
      System.out.println("Failed OK QB"); 
      System.out.println("Expected: qb ok 79.6"); 
      System.out.println("Got: " + qb); 
     } 

     // Test max quarterback 
     qb.setFirstName("qb"); 
     qb.setLastName("max"); 
     qb.setAttempts(100); 
     qb.setCompletions(78); 
     qb.setYards(1275); 
     qb.setTouchdowns(12); 
     qb.setInterceptions(0); 
     if ("qb max 158.3".equals(qb.toString())) { 
      System.out.println("Passed max QB"); 
     } else { 
      System.out.println("Failed max QB"); 
      System.out.println("Expected: qb max 158.3"); 
      System.out.println("Got: " + qb); 
     } 

     // Test completions too high 
     qb.setFirstName("qb"); 
     qb.setLastName("comp2hi"); 
     qb.setAttempts(100); 
     qb.setCompletions(78); 
     qb.setYards(1000); 
     qb.setTouchdowns(7); 
     qb.setInterceptions(5); 
     if ("qb comp2hi 110.8".equals(qb.toString())) { 
      System.out.println("Passed completions too high"); 
     } else { 
      System.out.println("Failed completions too high"); 
      System.out.println("Expected: qb comp2hi 110.8"); 
      System.out.println("Got: " + qb); 
     } 

     // Test completions too low 
     qb.setFirstName("qb"); 
     qb.setLastName("comp2low"); 
     qb.setAttempts(100); 
     qb.setCompletions(25); 
     qb.setYards(1000); 
     qb.setTouchdowns(7); 
     qb.setInterceptions(5); 
     if ("qb comp2low 71.3".equals(qb.toString())) { 
      System.out.println("Passed completions too low"); 
     } else { 
      System.out.println("Failed completions too low"); 
      System.out.println("Expected: qb comp2low 71.3"); 
      System.out.println("Got: " + qb); 
     } 

     // Test completions 0 
     qb.setFirstName("qb"); 
     qb.setLastName("comp0"); 
     qb.setAttempts(100); 
     qb.setCompletions(30); 
     qb.setYards(1000); 
     qb.setTouchdowns(7); 
     qb.setInterceptions(5); 
     if ("qb comp0 71.3".equals(qb.toString())) { 
      System.out.println("Passed completions 0"); 
     } else { 
      System.out.println("Failed completions 0"); 
      System.out.println("Expected: qb comp0 71.3"); 
      System.out.println("Got: " + qb); 
     } 

     // Test yards too high 
     qb.setFirstName("qb"); 
     qb.setLastName("yards2hi"); 
     qb.setAttempts(100); 
     qb.setCompletions(50); 
     qb.setYards(1275); 
     qb.setTouchdowns(7); 
     qb.setInterceptions(5); 
     if ("qb yards2hi 98.3".equals(qb.toString())) { 
      System.out.println("Passed yards too high"); 
     } else { 
      System.out.println("Failed yards too high"); 
      System.out.println("Expected: qb yards2hi 98.3"); 
      System.out.println("Got: " + qb); 
     } 

     // Test yards too low 
     qb.setFirstName("qb"); 
     qb.setLastName("yards2low"); 
     qb.setAttempts(100); 
     qb.setCompletions(50); 
     qb.setYards(250); 
     qb.setTouchdowns(7); 
     qb.setInterceptions(5); 
     if ("qb yards2low 58.8".equals(qb.toString())) { 
      System.out.println("Passed yards too low"); 
     } else { 
      System.out.println("Failed yards too low"); 
      System.out.println("Expected: qb yards2low 58.8"); 
      System.out.println("Got: " + qb); 
     } 

     // Test yards 0 
     qb.setFirstName("qb"); 
     qb.setLastName("yards0"); 
     qb.setAttempts(100); 
     qb.setCompletions(50); 
     qb.setYards(300); 
     qb.setTouchdowns(7); 
     qb.setInterceptions(5); 
     if ("qb yards0 58.8".equals(qb.toString())) { 
      System.out.println("Passed yards 0"); 
     } else { 
      System.out.println("Failed yards 0"); 
      System.out.println("Expected: qb yards0 58.8"); 
      System.out.println("Got: " + qb); 
     } 

     // Test td too high 
     qb.setFirstName("qb"); 
     qb.setLastName("td2hi"); 
     qb.setAttempts(100); 
     qb.setCompletions(50); 
     qb.setYards(1000); 
     qb.setTouchdowns(14); 
     qb.setInterceptions(5); 
     if ("qb td2hi 104.2".equals(qb.toString())) { 
      System.out.println("Passed td too high"); 
     } else { 
      System.out.println("Failed td too high"); 
      System.out.println("Expected: qb td2hi 104.2"); 
      System.out.println("Got: " + qb); 
     } 

     // Test td 0 
     qb.setFirstName("qb"); 
     qb.setLastName("td0"); 
     qb.setAttempts(100); 
     qb.setCompletions(50); 
     qb.setYards(1000); 
     qb.setTouchdowns(0); 
     qb.setInterceptions(5); 
     if ("qb td0 64.6".equals(qb.toString())) { 
      System.out.println("Passed td 0"); 
     } else { 
      System.out.println("Failed td 0"); 
      System.out.println("Expected: qb td0 64.6"); 
      System.out.println("Got: " + qb); 
     } 

     // Test int too low 
     qb.setFirstName("qb"); 
     qb.setLastName("int2low"); 
     qb.setAttempts(100); 
     qb.setCompletions(50); 
     qb.setYards(1000); 
     qb.setTouchdowns(7); 
     qb.setInterceptions(11); 
     if ("qb int2low 69.2".equals(qb.toString())) { 
      System.out.println("Passed int too low"); 
     } else { 
      System.out.println("Failed int too low"); 
      System.out.println("Expected: qb int2low 69.2"); 
      System.out.println("Got: " + qb); 
     } 

     // Test int 0 
     qb.setFirstName("qb"); 
     qb.setLastName("int0"); 
     qb.setAttempts(1000); 
     qb.setCompletions(500); 
     qb.setYards(10000); 
     qb.setTouchdowns(70); 
     qb.setInterceptions(95); 
     if ("qb int0 69.2".equals(qb.toString())) { 
      System.out.println("Passed int 0"); 
     } else { 
      System.out.println("Failed int 0"); 
      System.out.println("Expected: qb int0 69.2"); 
      System.out.println("Got: " + qb); 
     } 

     // Test copy and equals 
     Quarterback qbcopy = new Quarterback(); 
     qbcopy = qb.copy(); 
     if (qbcopy.equals(qb)) { 
      // copy and equals works 
      System.out.println("Passed qb copy and equals"); 
     } else { 
      System.out.println("Failed qb copy and equals"); 
      System.out.println("Original qb: " + qb); 
      System.out.println("Copied qb: " + qbcopy); 
     } 

    } 

} 

回答

9
public Quarterback() 
{ 
    new Quarterback(); 
} 

這個代碼進入一個無限遞歸循環。刪除new Quarterback();行。

而且,由於我們正在做家庭作業幫助...

public boolean equals(Object o),如果字段的任何相等你返回true。更可能的是,您要確保所有的字段在返回true之前相等(使用&&運算符)。

您的getRating函數實際上並不需要任何參數。你可以刪除它們。

在相關說明中,您的驅動程序將始終失敗,因爲通過評分永遠不會計算。刪除getRating的參數後,將passerRating替換爲getRating()中的Quarterback.toString()

+0

謝謝,我現在明白了。 – Dakota 2011-03-19 07:56:45

0

還與另外一個問題是Quarterback類的toString()被稱爲遞歸只是更改如下,以返回預期的字符串,

public String toString() { 
    return this.firstName + " " + this.lastName + " " + getRating(); 
} 

,並報送複製Quarterback對象不需要寫一個單獨的copy()方法只覆蓋clone()方法如下...在此之前,由Quarterback類實現Cloneable接口,

public Quarterback clone() throws CloneNotSupportedException { 
    Quarterback qb = (Quarterback) super.clone(); 
    return qb; 
} 

QBtester叫它,

Quarterback qbCopy = null; 
    try { 
    qbCopy = qb.clone(); 
    } catch (CloneNotSupportedException e) { 
}