Friday, July 22, 2011

Friday 7/22/2011


using System;

namespace SimpleInterface
{
interface IStorable
{
//no access modifiers, methods are public
//no implementation
void Read();
void Write(object obj);
//notice that the property declaration doesn't provide an implementation for get and set, but simply designates that there is a get and set
//Interface methods are implicitly public beacuse an interface is a contract meant to be used by other classees
int Status { get; set; }
}

//create a class which implements the IStorable interface
public class Document : IStorable
{
public Document(string s)
{
Console.WriteLine("Creating document with: {0}", s);
}

//implement the Read method
public void Read()
{
Console.WriteLine("Implementing the Read Method for IStorable");
}

//implement the Write method
public void Write(object o)
{
Console.WriteLine("Implementing the Write Method for IStorable");
}

public int Status { get; set; }
}

//Take our interface out for a spin

public class Tester
{
static void Main()
{
//access the methods in the Document object
Document doc = new Document("Test Document");
doc.Status = -1;
doc.Read();
Console.WriteLine("Document Status: {0}", doc.Status);
}
}
}




using System;

namespace ExtendAndCombineInterface
{
interface IStorable
{
void Read();
void Write(object obj);
int Status { get; set; }
}

//here's the new interface
interface ICompressible
{
void Compress();
void Decompress();
}

//extend the interface
interface ILoggedCompressible : ICompressible
{
void LogSavedBytes();
}

//Combine Interfaces
interface IStorableCompressible : IStorable, ILoggedCompressible
{
void LogOriginalSize();
}

//yet another interface
interface IEncryptable
{
void Encrypt();
void Decrypt();
}

public class Document : IStorableCompressible, IEncryptable
{
//hold the data for IStorable's Status property
private int status = 0;
//the document constructor
public Document(string s)
{
Console.WriteLine("Creating the document with: {0}", s);
}

//implement IStorable
public void Read()
{
Console.WriteLine("Implementing the Read Method for IStorable");
}

public void Write(object o)
{
Console.WriteLine("Implementing the Write Method for IStorable");
}

public int Status { get; set; }

//implement ICompressible
public void Compress()
{
Console.WriteLine("Implementing Compress");
}


public void Decompress()
{
Console.WriteLine("Implementing Decompress");
}

//implement ILoggedCompressible
public void LogSavedBytes()
{
Console.WriteLine("Implementing LogSavedBytes");
}

//implement IStorableCompressible
public void LogOriginalSize()
{
Console.WriteLine("Implementing LogOriginalSize");
}

//implement IEncrytable
public void Encrypt()
{
Console.WriteLine("Implementing Encrypt");
}

public void Decrypt()
{
Console.WriteLine("Implementing Decrypt");
}
}

public class Tester
{
static void Main()
{
//create a document object
Document doc = new Document("Test Document");
doc.Read();
doc.Compress();
doc.LogSavedBytes();
doc.Compress();
doc.LogOriginalSize();
doc.Compress();
doc.Read();
doc.Encrypt();
}
}
}




using System;

