1 Attachment(s)
Please help very basic :)
i have just started my first c# class and have an assignement. what i need to do is build a program that lets a user input 5 test scores then it displays your letter grade based on the average. my teacher hasnt responded in a week so im relying on your help. i seem to have the program accepting and storing the 5 test scores. im not sure if the average is "actually" being calculated. i am sure though that the average is not being applied to the switch statement. when the program runs and returns the grade letter its blank not displaying the default either.
my thought is i just need a simple line in the program to initiate the grade letter test and then store the letter for string grade. something like:
myTest.SetGrade(); // to initiate the test then
Console.WriteLine("Grade: ", myTest.Grade) // to show the letter but thats not happening
i will attach a zip of my current visual studio code if that is of use and i will copy it below.
I cant express enough how grateful i am for your help, i have spend hours online and in my book and am getting no where
using System;
namespace TicketSpace
{
public class Test
{
private int exam1;
private int exam2;
private int exam3;
private int exam4;
private int exam5;
private int average;
private string grade;
public Test()
{
}
public Test(int test1, int test2,int test3,int test4,int test5)
{
exam1 = test1;
exam2 = test2;
exam3 = test3;
exam4 = test4;
exam5 = test5;
}
public void DetermineAverage( )
{
average = (exam1 + exam2 + exam3 + exam4 + exam5) / 5;
}
public string Grade
{
get
{
return grade;
}
}
public void SetGrade(int average)
{
switch (average / 10)
{
case 9: grade = "A";
break;
case 8: grade = "B";
break;
case 7: grade = "C";
break;
case 6: grade = "D";
break;
default: grade = "F";
break;
}
}
}
}
NEXT CLASS
using System;
namespace TicketSpace
{
public class TestApp
{
static void Main( )
{
int exam1,
exam2,
exam3,
exam4,
exam5;
exam1 = InputScore("Exam 1", out exam1);
exam2 = InputScore("Exam 2", out exam2);
exam3 = InputScore("Exam 3", out exam3);
exam4 = InputScore("Exam 4", out exam4);
exam5 = InputScore("Exam 5", out exam5);
Test myTest = new Test(exam1, exam2, exam3, exam4, exam5);
myTest.DetermineAverage();
Console.WriteLine("Grade: ", myTest.Grade); // THIS IS WHERE THE PROGRAM ISNT DOING WHAT IT IS SUPPOSED TO.
Console.ReadLine( );
}
public static int InputScore(string whichScore,
out int s)
{
string inValue;
Console.Write("Enter the {0}: ",
whichScore);
inValue = Console.ReadLine();
s = Convert.ToInt32(inValue);
return s;
}
}
}
Re: Please help very basic :)
It looks like you're not setting the grade value after you get the average of the exams with the call to myTest.DetermineAverage().
Also, there's an error in your format string argument in the call to Console.WriteLine("Grade: ", myTest.Grade); It should be Console.WriteLine("Grade: {0}", myTest.Grade);
Re: Please help very basic :)
Quote:
Originally Posted by
RaleTheBlade
It looks like you're not setting the grade value after you get the average of the exams with the call to myTest.DetermineAverage().
Also, there's an error in your format string argument in the call to Console.WriteLine("Grade: ", myTest.Grade); It should be Console.WriteLine("Grade: {0}", myTest.Grade);
thanks for the reply
to confirm what you mean about setting the value are you saying this....
public string Grade
{
get
{
return grade;
}
}
should be changed to this...???
public string Grade
{
get
{
return grade;
}
set
{
grade = value;
}
}
and are you also saying instead of having:
Console.WriteLine("Grade: ", myTest.Grade)
it should be:
Console.WriteLine("Grade: {0}", myTest.Grade);
just wanna make sure i understand clearly what is needed thanks!
Re: Please help very basic :)
The whole point is for you to try it yourself. Also, why would he steer you in the wrong direction?
Re: Please help very basic :)
Quote:
Originally Posted by
imrbrett
thanks for the reply
to confirm what you mean about setting the value are you saying this....
public string Grade
{
get
{
return grade;
}
}
should be changed to this...???
public string Grade
{
get
{
return grade;
}
set
{
grade = value;
}
}
and are you also saying instead of having:
Console.WriteLine("Grade: ", myTest.Grade)
it should be:
Console.WriteLine("Grade: {0}", myTest.Grade);
just wanna make sure i understand clearly what is needed thanks!
Ehhh... not quite. Just adding a set accessor to the Grade property isn't going to solve your problem :) You still need to determine the grade letter using the average you get from a call to DetermineAverage(). Your SetGrade() method does a good job of converting the average to a letter grade, so stick with that.
Just call the SetGrade() method and pass in the test average after you call DetermineAverage() and you should be set.
As for the Console.Writeline() stuff, yes... just like that :D Also, you may want to check how out to use code tags. Check out this thread: http://www.codeguru.com/forum/showthread.php?t=401115