aspose file tools*
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
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: 39791
    
  28
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: 19760
    
  20

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: 39791
    
  28
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