Question

C# programming Assume we have a student final exam score lookup table like this: John 40...

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:

Homework Answers

Answer #1

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.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
3.2 Class Dictionary This class implements a dictionary using a hash table in which collisions are...
3.2 Class Dictionary This class implements a dictionary using a hash table in which collisions are resolved using separate chaining. The hash table will store objects of the class Data. You will decide on the size of the table, keeping in mind that the size of the table must be a prime number. A table of size between 5000-10000, should work well. You must design your hash function so that it produces few collisions. A bad hash function that induces...
using System; public static class Lab5 { public static void Main() { // declare variables int...
using System; public static class Lab5 { public static void Main() { // declare variables int inpMark; string lastName; char grade = ' '; // enter the student's last name Console.Write("Enter the last name of the student => "); lastName = Console.ReadLine(); // enter (and validate) the mark do { Console.Write("Enter a mark between 0 and 100 => "); inpMark = Convert.ToInt32(Console.ReadLine()); } while (inpMark < 0 || inpMark > 100); // Use the method to convert the mark into...
I am making a game like Rock Paper Scissors called fire water stick where the rules...
I am making a game like Rock Paper Scissors called fire water stick where the rules are Stick beats Water by floating on top of it Water beats Fire by putting it out Fire beats Stick by burning it The TODOS are as follows: TODO 1: Declare the instance variables of the class. Instance variables are private variables that keep the state of the game. The recommended instance variables are: 1. 2. 3. 4. 5. 6. A variable, “rand” that...
this is the book name. Data Structures and Abstractions with Java 1) Description: The sample programs...
this is the book name. Data Structures and Abstractions with Java 1) Description: The sample programs in Chapter 1 of your textbook are not complete. They are used for illustration purpose only. The implementation of Listing 1-1 on page 39 is explained in Chapter 2. And, in order to see the result of using it, we will need the following set of files: i. BagInteface.java – the specification only. ii. ArrayBag.java – the implementation of BagInerface.java. iii. ArrayBagDemo.java – a...
please write the code in java so it can run on jGRASP import java.util.Scanner; 2 import...
please write the code in java so it can run on jGRASP import java.util.Scanner; 2 import java.io.*; //This imports input and output (io) classes that we use 3 //to read and write to files. The * is the wildcard that will 4 //make all of the io classes available if I need them 5 //It saves me from having to import each io class separately. 6 /** 7 This program reads numbers from a file, calculates the 8 mean (average)...