The full credit for this exercise is 15 points. To receive full credit, you will need to successfully create the source code for the
application. You will submit your source code below.
Create a command-line application named GradePointAverage that calculates the grade-point average of the letter grades represented by a string
that the user supplies as a command-line argument. Probably the easiest way to begin this exercise is to start with your GradePoints program. Your solution to this exercise will use the static method
gradePoints()
that you created previously.
Additionally, you will need to create another static method
gradePointAverage()
that accepts a
String
reference as parameter. The characters contained in this
String
object will represent a collection of letter grades. The
gradePointAverage()
method should return a
double
which represents the grade-point average (GPA) of the letter grades. For example, after the following code executes, the variable
result
contains 3.25:
double result = gradePointAverage("ABAC");
In your
gradePointAverage()
method you will need to loop through the characters contained in the
String
object. There are two methods provided by the
String
class that you will find useful. The method
length()
returns the number of characters contained in a
String
. The method
charAt(int)
returns the
char
at the specified index.
The indexes for the chars contained in a
String
object begin with 0. Here's an example of some code that uses both of these methods:
String s = "Java rocks!"
System.out.println("The length of s is " + s.length());
System.out.println("The third char of s is " + s.charAt(2));
In your
main()
method, if the user does not supply exactly one command-line argument, display a usage message and exit the application. Otherwise calculate the GPA of the entered grade string. Here's a possible
main()
method:
public static void main(String[] args) {
if (args.length != 1) {
System.out.println("Usage: java Grades GradeString");
System.exit(0);
}
// Use gradePointAverage() to calculate the GPA
// of the grades represented in args[0]
}
In the text box below, cut and paste the source code for the GradePointAverage application.
Click the Submit button to submit your code.