It doesn't do what I want it to!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • truedecembr
    New Member
    • Feb 2008
    • 1

    It doesn't do what I want it to!

    Hi everyone, I am brand new to Java and not really even sure what I'm doing... I'm supposed to be writing a Timer class that is part of a stop watch application, and it seems to me that the program is correct, but when I run the tester, it is obviously not. The goal is to enter a base and a time, and find time % base, then tell how many times it cycled back to zero. I don't know what is wrong with my program, because it appears to me that it should work, but it doesn't! If anyone has any input I would appreciate it so much. Thanks!

    Here is the Timer class that I wrote:

    Code:
    public class Timer {
    	
    	private int myBase; //The base
    	private int myStart; //The number
    	private int myReturn; //The base applied to the number
    
    		
    	/**Creates a timer with the initial given base whose initial 
    	 * value is 0.
    	 */
    	
    	public Timer (int base){
    		myBase = base;
    	}
    	
    	/**Creates a timer with the initial value start. */
    	
    	public Timer (int base, int start){
    		myBase = base;
    		myStart = start;
    		myReturn = start % base;
    
    	}
    	
    	/**Returns the value myStart */
    	
    	public int getValue() {
    		return myReturn;
    		
    	}
    	
    	public int increment() {
    		int x = ((myStart + 1) % myBase)/myBase;
    		return x;
    			
    	}
    	
    	public int increment(int inc){
    		int x = ((myStart + inc) % myBase)/myBase;
    		return x;
    		
    	}
    			
    }
    And here is the tester:
    Code:
    /**
     * An application class to test the the Timer class.  
     *
     * Please note: this class uses programming constructs that have not yet been 
     * covered in class and as such you are not expected to understand all of the 
     * details of how this class works (yet).  It is sufficient to know that you 
     * can run this class as a Java application and it will test the functionality
     * of your Timer class.
     */
    public class TimerTester {
    	
    	public static void main (String[] args) {
    		
    		System.out.println("*** This is a tester for the Timer class.***\n");
    		int failedTests = 0;
    		
    		//******* Testing constructor and getValue() method *********
    		
    		Timer timer1 = new Timer(13); // one-arg constructor 
    		Timer timer2 = new Timer(1);  // edge case, base is 1
    		Timer timer3 = new Timer(4, 3); // two-arg constructor
    		Timer timer4 = new Timer(7, 11); // two-arg constructor, value > base
    		
    		// expected data member values values
    		int expVal1=0, expVal2=0, expVal3=3, expVal4=4;
    		
    		if (timer1.getValue() != expVal1) {
    			System.out.println("*** Test Failed: Error in one-argument " +
    					"constructor with parameter 13, or getValue() method. ****");
    			System.out.println("*** Expected data member value: " + expVal1);
    			System.out.println("*** Actual value: " + timer1.getValue() + "\n");
    			failedTests++;
    		}
    		
    		if (timer2.getValue() != expVal2) {
    			System.out.println("*** Test Failed: Error in one-argument " +
    					"constructor with parameter 2, or getValue() method. ****");
    			System.out.println("*** Expected data member value: " + expVal2);
    			System.out.println("*** Actual value: " + timer2.getValue() + "\n");
    			failedTests++;
    		}
    		
    		if (timer3.getValue() != expVal3) {
    			System.out.println("*** Test Failed: Error in two-argument " +
    					"constructor with parameters 4 and 2, or " +
    					"getValue() method. ****");
    			System.out.println("*** Expected data member value: " + expVal3);
    			System.out.println("*** Actual value: " + timer3.getValue() + "\n");
    			failedTests++;
    		}
    		
    		if (timer4.getValue() != expVal4) {
    			System.out.println("*** Test Failed: Error in two-argument " +
    					"constructor with parameters 7 and 11, or " +
    					"getValue() method. ****");
    			System.out.println("*** Expected data member value: " + expVal4);
    			System.out.println("*** Actual value: " + timer4.getValue() + "\n");
    			failedTests++;
    		}
    		
    		// ******* Testing increment() method *************
    		
    		/* timer1 : base == 13
    		 * timer2 : base == 1
    		 * timer3 : base == 4
    		 * timer4 : base == 7
    		 */
    		// increment each timer object
    		int retValue1 = timer1.increment();
    		int retValue2 = timer2.increment();
    		int retValue3 = timer3.increment();
    		int retValue4 = timer4.increment();
    		
    		// expected data member values
    		expVal1=1;
    		expVal2=0;
    		expVal3=0;
    		expVal4=5;
    		
    		// expected return values on increment
    		int expRetVal1=0;
    		int expRetVal2=1;
    		int expRetVal3=1;
    		int expRetVal4=0;
    		
    		if (timer1.getValue() != expVal1) {
    			System.out.println("*** Test Failed: Error in " +
    					"increment() method. ****");
    			System.out.println("*** Expected data member value: " + expVal1);
    			System.out.println("*** Actual data member value " + 
    					timer1.getValue() + "\n");
    			failedTests++;
    		}
    		
    		if (retValue1 != expRetVal1) {
    			System.out.println("*** Test Failed: Error in " +
    					"increment() method. ****");
    			System.out.println("*** Expected return value: " + expRetVal1);
    			System.out.println("*** Actual return value " + retValue1 + "\n");
    			failedTests++;
    		}
    		
    		if (timer2.getValue() != expVal2) {
    			System.out.println("*** Test Failed: Error in " +
    					"increment() method. ****");
    			System.out.println("*** Expected data member value: " + expVal2);
    			System.out.println("*** Actual data member value " + 
    					timer2.getValue() + "\n");
    			failedTests++;
    		}
    		
    		if (retValue2 != expRetVal2) {
    			System.out.println("*** Test Failed: Error in " +
    					"increment() method. ****");
    			System.out.println("*** Expected return value: " + expRetVal2);
    			System.out.println("*** Actual return value " + retValue2 + "\n");
    			failedTests++;
    		}
    		
    		if (timer3.getValue() != expVal3) {
    			System.out.println("*** Test Failed: Error in " +
    					"increment() method. ****");
    			System.out.println("*** Expected data member value: " + expVal3);
    			System.out.println("*** Actual data member value " + 
    					timer3.getValue() + "\n");
    			failedTests++;	
    		}
    		
    		if (retValue3 != expRetVal3) {
    			System.out.println("*** Test Failed: Error in " +
    					"increment() method. ****");
    			System.out.println("*** Expected return value: " + 0);
    			System.out.println("*** Actual return value " + retValue3 + "\n");
    			failedTests++;
    		}
    		
    		if (timer4.getValue() != expVal4) {
    			System.out.println("*** Test Failed: Error in " +
    					"increment() method. ****");
    			System.out.println("*** Expected data member value: " + expVal4);
    			System.out.println("*** Actual data member value " + 
    					timer4.getValue() + "\n");
    			failedTests++;	
    		}
    		
    		if (retValue4 != expRetVal4) {
    			System.out.println("*** Test Failed: Error in " +
    					"increment() method. ****");
    			System.out.println("*** Expected return value: " + expRetVal4);
    			System.out.println("*** Actual return value " + retValue4 + "\n");
    			failedTests++;
    		}
    		
    		// ************ Testing increment(int) method ****************
    		
    		/* timer1 : base == 13
    		 * timer2 : base == 1
    		 * timer3 : base == 4
    		 * timer4 : base == 7
    		 */
    		
    		// increment each timer object by a varying amount
    		retValue1 = timer1.increment(0);
    		retValue2 = timer2.increment(28);
    		retValue3 = timer3.increment(54);
    		retValue4 = timer4.increment(8493);
    		
    		// expected data member values after each increment
    		expVal1=1;
    		expVal2=0;
    		expVal3=2;
    		expVal4=0;
    		
    		// expected return values after each increment
    		expRetVal1=0;
    		expRetVal2=28;
    		expRetVal3=13;
    		expRetVal4=1214;
    		
    		if (timer1.getValue() != expVal1) {
    			System.out.println("*** Test Failed: Error in " +
    					"increment(int) method. ****");
    			System.out.println("*** Expected data member value: " + expVal1);
    			System.out.println("*** Actual data member value " + 
    					timer1.getValue() + "\n");
    			failedTests++;
    		}
    		
    		if (retValue1 != expRetVal1) {
    			System.out.println("*** Test Failed: Error in " +
    					"increment(int) method. ****");
    			System.out.println("*** Expected return value: " + expRetVal1);
    			System.out.println("*** Actual return value " + retValue1 + "\n");
    			failedTests++;
    		}
    		
    		if (timer2.getValue() != expVal2) {
    			System.out.println("*** Test Failed: Error in " +
    					"increment(int) method. ****");
    			System.out.println("*** Expected data member value: " + expVal2);
    			System.out.println("*** Actual data member value " + 
    					timer2.getValue() + "\n");
    			failedTests++;
    		}
    		
    		if (retValue2 != expRetVal2) {
    			System.out.println("*** Test Failed: Error in " +
    					"increment(int) method. ****");
    			System.out.println("*** Expected return value: " + expRetVal2);
    			System.out.println("*** Actual return value " + retValue2 + "\n");
    			failedTests++;
    		}
    		
    		if (timer3.getValue() != expVal3) {
    			System.out.println("*** Test Failed: Error in " +
    					"increment(int) method. ****");
    			System.out.println("*** Expected data member value: " + expVal3);
    			System.out.println("*** Actual data member value " + 
    					timer3.getValue() + "\n");
    			failedTests++;	
    		}
    		
    		if (retValue3 != expRetVal3) {
    			System.out.println("*** Test Failed: Error in " +
    					"increment(int) method. ****");
    			System.out.println("*** Expected return value: " + 0);
    			System.out.println("*** Actual return value " + retValue3 + "\n");
    			failedTests++;
    		}
    		
    		if (timer4.getValue() != expVal4) {
    			System.out.println("*** Test Failed: Error in " +
    					"increment(int) method. ****");
    			System.out.println("*** Expected data member value: " + expVal4);
    			System.out.println("*** Actual data member value " + 
    					timer4.getValue() + "\n");
    			failedTests++;	
    		}
    		
    		if (retValue4 != expRetVal4) {
    			System.out.println("*** Test Failed: Error in " +
    					"increment(int) method. ****");
    			System.out.println("*** Expected return value: " + expRetVal4);
    			System.out.println("*** Actual return value " + retValue4 + "\n");
    			failedTests++;
    		}
    		
    		if (failedTests == 0) {
    			System.out.println("*** Congratulations, all tests passed. ***");
    		}
    		else {
    			System.out.println("*** " + failedTests + " tests have failed. ***");	
    		}
    	}
    
    }
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Posting homework questions is against the posting guidelines, however, since you appeared to have tried to do the assignment and have questions about it, I will offer up some advice on it.
    Your increment functions don't appear to retain their "incrementation ", it probably returns the correct value the first time, but never updates it's internal members to reflect the incrementing, so future calls will be incorrect.
    Both increment functions appear to fail on this.

    Comment

    Working...