Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Old 10-31-2009, 04:37 AM   PM User | #1
etidd
New to the CF scene

 
Join Date: Sep 2009
Location: atlanta
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
etidd is an unknown quantity at this point
mean and standard deviation.

mean and standard deviation. write a program that reads a set of floating-point data values from the input. when the user indicates the end of input, print out the count of the values, the average, and the standard deviation. the average of a data set the sum of the terms / the number of terms.

the standard deviation is: the square root of the sum minus.. blah blah, no big deal, i can handle that.
--------------------------

i don't know how to read an unknown number of values and still account for all of the values. i don't know how to save the number of values. can you give me some tips on my homework assignment?
etidd is offline   Reply With Quote
Old 10-31-2009, 07:41 AM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 5,123
Thanks: 2
Thanked 554 Times in 542 Posts
Fou-Lu is a jewel in the roughFou-Lu is a jewel in the roughFou-Lu is a jewel in the rough
Use a loop while !invalid input. Define what you consider invalid, -1, a letter, nothing, etc. You can also prompt fist to ask how many entries there will be, and loop with that number.
Store the values in a vector or another type of collection. When it comes to sequential access and insertion where random access isn't necessary, I'd use a LinkedList. You could also extend this class just for the purpose of handling just floats with the sum, mean, std deviation and whatever else you need in it. Any collection is templatable so you can force it to only allow floats. This can actually save you a step since you can try/catch just the insertion directly into the collection instead of at the input. Kinda handy.
I assume you know how to handle input? Scanner is a good class to look at if not.
Oh and bookmark this one if you don't have it: http://java.sun.com/j2se/1.5.0/docs/api/
Collections are a part of the java.util package if you need to lookup some info.
__________________
Code:
struct User *upFou;
userInit(upFou, "Fou-Lu", 1);
printf("%s has %s to %s\n", (*upFou).Name, !quitSmoking(upFou) ? "FAILED" : "SUCCEEDED", (*upFou).Smoker == 1 ? "FAIL" : "PASS");
// Fou-Lu has FAILED to FAIL?  Lol
Fou-Lu is offline   Reply With Quote
Old 10-31-2009, 07:39 PM   PM User | #3
etidd
New to the CF scene

 
Join Date: Sep 2009
Location: atlanta
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
etidd is an unknown quantity at this point
Question

still lost here. will it be a for loop that captures the data values?
etidd is offline   Reply With Quote
Old 10-31-2009, 08:40 PM   PM User | #4
etidd
New to the CF scene

 
Join Date: Sep 2009
Location: atlanta
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
etidd is an unknown quantity at this point
...and what method can i implement to make sure that the inputs are actually numbers?
etidd is offline   Reply With Quote
Old 11-01-2009, 03:18 AM   PM User | #5
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 5,123
Thanks: 2
Thanked 554 Times in 542 Posts
Fou-Lu is a jewel in the roughFou-Lu is a jewel in the roughFou-Lu is a jewel in the rough
Got to be careful here or it will be considered cheating.
A for loop can be used, but remember that its a controlled loop based on a numerical value. It can be used only if you previously declared a value to loop until. That being said, this is technically legal: for (;;) which will create infinite loops. We rarely see this, if we need to create infinite listeners more often you'll see a while(true).

Validation can happen for datatypes during the scanner process. The next* methods throw three potential exceptions: InputMismatchException if the type cannot be retrieved using the next* method (you expect a number you get a string), NoSuchElementException if there is no token in the scanner, and IllegalStateException if the scanner has been closed. Should you catch the InputMismatchException, chomp the input with nextLine() and reprompt for input (otherwise, since you'll be in a loop it will likely generate an infinite prompt - this is because a failure on the next* will leave the token on the stack which will continuously evaluate as illegal).

Now, if you're at a point where exception handling is not handled it is likely that you will not be expected to handle input validation at this time. If you still want to, you can use the hasNext* methods to handle validation without taking off of the stack and actually throwing an exception. In all honesty, this is the more 'correct' approach to scanner handling.
I think thats about the most information I can give you without any code. Take a look at this tutorial from sun that shows some basic scanner handling: http://java.sun.com/docs/books/tutor.../scanning.html
__________________
Code:
struct User *upFou;
userInit(upFou, "Fou-Lu", 1);
printf("%s has %s to %s\n", (*upFou).Name, !quitSmoking(upFou) ? "FAILED" : "SUCCEEDED", (*upFou).Smoker == 1 ? "FAIL" : "PASS");
// Fou-Lu has FAILED to FAIL?  Lol
Fou-Lu is offline   Reply With Quote
Old 11-01-2009, 03:24 AM   PM User | #6
cs_student
New Coder

 
Join Date: Oct 2009
Location: ~/
Posts: 61
Thanks: 1
Thanked 8 Times in 8 Posts
cs_student is an unknown quantity at this point
You should have a look at the Scanner class.

Have a look at the next, nextLine, and nextDouble methods as they may all be of help to you.

There are a few different approaches you could take.
One way I would point out is:

You could scan in the data as a string and try to use the static Double method parseDouble to see if the input is a double, then deal with it accordingly.

Try and implement what you can and post if you have problems.


cs_student


Edit: Fou-Lu must of posted his while I was typing mine. He is much more experienced and I would use the procedures he layed out.

Last edited by cs_student; 11-01-2009 at 03:27 AM..
cs_student is offline   Reply With Quote
Old 11-06-2009, 11:43 AM   PM User | #7
Shyama_Mukherji
New to the CF scene

 
Join Date: Sep 2009
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Shyama_Mukherji is an unknown quantity at this point
Here are the brief steps to store n integers in an array of numbers where n is variable.

- Declare an integer : int n;
-use Scanner class to input value of n from keyboard using stmt
Scanner input = new Scanner(System.in);
- use a for loop to store n valuesin an array, for further use.


import java.io.*;
import java.util.Scanner;
public class inputvalues{
// Create a single shared Scanner for keyboard input
int ARRAY1[10];
private static Scanner scanner = new Scanner( System.in );
public static void main(String[] args) {
// Prompt the user
System.out.println( "Enter number of values: " );
// Read n from the keyboard.
int number = scanner.nextInt();

for (int i=0;i<number;i++)
ARRAY[i] = scanner.nextInt();

}
Now use the values stored in array1for your calculations of std dev etc.
Shyama_Mukherji is offline   Reply With Quote
Old 11-07-2009, 05:13 AM   PM User | #8
cs_student
New Coder

 
Join Date: Oct 2009
Location: ~/
Posts: 61
Thanks: 1
Thanked 8 Times in 8 Posts
cs_student is an unknown quantity at this point
Please use code tags next time you post.

Also, it looks like you just posted your assignment. Now we know what you need to do, but how do we help you? We don't know what your having trouble with.

What have you tried? Make sure to post (with code) the approaches you have taken to solve the problem.

Also, do you understand what the code you posted does? What do you think it does.

cs_student
cs_student is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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


All times are GMT +1. The time now is 01:06 AM.

Home - Contact Us - Archives - Link to CF - Resources - Top 

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.