Exemple #1
0
        static void Test_InsertAt_And_RemoveAt()
        {
            //This test will go over the InsertAt and RemoveAt methods, testing the various coditions that change how the array is arranged after a remove or insert
            ArrayInt defaultSize = new ArrayInt(); //array of 10 elements

            Console.WriteLine("----------------------TESTING InsertAt() METHODS---------------------- \n");
            Console.Write("\n");
            Console.WriteLine("InsertAt conditions to be tested: \n \t if array is full, if the insert is at the beginning, if insert is in middle of array, and if the insert is at the end of the array, \n");
            for (int i = 0; i < defaultSize.Size; i++)
            {
                defaultSize[i] = i;                            //fills array with values from 0 to Size (10 by default)
            }
            defaultSize.InsertAt(0, 100);                      //insert at beginning of array
            defaultSize.InsertAt(10, 100);                     //insert at beginning of array
            defaultSize.InsertAt((defaultSize.Size - 1), 100); //insert at end of array
            Console.WriteLine("100 inserted at beginning with full array, middle, expecting an array with these values: 100, ...., 100, ...., 100 \n");
            Console.WriteLine("\t" + defaultSize.GetDisplayText(" , ") + "\n");
            Console.Write("\n");
            Console.WriteLine("----------------------TESTING RemoveAt() METHODS---------------------- \n");
            Console.Write("\n");
            Console.WriteLine("RemoveAt conditions to be tested: \n \t if the insert is at the beginning, if insert is in middle of array, and if the insert is at the end of the array, \n");
            defaultSize.RemoveAt(0);                      //insert at beginning of array
            defaultSize.RemoveAt(9);                      //insert at beginning of array
            defaultSize.RemoveAt((defaultSize.Size - 3)); //insert at end of array
            Console.WriteLine("100 removed from beginning, middle, and end of array, expecting an array WITHOUT these values: 100, ...., 100, ...., 100 \n");
            Console.WriteLine("\t" + defaultSize.GetDisplayText(" , ") + "\n");
        }
Exemple #2
0
        static void Test_IsFull_And_IsEmpty_Method()
        {
            //This test will go over the IsFull and IsEmpty methods, testing the various coditions that change the return value
            ArrayInt defaultSize = new ArrayInt(); //creating ArrayInt object with empty elements\

            Console.WriteLine("----------------------TESTING IsFull() METHOD, USING FOR LOOP TO FILL ARRAY---------------------- \n");
            Console.Write("\n");
            for (int i = 0; i < defaultSize.Size; i++)
            {
                defaultSize[i] = i; //fills array with values
            }
            Console.WriteLine("Testing array filled with integers from 0 to: " + (defaultSize.Size - 1) + ", expecting IsFull to equal true: \n");
            Console.WriteLine("\t" + defaultSize.IsFull() + "\n");
            Console.WriteLine("Reinitialize same array with 10 elements, expecting IsFull() to equal false: \n");
            defaultSize = new ArrayInt();
            Console.WriteLine("\t" + defaultSize.IsFull() + "\n");
            Console.Write("\n");
            Console.WriteLine("----------------------TESTING IsEmpty() METHOD---------------------- \n");
            Console.WriteLine("Now testing IsEmpty method, previous array is no loger filled, expecting IsEmpty() to equal true: \n");
            Console.WriteLine("\t" + defaultSize.IsEmpty() + "\n");
            for (int i = 0; i < defaultSize.Size; i++)
            {
                defaultSize[i] = i; //fills array with values
            }
            Console.WriteLine("Testing array filled with integers from 0 to: " + (defaultSize.Size - 1) + ", expecting IsEmtpty() to equal false: \n");
            Console.WriteLine("\t" + defaultSize.IsEmpty() + "\n");
            Console.Write("\n");
        }
