|
Hi guys,
I am beginner and developing a test program.
this is the description:
Calculate the total mark and final grade:
Labs Mark is an integer from 0 to 10;
Assignmnet1 mark is an integer from 0 to 20
Assignmnet2 mark is an integer from 0 to 20
Final exam Mark is an integer from 0 to 50
Student passes the unit if and only if
• (Labs Mark + Assignment1 Mark + Assignment2 Mark)>=25 and Final exam Mark >=25
There are five grades, which should calculate as below:
• F If the total mark is less than 40;
• MF if (40<=total Mark<50) or (total mark>=50 but student didn’t passed);
• P if (50<= total Mark <60) and student passed;
• C if (60<= total Mark <70) and student passed;
• D if (70<= total Mark <80) and student passed;
• HD if total mark >=80;
this is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Assignmnet2
{
public class ITECH3219Student
{
//definition of Class's Field
private string FirstName;
private string SurName;
private int StudentId;
private int LabsMark;
private int Assignment1;
private int Assignment2;
private int Final;
// public string Grades;//In order to return the Student Grade.
public int TotalMark;
//defination of Class Properties
public string Fr_name
{
get { return FirstName; }
set { FirstName = value; }
}
public string Sr_Name
{
get { return SurName; }
set { SurName = value; }
}
public int St_Id
{
get { return StudentId; }
set { StudentId = value; }
}
public int Lb_Mark
{
get { return LabsMark; }
set
{
if (value > 0 && value <= 10)
{
LabsMark = value;
}
}
}
public int Ass1
{
get { return Assignment1; }
set
{
if (value > 0 && value <= 20)
{
Assignment1 = value;
}
}
}
public int Ass2
{
get { return Assignment2; }
set
{
if (value > 0 && value <= 20)
{
Assignment2 = value;
}
}
}
public int Fnl
{
get { return Final; }
set
{
if (value > 0 && value <= 50)
{
Final = value;
}
}
}
//define Constructor of Class
public ITECH3219Student(string Fr_Name, string Sr_Name, int St_Id, int Lb_Mark, int Ass1, int Ass2, int Fnl)
{
this.FirstName = Fr_Name;
this.SurName = Sr_Name;
this.StudentId = St_Id;
this.LabsMark = Lb_Mark;
this.Assignment1 = Ass1;
this.Assignment2 = Ass2;
this.Final = Fnl;
}
//define the class's methods
public bool Passed()
{
return ((LabsMark + Assignment1 + Assignment2) >= 25 && (Final >= 25));
}
public readonly string Grade()
{
}
public override string ToString()
{
return string.Format("Name:{0}\nSurname:{1}\nID:{2:D6}",
name, surname, studentID);
}
}
}
my problem is I don't know what should I write in this Part:
public readonly string Grade()
{
}
I appreciate if someone help me.
|
|
|
|
|
Use pre tags.
The modifier readonly can not be applied to methods (nor does it make sense, so why is it there anyway?)
What do you want to write there? The code to calculate the grade?
|
|
|
|
|
Firstly, thankx for your reply.
yes, I need the code to calculate the grade.
|
|
|
|
|
At a first glance it doesn't look too hard, what have you done so far?
edit: I have some code here, but I haven't tested it and I may not fully understand your requirements, so be careful and test it.
if (TotalMark >= 80)
return "HD";
if (TotalMark < 40)
return "F";
if ((LabsMarks + Assignment1 + Assignment2) >= 25 && Final >= 25)
{
if (TotalMark < 50)
return "MF";
if (TotalMark < 60)
return "P";
if (TotalMark < 70)
return "C";
if (TotalMark < 80)
return "D";
}
else
{
return "MF";
}
|
|
|
|
|
What were you smoking when you decided to check the scores in that order?
|
|
|
|
|
Nothing. What is wrong about the order?
Suppose TotalMark is 75, the expected result is D
if (TotalMark < 50)
return "MF";
if (TotalMark < 60)
return "P";
if (TotalMark < 70)
return "C";
if (TotalMark < 80)
return "D";
|
|
|
|
|
I didn't say that it wouldn't work, I asked why you went in that order? What made you decide to test the last case in the list first, and then go back to the first case?
By the way, if they passed, then there's no way they scored less than 50, since 50 is the minimum required to pass. So, you'll never get to the return "MF" under if (TotalMark < 50).
|
|
|
|
|
William Winner wrote:
By the way, if they passed, then there's no way they scored less than 50, since 50 is the minimum required to pass. So, you'll never get to the return "MF" under if (TotalMark < 50).
Ok I couldn't know that, there was no description of how TotalMark should be calculated
|
|
|
|
|
Ooh do you mean "why check for < 40 and >= 80 first"?
Because they are the only two cases where you don't need to know whether the student passed, so you can skip that calculation. I know it's pointless to optimize, it's a bit of a reflex (I learned to program in assembly first - I'll blame that)
Not that it's any faster..
|
|
|
|
|
Like the other responder said,
public readonly string Grade() is not the normal way to write a read-only property if that is what you're trying to do.
It should be
public string Grade
{
get
{
}
}
So, now you just need to test the scores to determine the grade...this isn't that complicated. Just go in order of what your prof gave you.
int totalMark = LabsMark + Assignment1 + Assignment2 + Final;
if (totalMark < 40)
{
return "F";
}
else if (!Passed())
{
return "MF";
}
else if (totalMark < 60)
{
return "P";
}
it's true that you don't need the else if's, but it makes the code a lot more readable.
|
|
|
|
|
I have Simulated OPC Server like Kepware,Matrikon and i need to access the OPC Server through C# Code.I have VB Code but I need to do in C#.
|
|
|
|
|
I have a senior developer that consistently checks for null after initializing an object...
<code>
CustomObject tmp = new CustomObject();
if (tmp == null)
{
throw new NullReferenceException();
}
</code>
I personally don't see the need to do this. Can anyone explain if this is a good/bad/old practice?
Thanks in advance.
|
|
|
|
|
Yes, that belongs in coding horrors.
|
|
|
|
|
Makes no sense to me.. there is no way a ctor can ever return null , at worst it throws it an exception
Besides, the whole
if (tmp == null)
{
throw new NullReferenceException();
}
Thing doesn't really make sense. If you're just going to throw a NullReferenceException then you might as well just try to use the object and get that exception automatically
|
|
|
|
|
yes, that is good old non-sense.
you could create a static class and method to do it for you though:
CustomObject tmp = StupidOperations.MakeSureItIsntNull( new CustomObject() );
That improves readability a bit.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|
But then you have a method call... DEFINE a macro instead.
|
|
|
|
|
Nice answer...
Thanks
Md. Marufuzzaman
I will not say I have failed 1000 times; I will say that I have discovered 1000 ways that can cause failure – Thomas Edison.
|
|
|
|
|
Hay... Nice answer, I agreed
Thanks
Md. Marufuzzaman
I will not say I have failed 1000 times; I will say that I have discovered 1000 ways that can cause failure – Thomas Edison.
|
|
|
|
|
The reason for that is maybe he is an old c++ developer.
IN C++ the new and all other allocation functions could
return a null pointer if the function couldn't allocate
the memory.
In C# this is not necessary, because it allocates the memory
block you requested or it throws an OutOfMemoryException.
But it is a common mistake made by the most old c++
developer (including me ) as they started
developing in C#.
Edit:
But throwing a NullReferenceException is very uncommon and cruel (can be misunderstood).
Greetings
Covean
|
|
|
|
|
Yes, his background is in C++
|
|
|
|
|
Just give him a friendly hint (senior developer don't like it if you say the produce senseless code )
that he hasn't to check for null and that those lines never will be executed -> just a waste of time.
Greetings
Covean
|
|
|
|
|
or just ask him to provide a class whose constructor does return null. That will keep him busy...
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|
Hehe, I like this idea...but will probably go with the gentle reminder approach.
|
|
|
|
|
Hi
I've got a pretty basic question. I've got an application that loads up one main form where users can login using face recognition. I want to include a button called "Alternate login". If the user clicks this button, another form should load providing an alternate means of logging in. But the other form should close though, and this new form must load in its place, with the same size etc. In essence, it shouldn't look like a new form is opened at all, it should simply look like only the controls on the form change.
Is there an efficient way to do this without actually hiding and showing controls? Another example would be a wizard interface, where the user clicks next the whole time and then the form stays the same, only the controls change.
|
|
|
|
|
Check out this tip/trick:
Multiple Subsequent Main Forms in C# Winform Apps.aspx[^]
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|