-5

i was need to write 2 methods in my student class which do the following

hasPassed() Should return True if the student has a year mark >= 40 or false if the marks is <40

toString() Should return a single string containing a summary of the student details held within the class e.g. “12345 Basil Fawlty, 23/08/1946”

here's the code i have for the above to methods, is what i have correct for what its asking for the above?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CourseWork
{
    public class Student
    {
        private static string firstname;
        private string secondname;
        private string dateofbirth;
        private string course;
        private int matricnumber;
        private double yearmark;

      public bool hasPassed()
        {
            if (yearmark >= 40)
                return true;
            else
                return false;
        }

      public void toString()
      {
          firstname = "Basil";
          secondname = "Fawlty";
          dateofbirth = "23/08/1946";
          course = "MA Hotel Management";
          matricnumber = 12345;
          yearmark = 55;
      }

        public Student()
        {
        }

        public string FirstName
        {
            get { return firstname; }
            set { firstname = value; }
        }

        public string SecondName
        {
            get { return secondname; }
            set { secondname = value; }
        }
        public string DateOfBirth
        {
            get { return dateofbirth; }
            set { dateofbirth = value; }
        }

        public string Course
        {
            get { return course; }
            set { course = value; }
        }
        public int MatricNumber
        {
            get { return matricnumber; }
            set
            {
                if (value <= 99999 && value >= 10000)
                {
                    matricnumber = value;
                }
                else
                {
                    Console.WriteLine("Invalid Matric Number: {0}", value);
                }

              matricnumber = value;
            }
        }
        public double YearMark
        {
            set
            {
                if (value <= 100 && value >= 0)
                {
                    yearmark = value;
                }
                else
                {
                    Console.WriteLine("Invalid Year Mark: {0}", value);
                }
              yearmark = value;
            }

        }
    }

i then need the above methods to be used in a get button that does the following

Get: Uses the values of the Student class methods to update the text boxes. The Student.hasPassed() method should be used to update the pass/fail label. The Student details summary should be updated by using Student.toString ().

but I'm having trouble coding it and i cant seam to call hasPassed() method or toString() method from my student class

so I've doing something wrong but cant see what it is any ideas how to go about fixing this?

i have a set button that basically lets me save update vaules in the student class, though i dont think thats saving them correcty, but wont know until i get the Get button working i have used Student student = new student() in the set button in the get button i need to use the toString method to show the eg 12345 Basil Fawlty, 23/08/194 in the txt boxes and in a label, i then need to use hasPassed() method in the Get button so that when a yearmark is >= 40 another label says Pass or fail if < 40

3 Answers 3

1

I dont completely read your question because there are lots of errors.

For example

public void toString()
      {
          firstname = "Basil";
          secondname = "Fawlty";
          dateofbirth = "23/08/1946";
          course = "MA Hotel Management";
          matricnumber = 12345;
          yearmark = 55;
      }

where is your object?

you should create an object like this: Student stu = new Student();

Be careful and ask your question more understandable!

Have a look : https://stackoverflow.com/questions/902994/how-to-ask-programming-questions-correctly

1
  1. The firstName variable is static. This will make all instances of Student share the same first name, which is not correct. Each Student object should have it's own first name.

  2. The class's instance variables are private and have no way of being set. You probably want to create a constructor that takes these variables as arguments.

    public Student(string firstName, string secondName, ...) 
    {
        this.firstName = firstName;
        this.secondName = secondName;
        ...
    }
    
  3. The hasPassed() method is correct. You can verify that the behavior is working by instantiating an instance of the Student class and calling hasPassed() on the instantiated object.

    double goodYearMark = 85;
    Student goodStudent = new Student("Basil", "Fawlty", ..., goodYearMark);
    Console.WriteLine("Good Student Passed? " + goodStudent.hasPassed());
    
    double badYearMark = 35;
    Student badStudent = new Student("Bad", "Student", ..., badYearMark);
    Console.WriteLine("Bad Student Passed? " + badStudent.hasPassed());
    
  4. The ToString() method should return a string value. Every object in .NET has a ToString() method, and you can override the default behavior using the override keyword. See the MSDN documentation for the Object.ToString Method.

    public override string ToString() 
    {
        return string.format("{0} {1}, {2}", firstName, secondName, dateOfBirth);
    }
    

The code examples above may not compile because I typed them directly into the response window, but hopefully they will be useful as guidance. Hope this helps!

2
  • I didn't noticed the property setters. You can disregard this part: "The class's instance variables are private and have no way of being set."
    – apack
    Oct 20, 2012 at 15:47
  • thanks is is very helpful, sadly the only teach you about methods, classes extra and haven't really taught us how to code them. so again thanks, post is very very helpful Oct 21, 2012 at 19:25
0

Read the toString requirement one more time, you're doing this wrong. What happens to your existing values when you call toString in your code now?

Also, check the two last property setters. Currently you're not preventing the user setting an invalid value.

You also need to create an instance of your class, and set initial values on it that you can return from toString.

Good luck, you're almost there :-)

1
  • tbh i dont really know how to this and now after working on this for last 2 days i had enough to trying to figure out what to do thanks for help anyway Oct 20, 2012 at 15:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.