Results 1 to 10 of 10
  1. #1
    WarmRegards's Avatar
    WarmRegards is offline Member
    Join Date
    Oct 2009
    Location
    Boston
    Posts
    12
    Rep Power
    0

    Question 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
    My output is as follows (does not match above):

    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 classes in message below:
    "All animals are equal, but some are more equal than others."
    - George Orwell

  2. #2
    WarmRegards's Avatar
    WarmRegards is offline Member
    Join Date
    Oct 2009
    Location
    Boston
    Posts
    12
    Rep Power
    0

    Default

    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

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    ========== Begin Processing ===========

    Avg value: -975.5
    Max value: 55
    Min value: -9999
    44 = 0
    That looks very much like you are including the sentinel as part of the data.

  4. #4
    WarmRegards's Avatar
    WarmRegards is offline Member
    Join Date
    Oct 2009
    Location
    Boston
    Posts
    12
    Rep Power
    0

    Default

    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

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Quote Originally Posted by WarmRegards View Post
    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.
    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.)

  6. #6
    WarmRegards's Avatar
    WarmRegards is offline Member
    Join Date
    Oct 2009
    Location
    Boston
    Posts
    12
    Rep Power
    0

    Default

    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);
    	}
    never actually gets displayed. It never gets printed, just ignored, not sure why. That and sentinel value is being ignored for whatever reason.

    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

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    > 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!

  8. #8
    WarmRegards's Avatar
    WarmRegards is offline Member
    Join Date
    Oct 2009
    Location
    Boston
    Posts
    12
    Rep Power
    0

    Default

    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

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,778
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by WarmRegards View Post
    However, I'm having difficulty following the logic of it all
    I find that hard to believe because you invented it all ...

    kind regards,

    Jos

  10. #10
    WarmRegards's Avatar
    WarmRegards is offline Member
    Join Date
    Oct 2009
    Location
    Boston
    Posts
    12
    Rep Power
    0

    Default

    My friend is helping me out tonight so it doesn't matter.

    Problem solved.
    "All animals are equal, but some are more equal than others."
    - George Orwell

Similar Threads

  1. Is this the correct Output?
    By Teny in forum New To Java
    Replies: 17
    Last Post: 04-13-2009, 01:52 PM
  2. Output correct grammar
    By JordashTalon in forum New To Java
    Replies: 2
    Last Post: 03-06-2009, 01:22 AM
  3. Java, output string, getting correct output? HELP!
    By computerboyo in forum New To Java
    Replies: 2
    Last Post: 02-26-2009, 12:44 AM
  4. Is my Pseudocode correct?
    By Clemenza1983 in forum New To Java
    Replies: 0
    Last Post: 01-29-2008, 05:07 AM
  5. To correct forum
    By Jman in forum Introductions
    Replies: 3
    Last Post: 01-18-2008, 03:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •