473,322 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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:

Expand|Select|Wrap|Line Numbers
  1. public class Timer {
  2.  
  3.     private int myBase; //The base
  4.     private int myStart; //The number
  5.     private int myReturn; //The base applied to the number
  6.  
  7.  
  8.     /**Creates a timer with the initial given base whose initial 
  9.      * value is 0.
  10.      */
  11.  
  12.     public Timer (int base){
  13.         myBase = base;
  14.     }
  15.  
  16.     /**Creates a timer with the initial value start. */
  17.  
  18.     public Timer (int base, int start){
  19.         myBase = base;
  20.         myStart = start;
  21.         myReturn = start % base;
  22.  
  23.     }
  24.  
  25.     /**Returns the value myStart */
  26.  
  27.     public int getValue() {
  28.         return myReturn;
  29.  
  30.     }
  31.  
  32.     public int increment() {
  33.         int x = ((myStart + 1) % myBase)/myBase;
  34.         return x;
  35.  
  36.     }
  37.  
  38.     public int increment(int inc){
  39.         int x = ((myStart + inc) % myBase)/myBase;
  40.         return x;
  41.  
  42.     }
  43.  
  44. }
  45.  
And here is the tester:
Expand|Select|Wrap|Line Numbers
  1. /**
  2.  * An application class to test the the Timer class.  
  3.  *
  4.  * Please note: this class uses programming constructs that have not yet been 
  5.  * covered in class and as such you are not expected to understand all of the 
  6.  * details of how this class works (yet).  It is sufficient to know that you 
  7.  * can run this class as a Java application and it will test the functionality
  8.  * of your Timer class.
  9.  */
  10. public class TimerTester {
  11.  
  12.     public static void main (String[] args) {
  13.  
  14.         System.out.println("*** This is a tester for the Timer class.***\n");
  15.         int failedTests = 0;
  16.  
  17.         //******* Testing constructor and getValue() method *********
  18.  
  19.         Timer timer1 = new Timer(13); // one-arg constructor 
  20.         Timer timer2 = new Timer(1);  // edge case, base is 1
  21.         Timer timer3 = new Timer(4, 3); // two-arg constructor
  22.         Timer timer4 = new Timer(7, 11); // two-arg constructor, value > base
  23.  
  24.         // expected data member values values
  25.         int expVal1=0, expVal2=0, expVal3=3, expVal4=4;
  26.  
  27.         if (timer1.getValue() != expVal1) {
  28.             System.out.println("*** Test Failed: Error in one-argument " +
  29.                     "constructor with parameter 13, or getValue() method. ****");
  30.             System.out.println("*** Expected data member value: " + expVal1);
  31.             System.out.println("*** Actual value: " + timer1.getValue() + "\n");
  32.             failedTests++;
  33.         }
  34.  
  35.         if (timer2.getValue() != expVal2) {
  36.             System.out.println("*** Test Failed: Error in one-argument " +
  37.                     "constructor with parameter 2, or getValue() method. ****");
  38.             System.out.println("*** Expected data member value: " + expVal2);
  39.             System.out.println("*** Actual value: " + timer2.getValue() + "\n");
  40.             failedTests++;
  41.         }
  42.  
  43.         if (timer3.getValue() != expVal3) {
  44.             System.out.println("*** Test Failed: Error in two-argument " +
  45.                     "constructor with parameters 4 and 2, or " +
  46.                     "getValue() method. ****");
  47.             System.out.println("*** Expected data member value: " + expVal3);
  48.             System.out.println("*** Actual value: " + timer3.getValue() + "\n");
  49.             failedTests++;
  50.         }
  51.  
  52.         if (timer4.getValue() != expVal4) {
  53.             System.out.println("*** Test Failed: Error in two-argument " +
  54.                     "constructor with parameters 7 and 11, or " +
  55.                     "getValue() method. ****");
  56.             System.out.println("*** Expected data member value: " + expVal4);
  57.             System.out.println("*** Actual value: " + timer4.getValue() + "\n");
  58.             failedTests++;
  59.         }
  60.  
  61.         // ******* Testing increment() method *************
  62.  
  63.         /* timer1 : base == 13
  64.          * timer2 : base == 1
  65.          * timer3 : base == 4
  66.          * timer4 : base == 7
  67.          */
  68.         // increment each timer object
  69.         int retValue1 = timer1.increment();
  70.         int retValue2 = timer2.increment();
  71.         int retValue3 = timer3.increment();
  72.         int retValue4 = timer4.increment();
  73.  
  74.         // expected data member values
  75.         expVal1=1;
  76.         expVal2=0;
  77.         expVal3=0;
  78.         expVal4=5;
  79.  
  80.         // expected return values on increment
  81.         int expRetVal1=0;
  82.         int expRetVal2=1;
  83.         int expRetVal3=1;
  84.         int expRetVal4=0;
  85.  
  86.         if (timer1.getValue() != expVal1) {
  87.             System.out.println("*** Test Failed: Error in " +
  88.                     "increment() method. ****");
  89.             System.out.println("*** Expected data member value: " + expVal1);
  90.             System.out.println("*** Actual data member value " + 
  91.                     timer1.getValue() + "\n");
  92.             failedTests++;
  93.         }
  94.  
  95.         if (retValue1 != expRetVal1) {
  96.             System.out.println("*** Test Failed: Error in " +
  97.                     "increment() method. ****");
  98.             System.out.println("*** Expected return value: " + expRetVal1);
  99.             System.out.println("*** Actual return value " + retValue1 + "\n");
  100.             failedTests++;
  101.         }
  102.  
  103.         if (timer2.getValue() != expVal2) {
  104.             System.out.println("*** Test Failed: Error in " +
  105.                     "increment() method. ****");
  106.             System.out.println("*** Expected data member value: " + expVal2);
  107.             System.out.println("*** Actual data member value " + 
  108.                     timer2.getValue() + "\n");
  109.             failedTests++;
  110.         }
  111.  
  112.         if (retValue2 != expRetVal2) {
  113.             System.out.println("*** Test Failed: Error in " +
  114.                     "increment() method. ****");
  115.             System.out.println("*** Expected return value: " + expRetVal2);
  116.             System.out.println("*** Actual return value " + retValue2 + "\n");
  117.             failedTests++;
  118.         }
  119.  
  120.         if (timer3.getValue() != expVal3) {
  121.             System.out.println("*** Test Failed: Error in " +
  122.                     "increment() method. ****");
  123.             System.out.println("*** Expected data member value: " + expVal3);
  124.             System.out.println("*** Actual data member value " + 
  125.                     timer3.getValue() + "\n");
  126.             failedTests++;    
  127.         }
  128.  
  129.         if (retValue3 != expRetVal3) {
  130.             System.out.println("*** Test Failed: Error in " +
  131.                     "increment() method. ****");
  132.             System.out.println("*** Expected return value: " + 0);
  133.             System.out.println("*** Actual return value " + retValue3 + "\n");
  134.             failedTests++;
  135.         }
  136.  
  137.         if (timer4.getValue() != expVal4) {
  138.             System.out.println("*** Test Failed: Error in " +
  139.                     "increment() method. ****");
  140.             System.out.println("*** Expected data member value: " + expVal4);
  141.             System.out.println("*** Actual data member value " + 
  142.                     timer4.getValue() + "\n");
  143.             failedTests++;    
  144.         }
  145.  
  146.         if (retValue4 != expRetVal4) {
  147.             System.out.println("*** Test Failed: Error in " +
  148.                     "increment() method. ****");
  149.             System.out.println("*** Expected return value: " + expRetVal4);
  150.             System.out.println("*** Actual return value " + retValue4 + "\n");
  151.             failedTests++;
  152.         }
  153.  
  154.         // ************ Testing increment(int) method ****************
  155.  
  156.         /* timer1 : base == 13
  157.          * timer2 : base == 1
  158.          * timer3 : base == 4
  159.          * timer4 : base == 7
  160.          */
  161.  
  162.         // increment each timer object by a varying amount
  163.         retValue1 = timer1.increment(0);
  164.         retValue2 = timer2.increment(28);
  165.         retValue3 = timer3.increment(54);
  166.         retValue4 = timer4.increment(8493);
  167.  
  168.         // expected data member values after each increment
  169.         expVal1=1;
  170.         expVal2=0;
  171.         expVal3=2;
  172.         expVal4=0;
  173.  
  174.         // expected return values after each increment
  175.         expRetVal1=0;
  176.         expRetVal2=28;
  177.         expRetVal3=13;
  178.         expRetVal4=1214;
  179.  
  180.         if (timer1.getValue() != expVal1) {
  181.             System.out.println("*** Test Failed: Error in " +
  182.                     "increment(int) method. ****");
  183.             System.out.println("*** Expected data member value: " + expVal1);
  184.             System.out.println("*** Actual data member value " + 
  185.                     timer1.getValue() + "\n");
  186.             failedTests++;
  187.         }
  188.  
  189.         if (retValue1 != expRetVal1) {
  190.             System.out.println("*** Test Failed: Error in " +
  191.                     "increment(int) method. ****");
  192.             System.out.println("*** Expected return value: " + expRetVal1);
  193.             System.out.println("*** Actual return value " + retValue1 + "\n");
  194.             failedTests++;
  195.         }
  196.  
  197.         if (timer2.getValue() != expVal2) {
  198.             System.out.println("*** Test Failed: Error in " +
  199.                     "increment(int) method. ****");
  200.             System.out.println("*** Expected data member value: " + expVal2);
  201.             System.out.println("*** Actual data member value " + 
  202.                     timer2.getValue() + "\n");
  203.             failedTests++;
  204.         }
  205.  
  206.         if (retValue2 != expRetVal2) {
  207.             System.out.println("*** Test Failed: Error in " +
  208.                     "increment(int) method. ****");
  209.             System.out.println("*** Expected return value: " + expRetVal2);
  210.             System.out.println("*** Actual return value " + retValue2 + "\n");
  211.             failedTests++;
  212.         }
  213.  
  214.         if (timer3.getValue() != expVal3) {
  215.             System.out.println("*** Test Failed: Error in " +
  216.                     "increment(int) method. ****");
  217.             System.out.println("*** Expected data member value: " + expVal3);
  218.             System.out.println("*** Actual data member value " + 
  219.                     timer3.getValue() + "\n");
  220.             failedTests++;    
  221.         }
  222.  
  223.         if (retValue3 != expRetVal3) {
  224.             System.out.println("*** Test Failed: Error in " +
  225.                     "increment(int) method. ****");
  226.             System.out.println("*** Expected return value: " + 0);
  227.             System.out.println("*** Actual return value " + retValue3 + "\n");
  228.             failedTests++;
  229.         }
  230.  
  231.         if (timer4.getValue() != expVal4) {
  232.             System.out.println("*** Test Failed: Error in " +
  233.                     "increment(int) method. ****");
  234.             System.out.println("*** Expected data member value: " + expVal4);
  235.             System.out.println("*** Actual data member value " + 
  236.                     timer4.getValue() + "\n");
  237.             failedTests++;    
  238.         }
  239.  
  240.         if (retValue4 != expRetVal4) {
  241.             System.out.println("*** Test Failed: Error in " +
  242.                     "increment(int) method. ****");
  243.             System.out.println("*** Expected return value: " + expRetVal4);
  244.             System.out.println("*** Actual return value " + retValue4 + "\n");
  245.             failedTests++;
  246.         }
  247.  
  248.         if (failedTests == 0) {
  249.             System.out.println("*** Congratulations, all tests passed. ***");
  250.         }
  251.         else {
  252.             System.out.println("*** " + failedTests + " tests have failed. ***");    
  253.         }
  254.     }
  255.  
  256. }
  257.  
