Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Need help with max, min, sorting array in given array

joker3000Apr 10 2015 — edited Apr 12 2015

Hi community, i'm JAVA learner and i don't know how to find max and min and sorting array without changing the original array and printing the results. Can anyone help me to fix my java code to find max, min and sorting please ?

Here is my java:

import java.util.Arrays;

import java.util.Random;

import java.util.*;

public class ProcessMarks {

        private static final int NMARKS = 125;

        private static final double mean = 65.0;

        private static final double std = 15.0;

        /**

         * Returns an array of NMARKS integer marks approximately normally distributed,

         * with specified mean (mean) and standard deviation (std),

         * and with values outside 0..100 removed.

         * @return the array of marks.

         */

        public static int[] getMarks() {

            Random rand = new Random(1001L);

            int mark;

            int[] theMarks = new int[NMARKS];

            int n = 0;

            while (n < NMARKS) {

                mark = (int) Math.round(std*rand.nextGaussian() + mean);

                if (mark >= 0 && mark <= 100)

                    theMarks[n++] = mark;

            }

            return theMarks;

        }

        public static int max(int[] getMarks) {

            int maximum = getMarks[0];

            for (int i=0; i<getMarks.length; i++) {

                if (getMarks[i] > maximum) {

                    maximum = getMarks;

                    }

                }

            return maximum;

            }

        /**

         * Test code

         * @param args not used

         */

        public static void main(String[] args) {

            System.out.println(" *Maximum Marks is = " + maximum);

             

            int[] testMarks = getMarks();

            for (int n = 0; n < testMarks.length; n++) {

                System.out.print(testMarks[n] + " ");

                if (n % 10 == 9)

                    System.out.println();

               

            }

        }

}

Comments

aJohny

Hope this helps

import java.util.Random;

public class ProcessMarks {

    private static final int NMARKS = 125;

    private static final double mean = 65.0;

    private static final double std = 15.0;

    /**

     * Returns an array of NMARKS integer marks approximately normally distributed,

     * with specified mean (mean) and standard deviation (std),

     * and with values outside 0..100 removed.

     * @return the array of marks.

     */

    public static int[] getMarks() {

        Random rand = new Random(1001L);

        int mark;

        int[] theMarks = new int[NMARKS];

        int n = 0;

        while (n < NMARKS) {

            mark = (int) Math.round(std * rand.nextGaussian() + mean);

            if (mark >= 0 && mark <= 100)

                theMarks[n++] = mark;

        }

        return theMarks;

    }

    public static int max(int[] getMarks) {

        int maximum = getMarks[0];

        for (int i = 0; i < getMarks.length; i++) {

            if (getMarks[i] > maximum) {

                maximum = getMarks[i];

            }

        }

        return maximum;

    }

    public static int min(int[] getMarks) {

        int minimum = getMarks[0];

        for (int i = 0; i < getMarks.length; i++) {

            if (getMarks[i] < minimum) {

                minimum = getMarks[i];

            }

        }

        return minimum;

    }

    /**

     * Test code

     * @param args not used

     */

    public static void main(String[] args) {

        int[] testMarks = getMarks();

      

        int maximum = max(testMarks);

        System.out.println(" *Maximum Marks is = " + maximum);

        int minimum = min(testMarks);

        System.out.println(" *Minimum Marks is = " + minimum);

        for (int n = 0; n < testMarks.length; n++) {

            System.out.print(testMarks[n] + " ");

            if (n % 10 == 9)

                System.out.println();

        }

    }

}

Hope this helps.

Cheers

AJ

aJohny

With Sorting:

import java.util.Arrays;

import java.util.Random;

public class ProcessMarks {

    private static final int NMARKS = 125;

    private static final double mean = 65.0;

    private static final double std = 15.0;

    /**

     * Returns an array of NMARKS integer marks approximately normally distributed,

     * with specified mean (mean) and standard deviation (std),

     * and with values outside 0..100 removed.

     * @return the array of marks.

     */

    public static int[] getMarks() {

        Random rand = new Random(1001L);

        int mark;

        int[] theMarks = new int[NMARKS];

        int n = 0;

        while (n < NMARKS) {

            mark = (int) Math.round(std * rand.nextGaussian() + mean);

            if (mark >= 0 && mark <= 100)

                theMarks[n++] = mark;

        }

        return theMarks;

    }

