In: Computer Science
Hi, I am running C# in Vis. Studio 2019 community. Trying to get my program to populate the username in the program after entered. I can enter a name and the three scores and average them as the program needs but the name is not adding next to the "Students name: " in the program. Any help would be appreciated and please place a note for what I am doing wrong. Thank you
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DoprBox08
{
class CollegeStudent
{
public static void Main(string[] args)
{
Console.Title = ("College Student ");
Console.WriteLine("Please enter your name: \nand the three scores:
");
string name = Console.ReadLine();
int midTerm1 = int.Parse(Console.ReadLine());
int midTerm2 = int.Parse(Console.ReadLine());
int finalExam = int.Parse(Console.ReadLine());
CollegeStudent s = new CollegeStudent(name, midTerm1, midTerm2,
finalExam); //Invoking the constructor
Console.WriteLine(s); //invokes ToString()
Console.ReadKey();
}
public override string ToString()
{
string str;
str = string.Format(" Student name: {0} \nSemester grade: {1}",
StudentName, SemesterGrade());
Console.WriteLine();
return str;
}
private string studentName;
private int midTerm1;
private int midTerm2;
private int finalExam;
public string StudenName
{
get { return studentName; }
set { studentName = value; }
}
public int MidTerm1
{
get { return midTerm1; }
set { midTerm1 = value; }
}
public int MidTerm2
{
get { return midTerm2; }
set { midTerm1 = value; }
}
public int FinalExam
{
get { return finalExam; }
set { finalExam = value; }
}
public object StudentName { get; private set; }
public CollegeStudent(string studentName, int midTerm1, int
midTerm2, int finalExam)
{
this.studentName = studentName;
this.midTerm1 = midTerm1;
this.midTerm2 = midTerm2;
this.finalExam = finalExam;
}
public double SemesterGrade()
{
double grade;
grade = 0.3 * MidTerm1 + 0.3 * MidTerm2 + 0.4 * FinalExam;
return grade;
}
}
}
There was a spell mistake in the property StudentName. Below is the incorrect and correct property name.
Incorrect
public string StudenName
{
get { return studentName; }
set { studentName = value; }
}
Correct
public string StudentName
{
get { return studentName; }
set { studentName = value; }
}
Also there is aaditional public object StudentName { get; private set; } ----This line is not required once you fix the above mentioned problem. You can comment the this line.
Below is the corrected code. Added comments wherever required
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DoprBox08
{
class CollegeStudent
{
public static void Main(string[] args)
{
Console.Title = ("College Student ");
Console.WriteLine("Please enter your name: \nand the three scores: ");
string name = Console.ReadLine();
int midTerm1 = int.Parse(Console.ReadLine());
int midTerm2 = int.Parse(Console.ReadLine());
int finalExam = int.Parse(Console.ReadLine());
CollegeStudent s = new CollegeStudent(name, midTerm1, midTerm2, finalExam); //Invoking the constructor
Console.WriteLine(s); //invokes ToString()
Console.ReadKey();
}
public override string ToString()
{
string str;
str = string.Format(" Student name: {0} \nSemester grade: {1}", StudentName, SemesterGrade());
Console.WriteLine();
return str;
}
private string studentName;
private int midTerm1;
private int midTerm2;
private int finalExam;
//This lin is problem. Spelling mistake - StudenName. It shoould be StudentName
public string StudentName
{
get { return studentName; }
set { studentName = value; }
}
public int MidTerm1
{
get { return midTerm1; }
set { midTerm1 = value; }
}
public int MidTerm2
{
get { return midTerm2; }
set { midTerm1 = value; }
}
public int FinalExam
{
get { return finalExam; }
set { finalExam = value; }
}
//This line of code should not be there. We have corrected the above property where Spelling mistake - StudenName. It shoould be StudentName
//Commenting below line resolved the issue. Now Name is showing in the result
//public object StudentName { get; private set; }
public CollegeStudent(string studentName, int midTerm1, int midTerm2, int finalExam)
{
this.studentName = studentName;
this.midTerm1 = midTerm1;
this.midTerm2 = midTerm2;
this.finalExam = finalExam;
}
public double SemesterGrade()
{
double grade;
grade = 0.3 * MidTerm1 + 0.3 * MidTerm2 + 0.4 * FinalExam;
return grade;
}
}
}
After correcting code as mentioned above. Below is the screenshot of output