In: Computer Science
For this lab you will implement the class Ball which represents a toy ball. Ball should have a method bounce() whose return type is void, and should have an integer member bounces that counts the number of times the ball has been bounced. Ball should also have a static method named getTotalBounces which counts the total number of times that any Ball object has been bounced. To do this, you will want to include a static integer member totalBounces which counts the total number of bounces. Once implemented the following code in the main of UseBall should output the number 2 (You should create UseBall.java as well, right?): Ball b1 = new Ball(); Ball b2 = new Ball(); b1.bounce(); b2.bounce(); System.out.println(Ball.getTotalBounces()); Next, we will add some functionality to Ball. First, implement a method void reset(int count) which sets bounces to the value provided in count. How does this affect totalBounces? Next, include an integer variable to keep track of the total number of balls which have been created. Should this variable be static or dynamic? Use this variable to implement the static method getAverageBounces() which returns the average of the number of times that any ball has been bounced. Finally, as a challenge, try to implement the static method bounceAll() which bounces each of the balls at once. This will be difficult unless you include one more static integer member in the class - and even then it might be a bit tricky. Give it your best shot, and don't hesitate to ask the lab assistants for help!
The java program to implement the above functionality is :
Ball.java
import java.util.ArrayList;
public class Ball {
int numOfBounces=0;
static int totalBounces=0;
static int numOfBalls=0;
static ArrayList<Ball> list = new ArrayList<Ball>();
public Ball(){
numOfBalls++;
list.add(this);
}
void bounce(){
this.numOfBounces++;
totalBounces++;
}
void reset(int count){
totalBounces= totalBounces-numOfBounces+count;
this.numOfBounces=count;
}
static double getAverageBounces(){
return totalBounces/numOfBalls;
}
static void bounceAll(){
for(Ball b : list){
b.numOfBounces++;
totalBounces++;
}
}
}
UseBall.java
public class UseBall {
public static void main(String[] args){
Ball b1 = new Ball();
Ball b2 = new Ball();
for(int i=0;i<5;i++)
b1.bounce();
for(int i=0;i<5;i++)
b2.bounce();
System.out.println("number of Bounce for b1 is "+b1.numOfBounces);
System.out.println("number of Bounce for b2 is "+b2.numOfBounces);
System.out.println("Total number of Bounces for all balls is "+b1.totalBounces);
System.out.println("Avg no of bounces "+Ball.getAverageBounces());
System.out.println("---------------------------------------------------");
b2.reset(2);
System.out.println("number of Bounce for b1 is "+b1.numOfBounces);
System.out.println("number of Bounce for b2 is "+b2.numOfBounces);
System.out.println("Total number of Bounces for all balls is "+b1.totalBounces);
System.out.println("Avg no of bounces "+Ball.getAverageBounces());
System.out.println("---------------------------------------------------");
Ball.bounceAll();
System.out.println("number of Bounce for b1 is "+b1.numOfBounces);
System.out.println("number of Bounce for b2 is "+b2.numOfBounces);
System.out.println("Total number of Bounces for all balls is "+b1.totalBounces);
System.out.println("Avg no of bounces "+Ball.getAverageBounces());
}
}
The output screenhsot for the above program is :
If you have any queries reagrding this answer, please reach out through the comment section.