I have this so far for an assignment that needs to create a gradStudent class th
ID: 3852191 • Letter: I
Question
I have this so far for an assignment that needs to create a gradStudent class that inherits from a student class. But i need to also have the Degree Name, their GPA and also their Masters Program name. But Im stuck on having to show the GPA. I only get the grade. I might have put the wrong equation. I just want to make sure its right.
class Stud
{
protected string fName, lName;
protected float ex1, ex2, ex3;
protected float avg;
public Stud()
{
fName = lName = "";
ex1 = ex2 = ex3 = avg = 0;
}
public Stud(string fName, string ln, float e1, float e2, float e3)
{
this.fName = fName;
this.lName = ln;
ex1 = e1;
ex2 = e2;
ex3 = e3;
}
public void calAvg()
{
float tmp = ex1 + ex2 + ex3;
this.avg = tmp / 3.0f;
}
public virtual void disp()
{
Console.WriteLine(fName + " " + lName + " " + avg);
}
}
class GradStud : Stud
{
string bsName, prg;
float bsAvg;
public GradStud(string fName, string ln, float e1, float e2, float e3, string bn, string p, float ba) : base(fName, ln, e1, e2, e3)
{
bsName = bn;
prg = p;
bsAvg = ba;
}
public override void disp()
{
base.disp();
Console.WriteLine(bsName + " " + prg);
}
}
static void Main(string[] args)
{
Stud s1 = new Stud("Jane", "Doe", 100f, 95f, 75f);
s1.calAvg();
s1.disp();
GradStud ds2 = new GradStud("Joe", "Smith", 45, 85, 95, "Computer Engineering,", "Computer Science", 3.50f);
ds2.calAvg();
ds2.disp();
Stud[] list = { new Stud("George", "Hall", 100f, 95f, 75f),
new GradStud("Julia", "Hernadez", 45, 85, 95, "Psychology,", "Biology", 3.50f) };
Console.WriteLine(" ");
foreach (var s in list)
{
s.calAvg();
s.disp();
}
Console.ReadKey();
}
}
}
Explanation / Answer
Error: Put main method in class
using System;
class Stud
{
protected string fName, lName;
protected float ex1, ex2, ex3;
protected float avg;
public Stud()
{
fName = lName = "";
ex1 = ex2 = ex3 = avg = 0;
}
public Stud(string fName, string ln, float e1, float e2, float e3)
{
this.fName = fName;
this.lName = ln;
ex1 = e1;
ex2 = e2;
ex3 = e3;
}
public void calAvg()
{
float tmp = ex1 + ex2 + ex3;
this.avg = tmp / 3.0f;
}
public virtual void disp()
{
Console.WriteLine(fName + " " + lName + " " + avg);
}
}
class GradStud : Stud
{
string bsName, prg;
float bsAvg;
public GradStud(string fName, string ln, float e1, float e2, float e3, string bn, string p, float ba) : base(fName, ln, e1, e2, e3)
{
bsName = bn;
prg = p;
bsAvg = ba;
}
public override void disp()
{
base.disp();
Console.WriteLine(bsName + " " + prg);
}
}
public class Test
{
static void Main(string[] args)
{
Stud s1 = new Stud("Jane", "Doe", 100f, 95f, 75f);
s1.calAvg();
s1.disp();
GradStud ds2 = new GradStud("Joe", "Smith", 45, 85, 95, "Computer Engineering,", "Computer Science", 3.50f);
ds2.calAvg();
ds2.disp();
Stud[] list = { new Stud("George", "Hall", 100f, 95f, 75f),new GradStud("Julia", "Hernadez", 45, 85, 95, "Psychology,", "Biology", 3.50f) };
Console.WriteLine(" ");
foreach (var s in list)
{
s.calAvg();
s.disp();
}
Console.ReadKey();
}
}
Output: