Upload
gyles-jones
View
214
Download
0
Embed Size (px)
Citation preview
1301 Review Part 2
More Stuff You Should Know
Briana B. MorrisonCSE 1302C
Spring 2010
2CSE 1302C
Topics• Defining classes
• Using objects
3CSE 1302C
Defining a Class• Contain members
– Attributes (data)• Instance• Class (static)
– Methods (behaviors)• Instance• Class (static)
• Access (public, protected, private)
4CSE 1302C
Trophy Example• Define a class that would store the
trophy name and points
• Attributes
• What methods– Constructor– Properties (accessors / mutators)– Ability to print (overload ToString)
5CSE 1302C
class Trophy {
// attributes private string _Name; private int _Points = 10;
// default constructor public Trophy()
{ _Name = "Yeah that's a problem"; _Points = 0; }
// overloaded constructor public Trophy (string n, int p) { _Name = n; Points = p; }}
6CSE 1302C
class Trophy { // properties public string Name { get { return _Name; } }
public int Points { get { return _Points; } set { if (value > 0) _Points = value; } }}
7CSE 1302C
class Trophy
{
// overloaded ToString
public override string ToString()
{
return _Name + " (" + _Points + ")";
}
}
8CSE 1302C
Using a Class• Declare and instantiate an instance
• Print the values of the instance
• Create a collection (array) of trophy instances
• Read in values, sum the points in the collection
9CSE 1302C
// declare and instantiate instance
Trophy t = new Trophy("100 kills", 10);
// print values
Console.WriteLine(t);
10CSE 1302C
// create collectionTrophy[ ] data_storage;Console.Write("How many trophies: ");int trophie_count;trophie_count = Int32.Parse(Console.ReadLine());data_storage = new Trophy[trophie_count];
// read in valuesstring name;int points;for (int i = 0; i < trophie_count; i++){ Console.Write("Enter trophy " + i + " name:"); name = Console.ReadLine(); Console.Write("Enter points for this trophy: "); points = Int32.Parse(Console.ReadLine()); data_storage[i] = new Trophy(name, points);}
11CSE 1302C
// print collection and total pointsint sum = 0;Console.WriteLine("Uber Gamer Prophile");// for (int i = 0; i < data_storage.Length; i++)// Console.WriteLine(data_storage[i]);// data_storage[0].Points += 9;foreach (Trophy troph in data_storage) Console.WriteLine(troph);foreach (Trophy troph in data_storage) sum += troph.Points;Console.WriteLine("Your points are " + sum);
12CSE 1302C
Another Class Example• How about a superhero class
– Name– Superhero ability
13CSE 1302C
class Hero { public enum PowerType {Strength, Fly, Speed, MindControl, SprVision}; public string Name; public PowerType SuperPower;
}
Notice in the above that the enum allows us to define a new "type", but this is actually just a place holder for integers.
14CSE 1302C
class Hero {
public void Fight(Hero opponent) { if (this.SuperPower == PowerType.SprVision) { Console.WriteLine(this.Name + " beats up " + opponent.Name); } else { Console.WriteLine(Name + " fights " + opponent.Name); } } }
15CSE 1302C
Use the Hero Class• Create 2 heroes with powers and have
them fight.
•
16CSE 1302C
class Program { static void Main(string[ ] args) { Hero SpiderMan; SpiderMan = new Hero(); SpiderMan.Name = "Peter Parker"; SpiderMan.SuperPower = Hero.PowerType.BusVision;
Hero Venom = new Hero();// Hero Venom;// Venom = null; Venom.Name = "Venom"; Venom.SuperPower = Hero.PowerType.Fly;
Venom.Fight(SpiderMan); SpiderMan.Fight(Venom); Console.Write("Press ENTER"); Console.ReadLine(); } }
17CSE 1302C
class Program { static void Main(string[] args) { DoArrayStuff(); }
// ------------------------------------------------------- private static void DoArrayStuff() { int[ ] nums = new int[10]; //remember to not forget to use "new" to allocate memory for your object.
for (int i = 0; i < 10; i++) { Console.Write("Enter number " + i + " : "); nums[i] = Int32.Parse(Console.ReadLine()); }
float average = FindAverage(nums);
Console.WriteLine("The average is " + average);
int min = FindMin(nums); Console.WriteLine("The min is " + min); }
}
18CSE 1302C
private static int FindMin(int[] A) { int temp = A[0];
for (int i = 1; i < 10; i++) { if (temp > A[i]) temp = A[i]; }
return temp; }
// ------------------------------------------------------- private static float FindAverage(int[] A) { int sum = 0;
for (int i = 0; i < 10; i++) { sum += A[i]; }
return sum / 10f; }
Also note that the parameters (in this case "nums") when you invoke a method can be called something in the method itself (in this case "A"). Parameters match in position and type, so the name doesn't matter.
19CSE 1302C
Questions?