Q:

GPA calculation program using c#.net CIT420 project

0

A C# code was implemented to calculate a student’s GPA. The requirements were considered before writing the code are:

  • Module credit can not be negative.
  • The maximum module credit is 10.
  • The total number of credits a student can take is 18.
  • The maximum number of modules a student can take is 10.
  • The program must ask a user if they want to calculate another GPA and only accept ”n” or ”y” (”n” for no and ”y” for yes).
  • The maximum module grade is 100.
  • The minimum module grade is 0.
  • The program must convert the module grade to point based on Table 1.
  • The total input module credits should be at least 1

 

Table 1: The equivalent point for each grade

You have to test the provided code and check whether it meets the above requirements or not.

 

Submission instructions

2.1 Run files

When you run the code in the GPA.cs file (it can be downloaded from the Blackboard), the input you entered will be saved to a file called ”run No.”, for example, run 1. The number in the file name will be increased in each run.

2.2 Test cases template

You have to submit the input that you used to test each requirement and explain the expected output and what was the actual output.

2.2.1 Test case example

Consider the following requirement:

• The program must not accept the same module more than once.

We may use the following input to test if the code will accept the same module name again or not.

 

2
CIT420
4
90
CIT420
4
80
n

And the output of the code was:

Module name Module credit Module point
CIT420     4    19
CIT420     4    16

GPA: 4.375

The code saved the input and the output to the file called run 6.sh and output 6.sh, respectively. It is clear from the input and the output that the code does not meet this requirement and accepts the same module name twice. We can report this test case using the following template. 

Requirement  Test input Test actual output Test expected output
The program must not accept the same module more than once. run 6.sh  output 6.sh We expected an error message when the same module name uses again. 

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

note: this solution is not complete 100%, there are som mistakes in it

GPATest.cs

using System;
using System.IO;
using System.Collections.Generic;
namespace CIT420_project
{
	class TestCase {
		module [] modules;
		int module_num;
		char con;
		public void setmodules(module [] modules){ 
			
			//this.modules=modules;
			this.modules = (module[])modules.Clone();
		}
		public void setmodule_num(int module_num){ 
			this.module_num=module_num;
		}
		public void setcon(char con){ 
			this.con=con;
		}
		public module [] getmodules(){
			return modules;
		}
		public int getmodule_num(){
			return module_num;
		}
		public char getcon(){
			return con;
		}
		}
	class module {
		 String module_name;
		 int module_credit;
		 int module_grade;
		 double module_point;
		
		public void setmodule_name(String module_name){ 
			this.module_name=module_name;
		}
		public void setmodule_credit(int module_credit){  
			this.module_credit=module_credit; 
		}
		public void setmodule_grade(int module_grade){
			this.module_grade=module_grade;
		}
		public void setmodule_point(double module_point){
			this.module_point=module_point;
		}
		public String getmodule_name(){
			return module_name;
		}
		public int getmodule_credit(){
			return  module_credit;
		}
		public int getmodule_grade(){
			return  module_grade;
		}
		public double getmodule_point(){
			return  module_point;
		}
		
	}
    class Program
    {
	static int line=-1;
        static void Main(string[] args)
        {
	var d = new DirectoryInfo(@"runs");
	List<TestCase> TestCase_list = new List<TestCase>();
	foreach(FileInfo fi in d.GetFiles("run_*")){
	Console.WriteLine(fi.FullName);
	line=-1;
		String run_file=fi.FullName;
		
		
		while(true){
		Console.WriteLine("Enter number of modules:");
		int modules_num=enter_int(enter_val_cmd(run_file),run_file);
		module [] modules= new module[modules_num]; 
		for (int i=0 ;i < modules_num;++i){
		
			modules[i]=new module(); 
			Console.WriteLine("Module "+(i+1));
			Console.WriteLine("Module name: ");
			modules[i].setmodule_name(enter_val_cmd(run_file));
			Console.WriteLine("Module credit: ");
			modules[i].setmodule_credit(enter_int(enter_val_cmd(run_file),run_file));
			Console.WriteLine("Module grade: ");
			modules[i].setmodule_grade(enter_int(enter_val_cmd(run_file),run_file));
			modules[i].setmodule_point(grade_to_point(modules[i].getmodule_grade())*modules[i].getmodule_credit());
			
		}
		
		save_output(run_file,modules,modules_num);
		
		Console.WriteLine("Do you want to calculate another GPA? (y or n)");
		char con= enter_val_cmd(run_file)[0];
		// Do not move after the if as this will cause the test case not be saved because of break
		TestCase testcase= new TestCase();
		testcase.setmodules(modules);
		testcase.setmodule_num(modules_num);
		testcase.setcon(con);
		TestCase_list.Add(testcase);
		
		if(con=='n'){
			break;
		}
		
		
		}
		}
		Marking(TestCase_list);
        }
	static double grade_to_point(int grade){
	if(grade >= 95 && grade <=100){ 
		return 5;
	}
	if(grade >= 90 && grade < 95){
		return 4.75;
	}
	if(grade >= 85 && grade < 90){
		return 4.5;
	}
	if(grade >= 80 && grade < 85){
		return 4;
	}
	if(grade > 75 && grade < 80){
		return 3.5;
	}
	if(grade >= 70 && grade < 75){
		return 3;
	}
	if(grade >= 65 && grade < 70){
		return 2.5;
	}
	if(grade >= 60 && grade < 65){
		return 2;
	}
	else{
		return 1;
	}
	}
	static double GPA(module [] modules){
		double total_credits=0;
		double total_points=0;
		foreach(module m in modules){
		total_credits+=m.getmodule_credit();
		total_points+=m.getmodule_point();
		}
		return (total_points/total_credits); 
	}
	
	
	
	
	
