This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Object not reading from the constructor.  RSS feed

 
Ranch Hand
Posts: 90
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a constructor that looks like this


and a main method that ignores the overtime arg when called, that looks like this:



i usually dont like to post all my code, but in this case i feel its necessary.
I cant find out why the overtimeMultiplier is not read at all, its as if its not even there when called into action.
any ideas? suggestions?







 
Author and ninkuma
Marshal
Posts: 66801
168
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look closely at line 19.
 
rick pine
Ranch Hand
Posts: 90
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes i noticed that, and changed constructor like this

but it still ignores it.
I also tried initializing the value on the constructor to 1.5 and just having an unassigned local variable, no luck either.
 
Bartender
Posts: 620
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is very difficult to guess what's wrong without seeing how the variables are declared in your class, and showing what the actual inputs and outputs are.
 
rick pine
Ranch Hand
Posts: 90
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Allright , here it goes.
class WageCalculator {



 
rick pine
Ranch Hand
Posts: 90
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
forgot the outpu
Base Rate: 10
Hours: 60
Would you like to enter an overtime multiplier(yes/no)? yes
Overtime multiplier: 2
Base wage: 400.0
Overtime wage: 300.0
Total wage: 700.0

The multiplier, does not get read. For good or for bad. even if i enter something less than 1.5 , it still runs. Im baffled! I think i have it set up correctly but obviously somehting is wrong.
 
Fred Kleinschmidt
Bartender
Posts: 620
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And where are the inputs and outputs?

Note that line 40 of WageCaluclator ignores overtimeMultiplier and uses a hard-coded 1.5 instead.
 
rick pine
Ranch Hand
Posts: 90
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the inputs are 10 for baseRate
60 for hours
and 2 for overtimeMultiplier

so the reason i hard coded a 1.5 is so that the overtime is payed at 1.5. but if the user decides to enter a multiplier then that multiplier should execute and multiply by 1.5 to increase the amount payed. Are you telling me that my method is wrong?? oh god no!!
 
Saloon Keeper
Posts: 1037
43
Eclipse IDE Hibernate jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

rick pine wrote:even if i enter something less than 1.5 , it still runs


  • Please check If you have replaced hard coded argument 1.5 with local variable overtimeMultiplier while creating an object of WageCalculator in main method.


  • If inputs are

  • Base Rate: 10
    Hours: 60
    Would you like to enter an overtime multiplier(yes/no)? yes
    Overtime multiplier: 2

    and you expect following as correct output
    Base wage: 400.0
    Overtime wage: 400.0
    Total wage: 800.0

    then you need to replace hard coded 1.5 with overtimeMultiplier in method overTimeWage() because although user enters 2 as Overtime multiplier It will calculate
    ( 1.5 * 10 ) * ( 60 - 40 ); resulting 300

  • If you want you can use conditional operator to reduce code lines in method baseWage() and overTimeWage().


  • You can override toString method of Object into WageCalculator class to display information so you just need to print reference variable n1 using print statement in main method like this System.out.println(n1); then you don't need write three separate print statements in main method.

  •  
    rick pine
    Ranch Hand
    Posts: 90
    Java Netbeans IDE
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    oh wow!! i missed the fact that i had already initialized baseRate to 1.5 in the first constructor.
    Also thanks for the idea to reduce lines of code, that will come handy in the future.
     
    Consider Paul's rocket mass heater.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!