0% found this document useful (0 votes)
5 views2 pages

Lab3 25 v2

The document outlines a lab assignment focused on refactoring a procedural programming approach to an object-oriented design for calculating student grades. It instructs the creation of a `Student` class with methods for calculating averages and determining letter grades, as well as static members to track the top student in math. Additionally, it includes tasks to test the functionality of the class and its methods in a `Main` class context.

Uploaded by

saeednabawy950
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

Lab3 25 v2

The document outlines a lab assignment focused on refactoring a procedural programming approach to an object-oriented design for calculating student grades. It instructs the creation of a `Student` class with methods for calculating averages and determining letter grades, as well as static members to track the top student in math. Additionally, it includes tasks to test the functionality of the class and its methods in a `Main` class context.

Uploaded by

saeednabawy950
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Lab 3

1. The provided code uses static methods to calculate a student's average grade and
determine their letter grade (procedural programming). Your task is to refactor this
implementation into an object-oriented approach.

static double calculateAverage(int[] grades) {


int sum = 0;
for (int grade : grades) {
sum += grade;
}
return (double) sum / grades.length;
}

static char determineGrade(double average) {


if (average >= 90) return 'A';
else if (average >= 80) return 'B';
else if (average >= 70) return 'C';
else if (average >= 60) return 'D';
else return 'F';
}

Instructions:
a) Create a `Student` class that:
- Stores the student's name and grades
-Has constructor Student(String name, int[] grades)
- Has a method `calculateAverage()` to compute the average grade.
- Has a method `determineGrade()` to determine the letter grade.
- Has a method `getName()` to retrieve the student's name.

b) In the `Main` class, create 3 `Student` objects and use it to:


- Calculate and print the student's average grade.
- Determine and print the student's letter grade.
2. Now that we have converted the procedural approach to an object-oriented design,
we will explore when/how to use static members correctly.

Extend the `Student` class by:

a) Adding a static variable `TopStudentMath` to store the highest grade in the first
subject (assumed to be math) among all students.
b) Adding a static variable `TopStudentName` to store the name of the student
with the highest grade in the first subject.
c) Implementing a static method `getTopStudentName()` to return the name of the
student with the highest grade in the first subject.
d) Updating the constructor to check if the new student has a higher grade than
`TopStudentMath`, and update `TopStudentName` accordingly.
e) Try the following statements in the method getTopStudentName():
a. System.out.println(name); // Does it work, comment!
b. System.out.println(getName()); // Does it work, comment!
c. System.out.println(topStudentName); // Does it work, comment!
f) Try the following statements in the method determineGrade():
a. System.out.println(name); // Does it work, comment!
b. System.out.println(getName()); // Does it work, comment!
c. System.out.println(TopStudentName); // Does it work, comment!

Try the following code statements in the main method that uses the student class

a. Student s = new Student("Alice", new int[]{90, 95, 85});


b. System.out.println(s.getTopStudentName()); // Does it work, comment!
c. System.out.println(Student.getTopStudentName()); // Does it work,
comment!
d. System.out.println(s.getName()); // Does it work, comment!
e. System.out.println(Student.getName()); // Does it work, comment!

You might also like