ms_learn_csharp/006_Calc_final_GPA/006_csharp.md

435 lines
14 KiB
Markdown
Raw Normal View History

2024-07-17 16:32:54 -04:00
# Guided project
## Calculate final GPA
### Introduction
Developers perform some tasks nearly every day. Tasks including declaring
string and numeric variables, assigning and extracting values, and performing
calculations are not only routine, but essential. Equally important is the
task of communicating results to the application user. Mastering the ability
to apply these skills to solve a given problem is something that every
developer must learn to do.
Suppose you're a teacher's assistant at a university. You're tasked with
developing an application that helps calculate students' grade point average.
The application uses the students' grades and credit hours taken to calculate
their overall GPA. You're also provided a required format for reporting the
students' GPA.
This module will guide you through the steps required to develop your GPA
Calculator application. Your code will declare and assign values to variables
based on course information, perform various numeric calculations, and format
and display the results. Calculations include determining the sum of the grade
points earned and total credit hours. To display the results in the required
format, you'll need to manipulate a decimal value to display a total of three
digits. You'll also use `Console.WriteLine()` methods as well character escape
sequences that help to format your results.
By the end of this module, you'll be able to write code that uses various
variable types, performs numeric calculations, and displays formatted data for
the user.
### Learning objectives
In this module, youll practice how to:
- Work with variables to store and retrieve data
- Perform basic math operations
- Format strings to present results
#### Prerequisites
- Beginner level experience with a .NET editor
- Beginner level experience with basic C# syntax rules
- Beginner level experience with performing math operations on variables
- Beginner level experience with creating literal values and declare variables
of basic data types like `string`, `int`, and `decimal`
- Beginner level experience with string concatenation and string interpolation
---
## Prepare for guided project
You'll be using the .NET Editor as your code development environment. You'll
be writing code that uses string and numeric variables, performs calculations,
then formats and displays the results to a console.
### Project overview
You're developing a Student GPA Calculator that will help calculate students'
overall Grade Point Average. The parameters for your application are:
- You're given the student's name and class information.
- Each class has a name, the student's grade, and the number of credit hours
for that class.
- Your application needs to perform basic math operations to calculate the GPA
for the given student.
- Your application needs to output/display the students name, class
information, and GPA.
#### To calculate the GPA:
- Multiply the grade value for a course by the number of credit hours for that
course.
- Do this for each course, then add these results together.
- Divide the resulting sum by the total number of credit hours.
You're provided with the following sample of a student's course information and GPA:
Output
```txt
Student: Sophia Johnson
Course Grade Credit Hours
English 101 4 3
Algebra 101 3 3
Biology 101 3 4
Computer Science I 3 4
Psychology 101 4 3
Final GPA: 3.35
```
### Setup
Use the following steps to prepare for the Guided project exercises:
Open the .NET Editor coding environment.
Copy and paste the following code into the .NET Editor. These values represent
the student's name and course details.
```cs
string studentName = "Sophia Johnson";
string course1Name = "English 101";
string course2Name = "Algebra 101";
string course3Name = "Biology 101";
string course4Name = "Computer Science I";
string course5Name = "Psychology 101";
int course1Credit = 3;
int course2Credit = 3;
int course3Credit = 4;
int course4Credit = 4;
int course5Credit = 3;
```
Now you're ready to begin the Guided project exercises. Good luck!
---
## Exercise - Store the numeric grade values for each course
In this exercise, you'll begin setting up variables needed to calculate a
student's GPA. Let's get started.
> Important
> You need to have completed the Setup instructions in the previous unit,
Prepare, before you begin this Exercise.
### Create variables to store the grade values
In this task, you'll identify the numeric equivalents for the student's earned
letter grade. Then you'll declare variables to store the numeric grade value
for each class. The numeric equivalents are represented as whole numbers, so
you'll be using the Integer data type to store the values.
Ensure that you have the .NET Editor open, and that you have the variables
prepared with the student's name, course names, and credit hours.
In the Prepare unit for this Guided project module, the Setup instructions
have you copy student course information into the editor. If necessary, go
back and complete the Setup instructions.
Review the following letter grade numeric equivalent values A = 4 grade points
B = 3 grade points
Scroll down to the bottom of your code and create a new blank line.
To declare an Integer variable for each numeric grade value, enter the following code:
```cs
int gradeA = 4;
int gradeB = 3;
```
Notice fixed values are used to represent the numeric grades. This technique
helps make your code easy to understand and help to prevent typos if you need
to enter different grades repeatedly. Values for grades C, D, and F are
omitted for now since they're unused.
Review the student's grades for each course:
```txt
Course Grade
English 101 A
Algebra 101 B
Biology 101 B
Computer Science I B
Psychology 101 A
```
You'll be using this information to create variables that will store the
numeric grade values for each course.
To create variables that will store the grades for each course, enter the
following code:
```cs
int course1Grade = gradeA;
int course2Grade = gradeB;
int course3Grade = gradeB;
int course4Grade = gradeB;
int course5Grade = gradeA;
```
To display the course names along with the numeric grade, enter the following code:
```cs
Console.WriteLine($"{course1Name} {course1Grade}");
Console.WriteLine($"{course2Name} {course2Grade}");
Console.WriteLine($"{course3Name} {course3Grade}");
Console.WriteLine($"{course4Name} {course4Grade}");
Console.WriteLine($"{course5Name} {course5Grade}");
```
Your application's output should match the following output:
```cs
English 101 4
Algebra 101 3
Biology 101 3
Computer Science I 3
Psychology 101 4
```
If your output doesn't match, be sure to check your variable names.
Take a moment to consider the current output and the final output of your
application.
In the final output of your application, you want to display the class name,
grade, and credit hours. This is a good time to add the credit hours into your
print statements.
To add the credit hours for each class to the print statements, update your
code as follows:
```cs
Console.WriteLine($"{course1Name} {course1Grade} {course1Credit}");
Console.WriteLine($"{course2Name} {course2Grade} {course2Credit}");
Console.WriteLine($"{course3Name} {course3Grade} {course3Credit}");
Console.WriteLine($"{course4Name} {course4Grade} {course4Credit}");
Console.WriteLine($"{course5Name} {course5Grade} {course5Credit}");
```
### Check your work
In this task, you'll run the code and verify the output is correct.
Review your output and verify that the course names, grades, and credit hours are all correct:
Output
```txt
English 101 4 3
Algebra 101 3 3
Biology 101 3 4
Computer Science I 3 4
Psychology 101 4 3
```
If your code displays different results, you'll need to review your code to
find your error and make updates. Run the code again to see if you've fixed
the problem. Continue updating and running your code until your code produces
the expected results.
---
## Exercise
### Calculate the sums of credit hours and grade points
In this exercise, you'll calculate and store the total number of credit hours
and the total grade points earned for each course. These values will later be
used to calculate the GPA. Since both the credit hours and grade values are
represented as whole numbers, you'll store the sums using the Integer data type.
### Create variables to store the average
Recall that to calculate a student's GPA, you need the total number of credit
hours, and the total number of grade points earned. The grade points earned
for a course is equal to the product of the number of credit hours for that
course and numeric grade value earned. For example:
```txt
Course Credit Credit Hours Grade Points
English 101 4 3 12
```
In this task, you'll create the variables to store values that are needed to
calculate the GPA. You'll create a variable to store the sum of the total
credit hours for each course, and another variable to store the sum of the
grade points the student earned for each course.
In the .NET Editor, locate the `Console.WriteLine()` statements that are used
to display the course information.
Create a blank code line above the `Console.WriteLine()` statements.
On the blank code line that you created, to create a variable that will store
the total number of credit hours, enter the following code:
```cs
int totalCreditHours = 0;
```
Notice that the total is initialized to 0. This initialization allows you to
increment the sum while keeping your code organized.
To increment the sum to represent the total number of credit hours, enter the
following code:
```cs
totalCreditHours += course1Credit;
totalCreditHours += course2Credit;
totalCreditHours += course3Credit;
totalCreditHours += course4Credit;
totalCreditHours += course5Credit;
```
Recall that the `+=` operator is shorthand notation to add a value to a
variable. These lines of code have the same result as adding each `courseCredit`
variable on one line, for example:
```cs
totalCreditHours = course1Credit + course2Credit + course3Credit + course4Credit + course5Credit;
```
To create a variable that will store the total number of grade points earned for each course, enter the following code:
```cs
int totalGradePoints = 0;
```
To increment the sum by the grade points earned for the first course, enter
the following code:
```cs
totalGradePoints += course1Credit * course1Grade;
```
Recall that the grade points earned for a course is equal to the course credit
hours multiplied by the earned grade. In this line of code, you use the
compound assignment operator to add the product of `course1Credit * course1Grade`
to `totalGradePoints`.
To increment the sum by the grade points earned for the remainder of the
courses, enter the following code:
```cs
totalGradePoints += course2Credit * course2Grade;
totalGradePoints += course3Credit * course3Grade;
totalGradePoints += course4Credit * course4Grade;
totalGradePoints += course5Credit * course5Grade;
```
Take a minute to review your code.
Notice that the code you wrote breaks down the problem into manageable pieces
rather than trying to calculate the GPA in one large operation. First, you
initialized and calculated the value of `totalCreditHours`. Then you
initialized and calculated the value of `totalGradePoints`. Afterwards, you'll
use these values in your final calculation.
Now that your code is calculating a value for `totalGradePoints`, let's verify
that your calculations are correct before continuing. It's important to stop
and check your work periodically. Checking your work early in the development
process will make it easier to locate and fix any errors in your code.
To display the values of `totalGradePoints` and `totalCreditHours`, enter the
following code:
```cs
Console.WriteLine($"{totalGradePoints} {totalCreditHours}");
```
You'll remove this `WriteLine()` statement later since it isn't needed in the
final output.
### Check Your Work
In this task, you'll run the code and verify that the output is correct.
Check that your code is similar to the following:
```cs
string studentName = "Sophia Johnson";
string course1Name = "English 101";
string course2Name = "Algebra 101";
string course3Name = "Biology 101";
string course4Name = "Computer Science I";
string course5Name = "Psychology 101";
int course1Credit = 3;
int course2Credit = 3;
int course3Credit = 4;
int course4Credit = 4;
int course5Credit = 3;
int gradeA = 4;
int gradeB = 3;
int course1Grade = gradeA;
int course2Grade = gradeB;
int course3Grade = gradeB;
int course4Grade = gradeB;
int course5Grade = gradeA;
int totalCreditHours = 0;
totalCreditHours += course1Credit;
totalCreditHours += course2Credit;
totalCreditHours += course3Credit;
totalCreditHours += course4Credit;
totalCreditHours += course5Credit;
int totalGradePoints = 0;
totalGradePoints += course1Credit * course1Grade;
totalGradePoints += course2Credit * course2Grade;
totalGradePoints += course3Credit * course3Grade;
totalGradePoints += course4Credit * course4Grade;
totalGradePoints += course5Credit * course5Grade;
Console.WriteLine($"{totalGradePoints} {totalCreditHours}");
Console.WriteLine($"{course1Name} {course1Grade} {course1Credit}");
Console.WriteLine($"{course2Name} {course2Grade} {course2Credit}");
Console.WriteLine($"{course3Name} {course3Grade} {course3Credit}");
Console.WriteLine($"{course4Name} {course4Grade} {course4Credit}");
Console.WriteLine($"{course5Name} {course5Grade} {course5Credit}");
```
To run your code and display the current values of `totalGradePoints` and
`totalCreditHours`, select Run.
Verify that your application's output matches the following output:
Output
```txt
57 17
English 101 4 3
Algebra 101 3 3
Biology 101 3 4
Computer Science I 3 4
Psychology 101 4 3
```
If your code displays different results, you'll need to review your code to
find your error and make updates. Run the code again to see if you've fixed
the problem. Continue updating and running your code until your code produces
the expected results.