namespace ExtendAndCombineInterface
{
interface IStorable
{
void Read();
void Write(object obj);
int Status{get;set;}
}

//here's the new interface
interface ICompressible
{
void Compress();
void Decompress();
}

//Extend the interface
interface ILoggedCompressible : ICompressible
{
void LogSavedBytes();
}

//Combines Interfaces
interface IStorableCompressible : IStorable, ILoggedCompressible
{
void LogOriginalSize();
}

//yet another interface
interface IEncryptable
{
void Encrypt();
void Decrypt();
}

public abstract class Document{}

public class BigDocument : Document, IStorableCompressible, IEncryptable
{
//hold the data for IStorable's Status property
private int status = 0;

//the document constructor
public BigDocument(string s)
{
Console.WriteLine("Creating the document with {0}", s);
}

//implement IStorable
public void Read()
{
Console.WriteLine("Implementing the Read Method for IStorable");
}

public void Write(object o)
{
Console.WriteLine("Implementing the Write Method for IStorable");
}

public int Status{get;set;}

//implement ICompressible
public void Compress()
{
Console.WriteLine("Implementing Compress");
}

public void Decompress()
{
Console.WriteLine("Implementing Decompress");
}

//implement ILoggedCompressible
public void LogSavedBytes()
{
Console.WriteLine("Implementing LogSavedBytes");
}


//implement IStorableCompressible
public void LogOriginalSize()
{
Console.WriteLine("Implementing LogOriginalSize");
}

//implement IEncryptable
public void Encrypt()
{
Console.WriteLine("Implementing Encrypt");
}

public void Decrypt()
{
Console.WriteLine("Implementing Decrypt");
}
}

class LittleDocument : Document, IEncryptable
{
public LittleDocument(string s)
{
Console.WriteLine("Creating document with: {0}", s);
}

void IEncryptable.Encrypt()
{
Console.WriteLine("Implementing Encrypt");
}

void IEncryptable.Decrypt()
{
Console.WriteLine("Implementing Decrypt");
}
}

public class Tester
{
static void Main()
{
Document[] folder = new Document[5];
for(int i = 0; i < 5; i++)
{
if(i % 2 == 0)
{
folder[i] = new BigDocument("Big Document # " + i);
}
else
{
folder[i] = new LittleDocument("Little Document # " + i);
}
}

foreach(Document doc in folder)
{
//cast the document to the various interfaces
IStorable isStorableDoc = doc as IStorable;
if(isStorableDoc != null)
{
isStorableDoc.Read();
}
else
{
Console.WriteLine("IStorable not supported");
}

ICompressible icDoc = doc as ICompressible;
if(icDoc!=null)
{
icDoc.Compress();
}
else
{
Console.WriteLine("Compressible not supported");
}

ILoggedCompressible ilcDoc = doc as ILoggedCompressible;
if (ilcDoc != null)
{
ilcDoc.LogSavedBytes();
ilcDoc.Compress();
//ilcDoc.Read();
}
else
{
Console.WriteLine("LoggedCompressible not supported");
}

IStorableCompressible isc = doc as IStorableCompressible;
if (isc!=null)
{
isc.LogOriginalSize(); //IStorableCompressible
isc.LogSavedBytes(); //ILoggedCompressible
isc.Compress(); //ICompressible
isc.Read(); //IStorable
}
else
{
Console.WriteLine("StorableCompressible not supported");
}

IEncryptable ie = doc as IEncryptable;
if (ie != null)
{
ie.Encrypt();
}
else
{
Console.WriteLine("Encryptable not supported");
}

} //end for
} //end main
} //end class
} //end namespace




using System;

namespace overridingInterface
{
interface IStorable
{
void Read();
void Write();
}

//simplify Document to implement only IStorable
public class Document : IStorable
{
//the document constructor
public Document(string s)
{
Console.WriteLine("* Public class Document: IStorable *");
Console.WriteLine("Creating document with {0}", s);
}

//make read virtual
public virtual void Read()
{
Console.WriteLine("* Public class Document: IStorable *");
Console.WriteLine("Document Read Method for IStorable");
}

//NB: Not virtual!
public void Write()
{
Console.WriteLine("* Public class Document: IStorable *");
Console.WriteLine("Document Write Method for IStorable");
}
}

//Derive from Document
public class Note : Document
{
public Note(String s) : base(s)
{
Console.WriteLine("Creating note with: {0}", s);
}

//override read method

public override void Read()
{
Console.WriteLine("Overriding the Read method for Note!");
}

//implement my own Write method
public new void Write()
{
Console.WriteLine("Implementing the Write method for note!");
}
}


//In tester, the read and write methods are called in four ways
//through the base class reference to a derived object
//through an interface created from the base class reference to the derived object
//through a derived object
//through an interface created from the derived object
public class Tester
{
static void Main()
{
//create a document reference to a Note object
Document theNote = new Note("Test Note");
IStorable isNote = theNote as IStorable;
if(isNote != null)
{
isNote.Read();
isNote.Write();
}

Console.WriteLine("\n");

//direct call to the methods
theNote.Read();
theNote.Write();

Console.WriteLine("\n");

//create a note object
Note note2 = new Note("Second Test");
IStorable isNote2 = note2 as IStorable;
if (isNote2 != null)
{
Console.WriteLine("isNote2 != null");
isNote2.Read();
isNote2.Write();
}

Console.WriteLine("\n");

//directly call the methods
note2.Read();
note2.Write();
}
}
}




using System;

namespace Programming_CSharp
{
//a simple class to store in the array
public class Employee
{
public Employee(int empID)
{
this.empID = empID;
}
public override string ToString()
{
string empIDString = "Employee ID " + empID.ToString();
return empIDString;
}
private int empID;
} //end class Employee

public class Tester
{
static void Main()
{
int[] intArray;
Employee[] empArray;
intArray = new int[5];
empArray = new Employee[3];

//populate the array
for (int i = 0; i < empArray.Length; i++)
{
empArray[i] = new Employee(i + 5);
}

for (int i = 0; i < intArray.Length; i++)
{
Console.WriteLine(intArray[i].ToString());
}

for (int i = 0; i < empArray.Length; i++)
{
Console.WriteLine(empArray[i].ToString());
}
}
}
}




using System;
using System.Collections.Generic;
using System.Text;