	// The following methods for the project marking. Please ignore it.
	static int enter_int(String input, String run_file){
		int result;
		while(int.TryParse(input, out result)==false){
			Console.WriteLine("The value you entered can not be parsed as an integer value. Please enter another value.");
		input=enter_val_cmd(run_file);
		}
		return result;
	}
	
	static String enter_val_cmd(String run_file){
		var fileLines = System.IO.File.ReadAllLines(run_file);
		line++;
		return fileLines[line];
	}
	
	static void save_output(String run_file,module [] modules, int modules_num){
		String output_file=run_file.Replace("run_", "output_");
		Console.WriteLine("Module name\tModule credit\tModule point\n");
		String output="\nModule name\tModule credit\tModule point\n";
		for (int i=0 ; i < modules_num;++i){
			Console.WriteLine(modules[i].getmodule_name()+"\t"+modules[i].getmodule_credit()+"\t"+modules[i].getmodule_point());
			output+=modules[i].getmodule_name()+"\t"+modules[i].getmodule_credit()+"\t"+modules[i].getmodule_point()+"\n";
		}
		Console.WriteLine("GPA:"+GPA(modules));
		output+="\nGPA:"+GPA(modules);
		using(var sw = new StreamWriter(output_file, true))
    	{
        sw.WriteLine(output);
    	}
		
	}
	//https://stackoverflow.com/questions/1078003/how-would-you-make-a-unique-filename-by-adding-a-number
private static string numberPattern = "{0}";
public static string NextAvailableFilename(string path)
{
   
    if (Path.HasExtension(path))
        return GetNextFilename(path.Insert(path.LastIndexOf(Path.GetExtension(path)), numberPattern));

    return GetNextFilename(path + numberPattern);
}

private static string GetNextFilename(string pattern)
{
    string tmp = string.Format(pattern, 1);
    if (tmp == pattern)
        throw new ArgumentException("The pattern must include an index place-holder", "pattern");

    if (!File.Exists(tmp))
        return tmp; 

    int min = 1, max = 2; 

    while (File.Exists(string.Format(pattern, max)))
    {
        min = max;
        max *= 2;
    }

    while (max != min + 1)
    {
        int pivot = (max + min) / 2;
        if (File.Exists(string.Format(pattern, pivot)))
            min = pivot;
        else
            max = pivot;
    }

    return string.Format(pattern, max);
}

static int Marking (List<TestCase> TestCase_list){
	Console.WriteLine("Marking");
	Console.WriteLine("Number of test cases "+TestCase_list.Count);
	IDictionary<int, int> Marks = new Dictionary<int,int>();
	foreach (TestCase testcase in TestCase_list){
			UpdateMarks(Marks,1,requirement1(testcase));
			UpdateMarks(Marks,2,requirement2(testcase));
			UpdateMarks(Marks,3,requirement3(testcase));
			UpdateMarks(Marks,4,requirement4(testcase));
			UpdateMarks(Marks,5,requirement5(testcase));
			UpdateMarks(Marks,6,requirement6(testcase));
			UpdateMarks(Marks,7,requirement7(testcase));
			UpdateMarks(Marks,8,requirement8(testcase));
			UpdateMarks(Marks,9,requirement9(testcase));
		
	}
	int Total_grade=0;
	foreach (KeyValuePair<int,int> kvp in Marks)
	{
    
    Console.WriteLine("Requirement = {0}, Point = {1}", kvp.Key, kvp.Value);
	Total_grade+=kvp.Value;
	}
	Console.WriteLine("Total points "+Total_grade);
	return 0;
}
static void UpdateMarks (IDictionary<int, int> Marks,int key , int grade){
	if((Marks.ContainsKey(key) && grade==0) || !Marks.ContainsKey(key)){
			if(!Marks.ContainsKey(key)&& grade!=0)
				Marks.Add(key,grade);
			if (Marks.ContainsKey(key) && grade!=0)	
			Marks[key]=grade;
		}
		
}
static int requirement1(TestCase testcase){
	foreach( module m in  testcase.getmodules()){
		if(m.getmodule_credit() < 0)
		return 2;
	}
	return 0;
}
static int requirement2(TestCase testcase){
	foreach( module m in  testcase.getmodules()){
		if(m.getmodule_credit() > 10)
		return 2;
	}
	return 0;
}	

static int requirement3(TestCase testcase){
	int total=0;
	foreach( module m in  testcase.getmodules()){
		total+=m.getmodule_credit();
	}
	
	if(total>18)
	return 2;
	else{
		return 0;
	}
}
static int requirement4(TestCase testcase){
	if(testcase.getmodule_num()>10)
	return 2;
	else
	return 0;
}
static int requirement5(TestCase testcase){
	if(testcase.getcon() != 'n' && testcase.getcon() != 'y')
	return 2;
	else
	return 0;
}
static int requirement6(TestCase testcase){
	foreach( module m in  testcase.getmodules()){
		if(m.getmodule_grade() > 100)
		return 2;
	}
	return 0;
}
static int requirement7(TestCase testcase){
	foreach( module m in  testcase.getmodules()){
		if(m.getmodule_grade() < 0)
		return 2;
	}
	return 0;
}
static int requirement8(TestCase testcase){
	foreach( module m in  testcase.getmodules()){
		if(m.getmodule_grade() ==75)
		return 2;
	}
	return 0;
}
static int requirement9(TestCase testcase){
	bool All_modules_credit_zero_or_below=true;
	foreach( module m in  testcase.getmodules()){
		if(m.getmodule_credit() >0)
		All_modules_credit_zero_or_below=false;
	}
	
	if(All_modules_credit_zero_or_below==true)
	return 2;
	else{
		return 0;
	}
}
    }
}

 

GPA.cs

using System;
using System.IO;
namespace CIT420_project
{
	class module {
		 String module_name;
		 int module_credit;
		 int module_grade;
		 double module_point;
		
		public void setmodule_name(String module_name){  
			this.module_name=module_name;
		}
		public void setmodule_credit(int module_credit){  
			this.module_credit=module_credit; 
		}
		public void setmodule_grade(int module_grade){
			this.module_grade=module_grade;
		}
		public void setmodule_point(double module_point){
			this.module_point=module_point;
		}
		public String getmodule_name(){
			return module_name;
		}
		public int getmodule_credit(){
			return  module_credit;
		}
		public int getmodule_grade(){
			return  module_grade;
		}
		public double getmodule_point(){
			return  module_point;
		}
		
	}
    class Program
    {
        static void Main(string[] args)
        {
		String run_file=NextAvailableFilename("run_.sh");
		
		while(true){
		Console.WriteLine("Enter number of modules:");
		int modules_num=enter_int(enter_val_cmd(run_file),run_file);
		module [] modules= new module[modules_num]; 
		for (int i=0 ;i < modules_num;++i){
		
			modules[i]=new module(); 
			Console.WriteLine("Module "+(i+1));
			Console.WriteLine("Module name: ");
			modules[i].setmodule_name(enter_val_cmd(run_file));
			Console.WriteLine("Module credit: ");
			modules[i].setmodule_credit(enter_int(enter_val_cmd(run_file),run_file));
			Console.WriteLine("Module grade: ");
			modules[i].setmodule_grade(enter_int(enter_val_cmd(run_file),run_file));
			modules[i].setmodule_point(grade_to_point(modules[i].getmodule_grade())*modules[i].getmodule_credit());

		}
		
		save_output(run_file,modules,modules_num);
		Console.WriteLine("Do you want to calculate another GPA? (y or n)");
		char con= enter_val_cmd(run_file)[0];
		if(con=='n'){
			break;
		}
		}
        }
	static double grade_to_point(int grade){
	if(grade >= 95 && grade <=100){ 
		return 5;
	}
	if(grade >= 90 && grade < 95){
		return 4.75;
	}
	if(grade >= 85 && grade < 90){
		return 4.5;
	}
	if(grade >= 80 && grade < 85){
		return 4;
	}
	if(grade > 75 && grade < 80){
		return 3.5;
	}
	if(grade >= 70 && grade < 75){
		return 3;
	}
	if(grade >= 65 && grade < 70){
		return 2.5;
	}
	if(grade >= 60 && grade < 65){
		return 2;
	}
	else{
		return 1;
	}
	}
	static double GPA(module [] modules){
		double total_credits=0;
		double total_points=0;
		foreach(module m in modules){
		total_credits+=m.getmodule_credit();
		total_points+=m.getmodule_point();
		}
		return (total_points/total_credits);
	}
	
	
	// The following methods for the project marking. Please ignore it.
	static int enter_int(String input, String run_file){
		int result;
		while(int.TryParse(input, out result)==false){
			Console.WriteLine("The value you entered can not be parsed as an integer value. Please enter another value.");
		input=enter_val_cmd(run_file);
		}
		return result;
	}
	static String enter_val_cmd(String run_file){
		String val=Console.ReadLine();
		
		using(var sw = new StreamWriter(run_file, true))
    	{
        sw.WriteLine(val);
    	}
		return val;
	}
	static void save_output(String run_file,module [] modules, int modules_num){
		String output_file=run_file.Replace("run_", "output_");
		Console.WriteLine("Module name\tModule credit\tModule point\n");
		String output="\nModule name\tModule credit\tModule point\n";
		for (int i=0 ; i < modules_num;++i){
			Console.WriteLine(modules[i].getmodule_name()+"\t"+modules[i].getmodule_credit()+"\t"+modules[i].getmodule_point());
			output+=modules[i].getmodule_name()+"\t"+modules[i].getmodule_credit()+"\t"+modules[i].getmodule_point()+"\n";
		}
		Console.WriteLine("GPA:"+GPA(modules));
		output+="\nGPA:"+GPA(modules);
		using(var sw = new StreamWriter(output_file, true))
    	{
        sw.WriteLine(output);
    	}
		
	}
	//https://stackoverflow.com/questions/1078003/how-would-you-make-a-unique-filename-by-adding-a-number
private static string numberPattern = "{0}";
public static string NextAvailableFilename(string path)
{
   
    if (Path.HasExtension(path))
        return GetNextFilename(path.Insert(path.LastIndexOf(Path.GetExtension(path)), numberPattern));

    return GetNextFilename(path + numberPattern);
}

private static string GetNextFilename(string pattern)
{
    string tmp = string.Format(pattern, 1);
    if (tmp == pattern)
        throw new ArgumentException("The pattern must include an index place-holder", "pattern");

    if (!File.Exists(tmp))
        return tmp; 

    int min = 1, max = 2; 

    while (File.Exists(string.Format(pattern, max)))
    {
        min = max;
        max *= 2;
    }

    while (max != min + 1)
    {
        int pivot = (max + min) / 2;
        if (File.Exists(string.Format(pattern, pivot)))
            min = pivot;
        else
            max = pivot;
    }
    return string.Format(pattern, max);
}
    }
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now