tags:

views:

54

answers:

3

Let me start by saying, I am new to Java programming. I have coded something out that performs in the manner that I want it to. However, it is not very well written.

Here is my code:

import java.lang.Math;

public class Advisor_Score {

    public static void main(String[] args){

        double p1_1[] = {101,1,1,1.5,.5};
        double p1_2[] = {101,2,2.5,2,4};
        double p2_1[] = {102,1,5,5,5,5,5,5,5};
        double p2_2[] = {102,2,2,5,3,4,5,1.5,2.5,5};

        //These arrays represent individual users. The first value in the array is their customer number and the second is domain.
        double All_users[][]={p1_1,p1_2,p2_1,p2_2};

        //This is a 2-dimensional array takes into account all users.

        double[] sum = new double[All_users.length];
        double[] raw_advisor = new double[All_users.length];
        double[] advisor_score = new double[All_users.length];
        for (int i=0;i<All_users.length;i++){
            for(int j=2;j<All_users[i].length;j++){
                    sum[i]+=All_users[i][j];
            }

            raw_advisor[i]=((sum[i]-(3*(All_users[i].length-2)))/4);
            advisor_score[i]= 2.5+(2.5*(1-Math.pow(Math.E, -.5*raw_advisor[i])));
            System.out.print("Customer ID "+All_users[i][0]);
            System.out.print(", Domain "+All_users[i][1]);
            System.out.println(": "+advisor_score[i]);
        }   
    }   
}

However I clearly over rely on the main method. I was wondering if anyone could help me integrate more methods and streamline the whole process. Thanks in advance, I am sorry again. I am new to Java and programming.

A: 

This is a really small amount of code, and if it works, why break it? That being said, read up a little on the Object Oriented paradigm, and then look at your problem the code is solving, and think about how it would work in that context. Good object oriented design generally tends to start out on paper, for me anyways.

Alex Hart
+1  A: 

This particular program, given it's small size, I would not refactor. Making an OO 'hello world' is trying to make a limousine out of a tricycle, IMO. In general, though, when I start a larger project I want to design with OO concepts in mind.

At a high level, I try to picture the 'entities' I will be building i.e. concrete 'employees,' abstract 'actions,' and their relationships. These real-world 'entities' typically become classes - which OO lends itself to modeling very well via inheritance and other related concepts. Their relationships are then described by the interfaces they expose - being careful to hide the 'internals' of each object to keep everything de-coupled.

Going more into detail, I identify the 'utility' code which multiple parts of my application might use - this may become a static/global helper class.

More detailed still, within a class, I try to restrict functions/methods to accomplishing one goal only, to reduce side effects.

Of course, I don't always get it right the first time. However, building larger and larger projects helps you recognize when certain design patterns work and when they will not. Time, budget, technologies, and so on, all play a part in this decision-making.

Alex
+2  A: 

Although it may be small, it could be good practice taking a crack at making this more object oriented - here's a possible start.

The first thing suggested is from your comment

// These arrays represent individual users.
// The first value in the array is their customer number and the second is domain.

You don't say what the remaining numbers are, but from other comments I gather they are some kind of score. This suggests something like

public class CustomerDomain
{
    private int customerNumber;
    private int customerDomainID;
    private List<Double> scores = new ArrayList<Double>();

    public CustomerDomain(int number, int domainID)
    {
        this.customerNumber = number;
        this.customerDomainID = domainID;
    }

    public void addScore(double score)
    {
        this.scores.add(score);
    }
    public List<Double> getScores()
    {
        return this.scores;
    }
}

Although in real life I'd probably not have the scores embedded in the customer class.

Stephen P