0

Hey guys I'm having problems figuring out how to write my driver class for my Array class.

My questions

1.How do I make the array in my Array class reference the array in my Driver class?

2.How do I pass my class methods from my Array class to my Driver class so I can print them out?

Here's my two bits of code

Main class:

import javax.swing.*;
import java.util.*;
public class Array
{

    int [] numbers = new int[]{3,2,1,1,5};

    double sum = 0;
    int max = 0;
    int min = numbers[0];
    double sd = 0;
    int mode = 0;
    int modeCount = 0;
    public double average()
    {
        for(int i=0; i<numbers.length; i++)
        {
            sum = sum + numbers[i];
        }

        double average = sum / numbers.length;
        return average;
    }

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

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

    public double standardDeviation()
    {
        for (int i=0; i<numbers.length;i++)
        {
            sum = sum + numbers[i];
            double average = sum / numbers.length;
            {
                sd += ((numbers[i] - average)*(numbers[i] - average)) / (numbers.length - 1);
            }
        }
        double standardDeviation = Math.sqrt(sd);
        return standardDeviation;
    }

    public int mode()
    {
        for (int i = 0; i < numbers.length; ++i) 
        {
            int count = 0;
            for (int j = 0; j < numbers.length; ++j) 
            {
                if (numbers[j] == numbers[i]) ++count;
            }
            if (count > modeCount) {
                modeCount = count;
                mode = numbers[i];
            }
        }
        return mode;
    }
}

Driver class:

import javax.swing.*;
import java.util.*;

public class ArrayTest
{
    public static void main(String [] args)
    {
        int[] numbers;
        numbers = new int [20];
        Random rand = new Random(2621); 
        int maxRange = 65;
        int minRange = 20;

        for(int i=0; i<20; i++)
        {
            numbers[i] = rand.nextInt(maxRange - minRange + 1) + minRange;       
        }

        Arrays.sort(numbers);
    }
}
2
Contributors
1
Reply
15
Views
4 Years
Discussion Span
Last Post by stultuske
0

well ... first of all, you'll have to use it.
instantiate it, and call the methods through that instance. I would recommend re-naming your Array class though, for instance MyArray, that way it's easier to see for us that you're talking about a class you wrote and not an actual array (without going through the code)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.