3

I have a very simple task in Java, and I am not sure which structure to give to my project.

I want to create a little project in Java, that makes some statistical calculations. For example, I will need to create a method that gets an array, and returns the mean, another method gets an array, and returns a standard deviation, I will also need a method that gets two arrays, and returns the correlation coefficient.

What I want to know, is how to do this now that I have opened a new project in Eclipse ?

Should it all be in one class ? Should I have a separate class for each method, making it a static method ? At the end, I want to give this code for someone else to integrate it in his project. I need to do it as simple and efficient as possible.

Can you please guide me on how to do it ? One class, several classes ? Public / private ? I am not familiar with these things, but I can program the methods themselves.

Thank you in advance

user3275222
  • 225
  • 3
  • 12
  • If it is a simple task then just use one class. – Alexandre Santos May 19 '14 at 06:31
  • 1
    You're asking a question about basic OO principles - there are many answers. Try reading up at http://en.wikipedia.org/wiki/SOLID_(object-oriented_design) for starters. – Evan Knowles May 19 '14 at 06:31
  • Single class or separate class, it's up to you actualy – DnR May 19 '14 at 06:32
  • 1
    From what you describe (mean, standard deviation,...), it sounds like you should be able to implement static methods in a single class. If you don't need to maintain a state between method calls, static mehtods make sense. – Marcel Steinbach May 19 '14 at 06:32

7 Answers7

2

All your methods have the following attributes:

  1. That they don't possibly have another implementation as long as your give them specific enough names. After all mathematical doesn't change. This means you don't possibly need structure like interfaces or subclasses.

  2. That when people use them they have tendency to use several of them or group by functionality. That means you should group your methods by usage e.g. statistical methods; signal processing methods; and so on.

  3. That the methods don't keep internal status and all the output is returned without any side effect of other callers/threads. Thus your methods don't have to have class contain themselves or any statue variables.

  4. That your methods essentially provider utility to the main program but the semantics of the methods doesn't vary due to the caller or calling context.

So as all the above shows, your methods should be inside 1 or several classes as grouped by their nature or usage. The methods should be static methods without any side effect. That's exactly what java.lang.Math does.

Alex Suo
  • 2,977
  • 1
  • 14
  • 22
1

I want to create a little project in Java, that makes some statistical calculations. For example, I will need to create a method that gets an array, and returns the mean, another method gets an array, and returns a standard deviation, I will also need a method that gets two arrays, and returns the correlation coefficient.

Looks to me that you are interested in creating a utility class for statistical calculation. The scope of how to achieve is this quite broad but it is advised to follow common coding conventions and basic OOP concnepts.

Should it all be in one class ? Should I have a separate class for each method, making it a static method ?

Since each of the methods ( mean, standard deviation ...) are related to the same core background (i.e to perform some statistical calculation), it seems logical to have a single utility class with a separate static methods for each of the function that you need to create.

Of-course you will have to take care of the basic OOP concepts like (data hiding) keeping the fields private and exposing them properly public getter/setters. Also, it would be a good idea to keep your calculation methods private and just exposing a public method which calls your private functions. Something like

public class MyUtilityClass{
    // A bunch of private fields
    int field1; ...


    private MyUtilityClass(){} // We don't want anyone to create an object of this class

    // method exposed to user
    public static float calcArithmeticMean(float[] arr1, float[] arr2){
        return getMean(arr1, arr2);
    }

    // method for internal use
    private float getMean(float[] f1, float[] f2){
        //do your calculation here
    }



    // remember to expose only those fields that you want the user be able to access
    // getter/setters here

}

At the end, I want to give this code for someone else to integrate it in his project.

If you follow proper OOP coding conventions, then your utility class will be portable and anyone will be able to understand and extend it in their application.

Saif Asif
  • 5,516
  • 3
  • 31
  • 48
1

I would create a single class representing the array of numbers itself.

   public class DataSet extends HashSet<Double> {
        public double mean () {
            // implementation
        }
        public double standardDeviation () {
            // implementation
        }
        public double correlationCoefficient (DataSet other) {
            // implementation
        }
    }
aepryus
  • 4,715
  • 5
  • 28
  • 41
1

My first suggestion is to start your project using Maven. It gives you a solid project structure with a great tool to manage your jar file dependencies and build lifecycle. In addition, all major Java IDEs, including Eclipse, easily create, understand and use your Maven settings.

Secondly, for your application design, it is recommended to avoid using lots of static methods because they hurt testability of your code as for example explained here.

Regarding the number of classes and methods, it depends on your specific use case but the guideline is to try to aggregate similar methods, based on their responsibilities, in one class while separating classes if there are too many responsibilities being handled by a single class. Low coupling and high cohesion are your friends in this case.

Arrays may be slightly faster than collections but be careful with them because they are reifiable and do not mix well with generics. Generally, rely on Collections. Also, if you can use Java version 8, have a look at Streams API.

Last but not least, Java has tons of open source code out there. So, always look for a library before starting to write one. In case of Math, have a look at this and that.

Community
  • 1
  • 1
Amir Moghimi
  • 1,391
  • 1
  • 11
  • 19
0

Create one class with a different methods with public access for each calculation type(one method for each of mean, standard deviation and so on). These methods can internally refer to helper methods in another utility class(es) not publicly accessible, as per your convenience.

Put all these classes in a single package and export it for integrating in other projects.

Since it will be used by others as a library by others , make sure you document and comment it as much as possible.

A Nice Guy
  • 2,676
  • 4
  • 30
  • 54
0

I vote for single class. The methods should be static and the parameters that you don't want to show should be private.

0

It depends on many thing such as other part of project, future changes and extensions,... I suggest to start with single-class/public-static and change it in demand when you expand the project.

Farvardin
  • 5,336
  • 5
  • 33
  • 54