Presentation is loading. Please wait.

Presentation is loading. Please wait.

/// Pushes value onto stack /// Preconditions: None /// Postconditions: size is incremented by one /// [in] top item on stack /// Nothing public void push(int.

Similar presentations


Presentation on theme: "/// Pushes value onto stack /// Preconditions: None /// Postconditions: size is incremented by one /// [in] top item on stack /// Nothing public void push(int."— Presentation transcript:

1 /// Pushes value onto stack /// Preconditions: None /// Postconditions: size is incremented by one /// [in] top item on stack /// Nothing public void push(int value) { // Copy data to new node; Node n = new Node(); n.data = value; // Reset top to be this new node n.next = top; top = n; // Increment size size++; }

2

3

4 Output from Singleton Pattern Logger Program

5 using System; namespace SingletonPattern { class Program { /// /// Test program to demonstrate logger singleton pattern /// static void test() { Logger.WriteLine("Entering test()"); Logger.WriteLine("Leaving test()"); } /// /// The main entry point for the application. /// [STAThread] static void Main(string[] args) { // IsOn is now true for ANY method that uses logger // until someone sets IsOn to false. Logger.IsOn = true; Logger.WriteLine("Entering Main()"); double sum=0; test(); // Artificial delay... for (int i=0; i<10000; i++) for (int j=0; j<10000; j++) sum = sum + 1; Logger.WriteLine("Exiting Main()"); }

6

7

8 /// /// Demonstrates basic type (decimal) and (double) /// static void testDecimal() { decimal oneThirdDEC = (decimal) 1.0 / (decimal) 3; decimal oneNinetiethDEC = (decimal) 1.0/ (decimal) 90; double oneThirdDBL = 1.0/3; double oneNinetiethDBL = 1.0/90; Console.WriteLine("Decimal- (1/3: - 30*1/90): {0}", oneThirdDEC-30*oneNinetiethDEC); Console.WriteLine("Double- (1/3: -30*1/90): {0}", oneThirdDBL-30*oneNinetiethDBL); } /// /// Demonstrates basic type (decimal) and (double) /// static void testDecimal() { decimal oneThirdDEC = (decimal) 1.0 / (decimal) 3; decimal oneNinetiethDEC = (decimal) 1.0/ (decimal) 90; double oneThirdDBL = 1.0/3; double oneNinetiethDBL = 1.0/90; Console.WriteLine("Decimal- (1/3: - 30*1/90): {0}", oneThirdDEC-30*oneNinetiethDEC); Console.WriteLine("Double- (1/3: -30*1/90): {0}", oneThirdDBL-30*oneNinetiethDBL); } Demonstrating decimal type

9 using System; using System.Collections; namespace Inheritance { /// /// Summary description for Program /// class Program { /// /// The main entry point for the application. /// [STAThread] static void Main(string[] args) { Circle myCircle = new Circle(10); Rectangle myRectangle = new Rectangle(5,10); Square mySquare = new Square(10); ArrayList list = new ArrayList(); list.Add(myCircle); list.Add(myRectangle); list.Add(mySquare); foreach (Shape s in list) { Console.Write("Type: " + s.GetType()); Console.Write(" Id: " + s.Id); Console.Write(" Area: " + s.area()); Console.WriteLine(" Perimeter: " + s.perimeter()); } Shape CircleRectangle Square

10 using System; namespace Inheritance { /// /// Abstract class for all shapes /// public abstract class Shape { /// /// A sequence number that keeps track of the last id assigned /// to a shape. Each time a new shape is created the current sequence number /// is assigned to the id of the new shape and the sequence number is /// incremented. This technique ensures that each shape has a unique id. /// private static int globalId=0; /// /// The shape's unique id. /// private int id; public int Id { get... } /// /// Shape's color... 0=black, 1=white, 2=red, 3=blue /// private int color; public int Color { get... set... } /// /// All shapes implement this method /// /// Area of shape public abstract double area(); /// /// All shapes implement this method /// /// Perimeter of shape public abstract double perimeter(); /// /// All subclasses need to call this constructor to ensure that the /// new shape gets a proper id. /// protected Shape() { id = globalId; globalId++; } }

11 using System; namespace Inheritance { /// public class Rectangle : Inheritance.Shape { private double length; private double width; public double Length { get { return length; } set { length=value; } public double Width { get { return width; } set { width=value; } public Rectangle() : base() { length = width = 0; } public Rectangle(double valueLength, double valueWidth) : base() { length = valueLength; width = valueWidth; } public override double area() { return length*width; } public override double perimeter() { return length*2 + width*2; } }

12 using System; namespace Inheritance { /// public class Square : Inheritance.Rectangle { public Square() : base() { } public Square(double valueLength, double valueWidth) : base(valueLength, valueWidth) { if (valueLength != valueWidth) { throw new System.ArgumentException("Length != Width"); } public Square(double side) : base(side,side) { } using System; namespace Inheritance { public class Circle : Inheritance.Shape { private double radius; public double Radius { get... set... } public override double area() { return 3.14159*radius*radius; } public override double perimeter() { return 2*3.14159*radius; } public Circle() : base() { radius = 0; } public Circle(double valueRadius) : base() { radius = valueRadius; }

13

14 static void testProgrammerThrownExceptionCaught() { Console.Write("Enter a number (1-10), to generate exception, go outside this range: "); try { int i = Int32.Parse(Console.ReadLine()); if ((i 10)) { throw new System.ArgumentException("Bad Number"); } // Catch both out of range and parse exceptions catch (Exception e) { Console.WriteLine("Caught exception... error was: " + e.Message); } Programmer Thrown Exception Caught

15 static void testProgrammerThrownExceptionUnCaught() { Console.Write("Enter a number (1-10), to generate exception, go outside this range: "); int i = Int32.Parse(Console.ReadLine()); if ((i 10)) { throw new System.ArgumentException("Bad Number"); } Programmer Thrown Exception UNCaught

16 using System; namespace GarbageCollection { /// /// Class which illustrates garbage collection /// public class Tally { private static int instanceCount; public static int InstanceCount { get { return instanceCount; } public Tally() { instanceCount++; } ~Tally() { instanceCount--; }

17 using System; namespace GarbageCollection { /// /// Summary description for Class1. /// class Program { static void testObjectGarbageCollection() { for (int i=0; i<10000; i++) { new Tally(); } Console.WriteLine("test: Number of Tallys is: " + Tally.InstanceCount); } /// /// The main entry point for the application. /// [STAThread] static void Main(string[] args) { Console.WriteLine("********** Test Object Garbage Collection **********"); testObjectGarbageCollection(); // Print out instance count... it's the SAME because garbage collection // hasn't been triggered yet so object haven't actually been released // which means the destructor ~Tally() hasn't been called. Console.WriteLine("main: Number of Tallys is: " + Tally.InstanceCount); Console.WriteLine("main: Total Memory: " + System.GC.GetTotalMemory(true)); // Force garbage collection Console.WriteLine("main: Forcing garbage collection to occur..."); System.GC.Collect(); Console.WriteLine("main: Number of Tallys is: " + Tally.InstanceCount); Console.WriteLine("main: Total Memory: " + System.GC.GetTotalMemory(true)); }

18 using System; using System.IO; namespace FileIOExample { /// ///Demonstrates File I/O /// class Program { static void testFileWrite() { Console.Write("Type in a filename to write to: "); String name = Console.ReadLine(); // Open the file for reading try { StreamWriter sw = new StreamWriter(name,false); Console.WriteLine("***** Writing to File *****"); Console.WriteLine("Type in lines of text to place in file."); Console.WriteLine("Type a single period ->.<- on a line to...text"); while (true) { String line = Console.ReadLine(); if (line == ".") break; sw.WriteLine(line); } sw.Close(); } catch (Exception e) { Console.WriteLine("Error writing to file: " + e.Message); } static void testFileRead() { Console.Write("Type in a filename to read from: "); String name = Console.ReadLine(); // Open the file for reading try { StreamReader sr = new StreamReader(name); Console.WriteLine("***** Outputting File *****"); while (true) { String line = sr.ReadLine(); if (line == null) break; Console.WriteLine(line); } sr.Close(); } catch (Exception e) { Console.WriteLine("Error reading file: " + e.Message); } /// /// The main entry point for the application. /// [STAThread] static void Main(string[] args) { Console.WriteLine("***** Test File Write *****"); testFileWrite(); Console.WriteLine("***** Test File Read *****"); testFileRead(); }


Download ppt "/// Pushes value onto stack /// Preconditions: None /// Postconditions: size is incremented by one /// [in] top item on stack /// Nothing public void push(int."

Similar presentations


Ads by Google