Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Sep 24, 2010, 06:43 PM   #1
crazykid888
macrumors newbie
 
Join Date: Aug 2010
Help with java!!

hey there,
im writting a simple program for school that takes input of temperature and dewpoint and converts it to the humidex.

My program works except i still need to round my humidex number off.
I've tried inserting Math.round but i always get an error.
Im wondering how do i use this method properly for it to work! (note, i only need one decimal space)

Here is a snippet of my code:

humidex = + temp + + h ;

i need the humidex to be rounded to one decimal. where do i put Math.round?
crazykid888 is offline   0 Reply With Quote
Old Sep 24, 2010, 07:07 PM   #2
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by crazykid888 View Post
Here is a snippet of my code:

humidex = + temp + + h ;
Don't post snippets. Post complete sections of code, so someone else can compile and run them.

What do you intend that snippet to do? Without seeing the context of the calculation, it's impossible to tell the intent.

If you have code that gives an error when compiled, then post the code that gives the error. Saying "I get an error" without showing the code is useless. We can't see your screen. We can't read your mind. We have no idea what code you wrote that doesn't work, until you post the actual code.

EDIT:
As given, the snippet is equivalent to this:
Code:
 humidex = temp + h ;
Two of the +'s are completely unnecessary and have no effect. Which leads to the question: Do you think they're necessary? What do you think the extra +'s do?

Last edited by chown33; Sep 24, 2010 at 07:16 PM.
chown33 is offline   0 Reply With Quote
Old Sep 24, 2010, 07:25 PM   #3
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
If that code compiles, I am very surprised. Math.round returns the closest int when a float is passed in, and the closest long when a double is passed in. A common method to round to a decimal place using something like this is to shift using multiplication by a power of ten, then apply the round, then divide by the power of ten. If you want n places, you use 10^n. In this case, that's 10^1, which is 10.

As chown33 said, a complete example plus any errors and warnings will help a lot, as well as any definitions that one needs. Humidex is not a term I know.

We are happy to help, but we need something to work with.

-Lee

Edit: looked it up, there's a unary + in java, but it has no meaning here.
lee1210 is offline   0 Reply With Quote
Old Sep 24, 2010, 07:34 PM   #4
crazykid888
Thread Starter
macrumors newbie
 
Join Date: Aug 2010
Sorry guys, im knew to java
I understand it pretty well for just starting with it but i dont see why this Math.round doesnt work! lol :P

Quote:
Two of the +'s are completely unnecessary and have no effect. Which leads to the question: Do you think they're necessary? What do you think the extra +'s do?
Ill post all my code. We need to use the + to append a variable . without it you cant add it in and it generate an error.




Thanks for the quick response!

Last edited by crazykid888; Sep 28, 2010 at 08:14 PM. Reason: please use CODE tags where appropriate
crazykid888 is offline   0 Reply With Quote
Old Sep 24, 2010, 07:49 PM   #5
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
The binary + will add two values. Unary (single argument) + does something else. I don't know what, to be honest, and chown33 was asking if you do. I don't know if unary + applied to capital D double is going to do something odd, but you don't need it. Using capital D double is odd here in general as you don't need to put it in a collection or do anything else that requires an Object. All of the auto-boxing/unboxing is costly and unnecessary. I'd try w/ lowercase d double, and without the extraneous unary +s.

-Lee
lee1210 is offline   0 Reply With Quote
Old Sep 24, 2010, 08:33 PM   #6
chown33
macrumors 603
 
Join Date: Aug 2009
Well that's certainly some crazy code.

Lee1210 is right: change all the Double types to double and things will work better. I tried it and it works.

I don't know how or where you learned to code that way, but it's about as wrong as it could possibly be, while still managing to somewhat work. The key word there is "somewhat", because there a whole lot of cases where this approach simply won't work, or it requires some extra conversions to be performed. I'd like to know if this practice is being taught by someone, or whether you just stumbled across it in your own explorations.

The Double type is actually an Object type, and when it's used in arithmetic, it has to be converted back to its corresponding primitive type (double). In Java, the unary + happens to perform that function, by forcing the Double object to be "unboxed" to a double, which is the actual primitive type.

The reason Math.round() doesn't work is because Math.round() returns a long type, which cannot be assigned to a Double type. The error message is telling you this. It found a long type, but it expects a java.lang.Double, which is the full name of the Double type. Ironically, if you use the double type, then Math.round() will compile, because a long is assignable to a double.

Boxing and unboxing is actually a fairly complicated subject, even though it shouldn't be. It's complicated because the compiler isn't always able to figure out what it should do without specific "hints" in the source code, like unary + or certain other conversions.

I suggest restudying the primitive Java types, all of whose names begin with lower-case letters: int, long, double, float, etc. I also suggest discussing this your teacher, by showing him or her your code using Double, and asking for an explanation of why Math.round() doesn't compile.


EDIT:
A completely separate question is whether Math.round() is the thing to use in this case. You said you needed "one decimal space", which I interpret as one digit after the decimal point, such as "31.6" or "45.2".

If you look at what Math.round() does, and you should do so, it always rounds to the nearest integer value. That means it will return 32 if passed 31.6, which means it's not directly suitable for getting what you want.

However, it can be used as a building block to get what you want. Lee already gave an explanation, so try understanding that. If his explanation doesn't make sense, then post again.

Last edited by chown33; Sep 24, 2010 at 08:42 PM.
chown33 is offline   0 Reply With Quote
Old Sep 24, 2010, 08:40 PM   #7
crazykid888
Thread Starter
macrumors newbie
 
Join Date: Aug 2010
wow thanks.
I never knew the D would make a difference. Ill try this out and see how it works.
The reason my code is probably really bad is because ive never taken a programming courses until this year, coz my first year eng program requires it.

So Double is an object type and double is the data type right?
My silly lab instructor told me today that double"" was supposed to be Double always.
So i was a little confused because we learn it as double.
And maybe the reason why its so weird is because we were told to use a scanner instead of JOptionPane for user input.
Things Ive learned so far in this class have been the basics and some parsing and we've just moved into casting.


oh and,
Quote:
Originally Posted by chown33 View Post
A completely separate question is whether Math.round() is the thing to use in this case.
Our prof wanted us to use Math.round()
Thanks a lot for the help. Ill get back if it works or not.

Quote:
Originally Posted by lee1210 View Post
All of the auto-boxing/unboxing is costly and unnecessary. ""... and without the extraneous unary +s.

-Lee
what do you mean by these two things?
I understood everything else
and yes I'm the biggest newbie to java :P

Last edited by ergdegdeg; Sep 24, 2010 at 08:50 PM. Reason: please avoid consecutive posts by using "edit" or "multiquote"
crazykid888 is offline   0 Reply With Quote
Old Sep 24, 2010, 08:49 PM   #8
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by crazykid888 View Post
wow thanks.
I never knew the D would make a difference. Ill try this out and see how it works.
The reason my code is probably really bad is because ive never taken a programming courses until this year, coz my first year eng program requires it.
I didn't expect you to know the D would make a difference. On the whole, your code is fine, except for the use of Double instead of double.

Quote:
So Double is an object type and double is the data type right?
Yes, that's right, although I don't expect you to fully understand the difference at this point. That's OK because I wouldn't expect it from any beginner.

Quote:
My silly lab instructor told me today that double"" was supposed to be Double always.
So i was a little confused because we learn it as double.
Your lab instructor is wrong. When you learned it as double, that was the correct way.

Quote:
Thanks a lot for the help. Ill get back if it works or not.
Glad to help, and do let us know how it works out. Or if it doesn't work, feel free to post again.
chown33 is offline   0 Reply With Quote
Old Sep 24, 2010, 08:53 PM   #9
crazykid888
Thread Starter
macrumors newbie
 
Join Date: Aug 2010
It worked fine
you guys are awesome :P
thanks a ton!
crazykid888 is offline   0 Reply With Quote
Old Sep 24, 2010, 08:54 PM   #10
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
Quote:
Originally Posted by crazykid888 View Post
wow thanks.
I never knew the D would make a difference. Ill try this out and see how it works.
The reason my code is probably really bad is because ive never taken a programming courses until this year, coz my first year eng program requires it.

So Double is an object type and double is the data type right?
My silly lab instructor told me today that double"" was supposed to be Double always.
So i was a little confused because we learn it as double.

Thanks a lot for the help. Ill get back if it works or not.
Your lab instructor is wrong. You might not be in a position to tell them so, but if you don't need Objects, always use primitives. They are smaller, faster, and behave in a more straight-forward way. It's neat that Java has autoboxing/unboxing, it saves code. There are times it will bite you, though, and it is slow.

The primitive types all start with a lower case letter. Call them primitives, not data types, as data type is very broad. Each primitive has an Object wrapper with a capital letter that can be used when you need to place the value in a collection or pass it somewhere as an Object.

Everyone starts somewhere, but the problem with starting up in the clouds with Java instead of something simpler is you are almost immediately forced to write code you don't understand. Stick with it, but try to read every line of your code and explain out loud what it's doing. If there's something that you don't understand, write it down and ask your professor, or ask here. Maybe ask your lab instructor, but they have lost my trust. Maybe they meant something else and failed to thoroughly explain themselves.

-Lee
lee1210 is offline   0 Reply With Quote
Old Sep 24, 2010, 08:59 PM   #11
crazykid888
Thread Starter
macrumors newbie
 
Join Date: Aug 2010
ohone question actually
how do i get it to round to one decimal.
my answer without the rounding is 10.65...... a bunch of numbers
I want it to round at 10.7

right now using Math.round its rounding it off to 11.0

how do i fix this?
crazykid888 is offline   0 Reply With Quote
Old Sep 24, 2010, 09:47 PM   #12
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
You need to adjust by 10 as I explained above.
Take your value, multiply by 10, use Math.round, divide by 10.
Code:
double toRound = 45.23;
double roundedValue = Math.round(toRound * 10.)/10.;
roundedValue should be 45.2.
Computers do math in binary, though, so sometimes these things aren't exact, but with numbers of this magnitude it should be fine.

Earlier you asked what I meant by autoboxing and unboxing, and unary +. I didn't see that part of your post for whatever reason.

Boxing/unboxing is the process of wrapping a primitive (double) in it's Object wrapper (Double), and taking it back out. Java does this for you automatically in most cases, and this is referred to as autoboxing and autounboxing. The compiler sees that you've used a Double where a double is required, and it adds the code for you to get the double out of the Double. Also, when a Double is needed for, say, an ArrayList and you use a double, the compiler will create a new Double Object to use. This subject is a little involved, and it's probably early in your programming career to have to deal with it. Use primitives until someone says otherwise.

As for unary +:
+ temp + + h
This expression has two unary +s. This expression can be broken down like:
(+temp) + (+h)
The middle + is a binary + that adds two operands. The other two are over-verbosely saying that the expressions temp and h are "positive". This doesn't mean much, but this is a parallel to unary -, which is like multiplying by -1. Unary means only one operand. Other unary operators are:
! for not
++ both prefix and postfix increment
-- both prefix and postfix decrement

-Lee
lee1210 is offline   0 Reply With Quote
Old Sep 25, 2010, 11:32 AM   #13
crazykid888
Thread Starter
macrumors newbie
 
Join Date: Aug 2010
Thanks!!!!
crazykid888 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Some apps prompt to install Java 6 even when Java 7 is installed MacMan988 OS X Mavericks (10.9) 1 Apr 18, 2014 07:56 AM
Java 6 or Java 7 for OSX Mavericks for your Macbook Pro? Starfyre MacBook Pro 3 Dec 22, 2013 03:49 PM
Java 6.x security risk (?) vs Java 7.x and broken apps w/ v7.x installs? mgiamo Mac Basics and Help 1 Dec 15, 2013 03:48 PM
Chrome on OS X -- Java plugin / Java 7 vs Java 6 Issue booyahbooyah Mac Applications and Mac App Store 4 May 31, 2013 09:13 AM
Installed Java 7.07 but doesn't show up in Java preferences? -mattias- OS X 10.8 Mountain Lion 21 Aug 31, 2012 04:49 PM

Forum Jump

All times are GMT -5. The time now is 12:32 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC