Write a dass named ParkingMeter containing: . An instance variable named timelef
ID: 3872675 • Letter: W
Question
Write a dass 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 valus of timeleft is incrcased by 30; otherwise no increase is performed. add returns a boolean yalue: true if timeLeft was 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 yalue 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 otherwiseExplanation / Answer
public class ParkingMeter {
private int timeLeft = 0;
public boolean add(int n) {
if(n == 25) {
timeLeft += 30;
return true;
}
else return false;
}
public void tick() {
if(timeLeft > 0)
timeLeft--;
}
public boolean isExpired() {
if(timeLeft == 0) return true;
else return false;
}
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.