Starting from:

$25

COMP1210-Activity 4B Grade Calculations Solved

In this activity you will create two classes. First, you will create a class called Grade which will represent your grade in COMP 1210. The second class, which will be called GradeGenerator, is a driver program that will calculate your course average.  Words underlined and highlighted in blue represent concepts and terminology that will be important for Exam 1 on Wednesday. Boxed-in text does not have to be read in lab, but explains the highlighted concepts in context as an exam review.  See pages 3 - 5 for important information related to all future projects (highlighted in red).

 

Directions: 

Part 1: Grade.java – skeleton code (minimal code required to compile the class and methods with the specified method signatures) 

•        Create your Grade class and declare the following instance variables using the private access (or visibility) modifier:

o   exam1, exam2, and finalExam: double values to hold exam grades

o   activityAvg: double that holds the activity average   Read the boxed-in portions o quizAvg: double that holds the quiz average o projectAvg: double that holds the project average     after lab to help study for o studentName: String representing student name  Exam 1!

 

 Instance variables are declared inside of the class, but not inside of a method. Instance variables are available to any instance method in the class. They should be private to avoid violating encapsulation (see class notes / textbook).  When an object is created, space is set aside for the instance variables, and the variables are assigned initial values.

 

An access (or visibility) modifier specifies whether a member of a class can be accessed and modified from outside of the class (public) or inside of the class only (private). The protected visibility modifier will be further discussed in chapter 9 with respect to inheritance.

 

Which of your instance variables are reference types, which are primitive types, and what is the difference between the two? 

 

•        You will now declare three constants representing the three possible exams. Declare the three constants with public visibility.

 

  

 

Constants are fields in a class that contain a value that never changes. Constant names are capitalized and are declared as static (they exist even when an object has not be created and can be accessed using the class name, similar to static methods) and final (the value cannot be changed after it has been assigned). An example of a public constant: Math.PI

Why do public instance variables violate encapsulation while public constants do not? 
 

 

 

•        Now create private constants that represent the weight of each portion of your grade:

  

•        Add skeleton code (or stub) for a constructor to the Grade class that accepts the student name as a parameter.

 

  

 

A constructor is invoked when an object is created using the new operator and is used to initialize fields using parameter values or default values. Constructors have no return type and always have the same name as the class. Think about why a constructor would have public visibility rather than private visibility.

 

The formal parameter nameIn is a variable that has local scope and represents input being sent from the calling method. Because it is a local variable, it can only be accessed from within the constructor and will no longer exist when the end of the constructor is reached (space is no longer reserved in memory; see garbage collection).

 
 

•        Set up method skeleton code (or stubs) for the following methods:

o setLabAverages: no return value; takes two double parameters activityAvgIn and quizAvgIn.  The projectAvg will be set separately below.

  o setProjectAvg: no return; takes a double called projectAvgIn as a parameter.

 

<Do this one on your own

 o setExamScore: no return; takes an int parameter examType and a double examScore.

    o calculateGrade: returns a double representing your grade; no parameters.

                                 o toString: String return and no parameters.

  

 

Compile your program. If this were a project, this is the point where you would submit to the “Skeleton Code (Ungraded)” Web-CAT assignment to check the correctness of your method headers.

Part 2: Grade.java – Completing the constructor and instance methods  

 

Completing the Constructor 

•        The formal parameter String studentNameIn is considered to be a local variable that will not be accessible after the constructor ends.  You need to assign the studentNameIn to the instance variable studentName using an assignment statement within the constructor.  

 

  

 

Examples of declaration, assignment, and initialization. 
 
Declaration: int aNum;
Assignment: aNum = 1;
Declaration and initialization:

int bNum = 10;
 
 
 
 

Completing the Method: toString  

•        Complete the toString method so that it returns a String representation of a Grade object:

 

  

 

•        Test your constructor and toString in the interactions pane:

 ¼MMGrade comp1210Grade = new Grade("Pat"); 

M¼MMcomp1210Grade 

MM«MName: Pat 

 MM©MCourse Grade: 0.0 M¼MM

 

Completing the Set Methods: setLabAverages, setExamScore, setProjectAverage 

•        In your setLabAverages method, add the following code to store your activity and quiz average.

 

  

 

