Reputation: 329
I need to complete the following methods in the class below, labled part a and part b. The class is supposed to calculate the gpa for the array Grades, and add the bonus for AP class grades. P indicates an AP grade. For example "B-P" is an AP grade, and "B-" is a normal grade. For part a, I am supposed to get a result of 1.7122 for "C-P", and for part b, it needs to process the entire array of grades and get a result of 3.455333.....
I was told which parts are parts a and b and labeled them using comments. I also commented labeling where I think the rest of the code should go for part a and b to calculate the results.
Can someone please explain how to do this and what methods should be used?
Here is my code (I know it is incorrectly formatted, it isn't in JCreator, but I couldn't get it to copy on to here correctly):
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();
}
}
Upvotes: 0
Views: 1235
Reputation: 569
I'm not sure how much java you know but since this is homework I'll just write it in pseudocode and leave you to translate it to java. Let me know if you're really stuck though and we'll be happy to give you a hand with the specific code.
So, we'll start with calculateGrade. What you want to do is loop through all elements of ltrGrades until you find one that matches the first two characters of your input, which will give you the index of the letter grade you want. Then, you just have to return the value at the same index of dGrades. So,
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
Let me know once you have that working, or if it was unclear at all.
edit: before someone says this isn't the simplest way, I'm just describing the basic algorithmic functionality you're looking for. It can be improved using methods to, perhaps, find the string in the array
edit 2: here is a breakdown of how you would achieve each of the individual sections.
First, get the substring of str. Check http://www.tutorialspoint.com/java/java_string_substring.htm for information about substring, but essentially what you want is something like
String firstTwo = str.substring( 0,2 );
Next, iterate through ltrGrades, so something like
for ( int i=0; i<ltrGrades.length; i++ ) {
Then, to check if the i'th element of ltrGrades is the substring you want,
if ( firstTwo == lrtGrades[ i ] ) {
Finally, if it is then you would return the i'th element of dGrades, so
return dGrades[ i ]
Sorry if that's not exactly correct, it's all from memory so I'm sure there are some little mistakes in there but should give you the idea. Let me know when you have that all together working.
Upvotes: 1