1 Replies - 176 Views - Last Post: 12 February 2013 - 11:51 PM Rate Topic: -----

#1 awfootball7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 06-February 13

Array driver class Java

Posted 12 February 2013 - 11:38 PM

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
{

    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);
    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: Array driver class Java

#2 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Array driver class Java

Posted 12 February 2013 - 11:51 PM

Well, you need to create objects of your Array class in the main method, and using this object, you can call the respective functions.

Something like this.

Array arrayObject = new Array();


regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1