Calculation Returning Infinite Value, Why?

163 views Asked by At

I'm doing the culminating project for my intro java class and cannot figure out why I am getting an infinite value for the PercentageCap Calculation. Let me know if you see something in my code that I am missing or I need to look at. The program must have a main class and two seperate classes that feed into it so there is quite a bit of code! I have highlighted in bold where each class starts along with the calculation that I cannot figure out. If you have any additional questions about the program feel free to leave them in the comments.

DEMO PROGRAM

import javax.swing.JOptionPane;

// import statements

public class BaseballStatsDemo 
{

public static void main(String[]args)
{

    // variables

    String playerName, teamName, status, experience, theNumber, theHits, theRuns, theAtBats,  theHomeRuns, theRbi, theStrikeouts, theHitByPitch, theSacrificeFlies, theWalks, theBaseHits, theDoubles, theTriples, thePlayerSalary;
    double hits, runs, atBats, homeRuns, rbi, strikeouts, hitByPitch, sacrificeFlies, walks, baseHits, doubles, triples, number, playerSalary, salaryCap = 0;


    PlayerStats information = new PlayerStats();
    FranchiseStats franchiseInfo = new FranchiseStats();
    //user input

    playerName = JOptionPane.showInputDialog("Enter Player Name: ");
    teamName = JOptionPane.showInputDialog("Enter Team Name (IE Boston Red Sox, Los Angelos Dodges): ");

    theNumber = JOptionPane.showInputDialog("Enter Player Number: ");
    number = Double.parseDouble(theNumber);

    status = JOptionPane.showInputDialog("Enter Player Status (Active, DL, Inactive): ");

    experience = JOptionPane.showInputDialog("Rookie, Experienced, Veteran");

    thePlayerSalary = JOptionPane.showInputDialog("Enter Player Salary: ");
    playerSalary = Double.parseDouble(thePlayerSalary);


    theHits = JOptionPane.showInputDialog("Enter Total Hits:");
    hits = Double.parseDouble(theHits);

    theRuns = JOptionPane.showInputDialog("Enter Total Runs:");
    runs = Double.parseDouble(theRuns);

    theAtBats = JOptionPane.showInputDialog("Enter Total At Bats:");
    atBats = Double.parseDouble(theAtBats);

    theBaseHits = JOptionPane.showInputDialog("Enter Total Singles:");
    baseHits = Double.parseDouble(theBaseHits);

    theDoubles = JOptionPane.showInputDialog("Enter Total Doubles:");
    doubles = Double.parseDouble(theDoubles);

    theTriples = JOptionPane.showInputDialog("Enter Total Triples:");
    triples = Double.parseDouble(theTriples);

    theHomeRuns = JOptionPane.showInputDialog("Enter Total Homeruns:");
    homeRuns = Double.parseDouble(theHomeRuns);

    theWalks = JOptionPane.showInputDialog("Enter Total Walks:");
    walks = Double.parseDouble(theWalks);

    theRbi = JOptionPane.showInputDialog("Enter Total RBI's:");
    rbi = Double.parseDouble(theRbi);

    theStrikeouts = JOptionPane.showInputDialog("Enter Total Strikeouts");
    strikeouts = Double.parseDouble(theStrikeouts);

    theHitByPitch = JOptionPane.showInputDialog("Enter Total RBI's:");
    hitByPitch = Double.parseDouble(theHitByPitch);

    theSacrificeFlies = JOptionPane.showInputDialog("Enter Total Sacrifice Flies:");
    sacrificeFlies = Double.parseDouble(theSacrificeFlies);

    information.setHits(hits);
    information.setAtBats(atBats);
    information.setHomeRuns(homeRuns);
    information.setHitByPitch(hitByPitch);
    information.setSacrificeFlies(sacrificeFlies);
    information.setWalks(walks);
    information.setBaseHits(baseHits);
    information.setDoubles(doubles);
    information.setTriples(triples);

    franchiseInfo.setTeamName(teamName);
    franchiseInfo.setPlayerSalary(playerSalary);
    franchiseInfo.setSalaryCap(salaryCap);



    JOptionPane.showMessageDialog(null, "Player Name: " + playerName +
                                        "\nTeam Name: " + teamName +
                                        "\nPlayer Number: " + number +
                                        "\nStatus: " + status +
                                        "\nExperience: " + experience +
                                        "\nSalary: " + playerSalary +
                                        "\n\n Player Statistics" +
                                        "\nHits: " + hits +
                                        "\nAt Bats: " + atBats +
                                        "\nRuns: " + runs +
                                        "\nBase Hits: " + baseHits +
                                        "\nDoubles: " + doubles +
                                        "\nTriples: " + triples +
                                        "\nHome Runs: " + homeRuns +
                                        "\nWalks: " + walks +
                                        "\nRBI's: " + rbi +
                                        "\nStrikeouts: " + strikeouts + 
                                        "\nHit By Pitch: " + hitByPitch +
                                        "\nSacrifice Flies: " + sacrificeFlies +
                                        "\n\n Season Averages" +
                                        "\n Batting Average: " + information.calcAverage() +
                                        "\n Slugging Percentage: " + information.calcSluggingPercentage() +
                                        "\n On Base Percentage: " + information.calcOnBasePercentage() +
                                        "\n\n Percentage of Team Salary Cap: " + **franchiseInfo.calcPercentageCap()**
                                        );





}

}

Class 1

public class PlayerStats 
{
//Attributes (fields)

private String PlayersName;
private double Hits, AtBats, HomeRuns, HitByPitch, SacrificeFlies, 
                Walks, BaseHits, Doubles, Triples;

//Getters and Setters

public String getPlayersName()
{
    return PlayersName;
}

public void setHits (double theHits)
{
    Hits = theHits;
}

public double getHits()
{
    return Hits;
}

public void setAtBats (double theAtBats)
{
    AtBats = theAtBats;
}

public double getAtBats()
{
    return AtBats;
}

public void setHomeRuns (double theHomeRuns)
{
    HomeRuns = theHomeRuns;
}

public double getHomeRuns()
{
    return HomeRuns;
}

public void setHitByPitch (double theHitByPitch)
{
    HitByPitch = theHitByPitch;
}

public double getHitByPitch()
{
    return HitByPitch;
}

public void setSacrificeFlies (double theSacrificeFlies)
{
    SacrificeFlies = theSacrificeFlies;
}

public double getSacrificeFlies()
{
    return SacrificeFlies;
}

public void setWalks (double theWalks)
{
    Walks = theWalks;
}

public double getWalks()
{
    return Walks;
}

public void setBaseHits (double theBaseHits)
{
    BaseHits = theBaseHits;
}

public double getBaseHits()
{
    return BaseHits;
}

public void setDoubles (double theDoubles)
{
    Doubles = theDoubles;
}

public double getDoubles()
{
    return Doubles;
}

public void setTriples (double theTriples)
{
    Triples = theTriples;
}

public double getTriples()
{
    return Triples;
}

//Players Statistics Calculations

public double calcAverage()
{
    double Average;

    Average = Hits/AtBats;

    return Average;
}

public double calcSluggingPercentage()
{
    double SluggingPercentage;

    SluggingPercentage = (BaseHits) + (2*Doubles) + (3*Triples) + (4*HomeRuns)/AtBats;

    return SluggingPercentage;
}

public double calcOnBasePercentage()
{
    double OnBasePercentage;

    OnBasePercentage = (Hits+Walks+HitByPitch)/(AtBats+Walks+HitByPitch+SacrificeFlies);

    return OnBasePercentage;
}

}

CLASS 2