Exemple #3
0
        static void Test_Set_Get()
        {
            //This test will go over the setters and getters with [] AND the getters and setters of the size property
            ArrayInt defaultSize = new ArrayInt(); //creating ArrayInt object with empty elements

            //Testing the getters and setters for the this[] array index property
            Console.WriteLine("----------------------TESTING SETTINGS VALUES WITH [] AND READING THEM BACK---------------------- \n");
            defaultSize[3] = 7;
            Console.WriteLine("Setting index value 3 and getting it, expecting 7: \n");
            Console.WriteLine("\t" + defaultSize[3] + "\n");

            //lower bounds error testing
            Console.WriteLine("Testing invalid set index of -1, expecting error: \n");
            try
            {
                defaultSize[-1] = 7;
                Console.WriteLine("\t value at index -1 was successfully set, index within range \n");
            }
            catch
            {
                Console.WriteLine("\t attempting to set a value at index -1 resulted in an error, index out of range \n");
            }

            //Upper bounds error testing
            Console.WriteLine("Testing invalid set index of 15, expecting error: \n");
            try
            {
                defaultSize[15] = 7;
                Console.WriteLine("\t value at index 15 was successfully set, index within range \n");
            }
            catch
            {
                Console.WriteLine("\t attempting to set a value at index 15 resulted in an error, index out of range \n");
            }
            Console.Write("\n");

            //Now testing the getters and setters for the size proeprty
            Console.WriteLine("----------------------TESTING GET AND SET SIZE PROPERTY, USING FOR LOOP TO FILL ARRAY---------------------- \n");
            for (int i = 0; i < defaultSize.Size; i++)
            {
                defaultSize[i] = i; //fills array with values
            }
            Console.WriteLine("Default array object filled with integers from 0 to: " + (defaultSize.Size - 1) + " expecting array with 10 elements: \n");
            Console.WriteLine("\t" + defaultSize.GetDisplayText(" , ") + "\n");
            defaultSize.Size = 15;
            Console.WriteLine("Now setting the same array to 15 and filling with integers from 0 to: " + (defaultSize.Size - 1) + ", expecting array with 15 elements: \n");
            for (int i = 0; i < defaultSize.Size; i++)
            {
                defaultSize[i] = i; //fills array with values
            }
            Console.WriteLine("\t" + defaultSize.GetDisplayText(" , ") + "\n");
            Console.Write("\n");
        }
        void CreateArray()
        {
            int number;

            if (int.TryParse(txtNumberOfItems.Text, out number) && number > 0)
            {
                int n = number;
                dataGridViewArray.RowCount = n;
                Array = new ArrayInt(n);
                ShowArray();
            }
        }
Exemple #5
0
        static void Test_Constructor()
        {
            //This test will go over both constructors
            ArrayInt defaultSize = new ArrayInt(); //creating ArrayInt object

            Console.WriteLine("----------------------TESTING CONSTRUCTORS---------------------- \n");
            Console.WriteLine("Testing default constructor, expecting an empty array of 10: \n");
            Console.WriteLine("\t" + defaultSize.Size + "\n");
            ArrayInt defineSize = new ArrayInt(15); //creating ArrayInt object

            Console.WriteLine("Testing user-defined constructor, expecting an empty array of 15: \n");
            Console.WriteLine("\t" + defineSize.Size + "\n");
            Console.Write("\n");
        }
Exemple #6
0
        static void Test_Append()
        {
            //This test will go over the Append method, testing the various coditions that change where a value is appended
            ArrayInt defaultSize = new ArrayInt(); //array of 10 elements

            Console.WriteLine("----------------------TESTING Append() METHOD---------------------- \n");
            Console.Write("\n");
            Console.WriteLine("Append conditions to be tested: if array is empty, if only appends have been used, if setAt[] was used, \n");
            defaultSize.Append(2);
            defaultSize.Append(4);
            defaultSize[3] = 16;
            defaultSize.Append(32);
            Console.WriteLine("Three appends and a setAt have been used, expecting an array with the values: 2, 4, unknown, 16, 32 \n");
            Console.WriteLine("\t" + defaultSize.GetDisplayText(" , ") + "\n");
            Console.WriteLine("Append conditions to be tested: if array is full and append is used, \n");
            defaultSize = new ArrayInt(); //reinitialized array of 10 elements
            for (int i = 0; i < defaultSize.Size; i++)
            {
                defaultSize[i] = i; //fills array with values from 0 to Size (10 by default)
            }
            defaultSize.Append(100);
            Console.WriteLine("Appending to full array, expecting an array with " + ((defaultSize.Size * 2) + 1) + " elements and 100 appended in the 11th spot \n");
            Console.WriteLine("\t" + defaultSize.GetDisplayText(" , ") + "\n");
        }
 /// <summary>
 /// Creates the variables of the solution
 /// </summary>
 /// <returns></returns>
 public override Core.Variable[] CreateVariables()
 {
     Core.Variable[] variables = new Core.Variable[1];
     variables[0] = new ArrayInt(this.Problem.NumberOfVariables, this.Problem);
     return(variables);
 }
        static void Main(string[] args)
        {
            Console.WriteLine("//////////////////// - Interface Demo - //////////////////// \n");
            Console.WriteLine("Apple SmartPhone:");
            Apple apple = new Apple();
            apple.OS();
            apple.AppStore();
            apple.TouchID();

            Console.WriteLine("\n\n");
            Console.WriteLine("Iphone6 SmartPhone:");
            Iphone6 iphone6 = new Iphone6();
            iphone6.TouchID();

            Console.WriteLine("\n\n");
            Console.WriteLine("Samsung SmartPhone:");
            Samsung samsung = new Samsung();
            samsung.OS();
            samsung.AppStore();

            Console.WriteLine("\n\n");
            Console.WriteLine("Toyota class");
            Toyota toyota = new Toyota();
            //toyota.yearProduction = "2000";
            //Console.WriteLine(toyota.yearProduction);
            Console.WriteLine(Toyota.yearProduction);
            Toyota.yearProduction = "2000";
            Console.WriteLine(Toyota.yearProduction);

            Console.WriteLine("\n\n");
            Console.WriteLine("Perl class");
            Console.WriteLine(Perl.price);
            Perl.price = 300;
            Console.WriteLine(Perl.price);

            Console.WriteLine("\n\n");
            Console.WriteLine("SayHello class");
            SayHello sayHello = new SayHello();
            sayHello.Speak();
            sayHello.GoodBye();
            sayHello.Yell();

            Console.WriteLine("\n\n");
            Console.WriteLine("GenericInt class");
            ArrayInt arrayInt = new ArrayInt(10);
            for (int i = 0; i < 10; i++)
            {
                arrayInt.setItem(i, i + 1);
            }
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine(arrayInt.getItem(i));
            }

            //Console.WriteLine("\n\n");
            //Console.WriteLine("Repository class");
            //UserRepository userRepository = new UserRepository();
            //IEnumerable<User> ListUser = userRepository.GetAll();

            //foreach (User user in ListUser)
            //{
            //    Console.WriteLine(user.UserName);
            //}

            Console.WriteLine("\n\n");
            Console.WriteLine("Typeof class");
            TypeOf typeOf = new TypeOf();
            typeOf.PrintTypeOf(new Apple(), new SayHello());

               Console.ReadKey();
        }
        public void _02_02_CollectionsGenerischTest()
        {
            var meinArray = new ArrayInt(5);

            // Compiler wandelt folgende Zeile um in
            // meinArray.this[0].set(2)
            meinArray[0] = 2;
            meinArray[1] = 3;
            meinArray[2] = 5;
            meinArray[3] = 7;
            meinArray[4] = 11;

            int a = meinArray[3];

            for (int i = 0; i < meinArray.Length; i++)
            {
                meinArray[i] = i * 10;
            }

            // Dank IEnumarable können wir auf dem selbsdefinierten Array mittels 
            // einer foreach- Schleife iterieren

            foreach (int elem in meinArray)
            {
                Debug.WriteLine(elem);
            }

            int sum = 0;
            for (int i = 0; i < meinArray.Length; i++)
                // Compiler wandelt folgende Zeile um in
                // meinArray.this[i].get()
                sum += meinArray[i];

            // jetzt den generischen Typ nutzen
            var PreislisteGen = new ArrayGenerisch<Preis>(3);

            PreislisteGen[0] = new Preis(4, 99);
            PreislisteGen[1] = new Preis(1, 99);
            PreislisteGen[2] = new Preis(2, 29);

            var p1 = PreislisteGen[1];           


            var A8 = new ArrayGenerisch<Basics._04_Objektorientiert.Autobahn.Auto>(3);

            //A8[0].tanken(100);

            var deinArray = new ArrayGenerisch<double>(3);

            deinArray[0] = 3.14;
            deinArray[1] = 2.72;
            deinArray[2] = 9.81;

            // deinArray ist streng typisiert
            //deinArray[2] = "Hallo";

            double dblSum = 0;
            for (int i = 0; i < deinArray.Length; i++)
                dblSum += deinArray[i];


            // Vorgefertigte generische Collections einsetzen

            // 1) List- ein dynamischer Ersatz für Arrays
            var Preisliste = new List<Preis>(10);

            FüllePreisliste(Preisliste);

            // Compiler wandelt folgenden Aufruf um in
            // Preisliste.this[3].get()
            var preis3 = Preisliste[3];
            Assert.AreEqual(8, preis3.GetEuro());
            Assert.AreEqual(8, Preisliste[3].GetEuro());

            // Einen Preis aus der Liste entfernen
            Preisliste.RemoveAt(0); // 1. Element entfernen
            Preisliste.RemoveAt(Preisliste.Count - 1); // letztes Element entfernen
            Preisliste.RemoveRange(1, 2);

            FüllePreisliste(Preisliste);

            // Linked List
            var AktuellePreise = new LinkedList<string>();
            foreach (var p in Preisliste)
            {
                AktuellePreise.AddLast(p.ToString());
            }

            foreach (var el in AktuellePreise)
            {
                Debug.WriteLine(el);
            }

            // Iterieren über die LinkedList vorwärts
            LinkedListNode<string> actNode = null;
            for (actNode = AktuellePreise.First; actNode != null; actNode = actNode.Next)
            {
                Debug.WriteLine("preis: " + actNode.Value);

            }

            // Iterieren über die LinkedList rückwärts
            for (actNode = AktuellePreise.Last; actNode != null; actNode = actNode.Previous)
            {
                Debug.WriteLine("preis: " + actNode.Value);

            }

            foreach (var p in AktuellePreise)
            {
                // Current greift auf Value vom Node zu
                Debug.WriteLine("preis: " + p);
            }


            // 2 PReise entfernen
            Preisliste.Remove(new Preis(12, 45));

            // den 3. Preis entfernen
            Preisliste.RemoveAt(3);

            
            AktuellePreise.Clear();
            foreach (var p in Preisliste)
            {
                AktuellePreise.AddLast(p.ToString());
            }

            // Telefonbuch
            var telBuch = new Dictionary<string, int>();

            telBuch.Add("Anton", 4711);

            // neuer Eintrag hinzugefügt, da noch nicht unter dem Schlüssel vorhanden
            // Folgeder Indexeraufruf entspricht telbuch.Add("Berta", 6969);
            telBuch["Berta"] = 6969;

            // Eintrag geändert
            telBuch["Berta"] = 7766;

            telBuch["Cäsar"] = 3344;

            // Iterieren durch Dictionary 1
            var telBuchListe = new LinkedList<string>();
            foreach (var k in telBuch.Keys)
            {
                telBuchListe.AddLast(k + ": " + telBuch[k]);
            }

            // Iterieren durch Dictionary 2
            telBuchListe.Clear();
            foreach (KeyValuePair<string, int> pair in telBuch)
            {
                telBuchListe.AddLast(pair.Key + ": " + pair.Value);
            }

            // Queue

            var Warteschlange = new Queue<Tuple<int, string>>();


            // Aufträge in Warteschlange einstellen
            Warteschlange.Enqueue(new Tuple<int, string>(99, "Abwaschen"));
            Warteschlange.Enqueue(new Tuple<int, string>(77, "Abtrocknen"));
            Warteschlange.Enqueue(new Tuple<int, string>(66, "Wegräumen"));

            // Jobverarbeitung schaltet sich ein
            var Auftragsprotokoll = new LinkedList<string>();
            var Auftrag = Warteschlange.Dequeue();
            Auftragsprotokoll.AddLast("Führe aus: " + Auftrag.Item2);

            Warteschlange.Enqueue(new Tuple<int, string>(55, "Zumachen"));

            // Nachschauen, ohne zu entnehmen
            var erstes = Warteschlange.Peek();

            // Da die Queue IEnumerable implementiert, können alle aktuellen
            // Einträge mittels foreach- Schleife besucht werden
            foreach (var job in Warteschlange)
            {
                Debug.WriteLine(job.Item2);
            }

            var alleJobsDieMitABeginnen = Warteschlange.Where(r => r.Item2.StartsWith("A")).ToArray();

            Auftrag = Warteschlange.Dequeue();
            Auftragsprotokoll.AddLast("Führe aus: " + Auftrag.Item2);

            // Alle restlichen verarbeiten
            while (Warteschlange.Count > 0)
            {
                Auftrag = Warteschlange.Dequeue();
                Auftragsprotokoll.AddLast("Führe aus: " + Auftrag.Item2);
            }

        }