C# programming
Assume we have a student final exam score lookup table like this:
John |
40 |
Henry |
0 |
Mary |
59 |
Hillary |
39 |
Pikachu |
100 |
Write a class that helps check if student passes or fails the course. If a student's score is greater than or equal to a parameter passScore, we will consider it a pass. If a student's name is NOT found in the table, we assume they have missed the exam and thus consider failed. The method should be defined as:
public bool CheckPass(string name, int passScore)
So CheckPass("John", 40) returns true while CheckPass("Hillary",40) and CheckPass("William",40) returns false. It should also have the Add method that allows you to add the student and scores:
public void AddScore(string name, int score)
It should also contain a method called GetLowestPassScore, which returns the lowest score among students who have passed the course, given a particular passScore:
public int GetLowestPassScore(int passScore)
Please pay attention to encapsulation concerns. You can start from the template here.
using System;
using System.Collections.Generic;
public class ScoreLookup {
public void AddScore(string name, int score) {
}
public bool CheckPass(string name, int passScore)
{
return false;
}
public int GetLowestPassScore(int passScore) {
return 0;
}
}
public class Program
{
public static void Main()
{
ScoreLookup lookup = new
ScoreLookup();
lookup.AddScore("John Chan",
40);
lookup.AddScore("Henry Wong",
0);
lookup.AddScore("Mary Lee",
59);
lookup.AddScore("Hillary
Clinton",39);
lookup.AddScore("Pikachu",
100);
Console.WriteLine(lookup.CheckPass("John", 40));
Console.WriteLine(lookup.CheckPass("Mary", 60));
Console.WriteLine(lookup.CheckPass("Pikachu", 40));
Console.WriteLine(lookup.CheckPass("William", 40));
Console.WriteLine(lookup.CheckPass("Hillary", 40));
Console.WriteLine(lookup.GetLowestPassScore(50));
}
}
You may find Dictionary's TryGetValue method and Keys property to be useful. Consult previous Lecture Notes if you forgot some advanced Dictionary methods and properties.
Copy and paste the complete program here:
Please find the answer below, all the details are mentioned in the comments. Have added some additional test also in the code.
Program.cs
using System;
using System.Collections.Generic;
public class ScoreLookup {
// Creating a dictionary
// using Dictionary<TKey,TValue> class
private Dictionary<string, int> studentData = new
Dictionary<string, int>();
//method to add the name and score to the Dictionary
public void AddScore(string name, int score) {
studentData.Add(name,score);
}
//method to check student is passed or not
public bool CheckPass(string name, int passScore) {
//check if the key exist and if exist then check the score
if(studentData.ContainsKey(name) && studentData[name] >=
passScore){
return true;
}
else{
return false;
}
}
//method to get the lowest mark form passScore
public int GetLowestPassScore(int passScore) {
//set min_value to highest value
int min_value = 999;
//iterate through each entry in Dictionary and check if the value
is lowest
foreach (KeyValuePair<string,int> item in studentData){
if(item.Value >= passScore && item.Value <
min_value){
min_value = item.Value;
}
}
//return the min_value
return min_value;
}
}
public class Program
{
public static void Main(){
ScoreLookup lookup = new ScoreLookup();
lookup.AddScore("John Chan", 40);
lookup.AddScore("Henry Wong", 0);
lookup.AddScore("Mary Lee", 59);
lookup.AddScore("Hillary Clinton",39);
lookup.AddScore("Pikachu", 100);
Console.WriteLine(lookup.CheckPass("John", 40));
Console.WriteLine(lookup.CheckPass("Mary", 60));
Console.WriteLine(lookup.CheckPass("Pikachu", 40));
Console.WriteLine(lookup.CheckPass("William", 40));
Console.WriteLine(lookup.CheckPass("Hillary", 40));
Console.WriteLine(lookup.GetLowestPassScore(50));
//additonal tests
Console.WriteLine("Additional Tests:");
Console.WriteLine(lookup.CheckPass("John Chan", 40));
Console.WriteLine(lookup.CheckPass("Mary Lee", 60));
Console.WriteLine(lookup.CheckPass("Pikachu", 40));
Console.WriteLine(lookup.CheckPass("Hillary Clinton", 40));
}
}
Output:
Please let us know in the comments if you face any problems.
Get Answers For Free
Most questions answered within 1 hours.