Results 1 to 10 of 10
Thread: Getting correct output
- 11-01-2009, 01:07 AM #1
Getting correct output
Hey guys. The code I have below is all functioning and working well. However, I'm having difficulty following the logic of it all and therefore getting it to print the way I want it to print.
So this is actually a project, and here is an example of what a successful run needs to look like. My output needs to match this output exactly.
Java Code:6 Enter up to 20 Positive Ints, one per line 7 Use -9999 as a sentinel value 8 Enter Numbers: 9 44 10 55 11 0 12 -1 13 1 14 1 15 2 16 44 17 3 18 39 19 55 20 -9999 21 22 =========== Begin Processing ============ 23 24 25 Unique list: Printing 6 unique values: 26 numList[0] = 44 27 numList[1] = 55 28 numList[2] = 1 29 numList[3] = 2 30 numList[4] = 3 31 numList[5] = 39 32 33 Min value: 1 34 35 Max value: 55 36 37 Avg value: 24.0
Java Code:Enter up to 20 positive Ints, one per line Use -9999 as a sentinel value Enter Numbers: 44 55 0 -1 1 1 2 44 3 39 55 -9999 ========== Begin Processing =========== Avg value: -975.5 Max value: 55 Min value: -9999 44 = 0
"All animals are equal, but some are more equal than others."
- George Orwell
- 11-01-2009, 01:10 AM #2
All required documentation to go along with above question:
SOLUTION.java class:
Java Code:package homework; import java.util.Scanner; public class Solution { private int[] ReadInput() { int n = 0; Scanner in = new Scanner(System.in); int[] input = new int[20]; //Create a new array called "input" and allocate 20 blocks of memory to it System.out.println("Enter up to 20 positive Ints, one per line"); System.out.println("Use -9999 as a sentinel value"); System.out.println("Enter Numbers:"); while (n < 20) { input[n] = in.nextInt(); //Assign each n values into the integer array up to 20 values if (input[n] == -9999) { System.out.println("\n========== Begin Processing ===========\n"); break; } if (input[n] <= 0) { continue; //goes back to beginning of while loop } n++; //n only gets incremented if the value is desirable } return input; //returns the array } public void Process() { //call all the functions i wrote call read input, calculate unique, etc readInput int val[]; val = ReadInput(); Util.ReportAverage(Util.CalculateAverage(val));// Util.ReportMax(Util.CalculateMax(val)); Util.ReportMin(Util.CalculateMin(val)); Util.ReportUnique(Util.UniqueValues(val)); } }
UTIL.java
Java Code:package homework; public class Util { public static int[] UniqueValues(int[] NotUnique) { int n = 0; int i = 0; int unique[] = new int[20]; //new array of 20 ints while (NotUnique[n] != 0) //while that element does not contain unique { i = 0; //declare i as zero each time for second while loop to work. while (unique[i] !=0 ) //while elements in unique do not equal zero. { if (unique[i] == NotUnique[n]) { break; } } if (unique[i] == 0) { unique[i] = NotUnique[n]; //if it reaches end of line without finding an element again then add it to unique list System.out.println(unique[i] + " = " + n); } n++; } return unique; } public static void ReportUnique(int[] Unique) //don't need anything back { System.out.println("Unique values" + Unique); } public static int CalculateMin(int[] ValueArray) //int here is what I want back from the function, it is the return value, and () is parameters. We want it to retunr int values { int n = 0; int min = 0; while (ValueArray[n] != 0) { if (ValueArray[n] < min ) { min = ValueArray[n]; //then assign the new greater value to max } n++; } return min; } public static void ReportMin(int Min) //names are arbitrary, this function does not know about the other ValueArray { System.out.println("Min value: " + Min); } public static int CalculateMax(int[] ValueArray) // { int n = 0; int max = 0; while (ValueArray[n] != 0) { if (ValueArray[n] > max ) { max = ValueArray[n]; //then assign the new greater value to max } n++; } return max; } public static void ReportMax(int Max) { System.out.println("Max value: " + Max); } public static double CalculateAverage(int[] ValueArray) { double average; double count = 0; double total = 0; int n = 0; while (ValueArray[n] != 0 ) { total = ValueArray[n] + total; count = count + 1; n++; } average = total / count; return average; } public static void ReportAverage(double Average) //takes in a double { System.out.println("Avg value: " + Average); } }
and lastly TEST SOLUTION.java
Java Code:package homework; public class TestSolution { public static void main (String args[]) { Solution s = new Solution(); s.Process(); } }
"All animals are equal, but some are more equal than others."
- George Orwell
- 11-01-2009, 01:44 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
========== Begin Processing ===========
Avg value: -975.5
Max value: 55
Min value: -9999
44 = 0
- 11-01-2009, 01:59 AM #4
Shouldn't it break when it reaches sentinel value? I thought I have it doing that at the process() method. It does not seem to be assigning the values into an array like I want it to be doing.
"All animals are equal, but some are more equal than others."
- George Orwell
- 11-01-2009, 03:29 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
Process() doesn't have any break statement.
ReadInput() does. So maybe you want to start there to see if it is "assigning the values into an array like I want it to be doing".
Also can I suggest using Java case conventions: methods and variables start with a lower case letter.
You have a lot of comments in the code, but nothing describing the actual contents of the array. In particular you might want to document which array values you regard as "valid". That way you can check whether the other methods are doing the right thing and processing only valid array elements. (A precise description of what the array should contain is also required before you - or any one else - can say whether ReadInput() is correct.)
- 11-01-2009, 03:44 AM #6
Thanks for the suggestion, I changed the method and variable names.
As far as the rest goes, I'm not quite sure what to do.
I noticed that
Java Code:public static void reportUnique(int[] unique) //don't need anything back { System.out.println("Unique values = " + unique); }
A buddy of mine is a software engineer so he guided me through writing the code which explains why my level of understanding java is not at the same level as this code is written (even though it may seem simple to others)."All animals are equal, but some are more equal than others."
- George Orwell
- 11-01-2009, 04:59 AM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
> It never gets printed, just ignored, not sure why.
Perhaps you didn't wait long enough. I mean it's not clear that the program ever ends.
Anyway all this is beside the point. It's not your code. People at forums like this have a boundless willingness to help you. But for many that willingness does not extend to providing commentary and interpretation of code written by other people. Whoeever wrote that code might be helped if they were to present themselves and ask questions - because they have an understanding of what they wrote and what they intended.
But where does that leave you? You're not going to like this, and it gives me no pleasure to write it: but you need to throw that code away and write your own. Start at the beginning. Formulate a clear plan of how you intend to proceed. (and I do mean a plan of proceeding, not a knowledge about how the output should look). And then ask questions about that if you get stuck.
Good luck!
- 11-01-2009, 03:13 PM #8
Actually I did write the code. My buddy is my tutor, but he writes the code on his machine, gets it to compile and run then I start writing code on my laptop and he guides me through the process. He does not have me copy it. It's just that when we finished he said all I was left to do was call all the methods inside the Solution class and Process() method. I did that and I don't get any compile errors, all is good but something is off and he must have missed it as we wrote it.
My code is different from his but it accomplishes the same thing."All animals are equal, but some are more equal than others."
- George Orwell
- 11-01-2009, 04:29 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 11-01-2009, 04:41 PM #10
Similar Threads
-
Is this the correct Output?
By Teny in forum New To JavaReplies: 17Last Post: 04-13-2009, 12:52 PM -
Output correct grammar
By JordashTalon in forum New To JavaReplies: 2Last Post: 03-06-2009, 12:22 AM -
Java, output string, getting correct output? HELP!
By computerboyo in forum New To JavaReplies: 2Last Post: 02-25-2009, 11:44 PM -
Is my Pseudocode correct?
By Clemenza1983 in forum New To JavaReplies: 0Last Post: 01-29-2008, 04:07 AM -
To correct forum
By Jman in forum IntroductionsReplies: 3Last Post: 01-18-2008, 02:33 AM
Bookmarks