public class FranchiseStats 
{

String league, teamName;
double seriesWins, salaryCap, playerSalary;



public void setTeamName(String theTeamName)
{
    teamName = theTeamName;
}

public String getTeamName()
{
    return teamName;
}


public double franchiseSalaryCap()
{


    if(teamName == "New York Yankees")
    {
        salaryCap = 228995945;
        return salaryCap;
    }
    else if(teamName == "Boston Red Sox")
    {
        salaryCap = 158967286;
        return salaryCap;

    }
    else if(teamName == "Philadelphia Phillies")
    {
        salaryCap = 159578214;
        return salaryCap;
    }
    else if(teamName == "Anaheim Angels")
    {
        salaryCap = 142165250;
        return salaryCap;
    }
    else if(teamName == "Chicago White Sox")
    {
        salaryCap = 124065277;
        return salaryCap;
    }
    else if(teamName == "Chicago Cubs")
    {
        salaryCap = 104150726;
        return salaryCap;
    }
    else if(teamName == "New York Mets")
    {
        salaryCap = 88877033;
        return salaryCap;
    }
    else if(teamName == "San Francisco Giants")
    {
        salaryCap = 142180333;
        return salaryCap;
    }
    else if(teamName == "Minnesota Twins")
    {
        salaryCap = 75562500;
        return salaryCap;
    }
    else if(teamName == "Detroit Tigers")
    {
        salaryCap = 149046844;
        return salaryCap;
    }
    else if(teamName == "St. Louis Cardinals")
    {
        salaryCap = 116702085;
        return salaryCap;
    }
    else if(teamName == "Los Angeles Dodgers")
    {
        salaryCap = 216302909;
        return salaryCap;
    }
    else if(teamName == "Texas Rangers")
    {
        salaryCap = 127197575;
        return salaryCap;
    }
    else if(teamName == "Colorado Rockies")
    {
        salaryCap = 75449071;
        return salaryCap;
    }
    else if(teamName == "Atlanta Braves")
    {
        salaryCap = 89288193;
        return salaryCap;
    }
    else if(teamName == "Seattle Mariners")
    {
        salaryCap = 84295952; 
        return salaryCap;
    }
    else if(teamName == "Milwaukee Brewers")
    {
        salaryCap = 91003366;
        return salaryCap;
    }
    else if(teamName == "Baltimore Orioles")
    {
        salaryCap = 91793333;
        return salaryCap;
    }
    else if(teamName == "Cincinnati Reds")
    {
        salaryCap = 110565728;
        return salaryCap;
    }
    else if(teamName == "Houston Astros")
    {
        salaryCap = 24328538;
        return salaryCap;
    }
    else if(teamName == "Oakland Athletics")
    {
        salaryCap = 68577000;
        return salaryCap;
    }
    else if(teamName == "Washington Nationals")
    {
        salaryCap = 112431770;
        return salaryCap;
    }
    else if(teamName == "Miami Marlins")
    {
        salaryCap = 39621900;
        return salaryCap;
    }
    else if(teamName == "Arizona Diamondbacks")
    {
        salaryCap = 90158500;
        return salaryCap;
    }
    else if(teamName == "Cleveland Indians")
    {
        salaryCap = 82517300;
        return salaryCap;
    }
    else if(teamName == "Pittsburgh Pirates")
    {
        salaryCap = 66289524;
        return salaryCap;
    }
    else if(teamName =="Tampa Bay Devil Rays")
    {
        salaryCap = 57030272;
        return salaryCap;
    }
    else if(teamName == "Kansas City Royals")
    {
        salaryCap = 80491725;
        return salaryCap;
    }
    else if(teamName == "San Diego Padres")
    {
        salaryCap = 71689900;
        return salaryCap;
    }
    else
    {
        return salaryCap;
    }
}

public void setSalaryCap(double theSalaryCap)
{
    salaryCap = theSalaryCap;
}
public double getSalaryCap()
{
    return salaryCap;
}

public void setPlayerSalary (double thePlayerSalary)
{
    playerSalary = thePlayerSalary;
}


public double getPlayerSalary()
{
    return playerSalary;
}

public double calcPercentageCap()
{
    double PercentageCap;

    PercentageCap = playerSalary/salaryCap;

    return PercentageCap;
}

}

2

There are 2 answers

3
Makoto On

There's a lot of code, and you don't specify where you're getting the infinite value, but it's likely in one of your divisions - when you divide a double by zero, you get a signed infinity.

Make sure that your dividend is nonzero before dividing.

3
Le Duy Khanh On

salaryCap was not initialized, put it = 1.

replace

if(teamName == "New York Yankees")

by

if(teamName.equals("New York Yankees"))

your franchiseSalaryCap() always return 0