Transcript Document

/// <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++;
}
Output from Singleton Pattern
Logger 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);
}
Demonstrating
decimal 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);
Shape
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());
}
}
}
}
Circle
Rectangle
Square
}
track of the last id assigned
shape is created the current sequence number
new shape and the sequence number is
ensures that each shape has a unique id.
/// <summary>
/// All subclasses need to call this constructor to ensure that the
/// new shape gets a proper id.
/// </summary>
protected Shape()
{
id = globalId;
globalId++;
}
/// <summary>
/// All shapes implement this method
/// </summary>
/// <returns>Perimeter of shape</returns>
public abstract double perimeter();
/// <summary>
/// All shapes implement this method
/// </summary>
/// <returns>Area of shape</returns>
public abstract double area();
}
set ...
/// <summary>
/// Shape's color... 0=black, 1=white, 2=red, 3=blue
/// </summary>
private int color;
public int Color
{
get ...
/// <summary>
/// The shape's unique id.
/// </summary>
private int id;
public int Id
{
get...
}
namespace Inheritance
{
/// <summary>
/// Abstract class for all shapes
/// </summary>
public abstract class Shape
{
/// <summary>
/// A sequence number that keeps
/// to a shape. Each time a new
/// is assigned to the id of the
/// incremented. This technique
/// </summary>
private static int globalId=0;
using System;
}
}
length=value;
width=value;
length = width = 0;
public override double perimeter()
{
return length*2 + width*2;
}
public override double area()
{
return length*width;
}
public Rectangle(double valueLength, double valueWidth) : base()
{
length = valueLength;
width = valueWidth;
}
}
public Rectangle() : base()
{
}
}
set
{
public double Width
{
get
{
return width;
}
}
}
set
{
public double Length
{
get
{
return length;
}
namespace Inheritance
{
/// <summary>
///
/// </summary>
public class Rectangle : Inheritance.Shape
{
private double length;
private double width;
using System;
}
}
}
set ...
public Circle(double valueRadius) : base()
{
radius = valueRadius;
}
public Circle() : base()
{
radius = 0;
}
public override double perimeter()
{
return 2*3.14159*radius;
}
public override double area()
{
return 3.14159*radius*radius;
}
}
public double Radius
{
get ...
namespace Inheritance
{
public class Circle : Inheritance.Shape
{
private double radius;
using System;
}
public Square(double side) : base(side,side)
{
}
public Square(double valueLength, double valueWidth) : base(valueLength, valueWidth)
{
if (valueLength != valueWidth)
{
throw new System.ArgumentException("Length != Width");
}
}
namespace Inheritance
{
/// <summary>
///
/// </summary>
public class Square : Inheritance.Rectangle
{
public Square() : base()
{
}
using System;
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 exceptions
catch (Exception e)
{
Console.WriteLine("Caught exception... error was: " + e.Message);
}
}
Programmer
Thrown
Exception
Caught
atic 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 Thrown
Exception 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 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));
}
}
}
}
}
}
Console.WriteLine("***** Test File Read *****");
testFileRead();
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Console.WriteLine("***** Test File Write *****");
testFileWrite();
}
Console.WriteLine(line);
sr.Close();
}
catch (Exception e)
{
Console.WriteLine("Error reading file: " + e.Message);
}
}
while (true)
{
String line = sr.ReadLine();
if (line == null) break;
Console.WriteLine("***** Outputting File *****");
// Open the file for reading
try
{
StreamReader sr = new StreamReader(name);
static void testFileRead()
{
Console.Write("Type in a filename to read from: ");
String name = Console.ReadLine();
}
sw.WriteLine(line);
sw.Close();
}
catch (Exception e)
{
Console.WriteLine("Error writing to file: " + e.Message);
}
}
while (true)
{
String line = Console.ReadLine();
if (line == ".") break;
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");
// Open the file for reading
try
{
StreamWriter sw = new StreamWriter(name,false);
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();
using System;
using System.IO;