ms_learn_csharp/012_foreach_array_2/012_csharp.md
2024-07-18 21:50:25 -04:00

11 KiB
Raw Blame History

Challenge project

Develop foreach and if-elseif-else structures to process array data in C#

Demonstrate your ability to update a console app that implements arrays, foreach loops, and if statements in response to a user requested feature update.

Learning objectives

In this module, you'll demonstrate your ability to:

  • Revise a C# console application that uses nested foreach loops to access array data based on an updated feature specification.
  • Update the variables and if statements in a C# console application to create the code branches and calculations required to satisfy an updated feature specification.

Introduction

End users often ask developers to add new features to an application. User requests indicate that your application is being used, and more importantly, that the customer plans to continue using your application. The customer just wants some features updated. The ability to update an existing application based on user requests is very important. A successful update will preserve the integrity of the original application while providing the user with the improved experience that they requested.

Suppose you're a teacher's assistant at a school and that you developed an application to automate the grading process. The application uses arrays to store student names and graded assignments. The application also implements a combination of iteration and selection statements to calculate and report each student's final grade. Your application even differentiates between exam assignments and extra credit assignments when calculating student grades. Although the application does everything that the teacher asked for, you've received a request for new features. The teacher has asked you to update the grading report to show exam and extra credit assignment scores separately from the final numeric score and letter grade.

This module challenges you to add new capabilities to your Student Grading application.

In short, you need to use the teacher's updated grading report specification to:

  • update the iteration and selection code to calculate separate final scores for exams, extra credit assignments, and the overall grade.
  • update the code that writes the grading report to the console.

By the end of this module, you'll have an updated version of the Student Grading application that's able to calculate and report student grades in accordance with the teacher's updated requirements.

Note
This is a challenge project module where youll complete an end-to-end project from a specification. This module is intended to be a test of your skills; theres little guidance and no step-by-step instructions .

Learning Objectives

  • In this module, you'll demonstrate your ability to:

  • Use Visual Studio Code to revise a C# console application based on an updated feature specification.

  • Update the variables and if statements in a C# console application to create the code branches and calculations required to satisfy an updated feature specification.

Prerequisites

  • Experience using Visual Studio Code to develop, build, and run C# console applications that include console I/O and access the methods of .NET classes.
  • Experience using if statements in C# application to evaluate expressions and branch code accordingly.
  • Experience using a foreach loop to access elements of array variables.

Prepare

In this guided project, you'll use Visual Studio Code to develop portions of a C# console application. You'll begin by writing the code that performs various numeric calculations. All calculations must be completed within the existing iteration and selection structures. This Prepare unit provides you with the overall goals of the project and the requirements for your application. The Setup section describes how to set up your development environment, including a "Starter" code project.

Project specification

The Starter code project for this module is a C# console application that implements the following code features:

  • Uses arrays to store student names and assignment scores.

  • Uses a foreach statement to iterate through the student names as an outer program loop.

  • Uses an if statement within the outer loop to identify the current student 's name and access that student's assignment scores.

  • Uses a foreach statement within the outer loop to iterate through the assignment scores array and sum the values.

  • Uses an algorithm within the outer loop to calculate the average exam score for each student.

  • Use an if-elseif-else construct within the outer loop to evaluate the average exam score and assign a letter grade automatically.

  • Integrates extra credit scores when calculating the student's final score and letter grade as follows:

    • Detects extra credit assignments based on the number of elements in the student's scores array.
    • Applies a 10% weighting factor to extra credit assignments before adding extra credit scores to the sum of exam scores.

Your goal in this challenge is to implement the coding updates required to produce the teacher's requested score report.

The current score report lists the student's name followed by the calculated overall grade and letter grade. Here is the existing report format:

Student         Grade   Letter Grade

Sophia          95.6    A
Andrew          91.6    A-
Emma            89.2    B+
Logan           93      A

In addition to the student's final numeric score and letter grade, the teacher wants the updated report to include the exam score and the impact that extra credit work has on the student's final grade. The format of the updated score report should appear as follows:

