i want to create a program with the grades in a school. we have to find the average of the grades depending on how many courses the student has chosen.

also, we have to find how many courses each studend has passed.

  • the user has to give the number of the students in a class

  • for each student we have to give the number of the courses he has taken

  • for each course, we have to give the grades from a test and from the final exams

my teacher gave me the main class. i have solved a part from the problem but i don't think that it's correct. Need your help

using System;

namespace Lab6Exercise1
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            int N;

             Console.WriteLine ("Number Of Students");
            N=Int32.Parse(Console.ReadLine());

            Students[] M=new Students[N];

            for (int i = 0; i < N; i++)
            {
                Console.WriteLine("Number of courses for the {0}st student:", i + 1);
                int n = Int32.Parse(Console.ReadLine());

                M[i] = new Students(n);

                Console.WriteLine("Name:");
                M[i].SetName(Console.ReadLine());
                Console.WriteLine("Code");
                M[i].SetCode(Console.ReadLine());

                M[i].Grades();

                double average;
                M[i].AVERAGE(out average);


                Console.WriteLine("{0},{1},AVERAGE:{2:.00},Passed_Courses:{3}", M[i].GetCode(), M[i].GetName(), average, M[i].GetNumberPass());
            }
        }
    }
}

using System;

namespace Project1lab6
{
    public class Grade
    {

            //the variables given from the teacher
        double test;
        double final_exams;
        bool pass;





        public void Grades (int n)
        {


            for (int i = 0; i < n; i++)
            {

                do
                {
                    Console.WriteLine("give grade of test");
                    test = Double.Parse(Console.ReadLine());
                } while (test < 0 || test > 10);

                do
                {
                    Console.WriteLine("give grade from final exams");
                   final_exams = Double.Parse(Console.ReadLine());
                } while (final_exams < 0 || final_exams > 10);




            }
        }




        public double AVERAGE(out double average)
        {
            average = test * 0.4 + final_exams * 0.6;



            if (average >= 5.0)
                pass = true;

            return average;


        }
        }
    }

using System;

namespace Project1lab6
{
    public class Students
    {

            //the variables given from  the teacher
        string name;
        string Code;
        Grades[] grades;
        int NumberPass = 0;

             //set&get

        public void SetName(string Name)
        {
            this.name = Name;

        }

        public string GetName()
        {
            return name;
        }

             public void SetCode(string Code)
        {
            this.Code = Code;           

        }

        public string GetCode()
        {
            return Code;
        }

               public void SetNumberPass(bool pass)
        {

            if (pass == true)
                NumberPass++;
        }



        public int GetNumberPass()
        {
            return NumberPass;
        }



        }
    }

Recommended Answers

All 14 Replies

Why dont you tell us exactly what is troubling you. You are the one who needs it to be done, not we.
And you came with a manner, like we have to correct it, so dont expect from any of us to do the whole code instead of you. We are here to help, not to do the code instead of you.

------

One thing to mention, your Students class has no constructor, so you cannot pass any argument to ti, like you did:

  M[i] = new Students(n); //incorrect
  //correct way according to your code is:
  M[i] = new Students();

One more thing, you better needa course class, and a grate.
Because one student has one or more courses, and each course can have one or more grades.
If you want to make it right.
Else tell your teacher she needs some more work to do :)

ok. the main class is right for sure. if there there are mistakes are at the other classes. (Students & Grades).

i didn't use any constructor in my class because i have the Set&gets to give values to my variables. do i have to use always constructors?

also, i need your help with the the arrays... i don't know how to use them.

The getters and setters are only for the variables. If you want to create an object, you always need a constructor.

As with the arrays, what is it you want to achieve? If you are having trouble with understanding the principles of arrays, I suggest you to read through your course material. :)

The getters and setters are only for the variables. If you want to create an object, you always need a constructor.

No, not true. Constructor is just some method (like all others) that are access before any other. Only the static constructor is called before it (but this is not out debate now).

As said, you dont need not get,set accessors, nor constructor(s) of a class. You can simply have fields (like you have) and public (or private) methods. Public can be access from other classes, while private cannot (only from inside this class. There are even protrected members of a class, that can be access from derived classes (from this one).

well, i know that we define an array like this for example

int array[]=new int [20]

and we can use it like this

for(int i=0; i<20; i++)
array[i]= something;

but the array that i used in the problem generates an object, i think. right? so, i don't know how i can put my information in it.

sorry for the bad english. hope you can understand what i'm trying to say

Grades[] grades;

how can i use this?

int[] TheArray = new int[20]; //Declaring TheArray to hold 20 objects

//Adding to TheArray
TheArray[0] = 5; //Arrays always start at 0, so 0-19 in this case.
TheArray[1] = 10;
TheArray[2] = 15;

//Reading from the first entry in TheArray
Console.WriteLine(TheArray[0]);

//Reading from all of TheArray
foreach(int in TheArray)
{
    Console.WriteLine(int);
}

Hope that explains it simply :)

how about this?

Grades[] grades;

That would create an array of 'Grades'

Edit: Completely mis-understood the last bit, its covered in bleedi's post below

No, not true. Constructor is just some method (like all others) that are access before any other. Only the static constructor is called before it (but this is not out debate now).

In the case where you don't explicitly specify a constructor, a default one will be generated, which just instantiates a new object from the class without any parameters or user-defined initialization.

As for the array problem, think of the 'int' example you mentioned as an object. It's not an object, but the object arrays work very similarly.

Grade[] grades = new Grade[20];

for(int i = 0; i < 20; ++i) {
    grades[i] = new Grade();
}

This would generate 20 'Grade' objects. Then you can access them like this:

grades[5].DoSomething();

ok guys. thank you for your help and time. i'll try it more :)

I must say though Freedom, i'm looking at the Grade class and it is so confusing as to what its doing :)

in the grade class i read the grade from the test and the final exams. so, i can find the average

Aah yes i see now, skimmed over a couple of the lines which made it look like it wouldnt work :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.