We've been given an assignment to construct Object Oriented classes from just basically looking at a tester class that runs in the main method.
using System;
using school;
namespace testschool{
public class Tester {
static void Main(){
Faculty scienceFac=University.createFaculty("Science");
Department compSciDept= scienceFac.openNewDepartment("Computer Science");
Department physicsDept= scienceFac.openNewDepartment("Physics");
Console.WriteLine(Object.ReferenceEquals(
scienceFac, physicsDept.Faculty)); //expected to return scienceFac object
Console.WriteLine(University.numberOfFaculties());
//..... MORE CODE
I think he made the code as confusing as possible, and its really getting confusing. I'm just starting off and I'm already stuck but here's what I have so far.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace testschool
{
class University
{
List<Faculty> all_faculty = new List<Faculty>();
public Faculty createFaculty(string faculty_name)
{
Faculty new_faculty = new Faculty(faculty_name);
all_faculty.Add(new_faculty);
return new_faculty;
}
public int numberOfFaculties()
{
return all_faculty.Count;
}
}
class Faculty
{
string faculty_name;
List<Department> all_departments = new List<Department>();
public Faculty(string faculty_name)
{
this.faculty_name = faculty_name;
}
public Department openNewDepartment(string department_name)
{
Department new_department = new Department(department_name, this);
all_departments.Add(new_department);
return new_department;
}
}
class Department
{
string department_name;
Faculty parent_faculty;
public Department(string department_name, Faculty faculty)
{
this.department_name = department_name;
parent_faculty = faculty;
}
public Faculty Faculty
{
get { return parent_faculty; }
}
}
}
The two Questions I have so far is:
First:
this line: Faculty scienceFac = University.createFaculty("Science"); just seems to be calling University right off the bat, without an object reference. I declared University as a class since it also seems to have methods within it like .createFaculty("Science") and .numberOfFaculties(). So could that be a mistake? or is University actually a namespace or something else that has its own methods?
Second: a university has faculties, a faculty has departments, and eventually it goes to the point where there are students and courses. As seen on the code, I've used lists, but we haven't gone into that. Only arrays (yes, I'm aware lists are arrays too but say that I want to stick with just arrays). Is it possible to use to an array without declaring a predefined size? (I could always just set the size to something big like 999 but its not really practical)
Faculty scienceFac = University.createFaculty("Science");can only be used if you havepublic static Faculty createFaculty(string faculty_name)