//Warning: Do not change the given codes import java.util.Scanner; public class
ID: 671519 • Letter: #
Question
//Warning: Do not change the given codes
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
//Your code here
/*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. */
//Your code here
System.out.println(heat);
System.out.println(spurs);
}
}
}
}
_________________________________________________________________________________________________
public class NBATeam {
private String sTeamName;
private int nWin;
private int nLoss;
private String [] playerArray;
//Your code here
}//end of class definition
Explanation / Answer
Please find the required solution:
public class NBATeam {
private String sTeamName;
private int nWin;
private int nLoss;
private String[] playerArray;
public int getnWin() {
return nWin;
}
public void setnWin(int nWin) {
this.nWin = nWin;
}
public int getnLoss() {
return nLoss;
}
public void setnLoss(int nLoss) {
this.nLoss = nLoss;
}
// Your code here
public NBATeam(String sTeamName) {
this.sTeamName = sTeamName;
}
public void setSize(int size) {
playerArray = new String[size];
}
public void addAPlayer(String newPlayerName) {
for (int i = 0; i < playerArray.length; i++) {
if (playerArray[i] == null) {
playerArray[i] = newPlayerName;
break;
}
}
}
@Override
public String toString() {
String result = sTeamName + " [ ";
for (int i = 0; i < playerArray.length; i++) {
result += playerArray[i] + " ";
}
result += "]" + " win#:" + nWin + " loss#" + nLoss;
return result;
}
}// end of class definition
//Warning: Do not change the given codes
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();
heat.setSize(numberOfPlayers);
// 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: ");
numberOfPlayers = input.nextInt();
spurs.setSize(numberOfPlayers);
// 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();
spurs.addAPlayer(playerName);
}
// Your code here
/*
* 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 heatWin = 0, spurWin = 0;
for (int i = 0; i < 7; i++) {
if (Math.random() >= 0.5)
heatWin++;
else
spurWin++;
if (heatWin == 4 || spurWin == 4)
break;
}
heat.setnWin(heatWin);
heat.setnLoss(spurWin);
spurs.setnWin(spurWin);
spurs.setnLoss(heatWin);
// Your code here
System.out.println((heatWin > spurWin ? "Heats" : "Spurs")
+ " ***WIN the series*** ");
System.out.println(heat);
System.out.println(spurs);
input.close();
}
}
Sample Output:
How many players Heats own: 2Enter the name of Player #1: h1
Enter the name of Player #2: h2
How many players Spurs own: 2
Enter the name of Player #1: l1
Enter the name of Player #2: l2
Spurs ***WIN the series***
Heats [ h1 h2 ] win#:3 loss#4
Spurs [ l1 l2 ] win#:4 loss#3
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.