write a program which uses a class with the name Student, the class will have as data


- name
- AM
- grade1
- grade2
- grade3


all the data of the class have to be private

the program will ask the elements of two students, it will save them as objects and will show the final average of the two averages of the two students

how i will make my objects private? what changes i have to do? Also, the count of the averages i want to be in the class Student.... help me please

using System;
class Student
{
   
    public string name;
    public int AM;
    public double grade1;
    public double grade2;
    public double grade3;


}
class Program
{
    static void Main()
    {
        Student st1= new Student();
        Student st2= new Student();
        Console.WriteLine("give the elements of the firts student:");
        Console.WriteLine("Name:");
        st1.name= Console.ReadLine();
        Console.Write("AM:T0");
        st1.AM = Int32.Parse(Console.ReadLine());
        Console.WriteLine("first grade:");
        st1.grade1=Double.Parse(Console.ReadLine());
        Console.WriteLine("second grade:");
        st1.grade2=Double.Parse(Console.ReadLine());
        Console.WriteLine(" third grade:");
        st1.grade3=Double.Parse(Console.ReadLine());
        
        Console.WriteLine("give the elements of the second student:");
        Console.WriteLine("name:");
        st2.name= Console.ReadLine();
        Console.Write("AM:T0");
        st2.AM = Int32.Parse(Console.ReadLine());
        Console.WriteLine("first grade:");
        st2.grade1=Double.Parse(Console.ReadLine());
        Console.WriteLine("second grade:");
        st2.grade2=Double.Parse(Console.ReadLine());
        Console.WriteLine(" third grade:");
        st2.grade3=Double.Parse(Console.ReadLine());

double AV1,AV2,sum;

AV1= (grade1+grade2+grade3)/3;
AV2=(grade1+grade2+grade3)/3;
sum=(AV1+AV2)/2;
       
        Console.WriteLine("the average is {0}",sum);
        

       
        Console.ReadKey();
    }
}

Where do you initialite a new instance of a class Student? NoWhere.
For to do it so, you have to create a local variable of a class:

Student st1 = new Student(); //s1 is now a reference of a class.

for beginining...

Lines 5-9: You say they have to be private, but you declare them public.

so you mean i have to use a constructor?

@Momerath

yeah, i don't know how to do it private....
i have to use something to read the elements? or what?

another thing regarding those 5-9 lines:
Create properties with get,set accessors!
And you can set get as private:

public string name {private get; set; }

so you mean i have to use a constructor?

Constructor is nothing more then an ussual method, which has this privilegue, that it called as the 1st method after class initialization! So the answer is No!

ok, i corrected it....

So? we are willing to teach you how to do it correctly. Any progress?

well, yeah but i'm still confused with the get/set....

tell me if this it's ok

using System;

class Student

{

 
private string name;

private int AM;
private double grade1;
private double grade2;

private double grade3;

public string name{

get{
return name;
}

set{

what?

}

}

Check this out:

class Program
    {
        static void Main(string[] args)
        {
            Grade st1 = new Grade();
            List<Grade> gradesList = new List<Grade>();

            Console.WriteLine("Give the elements of the firts student:");

            Console.Write("Name: ");
            st1.Name = Console.ReadLine();

            Console.Write("AM:T0 ");
            st1.AM = Int32.Parse(Console.ReadLine());

            for (int i = 1; i <= 3; i++)
            {
                Console.Write(i + GetEnumerations(i) + " grade: ");
                Grade g = new Grade();
                g.Grades = double.Parse(Console.ReadLine());
                gradesList.Add(g);
            }

            //calculate and write out all the grades:
            Console.WriteLine("-----------------");
            Console.WriteLine("For the grades of {0}:", st1.Name);
            foreach (Grade g in gradesList)
                Console.WriteLine(g.Grades);
            Console.WriteLine("The average degree is: {0}.", Math.Round(gradesList.Sum(a => a.Grades) / gradesList.Count, 2));
            Console.ReadLine();
        }

        private static string GetEnumerations(int value)
        {
            string InWord = null;
            switch (value)
            { 
                case 1:
                    InWord = "st";
                    break;
                case 2:
                    InWord = "nd";
                    break;
                case 3:
                    InWord = "rd";
                    break;
                default:
                    InWord = "th";
                    break;
            }
            return InWord;
        }
    }

    class Student
    {
        public string Name { get; set; }
        public int AM { private get; set; }
    }

    class Grade : Student
    {
        public double Grades { get; set; }
    }

What do you think?
But thr properties are not private, otherwise you cannot access to them (you can write them, but not read - which Iam doing on the end of the code, where Iam writing outputs.

Mitja

the code i gave you it's for my school...
a few things that you wrote to your code, our teacher didn't teach us... so, he will understand that i didn't do it on my own. I want something more simple....

the code i posted at my last post is wrong?
what i have to write in the set...

Ok this should do it. And I understand what the teacher can suspect that this is not your code. :)

class Program
    {
        static void Main(string[] args)
        {
            Student st1 = new Student();
            Student st2 = new Student();

            Console.WriteLine("give the elements of the firts student:");
            Console.WriteLine("Name:");
            st1.Name = Console.ReadLine();
            Console.Write("AM:T0");
            st1.AM1 = Int32.Parse(Console.ReadLine());
            Console.WriteLine("first grade:");
            st1.Grade1 = Double.Parse(Console.ReadLine());
            Console.WriteLine("second grade:");
            st1.Grade2 = Double.Parse(Console.ReadLine());
            Console.WriteLine("third grade:");
            st1.Grade3 = Double.Parse(Console.ReadLine());

            Console.WriteLine("give the elements of the second student:");
            Console.WriteLine("name:");
            st2.Name = Console.ReadLine();
            Console.Write("AM:T0");
            st2.AM1 = Int32.Parse(Console.ReadLine());
            Console.WriteLine("first grade:");
            st2.Grade1 = Double.Parse(Console.ReadLine());
            Console.WriteLine("second grade:");
            st2.Grade2 = Double.Parse(Console.ReadLine());
            Console.WriteLine(" third grade:");
            st2.Grade3 = Double.Parse(Console.ReadLine());

            double AV1, AV2, sum;

            AV1 = (st1.Grade1 + st1.Grade2 + st1.Grade3) / 3;
            AV2 = (st2.Grade1 + st2.Grade2 + st2.Grade3) / 3;
            sum = (AV1 + AV2) / 2;

            Console.WriteLine("------------------");
            Console.WriteLine("The 1.st student {0} has an average of {1}.", st1.Name, Math.Round(AV1, 2));
            Console.WriteLine("The 2.nd student {0} has an average of {1}.", st2.Name, Math.Round(AV2, 2));
            Console.WriteLine("The both together have the average of {0}", Math.Round(sum, 2));
            Console.ReadLine();
        }
    }

    class Student
    {
        private string _Name;
        private int AM;
        private double grade1;
        private double grade2;
        private double grade3;

        public string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }

        public int AM1
        {
            get { return AM; }
            set { AM = value; }
        }

        public double Grade1
        {
            get { return grade1; }
            set { grade1 = value; }
        }

        public double Grade2
        {
            get { return grade2; }
            set { grade2 = value; }
        }

        public double Grade3
        {
            get { return grade3; }
            set { grade3 = value; }
        }
    }

Mitja.
(This is not either) :)

thank you so much!!! i really appreciate it!

No problem. Just mark the thread as answered (or vote as helpful) - to close the thread,
bye
Mitja

hello again,

i made some changes to my project, mu program runs but it doesn't read the elements, it only prints the WriteLines....

any help?

using System;
class Student
{

    private string name;
    private int AM;
    private double Grade1;
    private double Grade2;
    private double Grade3;


    public void SetN(string name)
    {
        this.name= name;
    }

    public string GetN()
    {

        return name;
    }

    public void Setam(int AM)
    {
        this.AM = AM;
    }

    public int GetAM()
    {

        return AM;
    }

    public void Setgr1(double Grade1)
    {

        this.Grade1= Grade1;
    }

    public double GetGr11()
    {

        return Grade1;
    }

    public void Setgr2(double Grade2)
    {

        this.Grade2 = Grade2;
    }

    public double GetGr2()
    {

        return Grade2;
    }

    public void Setgr3(double Grade3)
    {
        this.Grade3= Grade3;
    }

    public double GetGr3()
    {

        return Grade3;
    }
}

    class Program
    {
        static void Main()
        {
            Student st1= new Student();
            Student st2= new Student();
            Console.WriteLine("give the elements of the first student:");

            Console.WriteLine("Name:");
            st1.GetN();
          
            Console.Write("AM:T0");
            st1.GetAM();
           
            Console.WriteLine("first grade:");
            st1.GetGr1();
            
            Console.WriteLine("second grade:");
            st1.GetGr2 ();
           
            Console.WriteLine(" third grade:");
            st1.GetGr3();
           
            Console.WriteLine("give the elements of the second student:");
            Console.WriteLine("Name:");
            st2.GetN();
         
            Console.Write("AM:T0");
             st2.GetAM();
               
            Console.WriteLine("first grade:");
            st2.GetGr1 ();
          
            Console.WriteLine("second grade:");
            st2.GetGr2();
            
           Console.WriteLine(" third grade:");
            st2.GetGr3 ();
          

            double AV1, AV2, sum;


            AV1= (st1.GetGr1() + st1.GetGr2() + st1.GetGr3()) / 3;
            AV2= (st2.GetGr1() + st2.GetGr2() + st2.GetGr3()) / 3;
            sum = (AV1+ AV2) / 2;

            Console.WriteLine("the average of the first student  {0} is{1}", st1.GetN(), AV1);
            Console.WriteLine("the average of the second student {0} is {1}", st2.GetN(), AV2);
            Console.WriteLine("the sum average is {0}", sum);



            Console.ReadKey();
        }
    }
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.