Joanne
Write a generic class MyMathClass with at type parameter T where T is a numeric object
(Integer, Double or any class that extends java.lang.number) Add a method
standardDeviation (stdev) that takes an ArrayList of type T and returns a standard deviation
as type double. Use a for each loop where appropriate.
Hard code a couple of test arrays into your Demo file. You must use at least 2 different types
such as Double and Integer.
Your call will be something like:
System.out.println(“Standard Deviation 0-9 “ + MyMathClass.stdev(a));
Your class and method headers will be:
Research java’s Number class to see what useful method we are gaining access to.
ETC....Enter the amount of integers to find standard deviation:
10...
Sorry, invalid input. Try again.
End of catch block, inputValidYet = false and the input = 0
Last line in do-while loop, inputValidYet = false and the input = 0
Enter the amount of integers to find standard deviation:
Sorry, invalid input. Try again.
End of catch block, inputValidYet = false and the input = 0
Last line in do-while loop, inputValidYet = false and the input = 0
Enter the amount of integers to find standard deviation:
Sorry, invalid input. Try again.
End of catch block, inputValidYet = false and the input = 0
Last line in do-while loop, inputValidYet = false and the input = 0
Enter the amount of integers to find standard deviation:
When a scanner throws an InputMismatchException, the scanner will not pass the token that caused the exception, so that it may be retrieved or skipped via some other method.
Paul Clapham wrote:Kind of embarrassing for me as well... I was a professional Java programmer for years, but I don't know how to use a Scanner properly. Thing is, professional Java programmers don't write command-line code like that. Or if they do, it's just quick-and-dirty code for people who don't mind if it throws an exception if they type the wrong thing. Command-line applications have been pretty much obsolete these last 20 years, except in schools where students still get taught this kind of thing. (To be fair to the teachers, alternative methods of input are much harder for students to learn.)
Junilu Lacar wrote:From the JavaDocs for Scanner:
When a scanner throws an InputMismatchException, the scanner will not pass the token that caused the exception, so that it may be retrieved or skipped via some other method. So you should probably do something like sc.next() in your catch block. Or sc.nextLine(). One or the other should work.
Danny Treart wrote:So people in the industry tend to use the I/O streams, buffered readers, etc? (I need to study up on that stuff), right?
can you please explain your reasoning in more detail? Paul mentioned something about dealing with the enter key separately, and the sc.next()/sc.nextLine() kind of came to my mind vaguely. Anymore information on this?
Danny Treart wrote:Thank you for your help, can you please explain your reasoning in more detail? Paul mentioned something about dealing with the enter key separately, and the sc.next()/sc.nextLine() kind of came to my mind vaguely. Anymore information on this?
bacon. tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
|