Student         Exam Score      Overall Grade   Extra Credit

Sophia          92.2            95.88   A       92 (3.68 pts)
Andrew          89.6            91.38   A-      89 (1.78 pts)
Emma            85.6            90.94   A-      89 (5.34 pts)
Logan           91.2            93.12   A       96 (1.92 pts)

Setup

Use the following steps to prepare for the Challenge project exercises:

To download a zip file containing the Starter project code, select the following link: Lab Files.

Unzip the download files.

Note
If a folder named ChallengeProject already exists, you can select Replace the files in the destination to complete the copy operation.

You're now ready to begin the Challenge project exercises. Good luck!


Exercise

Update your formatted output

The Student Grading application is used to calculate and report student grades based on their graded exam and extra credit assignments. Your goal in this challenge is to update the code that generates a score report in accordance with the teacher's updated requirements.

Specification

In this first challenge exercise, you need to instantiate the variables that are required for the updated score report, and then update the Console.WriteLine() statement that writes student grades to the console.

Your updated application must:

  • use the existing arrays and array values for all student grade calculations.
  • use the nested structure provided by the existing foreach and if statements.
  • declare and initialize any other integer variables that are required for calculating sums.
  • declare and initialize any other decimal variables that are required for calculations and/or score report values.

The format of the update score report is:

Student         Exam Score      Overall Grade   Extra Credit

Sophia          0               95.8    A       0 (0 pts)
Andrew          0               91.2    A-      0 (0 pts)
Emma            0               90.4    A-      0 (0 pts)
Logan           0               93      A       0 (0 pts)

Note
Since this exercise does not include updating calculations, the score report will show 0 for the new fields as shown above.

Check your work

To validate that your code satisfies the specified requirements for this exercise, complete the following steps:

  • build and run your app.
  • Verify that your application creates the following output:
Student         Exam Score      Overall Grade   Extra Credit

Sophia          0               95.8    A       0 (0 pts)
Andrew          0               91.2    A-      0 (0 pts)
Emma            0               90.4    A-      0 (0 pts)
Logan           0               93      A       0 (0 pts)

Exercise

Update your calculated values

The Student Grading application is used to calculate and report student grades based on their graded exam and extra credit assignments. Your goal in this challenge is to update the code that calculates student grades in accordance with the teacher's updated requirements.

Specification

In this second challenge exercise, you need to instantiate the variables that are required for the updated score report, complete the required calculations, and then update the Console.WriteLine() statement that writes student grades to the console.

Your updated application must:

  • use the existing arrays and array values for all student grade calculations.
  • use the nested structure provided by the existing foreach and if statements.
  • calculate the sum of exam and extra credit assignment scores using variables from the first exercise or the original code.
  • calculate the average for exam scores and extra credit scores using variables from the first exercise or the original code.
  • calculate the final numeric score as follows: add 10% of the extra credit score sum to the exam score sum, and then divide that value by the number of exams.
  • calculate the extra credit points earned as follows: divide 10% of the extra credit score sum by the number of exams.

Tip
You will need to use (decimal) casting in your equations to preserve the fractional component during your calculations.

The required score report format is:

Student         Exam Score      Overall Grade   Extra Credit

Sophia          92.2            95.88   A       92 (3.68 pts)
Andrew          89.6            91.38   A-      89 (1.78 pts)
Emma            85.6            90.94   A-      89 (5.34 pts)
Logan           91.2            93.12   A       96 (1.92 pts)

Check your work

To validate that your code satisfies the specified requirements, complete the following steps:

  • Build and run your app.

  • Verify that your application creates the following output:

Student         Exam Score      Overall Grade   Extra Credit

Sophia          92.2            95.88   A       92 (3.68 pts)
Andrew          89.6            91.38   A-      89 (1.78 pts)
Emma            85.6            90.94   A-      89 (5.34 pts)
Logan           91.2            93.12   A       96 (1.92 pts)

Congratulations if you succeeded in this challenge!