1.Imagine a tollbooth with a class called toll Booth. The two data items are a type unsigned int to hold the total number of cars, and a type double to hold the total amount of money collected. A constructor initializes both these to 0. A member function called payingCar ( ) increments the car total and adds 0.50 to the cash total. Another function, called nopayCar ( ), increments the car total but adds nothing to the cash total. Finally, a member function called displays the two totals.
public class Main
{
public static void main(String[] args) {
tollbooth t=new tollbooth();
t.payingcar();
t.nopayingcar();
t.display();
}
}
class tollbooth
{
private long car;
private double amount;
public tollbooth()
{
car=0;
amount=0;
}
public void payingcar()
{
car++;
amount+=0.50;
}
public void nopayingcar()
{
car++;
}
public void display()
{
System.out.println("Total cars: "+car);
System.out.println("Total amount: "+amount);
}
}
Comments
Leave a comment