Feb 11 '08 #1
1 1595
Plater
7,872 Expert 4TB
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.
Feb 18 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

44
by: Mariusz Jedrzejewski | last post by:
Hi, I'll be very grateful if somebody can explain me why my Opera 7.23 (runing under linux) doesn't show me inner tables. Using below code I can see only "inner table 1". There is no problem with...
3
by: JHR | last post by:
Hey all, I'm trying to make a sidebar box float to the right of various items, and for those items to wrap if a user shrinks his browser window. Instead, in every browser I've tried except for...
0
by: James Thurley | last post by:
I'm trying to dynamically compile assemblies and cache them to disk, which seems to work fine. When the data I'm compiling from changes, I want to re-generate the assembly and use the new version....
149
by: Christopher Benson-Manica | last post by:
(Followups set to comp.std.c. Apologies if the crosspost is unwelcome.) strchr() is to strrchr() as strstr() is to strrstr(), but strrstr() isn't part of the standard. Why not? --...
14
by: Tom.PesterDELETETHISSS | last post by:
Hi, I think this question requires an in depth understanding of how a browser cache works. I hope I can reach an expert here. I may have found a quirk in the asp.net documentation or I don't...
16
by: Dany | last post by:
Our web service was working fine until we installed .net Framework 1.1 service pack 1. Uninstalling SP1 is not an option because our largest customer says service packs marked as "critical" by...
21
by: Dmitry Anikin | last post by:
I mean, it's very convenient when default parameters can be in any position, like def a_func(x = 2, y = 1, z): ... (that defaults must go last is really a C++ quirk which is needed for overload...
52
by: lovecreatesbeauty | last post by:
Why the C standard committee doesn't provide a standard implementation including the C compiler and library when the language standard document is published? C works on the abstract model of low...
9
by: Coleen | last post by:
Hi All :-) I am desperately looking for some help/information on how to direct page flow. Forget what I have done - here's what I need to do: I have a large ASPX.Net - VB.Net web application...
39
by: alex | last post by:
I've converted a latin1 database I have to utf8. The process has been: # mysqldump -u root -p --default-character-set=latin1 -c --insert-ignore --skip-set-charset mydb mydb.sql # iconv -f...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.