Defining Classes IN C Objectives Understand how to define cl

Defining Classes IN C#

Objectives

Understand how to define classes

Establish private and public accessible attributes/variables

Instantiating classes into objects

Background

Remember that classes are defined to hold data and behaviors (functions/methods). Sometimes these should be public (and visible to users of the classes) while other times these should be private (accessible only within the class itself).

Directions

Begin an empty console application

Create a new class \"Course\" that represents data about a college course.

This class should have,

a string to hold a short description of the course

an integer \"course number\"

an integer \"credit hours”

a string \"prefix\" (such as CSE or CGDD)

Remember to use appropriate scope; your attributes should be private and then provide public get/set accessors for each field.

Create a constructor that takes two strings and two integers (representing values for each attribute)

a. Initialize each variable within the class using these parameters to the constructor

5. Create a constructor that takes in no parameters
a. Initialize each variable within the class to defaults of your choosing

Override the ToString() method to output all field names and values of any instance.

I n m a i n , c reate two instances of the course class; one should use the default constructor

(with no parameters) and one should use the 4-parameter constructor.

Print each course to the screen to show them to the user.

Im having trouble code my main two instances, this is what i have so far

using System;

namespace HaileLab3
{
    class MainClass
    {
        public static void Main (string[] args)

        Course c1 = new Course();

        Console.WriteLine (c1.ToString());
    }

}

   using System;

namespace HaileLab3
{
    public class Course
    {
        private string description;
        private int coursenumber;
        private int credithours;
        private string prefix;
        //properties
        public string Description
        {
            get { return description; }
            set { description = value; }
        }
        public string Prefix
        {
            get { return prefix; }
            set { prefix = value; }
        }
        public int Coursenumber
        {
            get { return coursenumber; }
            set { coursenumber = value; }
        }
        public int Credithours
        {
            get { return credithours; }
            set { credithours = value; }
        }

        //constructor
        public Course ()
        {
            credithours = 4;
            coursenumber = 1101;
            prefix = \"EGD\";
            description = \"Engineeringgraphics\";
        }
        //overload
        public Course (string course, string prefix, int coursenumber, int credithours)
        {
            this.description = course;
            this.prefix = prefix;
            this.coursenumber = coursenumber;
            this.credithours = credithours;    
        }
            //overriding tostring by parent (object)
        public override string ToString ()
        {
            return (\"Description: \" + description + \"\ Coursenumber: \" + coursenumber + \"\ Credithours: \" + credithours + \"\ Prefix: \" + Prefix + \"\ \");
        }





    }
}

Solution

using System;
namespace HaileLab3
{
class MainClass
{
public static void Main (string[] args){
  
// Using non parameterized constructor
Course c1 = new Course();
Console.WriteLine (c1.ToString());
  
// Using parameterized constructor
Course c2 = new Course(\"Design and Analysis of Algorithms\",\"DAA\",707,5);
Console.WriteLine (c2.ToString());
  
}
}
  
public class Course
{
private string description;
private int coursenumber;
private int credithours;
private string prefix;
//properties
public string Description
{
get { return description; }
set { description = value; }
}
public string Prefix
{
get { return prefix; }
set { prefix = value; }
}
public int Coursenumber
{
get { return coursenumber; }
set { coursenumber = value; }
}
public int Credithours
{
get { return credithours; }
set { credithours = value; }
}

//constructor
public Course ()
{
credithours = 4;
coursenumber = 1101;
prefix = \"EGD\";
description = \"Engineeringgraphics\";
}
//overload
public Course (string course, string prefix, int coursenumber, int credithours)
{
this.description = course;
this.prefix = prefix;
this.coursenumber = coursenumber;
this.credithours = credithours;
}
//overriding tostring by parent (object)
public override string ToString ()
{
return (\"Description: \" + description + \"\ Coursenumber: \" + coursenumber + \"\ Credithours: \" + credithours + \"\ Prefix: \" + Prefix + \"\ \");
}

}

}

Defining Classes IN C# Objectives Understand how to define classes Establish private and public accessible attributes/variables Instantiating classes into objec
Defining Classes IN C# Objectives Understand how to define classes Establish private and public accessible attributes/variables Instantiating classes into objec
Defining Classes IN C# Objectives Understand how to define classes Establish private and public accessible attributes/variables Instantiating classes into objec
Defining Classes IN C# Objectives Understand how to define classes Establish private and public accessible attributes/variables Instantiating classes into objec

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site