If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Help converting doubles to integers...

I'm currently in a Java programming class and stuck on an assginment.

The assignment asks me to do the following:

Write a program that:
a. Prompts the user to input five decimal numbers.
b. Prints the five decimal numbers.
c. Converts each decimal number to the nearest integer.
d. Adds the five integers.
e. Prints the sum and average of the integers.

I did part a and b with no problem but theres nothing in the chapter we're covering that talks about converting decimal numbers to the nearest integer (i.e. if it's 3.3 round it down to 3 and if it's 3.6 round it up to 4). If I can figure out part c then I can finish the rest of it.

There is a small section on type conversion (casting) in the chapter but it only talks about how it works, no examples of code or how to use casting.

Well I think in the Math class there are methods for rounding numbers, but i'm assuming you're not meant to use external methods since it's an assignment. You may want to look up the Math class anyway for future use.

Anyway, to round it without using any if statements you should

a) Multiply number by 2
b) Convert to an int
c) Divide number by 2.

The result of this will be an int of the correct value after rounding. Eg

Once I figured out that adding .5 to the user inputted number the rest of the assignment was easy and I managed to submit it on time so I got full credit.

I have taken VB, JScript and C++. I've done far more C++ than anything else.

The class isn't too hard in terms of the actual coding it's just that I'm horrible at doing math

Java reminds me more of C with the "System.out.println"
statement. It's not too hard to adapt to a new language if you've learned prior ones, the syntax is similar for all of them. It's just a matter of learning the new way to make the same things happen in the new language.

The professor for my Java class is the same one I had for my C++ classes. He used to be a programmer with a 9-5 job so he knows his stuff. It's a good class.

Originally posted by Gabriel Once I figured out that adding .5 to the user inputted number the rest of the assignment was easy and I managed to submit it on time so I got full credit.

That's good new to hear.

Originally posted by Gabriel
I have taken VB, JScript and C++. I've done far more C++ than anything else.

Ah, interesting. Are you a Computer Science major?

Originally posted by Gabriel
The class isn't too hard in terms of the actual coding it's just that I'm horrible at doing math

Bah, who isn't from time to time?

Originally posted by Gabriel
Java reminds me more of C with the "System.out.println"
statement. It's not too hard to adapt to a new language if you've learned prior ones, the syntax is similar for all of them. It's just a matter of learning the new way to make the same things happen in the new language.