    public static int max(int[] getMarks) {

        int maximum = getMarks[0];

        for (int i = 0; i < getMarks.length; i++) {

            if (getMarks[i] > maximum) {

                maximum = getMarks[i];

            }

        }

        return maximum;

    }

    public static int min(int[] getMarks) {

        int minimum = getMarks[0];

        for (int i = 0; i < getMarks.length; i++) {

            if (getMarks[i] < minimum) {

                minimum = getMarks[i];

            }

        }

        return minimum;

    }

    /**

     * Test code

     * @param args not used

     */

    public static void main(String[] args) {

        int[] testMarks = getMarks();

       

        int maximum = max(testMarks);

        System.out.println(" *Maximum Marks is = " + maximum);

        int minimum = min(testMarks);

        System.out.println(" *Minimum Marks is = " + minimum);

        System.out.println(" *Before Sorting.");

        for (int n = 0; n < testMarks.length; n++) {

            System.out.print(testMarks[n] + " ");

            if (n % 10 == 9)

                System.out.println();

        }

        System.out.println();

        Arrays.sort(testMarks);

        System.out.println(" *After Sorting.");

        for (int n = 0; n < testMarks.length; n++) {

            System.out.print(testMarks[n] + " ");

            if (n % 10 == 9)

                System.out.println();

        }

        System.out.println();

    }

}

Cheers

AJ

RajenB

Hi,

Just to add to AJ's post:

Once you've sorted (using Arrays.sort(testMarks); ), just get the min and max by calling:

min = testMarks[0];

max = textMarks[testMarks.length-1];

If you don't want to change your original array (as you pointed out), make a copy before calling Arrays.sort().

Regards,
Rajen

aJohny

You could also use the below code after sorting to get the min and max value:

        int minValue = testMarks[0];

        int maxValue = testMarks[testMarks.length-1];

        System.out.println(" *Minimum Marks is = " + minValue);

        System.out.println(" *Minimum Marks is = " + maxValue);

Cheers

AJ

aJohny

If this answers the question, you can close the thread by marking the helpful/correct answers, so that it will help others also.

Cheers

AJ

joker3000

Thanks @"aJohny" and @"RajenB" . The reason i sort array is to find median and mean without change the original array, so maybe pass the array to temp and sort. How would i do it ?

   1/Keep original array

    2/ find Max, Min, Range, Mean, Median, Mode

    3/The grades method will return an array of characters, which are the grades corresponding to the integer marks in the array of marks.

    4/The gradeDistn method will accept an array of characters, which are the grades assigned for the array of marks, such as returned by the grades method.

i got it plan out each of methods through tutorial and Google. But i don't know how to put them all together to achieve this result:

A: 10
B: 30
C: 105
D: 75
E: 35

F: 10

Please help me out put them all together and display

Methods:

public static int range(int[] marks) {

int range=0;

return range;

}

public static double mean(int[] marks) {

double mean = 0;

double sum = marks[0];

          for (int i=0; i<marks.length; i++) {

     sum += marks[i];

     }

     return mean = sum / marks.length;

}

public static int median(int[] marks) {

int mid = 0;

int mid1 = marks.length/2;

     if (marks.length%2 == 1) {

return mid = marks[mid1];

}

else {

return mid = (marks[mid1-1] + marks[mid1]) / 2;

          }

}

