Re: Help with Java Calculator Program
ram@zedat.fu-berlin.de (Stefan Ram) writes:
On Oct 27, 9:37 am, r...@zedat.fu-berlin.de (Stefan Ram) wrote:
for( counter.reset(); integers.hasNext(); )
if( integers.getNext().isGreaterThan( 9 ))counter.increment();
I'm having problems with that code working.
It assumes appropriate types of ?counter? and ?integers?.
Thus, the operations ?reset()?, ?hasNext()?, ?getNext()?,
?isGreaterThan( 9 )? and ?increment()? usually still need to
be implemented.
Just for fun, here is a complete program with the above code
in them method ?main?, except that ?getNext? was replaced by
?next?:
public class Main
{
static class Counter
{ int value;
void reset(){ this.value = 0; }
void increment(){ if( value >= ++value )
throw new java.lang.RuntimeException(); }
public final java.lang.String toString()
{ return java.lang.String.valueOf( value ); }}
static class Integer
{ int value;
public Integer( final int value ){ this.value = value; }
public boolean isGreaterThan( final int value )
{ return this.value > value; }}
static final Counter counter = new Counter();
static final java.util.ArrayList<Integer> list =
new java.util.ArrayList<Integer>();
static
{ list.add( new Integer( 1 ));
list.add( new Integer( 11 ));
list.add( new Integer( 11 ));
java.lang.Runtime.getRuntime().addShutdownHook
( new java.lang.Thread
( new java.lang.Runnable()
{ public void run()
{ java.lang.System.out.println( counter ); }})); }
static final java.util.Iterator<Integer> integers = list.iterator();
public static void main( final java.lang.String[] args )
{
for( counter.reset(); integers.hasNext(); )
if( integers.next().isGreaterThan( 9 ))counter.increment(); }}
It prints ?2? because in the list, there are 2 numbers
greater than 9. Thus, it has counted the numbers greater
than 9.