(using java) Write a class named ParkingMeter containing: An instance variable n
ID: 3905401 • Letter: #
Question
(using java)
Write a class named ParkingMeter containing:
An instance variable named timeLeft of type int, initialized to 0.
A method named add that accepts an integer parameter. If the value of the parameter is equal to 25, the value of timeLeft is increased by 30; otherwise no increase is performed. add returns a boolean value: true if timeLeftwas increased, false otherwise.
A method named tick that accepts no parameters and returns no value. tick decreases the value of timeLeft by 1, but only if the value of timeLeft is greater than 0.
A method named isExpired that accepts no parameters. isExpired returns a boolean value: true if the value of timeLeft is equal to 0; false otherwise.
Explanation / Answer
class ParkingMeter{ //Data member private int timeLeft=0; //Method to add a value to timeLeft public boolean add(int x){ if(x==25){ timeLeft+=30; return true; } return false; } //Method to reduce by 1 public void tick(){ if(timeLeft>0) timeLeft--; } //Checks if timeLeft is 0 public boolean isExpired(){ return timeLeft==0; } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.