none
C# homework RRS feed

  • Question

  • Here is the task:

    Create a Console Application project.
    Please implement a Linear List (Linked List) named LinearCollection with the following
    methods:
     public bool Add(NodeElement element) – adds a new element to the end of the list;
     public NodeElement Get() – gets an element from the beginning of the list and removes it
    from the list;
     public void Print() – prints the list contents to the standard output (for each NodeElement
    call its Print() method);
    Nodes of the lists are descendants of type NodeElement and they are of two types:
    NodeElementLong (with public property “long Data”) and NodeElementString (with
    public property “string Data”) (both of the classes should be inherited from
    NodeElement class). In NodeElement you should specify virtual method Print which
    should be properly overridden in descendant classes.
    Both descendant classes should properly implement ICloneable interface.
    Use of generics is encouraged; take special attention to properly implement the
    IDisposable interface, because in the future a descendant class that might contain
    memory leaks could appear.
    Don’t use recursion at all.

    Here is what I did:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Naloga2
    {
        public class LinearCollection
        {
            public LinearCollection() 
            {
                ListOfElements = new List<NodeElement>();
            }
    
            public List<NodeElement> ListOfElements { get; set; }
    
            public bool Add(NodeElement element)
            {
                try
                {
                    ListOfElements.Add(element);
                    return true;
                }
                catch
                {
                    return false;
                }
            }
    
            public NodeElement Get()
            {
                NodeElement nodeElement = ListOfElements.FirstOrDefault();
                ListOfElements.Remove(nodeElement);
                return nodeElement;
            }
    
            public void Print()
            {
                foreach (NodeElement element in ListOfElements)
                {
                    NodeElementString neString = (NodeElementString)element;
                    Console.WriteLine(neString.Data);
                }
            }
        }
    
        public class NodeElement : IDisposable
        {
            public NodeElement() { }
    
            ~NodeElement()
            {
                Dispose(false);
            }
    
            public virtual void Print() 
            { 
                
            }
    
            public void Dispose()
            {
                Dispose(true);
                GC.SuppressFinalize(this);
            }
    
            protected virtual void Dispose(bool disposing)
            {
                if (disposing)
                {
    
                }
            }
        }
    
        public class NodeElementString : NodeElement, ICloneable
        {
            public NodeElementString() { }
    
            public String Data { get; set; }
    
            public override void Print()
            {
    
            }
    
            public object Clone()
            {
                NodeElementString temp = new NodeElementString();
                temp.Data = this.Data.Clone() as String;
                return temp;
            }        
        }
    
        public class NodeElementLong : NodeElement, ICloneable
        {
            public NodeElementLong() { }
    
            public NodeElementLong(long data)
            {
                Data = data;
            }
    
            public long Data { get; set; }
    
            public override void Print()
            {
                
            }
    
            public object Clone()
            {
                return new NodeElementLong(Data);
            }
        }
    
    }

    This is my main class:

    static void Main(string[] args)
            {
                LinearCollection linearCollection = new LinearCollection();
    
                // Read words from file and add them to collection
                string[] wordsFromFile = File.ReadAllLines("words.txt");
    
                foreach (string word in wordsFromFile)
                {
                    NodeElementString neString = new NodeElementString();
                    neString.Data = word;
                    linearCollection.Add(neString);
                }
    
                // Display words
                linearCollection.Print();
                Console.WriteLine("\n\n");
    
                // Add item to collection
                NodeElementString neNewItem = new NodeElementString();
                neNewItem.Data = "New item to add to list";
                linearCollection.Add(neNewItem);
                Console.WriteLine("New element was added to the list: {0}", neNewItem.Data);
                Console.WriteLine("\n\n");
    
                // Display collection after adding element
                linearCollection.Print();
                Console.WriteLine("\n\n");
    
                // Show first element and remove from list
                NodeElementString neFirst = (NodeElementString)linearCollection.Get();
                Console.Write("Element that will be erased: {0}", neFirst.Data);
                Console.WriteLine("\n\n");
    
                // Display collection after deleting element
                linearCollection.Print();
    
                Console.ReadLine();
            }

    Program works fine, but the class is not implemented as it should be.

    Can somebody please fix my mistakes?
    • Edited by wartmignef Wednesday, February 6, 2013 1:11 PM
    Wednesday, February 6, 2013 1:09 PM

Answers

  • No, because the entire purpose of the assignment is for you to learn to solve these types of problems on your own.  Figuring out what you've even done wrong is a *very* important first step, and diagnosing problems such as that is a skill that you need to learn to develop as a programmer.
    Wednesday, February 6, 2013 3:17 PM
  • In addition you'll likely notice that NodeElementString and NodeElementLong are basically the same except they don't deal with the same underlying type. So it seems you could create a generic NodeElement<T> class and then just have NodeElement<string> and NodeElement<long> for free.

    Feel free to ask if you have a particular problem when trying to write your own code but as other said, it's best to do this by yourself with some minimal help as needed rather than to just cut/paste some code (even if non homework I prefer to explain what is the issue or what needs to be done rather than to provide ready to use code).


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".


    Wednesday, February 6, 2013 7:14 PM

All replies

  • What is your issue?

    btw, the only obvious mistake is your collections Print() method. It should definitely look like this, using polymorphism:

    public void Print()
    {            
      foreach (NodeElement element in ListOfElements)
      {
        element.Print();                
      }
    }

    There are two more things, you may clean-up:

    1. IClonable should be already implemented in NodeElement.
    2. Make your both concrete node implementations look a like. Thus use for both the same type of constructor - preferrable the paramterized - and implement the Clone() method identically. Remember, string is invariant.
    Wednesday, February 6, 2013 3:10 PM
  • No, because the entire purpose of the assignment is for you to learn to solve these types of problems on your own.  Figuring out what you've even done wrong is a *very* important first step, and diagnosing problems such as that is a skill that you need to learn to develop as a programmer.
    Wednesday, February 6, 2013 3:17 PM
  • In addition you'll likely notice that NodeElementString and NodeElementLong are basically the same except they don't deal with the same underlying type. So it seems you could create a generic NodeElement<T> class and then just have NodeElement<string> and NodeElement<long> for free.

    Feel free to ask if you have a particular problem when trying to write your own code but as other said, it's best to do this by yourself with some minimal help as needed rather than to just cut/paste some code (even if non homework I prefer to explain what is the issue or what needs to be done rather than to provide ready to use code).


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".


    Wednesday, February 6, 2013 7:14 PM