JAVA programming Question DUE IN 2o Min.!! |
JAVA programming Question DUE IN 2o Min.!! |
![]()
Post
#1
|
|
![]() Senior Member ![]() ![]() ![]() ![]() ![]() Group: Human Posts: 525 Joined: Nov 2008 Member No: 695,913 ![]() |
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 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 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. CODE public class ParkingMeter { int timeLeft = 0; int maxTime; public ParkingMeter( int max) { maxTime = max ; } public boolean add ( int coin ) { if ( coin == 25 ) { if ( timeLeft +30 <maxTime ) { timeLeft += 30; return true; } } return false; } public void tick() { if ( timeLeft > 0 ) { timeLeft += 1; } } public boolean isExpired() { return timeLeft == 0; } } ======================================... Here are the errors: Remarks: ⇒ Your add method doesn't modify timeLeft when the new value of timeLeft equals to maxTime Remarks: ⇒ At Execution Problems Detected: ⇒ isExpired() does not return the correct value Fails When: ⇒ isExpired is false can some one fix this ?> EDIT******** CODE class ParkingMeter{ int timeLeft=0; public ParkingMeter(int timeLeft){ this.timeLeft=timeLeft; } public boolean add(int coin){ if(coin==25) { timeLeft+=30;return true;} else return false; } public void tick(){ if(timeLeft>0) timeLeft--; } public boolean isExpired(){ return timeLeft==0; } } this is the updated code im using and it says this Remarks: ⇒ We expected, but did not find: • a public default (or no) constructor. ⇒ We did NOT expect, but found: • a public constructor accepting an int parameter.
Reason for edit: Please use codeboxes for long codes; thanks. - Cristy
|
|
|
![]() |
![]()
Post
#2
|
|
![]() Senior Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Administrator Posts: 8,629 Joined: Jan 2007 Member No: 498,468 ![]() |
Topic Closed & Moved
|
|
|
![]() ![]() |