Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 8th, 2005, 5:38 AM   #1
shangnyun
Programmer
 
shangnyun's Avatar
 
Join Date: Jun 2005
Posts: 34
Rep Power: 0 shangnyun is on a distinguished road
Need Feedback and Simple Advice

Look at the following code. I just wrote it, and it's extremely exciting, because I've never written something this large completely from scratch before . It's a lot of fun, playing with the compiler, then trying to fix the errors by listening to the compiler . Anyway... this program is supposed to take 3 numbers from the user, then output the average of the three numbers to the screen. My assignment says "write a program that prompts the user to enter the first, the second and the third whole number (as integers) and outputs the average of three numbers (as a double)."

Now, my program so far does just that, except I have no type-casts in here, and hence the double value at the end will always just show a zero after the period. Do you think the assignment is saying that the program should be able to give the average of three numbers INCLUDING one space after the period? And how would I do that?

Thanks

import java.util.*;

public class NumberAverage
{
    public static void main(String[] args)
    {
    	int n1, n2, n3;
    	double numberAverage;

    	System.out.println("Hi!");
    	System.out.println("I will calculate the average of");
    	System.out.println("three whole numbers for you.");
    	System.out.println();
    	System.out.println("Please enter three whole numbers,");
    	System.out.println("separated by space and then hit");
    	System.out.println("the ENTER key:");

	   	Scanner scannerObject = new Scanner(System.in);

    	n1 = scannerObject.nextInt();
    	n2 = scannerObject.nextInt();
    	n3 = scannerObject.nextInt();

		System.out.println();
    	System.out.println("You entered " + n1 + ", " + n2 + " and " + n3);

    	numberAverage = (n1 + n2 + n3)/3;

		System.out.println();
		System.out.println("The average of the three numbers");
		System.out.println("you have entered is: " + numberAverage);
	}
}
shangnyun is offline   Reply With Quote
Old Jun 8th, 2005, 5:50 AM   #2
Berto
Programming Guru
 
Join Date: Aug 2004
Posts: 1,022
Rep Power: 6 Berto is on a distinguished road
Send a message via AIM to Berto Send a message via MSN to Berto
i changed the line

numberAverage = (n1 + n2 + n3)/3;

to

numberAverage = ((double)(n1 + n2 + n3))/3;

that way you are not doing integer division on the 3 numbers.

then use math.round or something, and yes i would give it to 1 or 2 deciaml places.
__________________
"Put your hand on a hot stove for a minute, and it seems like an hour. Sit with a pretty girl for an hour, and it seems like a minute. THAT'S relativity."

- Albert Einstein
Berto is offline   Reply With Quote
Old Jun 8th, 2005, 5:56 AM   #3
shangnyun
Programmer
 
shangnyun's Avatar
 
Join Date: Jun 2005
Posts: 34
Rep Power: 0 shangnyun is on a distinguished road
Sweeeet!! Thank you! That's exactly what I wanted. I think I'll just leave it like that. But let's say I want to LIMIT the spaces after the period to 1, 2 or 3, or heck, anything, how would I do that?

I don't know anything about the math class yet.

Thanks again!
shangnyun is offline   Reply With Quote
Old Jun 8th, 2005, 7:05 AM   #4
Berto
Programming Guru
 
Join Date: Aug 2004
Posts: 1,022
Rep Power: 6 Berto is on a distinguished road
Send a message via AIM to Berto Send a message via MSN to Berto
http://java.sun.com/j2se/1.5.0/docs/...lang/Math.html

import java.lang.Math;

and to round the numbers

Math.round(double);

look on the link all the info is there.,
__________________
"Put your hand on a hot stove for a minute, and it seems like an hour. Sit with a pretty girl for an hour, and it seems like a minute. THAT'S relativity."

- Albert Einstein
Berto is offline   Reply With Quote
Old Jun 8th, 2005, 7:11 AM   #5
shangnyun
Programmer
 
shangnyun's Avatar
 
Join Date: Jun 2005
Posts: 34
Rep Power: 0 shangnyun is on a distinguished road
I'm not quite sure how I would use this in my code to achieve the result I'm looking for, but thanks a lot!

I'm satisfied with the way my code is right now, without the rounding. The end-number is 4.6666666667 or something like that. That's fine with me, but still it's good to know about the math class
shangnyun is offline   Reply With Quote
Old Jun 8th, 2005, 9:36 AM   #6
Berto
Programming Guru
 
Join Date: Aug 2004
Posts: 1,022
Rep Power: 6 Berto is on a distinguished road
Send a message via AIM to Berto Send a message via MSN to Berto
import the library as i stated above in the code and add after the working out
 

numAverage  = Math.round(numAverage);

i think anyways i cant really test it out here.
__________________
"Put your hand on a hot stove for a minute, and it seems like an hour. Sit with a pretty girl for an hour, and it seems like a minute. THAT'S relativity."

- Albert Einstein
Berto is offline   Reply With Quote
Old Jun 9th, 2005, 10:27 AM   #7
skuinders
Hobbyist Programmer
 
skuinders's Avatar
 
Join Date: Jun 2005
Location: MA, US
Posts: 204
Rep Power: 4 skuinders is on a distinguished road
Math.round() will round to the nearest integer... so if you have x=1.333333 and you do:

double x = 1.333333;
x=Math.round(x);

x will be 1. Using this, we can round to any decimal place by simply multiplying by factors of 10 before rounding. For example, to round to 2 decimal places we would multiply by 10^2 or 100 and then use the round method, then divide by 100 after the rounding.

double x = 1.333333;
x*=100;
x=(double)Math.round(x)/100;

here, x will be 1.33.

it might be useful for you to write a general method for this.

public double myRound(double num, int dec) {
     return (double)Math.round(num*Math.pow(10,dec))/100;
}

of course, there are ways to do this within the API, but sometimes its just as easy to write your own.

Hope this helps!
__________________
"A stupid man's report of what a clever man says can never be accurate, because he unconciously translates what he hears into something he can understand."
- B. Russell

http://web.bryant.edu/~srk2
skuinders is offline   Reply With Quote
Old Jun 10th, 2005, 1:33 AM   #8
HeX
Programmer
 
HeX's Avatar
 
Join Date: May 2005
Location: Kosova
Posts: 94
Rep Power: 4 HeX is on a distinguished road
Send a message via MSN to HeX
Yep. However, you also can use this:
import java.text.*;
and let's say the variable that contains the double is "number", in the code you write:
DecimalFormat formatter = new DecimalFormat("0.00");//how many decimals
System.out.println(formatter.format(number));
__________________
countdown++;
HeX is offline   Reply With Quote
Old Jun 10th, 2005, 7:19 AM   #9
shangnyun
Programmer
 
shangnyun's Avatar
 
Join Date: Jun 2005
Posts: 34
Rep Power: 0 shangnyun is on a distinguished road
Thanks a lot guys. Appreciate it
shangnyun is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:24 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC