This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes constructor question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "constructor question" Watch "constructor question" New topic
Author

constructor question

Rene Rad
Greenhorn

Joined: Feb 10, 2010
Posts: 15
I'm studying java and as part of my project I have to create a constructor that initializes 6 instance variables, 2 of them are doubles (2 Strings and 2 ints too) and I need to work in that those two doubles do not go below 0. If one of them is negative it has to set the relevant field to 0. The other numeric fields will be initialized to 0.

I know how to create an if else that will check one field but I'm lost on how to do 2. Belowis what I have so far but it's wrong. Can anyone provide insight?

Thanks.


pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
You've got a bit of unnecessary redundancy in that code. I recommend that you just modify the single field inside the if block. e.g.,
Rene Rad
Greenhorn

Joined: Feb 10, 2010
Posts: 15
Thanks, this worked great. I commented this thread into my source code for credit. Home I don't get marks docked but whatever.

Thanks again.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37941
    
  22
Welcome to the Ranch

I suggest you see whether you can reduce those if-elses to a single line with the ?: operator.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Or in this case simply use Math.max.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37941
    
  22
Of course, Math.max uses ?: in the implementations I have seen.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: constructor question
 
Similar Threads
Symbol7 for Class error -- no package used just normal class
Program like ATM where person enters amount in dollars and cents but program uses int for monies
repeating code
calculating commission
Blanking out a button if they do not have enough funds in their bank.