2013-03-27 58 views
0

我需要在下面的類中完成以下方法,標註爲a部分和b部分。該班級應該計算陣列成績的GPA,併爲AP成績增加獎勵。 P表示AP等級。例如,「B-P」是AP等級,「B-」是正常等級。 對於一部分,我應該得到的1.7122結果爲「CP」,和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

寫下你的算法(解決問題)如何看僞碼。然後將其翻譯成Java。 – Patashu 2013-03-27 00:15:09

回答

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 ] 

對不起,如果這不完全正確,這一切都來自內存,所以我敢肯定,有一些小錯誤,但應該給你的想法。讓我知道你什麼時候一起工作。

+0

對不起,但你能再給我多一點嗎?我完全無能爲力 – 2013-03-27 00:58:37

+0

我收到錯誤:找不到字符串firstTwo = str的符號。子串(0,2); – 2013-03-27 01:39:20

+0

是完整的錯誤? – 2013-03-27 01:44:15