Java新手,帮助将此方法转换为类。过度依赖主要方法

时间:2010-09-14 00:21:30

标签: java methods

首先我要说的是,我是Java编程的新手。我编写了一些以我想要的方式执行的操作。但是,写得不是很好。

这是我的代码:

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]);
        }   
    }   
}
然而,我显然过分依赖主要方法。我想知道是否有人可以帮助我整合更多方法并简化整个过程。提前谢谢,我很抱歉。我是Java和编程的新手。

3 个答案:

答案 0 :(得分:2)

尽管它可能很小,但为了使这更加面向对象可能是一个很好的做法 - 这是一个可能的开始。

建议的第一件事是来自你的评论

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

你没有说出剩下的数字是什么,但是从我收集的其他评论来看,它们是某种得分。这表明类似

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

虽然在现实生活中我可能没有在客户类中嵌入分数。

答案 1 :(得分:1)

这个特别的程序,考虑到它的小尺寸,我不会重构。制造OO'hello world'正试图用三轮车制造一辆豪华轿车,IMO。但总的来说,当我开始一个更大的项目时,我想要考虑OO概念。

在高层次上,我试图描绘我将要建立的“实体”,即具体的“员工”,“抽象的”行动,以及他们之间的关系。这些真实世界的“实体”通常成为类--OO通过继承和其他相关概念很好地建模。然后他们的关系通过他们暴露的界面来描述 - 小心地隐藏每个对象的'内部'以保持所有去耦合。

更详细地说,我确定了应用程序的多个部分可能使用的“实用程序”代码 - 这可能会成为一个静态/全局帮助程序类。

更详细的是,在课堂上,我尝试将功能/方法限制为仅实现一个目标,以减少副作用。

当然,我并不总是第一次做对。但是,构建越来越大的项目可以帮助您识别某些设计模式何时起作用以及何时起作用。时间,预算,技术等都在决策中发挥作用。

答案 2 :(得分:0)

这是一个非常少量的代码,如果它有效,为什么打破它?话虽如此,请阅读面向对象的范例,然后查看代码正在解决的问题,并思考它在该环境中的工作方式。对于我来说,良好的面向对象设计通常倾向于从纸上开始。