pushes value onto stack /// preconditions: none /// postconditions: size is incremented by one ///...

18
/// <summary> Pushes value onto stack<br> /// <b>Preconditions:</b> None<br> /// <b>Postconditions:</b><tt>size</tt> is incremented by one</summary> /// <param name="value"> [in] top item on stack</param> /// <returns>Nothing</returns> 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++; }

Upload: tiffany-barber

Post on 21-Dec-2015

222 views

Category:

Documents


1 download

TRANSCRIPT

/// <summary> Pushes value onto stack<br>/// <b>Preconditions:</b> None<br>/// <b>Postconditions:</b><tt>size</tt> is incremented by one</summary>/// <param name="value"> [in] top item on stack</param>/// <returns>Nothing</returns>public void push(int value){

// Copy data to new node;Node n = new Node();n.data = value;

 // Reset top to be this new noden.next = top;top = n;

 // Increment sizesize++;

}

Output from Singleton PatternLogger Program

using System; namespace SingletonPattern{

class Program{

/// <summary>/// Test program to demonstrate logger singleton pattern/// </summary>static void test(){

Logger.WriteLine("Entering test()");Logger.WriteLine("Leaving test()");

/// <summary>/// The main entry point for the application./// </summary>[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()");

}}

}

/// <summary>/// Demonstrates basic type (decimal) and (double)/// </summary>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);}

/// <summary>/// Demonstrates basic type (decimal) and (double)/// </summary>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);}

Demonstratingdecimal type

using System;using System.Collections; namespace Inheritance{

/// <summary>/// Summary description for Program/// </summary>class Program{

/// <summary>/// The main entry point for the application./// </summary>[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

Circle Rectangle

Square

using System;

 namespace Inheritance

{ /// <summary>

/// Abstract class for all shapes

/// </summary>

public abstract class Shape

{/// <summary>

/// 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.

/// </summary>

private static int globalId=0;

 /// <summary>

/// The shape's unique id.

/// </summary>

private int id;

public int Id

{get...

/// <summary>

/// Shape's color... 0=black, 1=white, 2=red, 3=blue

/// </summary>

private int color;

public int Color

{get ...

set ...

/// <summary>

/// All shapes implement this method

/// </summary>

/// <returns>Area of shape</returns>

public abstract double area();

 /// <summary>

/// All shapes implement this method

/// </summary>

/// <returns>Perimeter of shape</returns>

public abstract double perimeter();

 /// <summary>

/// All subclasses need to call this constructor to ensure that the

/// new shape gets a proper id.

/// </summary>

protected Shape()

{id = globalId;

globalId++;

}}

using System;

  namespace Inheritance

{/// <summary>

///

/// </summary>

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;

}}

using System;

  namespace Inheritance

{/// <summary>

///

/// </summary>

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;

}}

}

 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 < 1) || (i > 10)) {

throw new System.ArgumentException("Bad Number");}

}// Catch both out of range and parse exceptionscatch (Exception e){

Console.WriteLine("Caught exception... error was: " + e.Message);}

}

ProgrammerThrownExceptionCaught

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 < 1) || (i > 10)) {

throw new System.ArgumentException("Bad Number");}

}

Programmer ThrownException UNCaught

using System; namespace GarbageCollection{

/// <summary>/// Class which illustrates garbage collection/// </summary>public class Tally{

private static int instanceCount; 

public static int InstanceCount{

get{

return instanceCount;}

public Tally(){

instanceCount++;}

 ~Tally(){

instanceCount--;}

}}

using System; namespace GarbageCollection{

/// <summary>/// Summary description for Class1./// </summary>class Program{

static void testObjectGarbageCollection(){

for (int i=0; i<10000; i++){

new Tally();}

 Console.WriteLine("test: Number of Tallys is: " + Tally.InstanceCount);

/// <summary>/// The main entry point for the application./// </summary>[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 collectionConsole.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));

}}

}

using System;

using System.IO;

  namespace FileIOExample

{/// <summary>

///Demonstrates File I/O

/// </summary>

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);

}}

 /// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main(string[] args)

{Console.WriteLine("***** Test File Write *****");

testFileWrite();

 Console.WriteLine("***** Test File Read *****");

testFileRead();

}}

}