LAB EXERCISE 19: Student GPA

Goal: In this exercise, you will learn how to:
1. Insert the Main Method (Main Entry Point to the Java Program)
2   Import a Java Class Library
3.  Create a Superclass
4.  Create Subclasses
5.  Declare Public Variables that can be accessed by a Subclass
6.  Create Public Void Method that can be accessed by a Subclass
7.  Create a Constructor
8.  Use a Try-Catch block statement for error handling and exceptions
9.  Create Methods that returns a value to the calling object’s method
10.  Use the This keyword to reference a method in a Superclass
11.  Create the Object from the Class
12.  Call the Object’s Method


Program Specifications:
Create an application to allow a user to enter the student information including student ID, name, and number of courses taken in the semester.  It will prompt the user to enter the student’s courses, the letter grade and credited units for each corse.  The application will then calculate the student’s GPA for the semester.
 
A. Pre-requisites:
1. Create a folder on your desktop Exercise-19

2. Launch Java EE- Eclipse
Note:  You will need to use the Java Perspective Workbench for this exercise

3. Setup your Eclipse Workspace to point to the Exercise-19 folder
a. Select File-> Switch Workspace
b. Browse and select your Exercie-19 folder as your Workspace.



B. Requirements:

1.  Create a Java Project and name it as StudentGPA

2.  Create the first Class that will have the Main Method

a.       Name the Class as MainApp

b.       Choose the main method to insert into the class

        

3.  Create the second Class that will be the superclass

a.      Name the Class as Student

b.      Do not select the main method stub

   

4.  Create a class called Course.  This subclass will inherit the methods and variables of the Student superclass

5.  Create a class called Grade.  This subclass will inherit the methods and variables of the Course superclass

6.  Create a class called GPA.   This subclass will inherit the methods and variables of the Grade superclass
 

C. Requirements for the Student Class:

1.  Insert the import java.util.Scanner class which will allow data to be inputted from the console. 
Insert the line @SuppressWarnings("resource") 

 

2.  Declare the Public variables studentID, studentName and semester using the String data type.
Declare the Public variable numCourses using the Integer data type
NOTE:  These variables will be accessed by the subclass

3.  Create the Constructor called Student() that that will prompt the user to input the data
Note: This special method executes code and does not return a value.  The method will be access by the subclass.   Below is the code to prompt the user to enter the data

Scanner inputStudentID = new Scanner(System.in);
System.out.print("Enter student ID: ");
studentID = inputStudentID.nextLine();

Scanner inputStudentName = new Scanner(System.in);
System.out.print("Enter student name: ");
studentName = inputStudentName.nextLine();

Scanner inputSemester = new Scanner(System.in);
System.out.print("Enter the semester: ");
semester = inputSemester.nextLine();

Scanner inputNumCourses = new Scanner(System.in);
System.out.print("Enter number of courses taken by student in " + semester + " :");
numCourses = inputNumCourses.nextInt();               
System.out.println();

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


D. Requirements for the Course Class:

1.  Insert the import java.util.Scanner class which will allow data to be inputted from the console. 
Insert the line @SuppressWarnings("resource") 

 

2.  The Course class will be a subclass of the Student parent class (superclass)
NOTE: A subclass will inherit the methods and variables from the superclass

Add the keyword "extends Student" after the name of class

 

3.  Declare the array as public that will store the student courses-  String courseNameArray [] = new String [numCourses]

 

4.  Create the Void Method - courseNameArray that will prompt the user to enter the courses
a.  Use a For Loop to perform the iteration of entering the courses taken by the student based on the number of courses entered.

// for loop to prompt the user to enter the course name using the number of courses taken
// note: numCourses is data was entered in the Student parent class.

for (int i = 0; i < numCourses; i++) {
       Scanner inputCourseName = new Scanner(System.in);
       inputCourseName = new Scanner(System.in);
       System.out.print("Enter the course " + (i+1) + " name for " + studentName +" :");
       courseNameArray[i] = inputCourseName.nextLine();
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


E. Requirements for the Grade Class:

1.  Insert the line @SuppressWarnings("resource") 

 

2.  The Grade class will be a subclass of the Course parent class (superclass)
NOTE: A subclass will inherit the methods and variables from the superclass

Add the keyword "extends Course" after the name of class

 

3.  Declare the public arrays that will store the data
String courseGrade [] = new String[numCourses]
int courseCreditHours [] = new int[numCourses]

4.  Create the Void Method - getGrade() that will prompt the user to enter the course grade and course units
a.  Call the getCourse method from the parent class – Course. 
     Use the “This” keyword to call the method
b.  Declare the scanner objects used to input the data from the console

Scanner inputGrade = new Scanner(System.in);
Scanner inputCreditHours = new Scanner(System.in);


c. Use a For Loop to perform the iteration of the course grade and units taken by the student based on the number of courses entered.

for (int i = 0; i < numCourses; i++) {
System.out.println();

System.out.print("Enter the letter grade for course " + this.courseNameArray[i] + " :");
courseGrade[i] = inputGrade.nextLine();

System.out.print("Enter the credit unit hours for course " + this.courseNameArray[i]  + " :");
courseCreditHours[i] = inputCreditHours.nextInt();

 }  

   d. Close the scanner objects

inputGrade.close();
inputCreditHours.close();



 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


F. Requirements for the GPA Class:

1.  The GPA class will be a subclass of the Grade parent class (superclass)
NOTE: A subclass will inherit the methods and variables from the superclass

Add the keyword "extends Grade" after the name of class

 

2.  Declare the local variables gpa

3.  Declare the local variables that will be using for the running totals-
coursePoints, totalPoints
and totalCreditHours using the double data type.

4.  Declare the public arrays that will store the data
int gradePoints [] =  new int[numCourses]

5.  Create the Method - calculateGPA() that will determine
a.  Use a For Loop and Switch-Case Block to go through each course to determine the grade points and get the running total to compute for the GPA

for (int i = 0; i < numCourses; i++) {                                                                 


// switch-case block to determine the equivalent grade points from the course letter grade
switch (courseGrade[i].toUpperCase()) {
    case "A":
             gradePoints[i] = 4;
             break;
    case "B":
             gradePoints[i] = 3;
             break;
     case "C":
             gradePoints[i] = 2;
             break;
     case "D":
             gradePoints[i] = 1;
             break;
    default:
             gradePoints[i] = 0;

      }

coursePoints = courseCreditHours[i] * gradePoints[i];
totalPoints = coursePoints + totalPoints;
totalCreditHours = courseCreditHours[i] + totalCreditHours;
}


   b. Calculate the GPA and return the value

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


G. Requirements for the MainApp Class:

1.  Add comments (documentation)– Program Description, Author and Date

2.  Create the instance of object called studentGPA from the GPA class

 

3.    Call the object's method to get the student information studentGPA.getGrade()

 

4.  Call the object's method studentGPA.calculateGPA() to calculate the student's GPA and store the results into the variable studentGrade

5. Display the results

// display the results on the screen
System.out.println();
System.out.print("Student semester GPA: " );
System.out.format("%.02f", studentGrade); 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


H. Test:  

1.   Save your Java code

2.   Compile and run your Java program.

3.   Verify there is no syntax, logical or run-time errors.

4.   Use the following set of test data to determine if the application is running properly

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


I. Submit your exercise in the Canvas Lab Exercise #19 Drop Box.

1. Submit the screen shot of the Eclipse Workbench window showing the Console output screen.
You can use Paint (save as JPG) or Word to paste the screenshot.

2. Zip up and submit the compressed StudentGPA subfolder that is in the Exercise-19 folder.
NOTE: Right click on the
subfolder and select Send to “Compress Folder”.  The file will have a file extension of .zip.