•        In your setProjectAverage, set the value of the instance variable representing project average.

 

  

 

•        Your setExamScore method should use the public constants to decide which of the exam score instance variables to set when the method is called: exam1, exam2, or finalExam.

 

  

Typically, this set method would include a boolean return and would return false if examType was not 1, 2, or 3; however, for brevity in this activity the method return type is void.

 

Completing the Method: calculateGrade  

•        Add code to calculate your grade. The weight constants are not necessarily useful to users of the Grade class, so they are private rather than public.

 

  

 

Note that the grade you are calculating is not an instance variable. In general you should try to keep as many variables local as possible to avoid method dependencies and logic errors. This is especially true for values that are calculated from field values.  It is usually preferable to calculate a value when needed rather than attempt to update it each time any of several fields it depends on is updated.   
 

•        Test your set methods in the interactions pane:

      M¼MMcomp1210Grade.setLabAverages(90, 80);     Project note: 

      M¼MMcomp1210Grade.setProjectAvg(95);           Always use 

M¼MMcomp1210Grade.setExamScore(Grade.EXAM_1, 85); public constants 

M¼MMcomp1210Grade.setExamScore(Grade.EXAM_2, 88); in your driver 

M¼MMcomp1210Grade.setExamScore(Grade.FINAL, 91); programs rather 

       M¼MMcomp1210Grade.calculateGrade()  than literal values whenever 

     MMMM89.5                                         possible.  

 

 

 

 

In the workbench to the top left-hand side of jGRASP, open the myGrade object and make sure that your instance variables have been set correctly.

 

then drag comp1210grade onto the canvas.  Be sure to change the viewer to “Basic” if this is not already selected.  Note the Basic view does not display the static variables/constants but rather only the instance variables.  In the canvas below, comp1210Grade has been dragged onto the canvas a second time.  The first one is set to the Basic viewer and the second one set to the toString() viewer, which displays the toString value to the object.

 

 

 

“Set” methods are mutator methods and modify your object's attributes. “Get” methods are accessor methods that return the specified attribute.

 

A get method should return the value, but should not modify an object's instance data.  
 

 

Project note: Do not depend on the toString method to test your other methods; for example, to test calculateGrade you should invoke calculateGrade directly.  
 

There may be instances in which you want to convert a number from one type to another, which can be done via assignment conversion, promotion, or casting.  All of the numbers were doubles in the above example, but conversion may be needed in when an expression contains integers and integer division is unwanted.

 

Question: Suppose that aNum is an integer.  Modify the following code 3 different times to using assignment conversion, promotion, and casting to produce a correct output (assume that integer division is not desired). 

 

int aNumTimes2 = aNum * 2; double result = aNumTimes2 / 3;
 

 

** Important project note: to score full points on the projects, you should use constants in your code rather than just the value itself (this avoids the use of “magic numbers” and is considered good programming practice).  Constant identifiers should always be capitalized.  For example, if you had an integer of value 1 that is used represent the color red, you should not use the value 1 throughout your code.  Instead, you should have a constant named RED set to the value of 1 and refer to the constant throughout your code. 

•        Constants should be public if they are useful outside of the class (for example, to allow a user to change an exam category without having to memorize that regular exam is 1 or 2 and the final = 3). 

•        Constants should be private if they are only useful inside of the class (for example, in a bowling class the total number of pins is 10; the user would not have to necessarily know or use this value, but the class itself would have to use it to calculate the score). 

 

 

Part 3: Driver Program 

 

•        Download GradeGenerator.java and GradeGenerator.jgrasp_canvas.xml from the Files page in Canvas (Files Lab Activities Activity_04B_files.zip) and save them in the same folder as your Grade.java.   

 

•        Complete the main method in GradeGenerator by invoking the methods required set the fields for grades in the Grade object that is created assigned to the variable comp1210Grade.

 

When setting the scores for exam1, exam1 and finalExam, do NOT use literal values to set the exam grades.  Instead use the appropriate constants.

Bad:  comp1210Grade.setExamScore(1, exam1Score); 

Good: comp1210Grade.setExamScore(Grade.EXAM_1, exam1Score); 

 

After completing the main method GradeGenerator.java, compile and run the program to test it.

More products