0

I'm supposed to construct 2 teams (heat and spurs), then simulate a best-of-seven game series where a random number is generated, and anything above 0.5 gives the heat a win.

The problem is that we've never done a problem that looks even close to this in lecture. We're given two codes and one class has to call another (I think).

Everything starting with "//construct spurs" was added by me. The "heat" part above it was given.

//Warning: Do not change the given codes
import java.util.Random;
import java.util.Scanner;

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

    //construct Team Heat
    NBATeam heat= new NBATeam("Heats");
     System.out.print("How many players Heats own: ");
     Scanner input = new Scanner (System.in);
     int numberOfPlayers = input.nextInt();

    // Prompt user to enter players into the Team
     for (int i = 0; i < numberOfPlayers; i++) {
           System.out.print("Enter the name of Player #" + (i + 1) + ": ");
           String playerName = input.next();
           heat.addAPlayer(playerName);
     }

    //construct Team Spurs
             NBATeam spurs= new NBATeam("Spurs");
     System.out.print("How many players Spurs own: ");
     Scanner spursInput = new Scanner (System.in);
     int spursNumOfPlayers = spursInput.nextInt();


     //Your code here
            for (int i = 0; i < spursNumOfPlayers; i++) {
           System.out.print("Enter the name of Player #" + (i + 1) + ": ");
           String playerName = input.next();
           spurs.addAPlayer(playerName);

/*simulate a series of atmost 7 games by generating a random number; 
 * if the random number is bigger than 0.5, Heat wins; otherwise Heat losses a game.
 *  As soon as team wins 4 games, the series is over. */

int gamesPlayed=0; int heatWins=0; int spursWins=0; Random randNum = new Random(); randNum.nextDouble();


     //Your code here
while(gamesPlayed<8){
                        if (randNum.nextDouble()>0.5){
                            gamesPlayed++; heatWins++;

                        }
                        else{
                            spursWins++; gamesPlayed++;
                        }
    if (heatWins==4){

        System.out.println("Heat win!");
    }

    else{
        System.out.println("Spurs win!");
    }
        System.out.println(heat);
        System.out.println(spurs);

    }
}


}

}

The code below was given in a separate window/file and I did not do anything to it, because I do not even know what I am being asked to do.

public class NBATeam {

private String sTeamName;
private int nWin;
private int nLoss;
private String[] playerArray;
//Your code here

}//end of class definition

The directions are as follows:

"Given are the partial java source codes for NBATeam and NBA classes. NBATeam class is the class for a basketball team. The class sets the name of the team and assigns members name (name is input by the user). NBA class creates two teams Heats and Spurs using NBATeam class. It asks for the number of players in each team, then asks the name of the members. Members are added to the respective team using addAPlayer(String newPlayerName) method in NBATeam class."

Adrian Cid Almaguer
  • 7,815
  • 13
  • 41
  • 63
  • On the two lines that have "NBATeam heat" it reads: "cannot find symbol. class: NBATeam." I was hoping could offer something that could make it be able to "find" that class. – rebecca Feb 22 '15 at 01:55

1 Answers1

0

First thing you need to do is put the the NBATeam java file in the same package as the NBA file. This should get rid of the "cannot find symbol. class: NBATeam." If the NBATeam is not in the same package, then you need to import it, same way java.util.Scanner is being imported.

Second you need to create a constructor in the NBATeam class that accepts a String, where you use that string to set the team name. If you don't know what a constructor is, you can just google "What is a constructor in Java". Here is one of the first results.

The next thing I would do, though not instructed (seems like a missing instruction if what you are showing is all there is), is create a "setter" for the String[] playerArray in the NBATeam class, i.e. public void setPlayerArray(String[] playerArray). If you don't know how to complete this, you can google "What is a setter in Java". Here is one of the first results. After getting the number of players, from the user, you will wan't to create a new empty String[] with the number of players, then use the "setter" you created to set the array in the NBATeams object, i.e. heat/spurs.

Then you need to add a method public void addAPlayer(String name) to the NBATeam class. The body of this methods should set a value in the playerArray array, using the name argument and the value of i in the loop to determine the insertion point in the array.

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720