Jump to content

[URGENT] HELP - Nested if [JAVA]

- - - - -

  • Please log in to reply
8 replies to this topic

#1
Aaron.H

Aaron.H

    Learning Programmer

  • Members
  • PipPipPip
  • 30 posts
Hey guys im trying to make a program more my assignment that will calculate mean, median, mode. Mean is easy to find.. but median and mode the problem is sorting I basically tied figuring out a method to sort the numbers from highest to lowest.. i Havent looked at this in a while and im confused.. can someone give me some insight maybe explain to me some psuedocode on how I could go about this. Also I am using arrays to store and organize values here is what I got so far. I need to finish this by tomorrow so help ASAP would be appreciated :$ thanks.

public static void secondmeanmedianmode ()
    {

    int[] numbers = new int [5];
    int[] numbersSort = new int [5];
    double mean;
    c.println ("\t\tWe shall begin, remember you have the option to \n\t\tuse the same number how many times you like.");
    c.println ();
    c.println ();
    c.println ();
    c.println ("\t\t\tPlease enter your first number . . .");
    numbers [0] = c.readInt ();
    c.println ();
    c.println ("\t\t\t" + numbers [0]);
    c.println ();
    c.println ();
    c.println ("\t\t\tPlease enter your first second . . .");
    c.println ();
    c.println ();
    numbers [1] = c.readInt ();
    c.println ();
    c.println ();
    c.println ("\t\t\t" + numbers [1]);
    c.println ();
    c.println ();
    c.println ("\t\t\tPlease enter your first third . . .");
    c.println ();
    c.println ();
    numbers [2] = c.readInt ();
    c.println ();
    c.println ();
    c.println ("\t\t\t" + numbers [2]);
    c.println ();
    c.println ();
    c.println ("\t\t\tPlease enter your first fourth . . .");
    c.println ();
    c.println ();
    numbers [3] = c.readInt ();
    c.println ();
    c.println ();
    c.println ("\t\t\t" + numbers [3]);
    c.println ();
    c.println ();
    c.println ("\t\t\tPlease enter your fifth number . . .");
    c.println ();
    c.println ();
    numbers [4] = c.readInt ();
    c.println ();
    c.println ();
    c.println ("\t\t\t" + numbers [4] + "\n\n");


    c.println ("Your numbers are:\t" + numbers [0] + " , " + numbers [1] + " , " + numbers [2] + " , " + numbers [3] + " , " + numbers [4]);
    c.println ();
    c.println ();

    mean = numbers [0] + numbers [1] + numbers [2] + numbers [3] + numbers [4] / 5;




    c.println (" Your mean is:\t" + mean);



    if (numbers [0] > numbers [1])
    {
        if (numbers [0] > numbers [2])
        {
        if (numbers [0] > numbers [3])

            {
            if (numbers [0] > numbers [4])
            {
                numbersSort [0] = numbers [0];
            }
            }
        }

        if (numbers [1] > numbers [1])
        {
        if (numbers [1] > numbers [2])
        {
            if (numbers [1] > numbers [3])

            {
                if (numbers [1] > numbers [4])
                {
                numbersSort [0] = numbers [0];
                }
            }
        }




        }




    }
    }


#2
sam_l

sam_l

    Learning Programmer

  • Members
  • PipPipPip
  • 52 posts
The problem is not sorting, it is your code and your reasoning. Consider the following; the mean is also known as the average, eg, the sum of the set divided by the size of the set. The mode is the most frequently occurring number in the set. The problem is either you or your teacher, but either way I recommend you drop the class.

#3
speachy_15

speachy_15

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
i think you have got to use a sorting method... like bubble sort... google it.. that's how i learned about it..:c-smile:
and yeah.. i think you do need to sort the arrays to find the median and the mode....
i think the median was... uh, yeah.... the middle part of the array...
and the mode.... i think the element that appears most, right? hmm.... i need to consult google...
don't worry... i'm also an extreme noob here.... just a suggestion... i hope it helps...:c-thumbup:

#4
Aaron.H

Aaron.H

    Learning Programmer

  • Members
  • PipPipPip
  • 30 posts
It's kind of to late.. this is my summative.

#5
speachy_15

speachy_15

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
oh.... i am so sorry....

#6
Aaron.H

Aaron.H

    Learning Programmer

  • Members
  • PipPipPip
  • 30 posts
Aw.. ive been here for hours trying to find out how to sort array int values..

#7
Aaron.H

Aaron.H

    Learning Programmer

  • Members
  • PipPipPip
  • 30 posts
BUMP, please someone explain to me a sorting method and how to implement it :(

#8
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
3 things:
  • I hope i'm not too late.
  • No idea what class the "c" is of. Used Scanner and System.out instead to make it work here.
  • I'm not sure what mode means, i'll just assume it's the most occuring number as said above.

        Scanner scanner = new Scanner(System.in);

        PrintStream out = System.out;

        int[] numbers = new int[5];

        int[] numbersSort = new int[5];

        double mean;

        out.println("\t\tWe shall begin, remember you have the option to \n\t\tuse the same number how many times you like.");

        out.println();

        out.println();

        out.println();

        out.println("\t\t\tPlease enter your first number . . .");

        numbers[0] = scanner.nextInt();

        out.println();

        out.println("\t\t\t" + numbers[0]);

        out.println();

        out.println();

        out.println("\t\t\tPlease enter your first second . . .");

        out.println();

        out.println();

        numbers[1] = scanner.nextInt();

        out.println();

        out.println();

        out.println("\t\t\t" + numbers[1]);

        out.println();

        out.println();

        out.println("\t\t\tPlease enter your first third . . .");

        out.println();

        out.println();

        numbers[2] = scanner.nextInt();

        out.println();

        out.println();

        out.println("\t\t\t" + numbers[2]);

        out.println();

        out.println();

        out.println("\t\t\tPlease enter your first fourth . . .");

        out.println();

        out.println();

        numbers[3] = scanner.nextInt();

        out.println();

        out.println();

        out.println("\t\t\t" + numbers[3]);

        out.println();

        out.println();

        out.println("\t\t\tPlease enter your fifth number . . .");

        out.println();

        out.println();

        numbers[4] = scanner.nextInt();

        out.println();

        out.println();

        out.println("\t\t\t" + numbers[4] + "\n\n");



        out.println("Your numbers are:\t" + numbers[0] + " , " + numbers[1] + " , " + numbers[2] + " , " + numbers[3] + " , " + numbers[4]);

        out.println();

        out.println();


        mean = [SIZE="6"][B][COLOR="red"]([/COLOR][/B][/SIZE] numbers[0] + numbers[1] + numbers[2] + numbers[3] + numbers[4] [B][COLOR="red"][SIZE="6"])[/SIZE][/COLOR][/B] / 5;





     [SIZE="4"]   [B][COLOR="red"]out.println(" Your mean is:\t" + mean);

        Arrays.sort(numbers);

        out.println("Median: " + numbers[2]);


        int mode=0;

        int maxOccurences=0;

        for(int number : numbers){

            int occurences=0;

            for(int number2 : numbers){

                if(number == number2){

                    occurences++;

                }

            }

            if(occurences>maxOccurences){

                maxOccurences = occurences;

                mode = number;

            }

        }


        out.println("Mode: " + mode);[/COLOR][/B][/SIZE]

Don't forget brackets for the mean calculation!!

This sorts from low to high.
I'm not sure why you would want to sort from highest to lowest.
If you really need it:
Change

int[] numbers = new int[5];

into:

Integer[] numbers = new Integer[5];

And instead of sorting like

Arrays.sort(numbers);

Do the following:

Arrays.sort(numbers, Collections.reverseOrder());


If your teacher asks why you used Integer instead of int.
Tell him/her the Integer class is a wrapper class of int, it can be used in the same way.
But the main difference is, that Integer is NOT a primitive type and thus Integer inherits from the Object class.

This is required for the usage of the Comparator( =Collections.reverseOrder() )

#9
sam_l

sam_l

    Learning Programmer

  • Members
  • PipPipPip
  • 52 posts
Java provides you a sort method, but you should be aware of which method it uses and you should also understand how each method works, their strengths and their weaknesses. Google is your best friend.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users