public class BasketballTeam { private int score; private String name; public Bas
ID: 3753299 • Letter: P
Question
public class BasketballTeam
{
private int score;
private String name;
public BasketballTeam(String s)
{
score = 0;
name = s;
}
public int getScore( )
{
return score;
}
public String getName( )
{
return name;
}
public String toString( )
{
return ("The " + name + " have " + score + " points. ");
}
public int freeThrow( )
{
score++;
return score;
}
public int threePoint( )
{
score += 3;
return score;
}
public int makeShot( )
{
score += 2;
return score;
}
}
2
1. Consider the class BasketballTeam above. What is the output of the following demo
driver program?
BOX IN YOUR ANSWER
public class BigGame
{
public static void main(String[ ] args)
{
BasketballTeam team1 = new BasketballTeam("Raiders");
BasketballTeam team2 = new BasketballTeam("Tigers");
team1.makeShot( );
team2.freeThrow( );
team1.threePoint( );
System.out.println ("Team 1 has " + team1.freeThrow( ) + " points.");
team2.makeShot( );
System.out.print(team1);
System.out.print(team2);
}
}
2. Using the BasketballTeam class above, write a complete demo driver class on the next
page, which simulates a basketball game in the following way:
instantiate two teams as above, and then assume the teams alternate possession of the ball
for 20 times each.
During each possession
(show each possession of the ball with an if-else
chain), the team with the ball has the following probabilities:
35% chance of making a 2-point shot
15% chance of making a 3-point shot
20% chance of making a free throw
30% chance of turning over the ball without scoring
Use a double variable set to equal Math.random( ), together with some
if-else statements, to control the probabilities. After each possession,
print the score of both teams to the screen.
Explanation / Answer
If we run the code given above, we will get the output as
it will create 2 teams Raiders and Tiger.
Then with team raider, makeshot function is called which will increase the score by 2.
Raider=2, Tiger=0
Then with tiger, freethrow function will increment the score by 1
Raider=2, Tiger=1
Then with team tiger, threePoint function is called which will increase the score by 3.
Raider=5, Tiger=1
Then with team raider, the score is shown after calling freeshot function which will increase the score by 1. So score of Raiders is printed as 6
Raider=6, Tiger=1
Then with team tiger, makeshot function is called which will increase the score by 2.
Raider=6, Tiger=3
At the end toString function of Raider and Tigers are called to print the content of the objects
Output:
run:
Team 1 has 6 points.
The Raiders have 6 points.
The Tigers have 3 points.
2. Answer
GameDriver.java
//driver class
public class GameDriver {
//main function to execute the class
public static void main(String[] args) {
//creating two teams for the match
BasketballTeam team1=new BasketballTeam("Raiders");
BasketballTeam team2=new BasketballTeam("Tigers");
//running the loop 40 times so that each team has ball for 20 times each
for(int i=1;i<=40;i++){
double rand=Math.random()*100;//creating a random variable for obtaining the random values
int prob=(int) rand;//converting double to integer value inorder to obtain the probability
if(i%2==0){//if the turn is odd then the first team will have the ball
if(prob>=65 && prob<=100){//probability is 35%
team1.makeShot();//calling for two point shot
}
else if(prob>=50 && prob<65){//probability is 15%
team1.threePoint();//calling for two point shot
}
else if(prob>=30 && prob<50){//probability is 20%
team1.freeThrow();//calling function to score one point
}
else{//probability is 30%
//can't score so pass the ball
}
}
else{
if(prob>=65 && prob<=100){//probability is 35%
team2.makeShot();//calling for two point shot
}
else if(prob>=50 && prob<65){//probability is 15%
team2.threePoint();//calling for two point shot
}
else if(prob>=30 && prob<50){//probability is 20%
team2.freeThrow();//calling function to score one point
}
else{//probability is 30%
//can't score so pass the ball
}
}
//end of for loop
}
//printing the scores of the team
System.out.println(team1.toString());
System.out.println(team2.toString());
//end of main function
}
//end of class
}
Output:
1st time execution
The Raiders have 25 points.
The Tigers have 37 points.
2nd time execution
The Raiders have 26 points.
The Tigers have 25 points.
3rd time execution
The Raiders have 30 points.
The Tigers have 35 points.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.