473,756 Members | 1,808 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

It doesn't do what I want it to!

1 New Member
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 1621
Plater
7,872 Recognized Expert Expert
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
3894
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 other browsers (I checked it under Konqueror). Thank you in advance for your help. Regards. /Mariusz <HTML>
3
10769
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 Internet Explorer (in Safari, Firefox, and other Mozilla-types), the box ends up overlapping with the second and third rows of the text (which are in an ordered list with some 'display: table-cell' formats defined in order to allow specific...
0
1410
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. After I re-generate the assembly, I get the type I want from it and then invoke a static method. I have found the following behaviour: 1. If I always regenerate the assembly with a completely new name,
149
25203
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? -- Christopher Benson-Manica | I *should* know what I'm talking about - if I ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
14
2096
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 understand what the SetAllowResponseInBrowserHistory does. While researching caching I tried the code sample at the following page : http://msdn2.microsoft.com/library/97wcd0a4(en-us,vs.80).aspx
16
4927
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 Microsoft must be installed on their servers. Now german Umlaute (ä, ü, ö) and quotes are returned incorrectly in SOAP fault responses. This can be easily verified: Implement the following in a web service method (just raises a SOAPException with a...
21
2335
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 resolution, isn't it?) and when calling, just omit parameter when you want to use defaults: a_func(, , 3)
52
3781
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 level machine. C stands for portability and platform and machine independent. If the C compiler and C standard library are written in C itself, is it possible that one "standard" C compiler plus library is enough? The standard implementation is...
9
1888
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 (around 35-40 pages) that once the user logs in, I need to direct them to the correct page, depending on what they select from the directory. In other words...when they login, if they are a new user, a new account screen comes up and from there a...
39
5869
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 ISO-8859-1 -t UTF-8 mydb.sql mydb_utf8.sql mysqlCREATE DATABASE mydb_utf8 CHARACTER SET utf8 COLLATE utf8_general_ci;
0
10034
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9872
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9843
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8713
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7248
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5142
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3358
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2666
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.