Question

In: Computer Science

For this lab you will implement the class Ball which represents a toy ball. Ball should...

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!

Solutions

Expert Solution

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.


Related Solutions

For this Lab you have to implement a class Builder. Your Builder class should have instance...
For this Lab you have to implement a class Builder. Your Builder class should have instance variable name. , Supply a constructor method for your Builder class and the following methods: getName(), makeRow(int n, String s), printPyramid(int n, String s). Examining the problem, we need to create a Builder class, declare Builder class as follows public class Builder { } Inside the Builder class, declare a String variable called name. Step 3: Defining the constructors: Remember that the purpose of...
JAVA - Design and implement a class called Flight that represents an airline flight. It should...
JAVA - Design and implement a class called Flight that represents an airline flight. It should contain instance data that represent the airline name, the flight number, and the flight’s origin and destination cities. Define the Flight constructor to accept and initialize all instance data. Include getter and setter methods for all instance data. Include a toString method that returns a one-line description of the flight. Create a driver class called FlightTest, whose main method instantiates and updates several Flight...
Based on a Node class; Use Java to implement the OrderedList class which represents an ordered...
Based on a Node class; Use Java to implement the OrderedList class which represents an ordered singly linked list that cannot contain any duplicates. Note that items in the OrderedList are always kept in descending order. Complete the class with the following methods. Default constructor Create an empty list i.e., head is null. boolean insert(int data) Insert the given data into the list at the proper location in the list. If the insertion is successful, the function returns true; otherwise,...
Define and implement a class named Coach, which represents a special kind of person. It is...
Define and implement a class named Coach, which represents a special kind of person. It is to be defined by inheriting from the Person class. The Coach class has the following constructor: Coach(string n, int sl) // creates a person with name n, whose occupation // is “coach” and service length is sl The class must have a private static attribute static int nextID ; which is the unique personID of the next Coach object to be created. This is...
Overview For this assignment, implement and use the methods for a class called Seller that represents...
Overview For this assignment, implement and use the methods for a class called Seller that represents information about a salesperson. The Seller class Use the following class definition: class Seller { public: Seller(); Seller( const char [], const char[], const char [], double ); void print(); void setFirstName( const char [] ); void setLastName( const char [] ); void setID( const char [] ); void setSalesTotal( double ); double getSalesTotal(); private: char firstName[20]; char lastName[30]; char ID[7]; double salesTotal; };...
Overview For this assignment, implement and use the methods for a class called Seller that represents...
Overview For this assignment, implement and use the methods for a class called Seller that represents information about a salesperson. The Seller class Use the following class definition: class Seller { public: Seller(); Seller( const char [], const char[], const char [], double ); void print(); void setFirstName( const char [] ); void setLastName( const char [] ); void setID( const char [] ); void setSalesTotal( double ); double getSalesTotal(); private: char firstName[20]; char lastName[30]; char ID[7]; double salesTotal; };...
Magic 8 Ball(JAVA) A magic 8 ball is a popular fortune telling toy in which the...
Magic 8 Ball(JAVA) A magic 8 ball is a popular fortune telling toy in which the user places the ball face down, asks a yes-or-no question and turns the ball face up to reveal the answer. The standard magic 8 ball has 20 standard answers shown below, where 10 are positive (green), 5 are non-committal (yellow), and 5 are negative (red) (credit: Wikipedia) Write a program that does the following: 1. Stores all the responses in a String array using...
In C++, Implement the following class that represents a clock. Clock - hour: int - minute:...
In C++, Implement the following class that represents a clock. Clock - hour: int - minute: int - meridiem: string + Clock() + Clock(hr: int, min: int, mer: string) + setTime(hr: int, min: int, mer: string): void + setHour(hr: int): void + setMinute(min: int): void + setMeridiem(mer: string): void + getHour(): int + getMinute(): int + getMeridiem(): string + void tick() + string asString() + string asStandard() Implementation Details: • The default constructor should set the clock to midnight (12:00...
in java Design and implement a class called Dog that contains instance data that represents the...
in java Design and implement a class called Dog that contains instance data that represents the dog’s name and age. Define the Dog constructor to accept and initialize instance data. Include getter and setter methods for the name and age. Include a method to compute and return the age of the dog in “person years” (seven times the dog’s age). Include a toString method that returns a one-line description of the dog. Create a Tester class called Kennel, whose main...
Design and implement a class Rectangle to represent a rectangle. You should provide two Constructors for...
Design and implement a class Rectangle to represent a rectangle. You should provide two Constructors for the class, the first being the default constructor and the second which takes the basic dimensions and sets up the private member variables with the appropriate initial values. Methods should be provided that allow a user of the class to find out the length, width, area and perimeter of the shape plus a toString()method to print the values of the basic dimensions. Now implement...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT