Log In · Register

 
 
Closed TopicStart new topic
JAVA classes with Instance Variable DUE IN 10 Min !!!
deteam
post Mar 13 2009, 10:04 PM
Post #1


Senior Member
*****

Group: Human
Posts: 525
Joined: Nov 2008
Member No: 695,913



Write a class named ParkingMeter containing:
Two instance variables named timeLeft and maxTime of type int. The value of timeLeft should be initialized to 0.
A constructor accepting a single integer parameter whose value is used to initialize the maxTime instance variable.
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. Furthermore, the increase occurs only if the value of timeLeft will not exceed the value of maxTime . add returns a boolean value: true if timeLeft was increase, 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 i get

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
 
mipadi
post Mar 13 2009, 10:09 PM
Post #2


Senior Member
******

Group: Administrator
Posts: 2,648
Joined: Apr 2008
Member No: 639,265



Based on the instructions you posted, that looks good. However, in add(), it timeLeft not supposed to increase at all if timeLeft + 30 > maxTime, or if timeLeft + 30 > maxTime, should timeLeft == maxTime? In other words, if incrementing by 30 would exceed the max time, do you increment up to the max time instead, or not increment at all?
 
deteam
post Mar 13 2009, 10:13 PM
Post #3


Senior Member
*****

Group: Human
Posts: 525
Joined: Nov 2008
Member No: 695,913



"Furthermore, the increase occurs only if the value of timeLeft will not exceed the value of maxTime ."

idk if that answers ur question but im still getting this error message . .

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



the example of when it fails is this

cents = 25
maxTime = 30
ticks = 7
isExpired = true <----------thats suppossed to be false.
 
mipadi
post Mar 13 2009, 10:24 PM
Post #4


Senior Member
******

Group: Administrator
Posts: 2,648
Joined: Apr 2008
Member No: 639,265



Your one line in add should read like this:

CODE
if (timeLeft + 30 <= maxTime) {


Note the <= maxTime.

Also, tick() should do this:

CODE
timeLeft--;


Note that you should decrement, not increment.
 
deteam
post Mar 13 2009, 10:30 PM
Post #5


Senior Member
*****

Group: Human
Posts: 525
Joined: Nov 2008
Member No: 695,913



thnx so much 4 ur help both codes worked perfeclty thumbsup.gif

Stay Up
-1-
Subliminal
 
manny-the-dino
post Mar 13 2009, 10:33 PM
Post #6


Senior Member
*******

Group: Administrator
Posts: 8,629
Joined: Jan 2007
Member No: 498,468



Topic Closed & Moved
 

Closed TopicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members: