1

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)

2
  • Please note that I have not tested the code yet in any way, I'm stuck with the university error and if its wrong I don't wanna keep working on wrong code right from the very start Commented Nov 11, 2014 at 5:23
  • Faculty scienceFac = University.createFaculty("Science"); can only be used if you have public static Faculty createFaculty(string faculty_name) Commented Nov 11, 2014 at 5:31

2 Answers 2

4

Specific answers to questions:

  1. University.createFaculty() is a static method. It's called using the type name rather than an instance.

  2. To create an array instance, you have to specify a size. But the variables themselves are declared without any size information.

I think that addresses what you were specifically asking about. Of course, in the educational setting, it's often better to consult with your teacher if they are available. This helps you get an answer that is tailored better to the course and what the teacher wants you to learn, as well as helps the teacher understand where each student is with respect to comprehension of the lessons and assignments.

Sign up to request clarification or add additional context in comments.

8 Comments

How is University.createFaculty() a static method? It doesn't mention anywhere in the code.
@DebopamChanda: the code the student wrote doesn't declare it as static. But it's clear from the code the teacher gave the student, where the method is called as University.createFaculty("Science"), that it's supposed to be declared as static
Because it is using a straight out class name that is not tied to an instance anywhere in the code that we see. Statics use a FQN to access functions. I have taught C# for 7 years, and I would say this is not C# fully but some ugly Java hybrid into C# code.
@Damon: I like your take on the code. But in its defense, when I write Java code I still use .NET naming conventions, which all the Java people hate. So I try not to be too harsh about people using Java naming conventions in C#. :)
@Damon: I was tempted to tag this as Java until I read the Console.WriteLine call.
|
0

1- as @Peter Duniho mentioned, the University.CreateFaculty() is a static method

public class University
{
    public static void CreateFaculty()
    {
        // a static method declared inside a class can be called without creating an instance of the object
    } 
}

2- to resize an array, you can do the following

int[] data=new int[2];
data[0]=1;
data[2]=2;

data=ResizeData(data,3); // this function will return new array with array with length=5

public int[] ResizeData(int[] data,int number)
{
    int[] newArray=new int[number]();
    for(int i=0;i<data.Length;i++) // copy the data from dataArray to the newArray
       newArray[i]=data[i];
    return newArray;
}

hope this will help you

7 Comments

IMHO, if you want to resize an array, it is better to use the Array.Resize() method
@PeterDuniho i am assuming that they are not using any kind of data structure, List, Arrays are data structure classes, and i provided him a solution working with normal arrays
@HadiHassan: the Array.Resize() method is for "normal arrays"
you can convert a List<T> back to an array.
@HadiHassan: suit yourself. It seems pretty clear to me from even the teacher's example that the code is expected to use at least a minimal set of C#-specific features. E.g. Object.ReferenceEquals(), Console.WriteLine(), etc. See also realloc in C++
|

Your Answer

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