namespace UsingForEach
{
//a simple class to store in the array
public class Employee
{
public Employee(int empID)
{
this.empID = empID;
}
public override string ToString()
{
string empString = "Employee ID " + empID.ToString();
return empString;
}

private int empID;
}

public class Tester
{
static void Main()
{
int[] intArray;
Employee[] empArray;
intArray = new int[5];
empArray = new Employee[3];

//populate the array
for (int i = 0; i < empArray.Length; i++)
{
empArray[i] = new Employee(i + 5);
}

foreach (int i in intArray)
{
Console.WriteLine(i.ToString());
}

foreach (Employee e in empArray)
{
Console.WriteLine(e.ToString());
}
}
}
}//end namespace




using System;
using System.Collections.Generic;

//implement a simplified, singly linked, sortable list

namespace UsingConstraints
{
public class Employee : IComparable
{
private string name;
public Employee(string name)
{
this.name = name;
}
public override string ToString()
{
return this.name;
}

//implement the interface
public int CompareTo(Employee rhs)
{
return this.name.CompareTo(rhs.name);
}
public bool Equals(Employee rhs)
{
return this.name == rhs.name;
}
} // end class Employee

//node must implement IComparable of Node of T
// constrain Nodes to only take items that implement IComparable
//by using the where keyword.

public class Node<T> :
IComparable<Node<T>> where T : IComparable<T>
{
//member fields
private T data;
private Node<T> next = null;
private Node<T> prev = null;

//constructor
public Node(T data)
{
this.data = data;
}

//properties
public T Data { get { return this.data; } }

public Node<T> Next
{
get { return this.next; }
}

public int CompareTo(Node<T> rhs)
{
//this works because of the constraint
return data.CompareTo(rhs.data);
}

public bool Equals(Node<T> rhs)
{
return this.data.Equals(rhs.data);
}

//methods
public Node Add(Node<T> newNode)
{
if(this.CompareTo(newNode) > 0) // goes before me
{
newNode.next = this;//new node points to me

//if i have a previous, set it to point
//the new node as its next
if(this.prev != null)
{
this.prev.next = newNode;
newNode.prev = this.prev;
}

//set prev in current node to point to new node
this.prev = newNode;
//return the newNode in case it is the new head
return newNode;
}
else //goes after me
{
// if I have a next, pass the new node along for comparison
if (this.next != null)
{
this.next.Add(newNode);
}

// I don't have a next so set the new node to be my next
//and set its prev to point to me
else
{
this.next = newNode;
newNode.prev = this;
}

return this;
}
}

public override string ToString()
{
string output = data.ToString();

if(next != null)
{
output += ", " + next.ToString();
}

return output;
}
}//end class Node

public class LinkedList<T> where T : IComparable<T>
{
//member fields
private Node<T> headNode = null;
//properties
//indexer

public T this[int index]
{
get{
int ctr = 0;
Node<T> node = headNode;
while (node != null && ctr <= index)
{
if (ctr == index)
{
return node.Data;
}
else
{
node = node.Next;
}

++ctr;
} //end while
throw new ArgumentOutOfRangeException();
}//end get
}//end indexer

//constructor
public LinkedList()
{
}

//methods
public void Add(T data)
{
if(headNode==null)
{
headNode = new Node(data);
}
else
{
headNode = headNode.Add(new Node(data));
}
}
public override string ToString()
{
if (this.headNode != null)
{
return this.headNode.ToString();
}
else
{
return string.Empty;
}
}
}//end class

//test engine
class Test
{
//entry point
static void Main(string[] args)
{
//make an instance, run the method
Test t = new Test();
t.Run();
}

public void Run()
{
LinkedList myLinkedList = new LinkedList();
Random rand = new Random();
Console.Write("Adding: ");

for(int i = 0; i < 10; i++)
{
int nextInt = rand.Next(10);
Console.Write("{0} ", nextInt);
myLinkedList.Add(nextInt);
}

LinkedList employees = new LinkedList();
employees.Add(new Employee("Douglass"));
employees.Add(new Employee("Paul"));
employees.Add(new Employee("George"));
employees.Add(new Employee("Ringo"));

Console.WriteLine("\nRetrieving collections...");
Console.WriteLine("Integers: " + myLinkedList);
Console.WriteLine("Employees: " + employees);
}
}
}//end namespace




using System;

class MainClass
{
public static void Main()
{
int[] sample = new int[10];
int i;

for(i=0; i<10; i=i+1)
{
sample[i] = i;
}

for(i=0; i<10; i=i+1)
{
Console.WriteLine("Sample[" + i + "]: " + sample[i]);
}
}
}


No comments: