Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C# Create the Course.cs file defining the Course class according to these requir

ID: 3575091 • Letter: C

Question

C#

Create the Course.cs file defining the Course class according to these requirements:

1. has a private string field to hold the name of the course

2. has a method to set the name field to a given string value

3. has a method to retrieve the name field

4. has a method that overrides the ToString() in order to be able to be displayed in a WPF window

This method should be declared exactly as given below adn then you should add your code to the body of the method to return the nae field:

public override string ToString()

{

//this method should return the name field

}

SOURCE CODE

namespace CreateClassesObjs
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{

Course choice;

public MainWindow()
{
InitializeComponent();
}

private void Window_Loaded(object sender, RoutedEventArgs e)
{
Course course1 = new Course();
Course course2 = new Course();
Course course3 = new Course();
Course course4 = new Course();
Course course5 = new Course();
Course course6 = new Course();
Course course7 = new Course();

course1.setName("IT 145");
course2.setName("IT 200");
course3.setName("IT 201");
course4.setName("IT 270");
course5.setName("IT 315");
course6.setName("IT 328");
course7.setName("IT 330");

this.comboBox.Items.Add(course1);
this.comboBox.Items.Add(course2);
this.comboBox.Items.Add(course3);
this.comboBox.Items.Add(course4);
this.comboBox.Items.Add(course5);
this.comboBox.Items.Add(course6);
this.comboBox.Items.Add(course7);
}

private void button_Click(object sender, RoutedEventArgs e)
{
choice = (Course)(this.comboBox.SelectedItem);
this.listBox.Items.Add(choice);
}

}
}

Explanation / Answer

Course.cs

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

namespace CreateClassesObjs
{
    class Course

    {
        private string courseName; //REQ1 Private string field to hold name of course

        public Course() //default constructor
        {
            courseName = "Default course name";
        }

        public Course(string initialCourseName) //overloaded constructor
        {
            courseName = initialCourseName;
        }

        public void SetName(string a) //REQ2 set name field to string. Because of the book's suggestion, I changed this public function to be capitalized
        {
            courseName = a;
        }
      
        public string GetName() //REQ3 Retrieve name field
        {
            return courseName;
        }

        public override string ToString() //REQ4 Returns name field by calling GetName()
        {
            return GetName();
        }
    }
}

MainWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CreateClassesObjs
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        Course choice;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Course course1 = new Course();
            Course course2 = new Course();
            Course course3 = new Course();
            Course course4 = new Course();
            Course course5 = new Course();
            Course course6 = new Course();
            Course course7 = new Course();

            course1.SetName("IT 145");
            course2.SetName("IT 200");
            course3.SetName("IT 201");
            course4.SetName("IT 270");
            course5.SetName("IT 315");
            course6.SetName("IT 328");
            course7.SetName("IT 330");

            this.comboBox.Items.Add(course1);
            this.comboBox.Items.Add(course2);
            this.comboBox.Items.Add(course3);
            this.comboBox.Items.Add(course4);
            this.comboBox.Items.Add(course5);
            this.comboBox.Items.Add(course6);
            this.comboBox.Items.Add(course7);
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            choice = (Course)(this.comboBox.SelectedItem);
            this.listBox.Items.Add(choice);
        }

    }
}