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: -
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: -
/**
-
* 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. ***");
-
}
-
}
-
-
}
-
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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>
|
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...
|
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,
|
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.
|
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
| |
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...
|
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)
|
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...
|
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...
|
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;
|
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...
| |
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...
|
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,...
|
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...
|
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...
|
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();...
|
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...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |