计算等级数组

时间:2013-03-27 00:03:20

标签: java arrays

我需要在下面的课程中完成以下方法,标记为a部分和b部分。该类应该计算阵列等级的gpa,并为AP等级添加奖励。 P表示AP等级。例如,“B-P”是AP等级,“B-”是正常等级。 对于a部分,我应该得到“C-P”的1.7122的结果,而对于b部分,它需要处理整个等级并得到3.455333的结果......

我被告知哪些部分是a和b部分,并使用注释标记它们。我还评论了标签,我认为其余代码应该用于a和b来计算结果。

有人可以解释一下如何做到这一点以及应该使用哪些方法?

这是我的代码(我知道它格式不正确,它不在JCreator中,但我无法将其正确复制到此处):

public class GPAFreeResponse 
{
    private String[] grades    = {"A P", "B+P", "B-P"};
    private String[] ltrGrades = {"A ", "A-", "B+", "B ", "B-",
                                  "C+", "C ", "C-", "D ", "E "};
    private double[] dGrades   = {4.0, 3.7, 3.3, 3.0, 2.7, 2.3, 
                                  2.0, 1.7, 1.3, 1.0, 0.0};
    private double[] bonusPts  = {.0488,.0488,.0366,.0366,.0366,
                                  .0122,.0122,.0122,.0061,.0061, 0.0};
    private double dGrade;

    public GPAFreeResponse()
    {
        dGrade= calculateGrade("C-P") + calculateBonus("C-P"); // part a
        System.out.println("Part 9a): " + dGrade);
        dGrade = calculateGPA();                // part b
        System.out.println("Part (b): " + dGrade);
    }

    public double calculateGPA()                        // part b
    {
        double dResult = 0.0;
        double dTotalQP = 0.0;
        double dBonusPt = 0.0;

        //more code goes here

        return dResult;
    }

    public double calculateGrade(String str)            // part a
    {
        double dResult = 0.0;

        // more stuff here

        return dResult;
    }

    public double calculateBonus(String str)            // part a
    {
        double dResult = 0.0;

        // and more stuff here

        return dResult;
    }

    public static void main(String[] args)
    {
        //create an instance of GPA
        new GPAExtra();
    }    
}

1 个答案:

答案 0 :(得分:1)

我不确定你知道多少java但是因为这是家庭作业我只是用伪代码写它并让你把它翻译成java。如果您真的感到困惑,请告诉我们,我们很乐意为您提供具体代码。

所以,我们将从calculateGrade开始。 你想要做的是循环遍历ltrGrades的所有元素,直到找到一个与你输入的前两个字符匹配的元素,这将为你提供你想要的字母等级的索引。然后,您只需要在dGrades的相同索引处返回该值。所以,

initialise string firstTwo as subtring of str from 0 to 1 // you'll have to work out how to actually implement this
repeat with ( i from 0 to the length of ltrGrades )
    if ( firstTwo is equal to the i'th element of ltrGrades ) then
        return the i'th element of dGrades
//catch case where it wasn't found
return 0.0

一旦你有了这个工作,或者根本不清楚,请告诉我。

编辑:在有人说这不是最简单的方法之前,我只是描述你正在寻找的基本算法功能。可以使用方法来改进它,或许可以在数组中找到字符串

编辑2:这里是您如何实现每个部分的细分。

首先,获取str的子字符串。检查http://www.tutorialspoint.com/java/java_string_substring.htm以获取有关子字符串的信息,但基本上您想要的是

String firstTwo = str.substring( 0,2 );

接下来,迭代ltrGrades,类似

for ( int i=0; i<ltrGrades.length; i++ ) {

然后,检查ltrGrades的第i个元素是否是你想要的子字符串,

if ( firstTwo == lrtGrades[ i ] ) {

最后,如果是,那么你将返回dGrades的第i个元素,所以

return dGrades[ i ]

很抱歉,如果这不是完全正确的话,那一切都来自记忆,所以我确信那里有一些小错误,但应该给你这个想法。让我知道你什么时候一起工作。

相关问题