• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Bear Bibeault
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Jj Roberts

Logic Errors

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How frustrating are logic errors? The program compiles, but does not spit out the expected answers. I'm working on a simple payroll calculator for a Java class I'm taking, and instead of getting the numbers I'm expecting, I'm getting all zero's. We're covering if and switch statements right now, so that's what this project centers around. The output for grossPay and netPay are 0.0 and 0.0. Other than that everything works. I'm assuming my problem is in the if statements. Can anyone spot the logic errors?

Also, in the default constructor, how do I set the char to a default value, similiar to a null string?

Here's the class:


And here's the driver:
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A big hint: put a line like

System.out.println("In grossPay")

in the private grossPay() method, and then run your program again.
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want a "null" for a char, try '\u0000'.
 
Greg Roberts
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Ernest Friedman-Hill:
A big hint: put a line like

System.out.println("In grossPay")

in the private grossPay() method, and then run your program again.



By doing that I realized I'm not calling the grossPay() and netPay() methods. In the project requirements it says the methods need to be private (this may be a typo, I'll ask).

I changed the methods to public and had the driver call them and it spits out computed numbers. But if they do have to be private is there any way to call those methods from the driver?
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Greg Roberts:
But if they do have to be private is there any way to call those methods from the driver?

Not from another class, no. Can you think of any methods in Employee from which you can call those methods that would make sense?
 
Greg Roberts
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I made some changes and I figured out how to use them while still keeping them private. Here are the changes I made to the display() method:



I also changed the grossPay() and netPay() so they return a double, instead of them being void methods.
[ March 07, 2005: Message edited by: Greg Roberts ]
 
Greg Roberts
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by David Harkness:
If you want a "null" for a char, try '\u0000'.



Tried that, got 4 errors for that alone!

illegal escape character
unclosed character literal (2)
';' expected
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Greg Roberts:
Tried that, got 4 errors for that alone!

illegal escape character
unclosed character literal (2)
';' expected

Hmm, the JavaDocs show '\u0000' as being the value of Character.MIN_VALUE. You used zeros and not capital-Os, right?
 
Greg Roberts
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, I used zero's. Compiler didn't like it. What would you normally use in a default constructor for a char variable?
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg:

I don't know if you have solved your problem or not, but I made one change and had your program running. The only thing I did was move the call to the grossPay() and netPay() methods inside the getGrossPay() and getNetPay() methods, respectively.

I think you logic issue is naming the the two methods as "calculateGrossPay" and "calculateNetPay".

Everything else seems to work.

Best of luck,

Russ
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Greg Roberts:
What would you normally use in a default constructor for a char variable?

Since all instance fields are initialized by the JVM (chars get '\u0000'), I'd remove the initialization of it from the constructor.

Just to test, I added the following line to one of my classes:and it compiled fine using JDK 1.4.2_05. I have no idea why javac isn't liking it on your end.
 
Ranch Hand
Posts: 341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Tried that, got 4 errors for that alone!

illegal escape character
unclosed character literal (2)
';' expected



What is your code?
You may foggot ';' at the end of expression.
 
A teeny tiny vulgar attempt to get you to buy our stuff
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic