Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Imagine a tollbooth at a bridge. Cars passing by the booth are expected to pay 5

ID: 3726234 • Letter: I

Question

Imagine a tollbooth at a bridge. Cars passing by the booth are expected to pay 50 centtoll. Mostly they do, but sometimes a car goes by without paying. The tollbooth keeps track of the numbers of the cars that have gone by, and of the total amount of the money collected. Model this tollbooth with a class that contains : Two data fields of type intto hold the total number of cars, and type doubleto hold the total amount of the money collected. A member function called payingCar()that increments the car total and adds 0.50 to the cash total. In java. Imagine a tollbooth at a bridge. Cars passing by the booth are expected to pay 50 centtoll. Mostly they do, but sometimes a car goes by without paying. The tollbooth keeps track of the numbers of the cars that have gone by, and of the total amount of the money collected. Model this tollbooth with a class that contains : Two data fields of type intto hold the total number of cars, and type doubleto hold the total amount of the money collected. A member function called payingCar()that increments the car total and adds 0.50 to the cash total. In java.

Explanation / Answer

// Java Tollbooth class to model a tollbooth at a bridge.

public class Tollbooth {

       // data fields

       private int numCars; // number of cars

       private double tollCollected; // amount of money collected

       // default constructor

       public Tollbooth()

       {

             numCars = 0;

             tollCollected = 0;

       }

       // parameterized constructor

       public Tollbooth(int cars, double toll)

       {

             this.numCars = cars;

             this.tollCollected = toll;

       }

       // getters

       public int getCars()

       {

             return numCars;

       }

      

       public double getToll()

       {

             return tollCollected;

       }

       //setters

       public void setCars(int cars)

       {

             this.numCars = cars;

       }

      

       public void setToll(double toll)

       {

             this.tollCollected = toll;

       }

       // method that increments the cars total and adds 0.5 to cash total when a car pays the toll

       public void payingCar()

       {

             this.numCars++;

             this.tollCollected += 0.5;

       }

}

// end of Tollbooth