Java - 获取StackOverflowError

时间:2011-03-19 07:02:21

标签: java

继承我的程序,我不断收到此错误

  

线程“main”中的异常   java.lang.StackOverflowError的

这是作业

  

描述
  对于此程序,您将创建一个Quarterback类。   这是一个简单的对象   代表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);
        }

    }

}

2 个答案:

答案 0 :(得分:9)

public Quarterback()
{
    new Quarterback();
}

此代码进入无限递归循环。删除new Quarterback();行。

因为我们正在做家庭作业帮助......

public boolean equals(Object o)中,如果任何字段相等,则返回true。更有可能的是,您希望在返回true之前确保所有字段相等(使用&&运算符)。

您的getRating函数实际上并不需要任何参数。你可以删除它们。

在相关说明中,您的驱动程序将始终失败,因为通过的评级永远不会被计算出来。删除getRating的参数后,将passerRating替换为getRating()中的Quarterback.toString()

答案 1 :(得分:0)

另一个问题是Quarterback类toString()是递归调用的,只需将其更改为返回预期的字符串,如下所示,

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

要复制Quarterback对象的状态,无需编写单独的copy()方法,只需覆盖clone()方法,如下所示......在该工具Cloneable之前按类Quarterback

进行接口
public Quarterback clone() throws CloneNotSupportedException {
  Quarterback qb = (Quarterback) super.clone();
  return qb;
}

QBtester中将其称为

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