Troubleshooting java script maximum / minimium / average / barchart code 2 classes 4 function methods

27 Views Asked by At

My goal is to have it output the min max avg and a barchart.

`

package arraystuff1;
    import java.util.Random;
    public class problem1 
    {
    public static void main(String[] args) 
    {
        int min=0;
        int max=0;
        int avg=0;
        //initialize array
        Random randomNumbers = new Random();
        final int Length = 30;
        int[] array1 = new int[Length];
        for ( int counter = 0; counter < array1.length; counter++ )
        {
        array1[ counter ] = 1+randomNumbers.nextInt( 100 );
        }
        //array tested and produces correct output
        for ( int counter = 0; counter < array1.length; counter++ )//minimum
        {
                 min = functionsp1.min(array1[counter],min);
                 if(counter == Length-1)
                     System.out.printf("The minimum value is: %d\n",min);
         
        }
        for ( int counter = 0; counter < array1.length; counter++ )//maximum
        {
                 max = functionsp1.max(array1[counter],max);
                 if(counter == Length-1)
                     System.out.printf("The maximum value is: %d\n",max);
         
        }
        for ( int counter = 0; counter < array1.length; counter++ )//average
        {
                 avg = functionsp1.avg(array1[counter],Length);
                 if(counter == Length-1)
                     System.out.printf("The average value is: %d\n",avg);
         
        }
        for ( int counter = 0; counter < array1.length; counter++ )//average
        {
                functionsp1.bar(array1[counter],Length);//print a line showing range of the bar graph  use switch case to build frequencies for each 10th unit
                functionsp1.freq(array1[counter],Length);//print stars representing number of random numbers that fit in this category
                     
         
        }
        
        
            
    }
}

` ///////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////

`

package arraystuff1;
public class functionsp1 
{
    public static int min(int counter,int min)//input array index and length
    {
        if (counter < min)
        {
            counter = min;
        }
        return counter;
    }
    public static int max(int counter,int max)
    {
        if (counter > max)
        {
            counter = max;
        }
        return counter;
    }
    public static int avg(int current,int total)//input array length for total and index for current
    {
        int sum;
        int fintotal;
        for (int counter = 0; counter < total; counter++ )
            {
            sum = current + sum; // random number 0-99 add 1 to make it 1-100
            if (counter == total)
                fintotal = sum/total;
            return fintotal;
            }
            System.out.printf("The average of the array is: %2d",fintotal);
            //System.out.printf( "%s%8s\n", "Index", "Value" ); // column headings
    return fintotal;
    }
    public static void bar(int index,int limit)//length and array variable
    {
        System.out.println("Grade Distribution:  ");
        for(int counter = 0; counter < limit;counter++)
        {
            if (counter ==10)
                System.out.printf("%5d: ", 100);
            else
                System.out.printf("%02d-%02d: ",counter*10,counter*10+9);
        }   
    }   
    public static void freq( int stars )
   {
      for(int counter=0;counter<stars ; counter ++)
               System.out.print("*");
   } // end main
}

`

I have verified the array initialized with 30 random numbers in the range of 1-100. My goal is to get the bar chart and the other functions to work but this is the first time I have played with an array any suggestions?

1

There are 1 best solutions below

0
DevilsHnd - 退した On

Let your Functionsp1 class methods deal with the Array for example:

//initialize array
java.util.Random randomNumbers = new java.util.Random();
final int length = 30;
int[] array1 = new int[length];
for (int counter = 0; counter < array1.length; counter++) {
    array1[counter] = randomNumbers.nextInt(100) + 1;
}
//array tested and produces correct output
    
Functionsp1 fnctn = new Functionsp1();
int min = fnctn.min(array1);
int max = fnctn.max(array1);
int avg = fnctn.avg(array1);
System.out.printf("The minimum value in array1 is: %d%n", min);
System.out.printf("The maximum value in array1 is: %d\n", max);
System.out.printf("The average value in array1 is: %d\n", avg);
System.out.println("Horizontal Graph For Numerical Range: " + min + " To " + max);
fnctn.bar(array1);

public class Functionsp1 {

    public int min(int[] array) {
        int min = Integer.MAX_VALUE;
        for (int i = 0; i < array.length; i++) {
            if (array[i] < min) {
                min = array[i];
            }
        }
        return min;
    }

    public int max(int[] array) {
        int max = Integer.MIN_VALUE;
        for (int i = 0; i < array.length; i++) {
            if (array[i] > max) {
                max = array[i];
            }
        }
        return max;
    }

    public int avg(int[] array) {
        int avg = 0;
        int sum = 0;
        for (int i : array) {
            sum += i;
        }
        avg = sum / array.length;
        return avg;
    }

    public void bar(int[] array) {
        System.out.println();
        System.out.println("Grade Distribution:");
        System.out.println("0 0         10        20        30        40        50"
                + "        60        70        80        90        100");

        StringBuilder sb = new StringBuilder("");
        for (int i = 0; i < array.length; i++) {
            int counter = 0;
            for (int j = 0; j < array[i]; j++) {
                if ((counter + 1) % 10 == 0) {
                    sb.append("+");
                }
                else {
                    sb.append("|");
                }
                counter++;
            }
            System.out.printf("%-3s%-103s(%-3s)%n", (i+1), sb.toString(), array[i]);
            sb.setLength(0);
            //System.out.printf("%-3s%-100s%n", (i + 1), String.join("", java.util.Collections.nCopies(array[i], "|")));
        }
        System.out.println();

    }

    public void freq(int stars) {
        for (int counter = 0; counter < stars; counter++) {
            System.out.print("*");
        }
    }
}

I still can't figure out what the freq() method is suppose to accomplish.

As the code above stands right now, if run will produce something like the following in the Console Window:

The minimum value in array1 is: 1
The maximum value in array1 is: 99
The average value in array1 is: 55
Horizontal Graph For Numerical Range: 1 To 99

Grade Distribution:
0 0         10        20        30        40        50        60        70        80        90        100
1  |||||||||+||||                                                                                         (14 )
2  |||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||                                                (55 )
3  |||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+             (90 )
4  |||||                                                                                                  (5  )
5  |||||||||+                                                                                             (10 )
6  |||||||||+|||||||||+||||||||                                                                           (28 )
7  |||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+||||         (94 )
8  |||||||||+|||||||||+|||||||                                                                            (27 )
9  |||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|            (91 )
10 |||||||||+|||||||||+|||||||||                                                                          (29 )
11 |||||||||+|||||||||+|||||||||+|||||||||+|||||||||                                                      (49 )
12 |||||||||+|||||||||+|||||||||+|||||||||+||                                                             (42 )
13 |||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||      (97 )
14 |||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+||||         (94 )
15 |||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||    (99 )
16 |                                                                                                      (1  )
17 |||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||                        (79 )
18 |||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||        (95 )
19 |||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+                                           (60 )
20 |||||||||+|||||||||+|||||||||+|||||||||+|                                                              (41 )
21 |||||||||+|||                                                                                          (13 )
22 |||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+||                               (72 )
23 |||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||                              (73 )
24 |||||||||+|||||||||+|||||||                                                                            (27 )
25 |||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+                       (80 )
26 |||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+||                     (82 )
27 |||||||||+|||||||||+|||||||||+||||                                                                     (34 )
28 |||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+|||||||||+||||||||     (98 )
29 |||||||||+|||||||||+|||||||||+                                                                         (30 )
30 |||||||||+|||||||||+|||||||||+|||||||||+|||||||||+||||                                                 (54 )