public static int mode(int[] marks) {

int maxvalue = -1;

int maxcount = 0;

for(int i = 0; i < marks.length; i++) {

int count = 0;

for(int j = 0; j < marks.length; j++) {

if(marks[j] == marks[i]) {

count++;

     }

}

aJohny

This is a different question, so please raise a new thread.

Anyhow I have added the methods for Min,Max,Range,Mode,Mean. It is also cloning the original array to a tempArray and using that.

The code is given below:

import java.util.ArrayList;

import java.util.Arrays;

import java.util.List;

import java.util.Random;

public class ProcessMarks {

    private static final int NMARKS = 5;

    private static final double mean = 65.0;

    private static final double std = 15.0;

    /**

     * Returns an array of NMARKS integer marks approximately normally distributed,

     * with specified mean (mean) and standard deviation (std),

     * and with values outside 0..100 removed.

     * @return the array of marks.

     */

    public static int[] getMarks() {

        Random rand = new Random(1001L);

        int mark;

        int[] theMarks = new int[NMARKS];

        int n = 0;

        while (n < NMARKS) {

            mark = (int) Math.round(std * rand.nextGaussian() + mean);

            if (mark >= 0 && mark <= 100)

                theMarks[n++] = mark;

        }

        return theMarks;

    }

    public static int max(int[] marks) {

        int maximum = marks[0];

        for (int i = 0; i < marks.length; i++) {

            if (marks[i] > maximum) {

                maximum = marks[i];

            }

        }

        return maximum;

    }

    public static int min(int[] marks) {

        int minimum = marks[0];

        for (int i = 0; i < marks.length; i++) {

            if (marks[i] < minimum) {

                minimum = marks[i];

            }

        }

        return minimum;

    }

    public static double mean(int[] marks) {

        double mean = 0;

        double sum = 0;

        for (int i = 0; i < marks.length; i++) {

            sum += marks[i];

        }

        return mean = sum / marks.length;

    }

    public static int median(int[] marks) {

        int mid = 0;

        int mid1 = marks.length / 2;

        if (marks.length % 2 == 1) {

            return mid = marks[mid1];

        } else {

            return mid = (marks[mid1 - 1] + marks[mid1]) / 2;

        }

    }

    public static Integer[] mode(int[] marks){

      List<Integer> modeArray = new ArrayList<Integer>();

      int maxCount=0;  

      for (int i = 0; i < marks.length; ++i){

        int count = 0;

        for (int j = 0; j < marks.length; ++j){

          if (marks[j] == marks[i]) ++count;

        }

        if (count > maxCount){

          maxCount = count;

          modeArray.clear();

          modeArray.add( marks[i] );

        } else if ( count == maxCount ){

          modeArray.add( marks[i] );

        }

      }

      return modeArray.toArray( new Integer[modeArray.size()] );

    }

    /**

     * Test code

     * @param args not used

     */

    public static void main(String[] args) {

        int[] marks = getMarks();

        int[] tempMarks = marks.clone();

       

        Arrays.sort(tempMarks);

       

       

        System.out.println(" *Original Array.");

        for (int n = 0; n < marks.length; n++) {

            System.out.print(marks[n] + " ");

            if (n % 10 == 9)

                System.out.println();

        }

        System.out.println();

        System.out.println(" *Sorted Temp Array.");

        for (int n = 0; n < tempMarks.length; n++) {

            System.out.print(tempMarks[n] + " ");

            if (n % 10 == 9)

                System.out.println();

        }

        System.out.println();

        int minValue     = tempMarks[0];

        int maxValue     = tempMarks[tempMarks.length - 1];

        int rangeValue   = maxValue - minValue;

        double meanValue = mean(tempMarks);

        Integer[] modeArray = mode(tempMarks);

        System.out.println(" *Minimum Marks is = " + minValue);

        System.out.println(" *Minimum Marks is = " + maxValue);

        System.out.println(" *Range is = " + rangeValue);

        System.out.println(" *Mean is = " + meanValue);

        for (int i = 0; i < modeArray.length; ++i) {

            System.out.println(" *Mode is = " + modeArray[i].intValue());

        }

       

    }

}

I didn't quite understand your full requirement.  As mentioned please raise different thread if you have more questions.

Cheers

AJ

unknown-7404

Thanks aJohny and RajenB . The reason i sort array is to find median and mean without change the original array, so maybe pass the array to temp and sort. How would i do it ?

You aren't going to learn much if you just ask others to do your homework for you.

If you don't understand the assignment you should ask your instructor for more explanation.

The forum is to help you with YOUR code - not to write code for you.

i got it plan out each of methods through tutorial and Google. But i don't know how to put them all together to achieve this result:

Great!

Why don't you PROVE that to us by posting your WORKING code for EACH of those?

Once we see that you have really written code that works and does EACH of those things we can help you put it together.

I, for one, won't believe you really wrote code for those unless you post it.

joker3000

Thanks for your advise. But i'm not IT guy. I just learn from Google, Youtube and other student lecture slides for my own knowledge. If i have a proper lecture, i won't need to ask onliner. But sure, i try my own code and post it for advice next time.

Thanks @"aJohny", i will study your sample code.

unknown-7404

Unless you try writing your OWN code you aren't going to learn much of anything.

You say you are not an IT guy. So if you REALLY want to learn I suggest you quit getting code from whatever site you are using

Then start working your way through The Java Tutorials. There are traills, WITH WORKING EXAMPLE CODE, that cover all of the basic Java functionality.

https://docs.oracle.com/javase/tutorial/

1 - 10
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on May 10 2015
Added on Apr 10 2015
10 comments
3,079 views