IN C# Create a program which will create a class called Teacher. Each teacher ha
ID: 3904600 • Letter: I
Question
IN C#
Create a program which will create a class called Teacher. Each teacher has a name, age, height and weight. These variables should be declared as private. Create the correct constructors accessors and functions. In the main you will create 5 teachers. Input the data for the 5 teachers from a text file which already has information in it. Name the File "teachinfo.txt". After setting up all the data, it is time to sort based on the user input. 1 to sort by name, 2 to sort by age, 3 to sort by height and 4 to sort by weight. (create a function for each one). You should use Bubble Sort. Use of Array.Sort or any other sort will result in a zero..
Explanation / Answer
Below is your code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace teachersData
{
//Teacher definition
class Teacher
{
//Private instance variables
private string name;
private int age;
private double height;
private double weight;
//Parameterized Constructor
public Teacher(string sname, int sage, double sheight, double sweight)
{
name = sname;
age = sage;
weight = sweight;
height = sheight;
}
//Getter method for name
public string getName()
{
return name;
}
//Getter method for age
public int getAge()
{
return age;
}
//Getter method for height
public double getHeight()
{
return height;
}
//Getter method for weight
public double getWeight()
{
return weight;
}
//Method that prints teacher data to console
public void print()
{
Console.WriteLine(" Name: " + name + " Age: " + age + " Height: " + height + " Weight: " + weight);
}
}
class Program
{
//Main method
static void Main(string[] args)
{
string sname;
int sage;
double sh, sw;
//Creating an array of teachers
Teacher[] teachers = new Teacher[5];
//File stream object
FileStream fileStream = new FileStream(@"D:\teachers.txt", FileMode.Open, FileAccess.Read);
//Reading data from file
using (var streamReader = new StreamReader(fileStream, Encoding.UTF8))
{
string line;
int i = 0;
//Iterating over file data
while ((line = streamReader.ReadLine()) != null)
{
//Reading required data from file
string[] strArr = null;
char[] splitchar = { ';' };
strArr = line.Split(splitchar);
sname = strArr[0];
sage = Convert.ToInt32(strArr[1]);
sh = Convert.ToDouble(strArr[2]);
sw = Convert.ToDouble(strArr[3]);
teachers[i] = new Teacher(sname, sage, sh, sw);
i++;
}
}
//Creating object for calling remaining functions
Program obj = new Program();
//Printing teachers data
obj.printTeachers(teachers);
int sortOption;
//Sorting options
while(true)
{
//Reading type of sort from user
Console.Write(" Select type of sort: 1 - Sort By Name 2 - Sort By Age 3 - Sort By Height 4 - Sort By Weight 5 - Exit Your Choice: " );
sortOption = Convert.ToInt32(Console.ReadLine());
//Calling respecctive function
switch (sortOption)
{
case 1: obj.sortByName(teachers); obj.printTeachers(teachers); break;
case 2: obj.sortByAge(teachers); obj.printTeachers(teachers); break;
case 3: obj.sortByHeight(teachers); obj.printTeachers(teachers); break;
case 4: obj.sortByWeight(teachers); obj.printTeachers(teachers); break;
case 5: return;
default: break;
}
}
Console.Read();
}
//Method that sorts teacher data by name
public void sortByName(Teacher[] teachers)
{
int i, j;
Teacher temp;
//Iterating over array
for (i = 0; i < 5; i++)
{
for (j = 0; j < 5 - 1; j++)
{
//Comparing name
if (String.Compare(teachers[j].getName(), teachers[j + 1].getName()) > 0)
{
//Swapping teacher objects
temp = teachers[j];
teachers[j] = teachers[j+1];
teachers[j+1] = temp;
}
}
}
}
//Method that sorts teacher data by age
public void sortByAge(Teacher[] teachers)
{
int i, j;
Teacher temp;
//Iterating over array
for (i = 0; i < 5; i++)
{
for (j = 0; j < 5 - 1; j++)
{
//Comparing ages
if (teachers[j].getAge() > teachers[j + 1].getAge())
{
//Swapping teacher objects
temp = teachers[j];
teachers[j] = teachers[j+1];
teachers[j+1] = temp;
}
}
}
}
//Method that sorts teacher data by height
public void sortByHeight(Teacher[] teachers)
{
int i, j;
Teacher temp;
//Iterating over array
for (i = 0; i < 5; i++)
{
for (j = 0; j < 5 - 1; j++)
{
//Comparing height
if (teachers[j].getHeight() > teachers[j + 1].getHeight())
{
//Swapping teacher objects
temp = teachers[j];
teachers[j] = teachers[j+1];
teachers[j+1] = temp;
}
}
}
}
//Method that sorts teacher data by weight
public void sortByWeight(Teacher[] teachers)
{
int i, j;
Teacher temp;
//Iterating over array
for (i = 0; i < 5; i++)
{
for (j = 0; j < 5 - 1; j++)
{
//Comparing weight
if (teachers[j].getWeight() > teachers[j + 1].getWeight())
{
//Swapping teacher objects
temp = teachers[j];
teachers[j] = teachers[j+1];
teachers[j+1] = temp;
}
}
}
}
//Method that prints teacher data
public void printTeachers(Teacher[] teachers)
{
int i;
//Iterating over array
for (i = 0; i < 5; i++)
{
//Printing a teacher
teachers[i].print();
}
}
}
}
Output
Name: John Doe Age: 21 Height: 5.8 Weight: 98.4
Name: Artish Nek Age: 19 Height: 6 Weight: 108.4
Name: Vicky Arora Age: 34 Height: 6.2 Weight: 111
Name: Mayank Chhabra Age: 29 Height: 5.5 Weight: 90
Name: Isha Kalra Age: 20 Height: 5 Weight: 92
Select type of sort:
1 - Sort By Name
2 - Sort By Age
3 - Sort By Height
4 - Sort By Weight
5 - Exit
Your Choice: 1
Name: Artish Nek Age: 19 Height: 6 Weight: 108.4
Name: Isha Kalra Age: 20 Height: 5 Weight: 92
Name: John Doe Age: 21 Height: 5.8 Weight: 98.4
Name: Mayank Chhabra Age: 29 Height: 5.5 Weight: 90
Name: Vicky Arora Age: 34 Height: 6.2 Weight: 111
Select type of sort:
1 - Sort By Name
2 - Sort By Age
3 - Sort By Height
4 - Sort By Weight
5 - Exit
Your Choice: 1
Name: Artish Nek Age: 19 Height: 6 Weight: 108.4
Name: Isha Kalra Age: 20 Height: 5 Weight: 92
Name: John Doe Age: 21 Height: 5.8 Weight: 98.4
Name: Mayank Chhabra Age: 29 Height: 5.5 Weight: 90
Name: Vicky Arora Age: 34 Height: 6.2 Weight: 111
Select type of sort:
1 - Sort By Name
2 - Sort By Age
3 - Sort By Height
4 - Sort By Weight
5 - Exit
Your Choice: 2
Name: Artish Nek Age: 19 Height: 6 Weight: 108.4
Name: Isha Kalra Age: 20 Height: 5 Weight: 92
Name: John Doe Age: 21 Height: 5.8 Weight: 98.4
Name: Mayank Chhabra Age: 29 Height: 5.5 Weight: 90
Name: Vicky Arora Age: 34 Height: 6.2 Weight: 111
Select type of sort:
1 - Sort By Name
2 - Sort By Age
3 - Sort By Height
4 - Sort By Weight
5 - Exit
Your Choice: 2
Name: Artish Nek Age: 19 Height: 6 Weight: 108.4
Name: Isha Kalra Age: 20 Height: 5 Weight: 92
Name: John Doe Age: 21 Height: 5.8 Weight: 98.4
Name: Mayank Chhabra Age: 29 Height: 5.5 Weight: 90
Name: Vicky Arora Age: 34 Height: 6.2 Weight: 111
Select type of sort:
1 - Sort By Name
2 - Sort By Age
3 - Sort By Height
4 - Sort By Weight
5 - Exit
Your Choice: 3
Name: Isha Kalra Age: 20 Height: 5 Weight: 92
Name: Mayank Chhabra Age: 29 Height: 5.5 Weight: 90
Name: John Doe Age: 21 Height: 5.8 Weight: 98.4
Name: Artish Nek Age: 19 Height: 6 Weight: 108.4
Name: Vicky Arora Age: 34 Height: 6.2 Weight: 111
Select type of sort:
1 - Sort By Name
2 - Sort By Age
3 - Sort By Height
4 - Sort By Weight
5 - Exit
Your Choice: 4
Name: Mayank Chhabra Age: 29 Height: 5.5 Weight: 90
Name: Isha Kalra Age: 20 Height: 5 Weight: 92
Name: John Doe Age: 21 Height: 5.8 Weight: 98.4
Name: Artish Nek Age: 19 Height: 6 Weight: 108.4
Name: Vicky Arora Age: 34 Height: 6.2 Weight: 111
Select type of sort:
1 - Sort By Name
2 - Sort By Age
3 - Sort By Height
4 - Sort By Weight
5 - Exit
Your Choice: 2
Name: Artish Nek Age: 19 Height: 6 Weight: 108.4
Name: Isha Kalra Age: 20 Height: 5 Weight: 92
Name: John Doe Age: 21 Height: 5.8 Weight: 98.4
Name: Mayank Chhabra Age: 29 Height: 5.5 Weight: 90
Name: Vicky Arora Age: 34 Height: 6.2 Weight: 111
Select type of sort:
1 - Sort By Name
2 - Sort By Age
3 - Sort By Height
4 - Sort By Weight
5 - Exit
Your Choice: 1
Name: Artish Nek Age: 19 Height: 6 Weight: 108.4
Name: Isha Kalra Age: 20 Height: 5 Weight: 92
Name: John Doe Age: 21 Height: 5.8 Weight: 98.4
Name: Mayank Chhabra Age: 29 Height: 5.5 Weight: 90
Name: Vicky Arora Age: 34 Height: 6.2 Weight: 111
Select type of sort:
1 - Sort By Name
2 - Sort By Age
3 - Sort By Height
4 - Sort By Weight
5 - Exit
Your Choice: