Wednesday, September 14, 2011

Tuesday 9.14.11

using System;
using System.Globalization;

namespace program
{
class mainClass
{

static void Main()
{
string longString = "7654321";
int actualInt = Int32.Parse(longString);
Console.WriteLine(actualInt);

string dblString = "-7654.321";
double actualDbl = Double.Parse(dblString, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign);
Console.WriteLine(actualDbl);

//to convert a string containing a Boolean value to a bool type
//this code requries the use of the System namespace
string boolString = "true";
bool actualBool = Boolean.Parse(boolString);
if(actualBool==true)
{
Console.WriteLine(actualBool);
}
}
}
}


static void Main()
{
string testStr = "school";
foreach (char c in testStr)
{
Console.WriteLine("Char: " + c.ToString());
}

testStr = "abc123";
for (int counter = 0; counter < testStr.Length; counter++)
{
Console.WriteLine(testStr[counter]);
}
}


static void Main()
{
string foo = "--TEST--";
Console.WriteLine(foo.Trim(new char[] { '-' }));

foo = ",-TEST-,-";
Console.WriteLine(foo.Trim(new char[] { '-', ',' }));

foo = "--TEST--";
Console.WriteLine(foo.TrimStart(new char[] { '-' }));

foo = ",-TEST-,-";
Console.WriteLine(foo.TrimStart(new char[] { '-', ',' }));

foo = "--TEST--";
Console.WriteLine(foo.TrimEnd(new char[] { '-' }));

foo = ",-TEST-,-";
Console.WriteLine(foo.TrimEnd(new char[] { '-', ',' }));
}


static void Main()
{
StringBuilder sb = new StringBuilder("First line of string");

//terminate teh first line
sb.AppendLine();

//add a second line
sb.AppendLine("Second line of string");
Console.WriteLine(sb.ToString());
}


using System;
using System.Collections;


class Product {
public string name;
double cost;
int onhand;

public Product(string n, double c, int h)
{
name = n;
cost = c;
onhand = h;
}

public override string ToString(){
return String.Format("{0, -10} Cost: {1,6:C} On hand: {2}", name, cost, onhand);
}
}

//create an IComparer for Product objects
class ProductComparer : IComparer
{
//implements the IComparable interface
public int Compare(object obj1, object obj2)
{
Product a, b;
a = (Product)obj1;
b = (Product)obj2;
return a.name.CompareTo(b.name);
}
}

class ICompareDemo
{
public static void Main()
{
ProductComparer comp = new ProductComparer();
ArrayList inv = new ArrayList();

//add elements to the list
inv.Add(new Product("A", 5.5, 3));
inv.Add(new Product("C", 8.9, 2));
inv.Add(new Product("B", 3.0, 4));
inv.Add(new Product("D", 1.8, 8));

Console.WriteLine("Product list before sorting: ");
foreach (Product i in inv)
{
Console.WriteLine(" " + i);
}
Console.WriteLine();

//sort the list using an IComparer
inv.Sort(comp);

Console.WriteLine("Product list after sorting:");
foreach (Product i in inv)
{
Console.WriteLine(" " + i);
}
}
}


using System;

class MainClass
{
public static void Main()
{
int[] nums = new int[10];
int avg = 0;

nums[0] = 99;
nums[1] = 10;
nums[2] = 100;
nums[3] = 18;
nums[4] = 78;
nums[5] = 23;
nums[6] = 63;
nums[7] = 9;
nums[8] = 87;
nums[9] = 49;

for (int i = 0; i < 10; i++)
{
avg = avg + nums[i];
}

avg = avg / 10;

Console.WriteLine("Average: " + avg);
}
}

No comments: