Presentation is loading. Please wait.

Presentation is loading. Please wait.

1301 Review Part 2 More Stuff You Should Know Briana B. Morrison CSE 1302C Spring 2010.

Similar presentations


Presentation on theme: "1301 Review Part 2 More Stuff You Should Know Briana B. Morrison CSE 1302C Spring 2010."— Presentation transcript:

1 1301 Review Part 2 More Stuff You Should Know Briana B. Morrison CSE 1302C Spring 2010

2 2 CSE 1302C Topics Defining classes Using objects

3 3 CSE 1302C Defining a Class Contain members –Attributes (data) Instance Class (static) –Methods (behaviors) Instance Class (static) Access (public, protected, private)

4 4 CSE 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)

5 5 CSE 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; }

6 6 CSE 1302C class Trophy { // properties public string Name { get { return _Name; } } public int Points { get { return _Points; } set { if (value > 0) _Points = value; }

7 7 CSE 1302C class Trophy { // overloaded ToString public override string ToString() { return _Name + " (" + _Points + ")"; }

8 8 CSE 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

9 9 CSE 1302C // declare and instantiate instance Trophy t = new Trophy("100 kills", 10); // print values Console.WriteLine(t);

10 10 CSE 1302C // create collection Trophy[ ] data_storage; Console.Write("How many trophies: "); int trophie_count; trophie_count = Int32.Parse(Console.ReadLine()); data_storage = new Trophy[trophie_count]; // read in values string 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); }

11 11 CSE 1302C // print collection and total points int 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);

12 12 CSE 1302C Another Class Example How about a superhero class –Name –Superhero ability

13 13 CSE 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.

14 14 CSE 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); }

15 15 CSE 1302C Use the Hero Class Create 2 heroes with powers and have them fight.

16 16 CSE 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(); }

17 17 CSE 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); }

18 18 CSE 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.

19 19 CSE 1302C Questions?


Download ppt "1301 Review Part 2 More Stuff You Should Know Briana B. Morrison CSE 1302C Spring 2010."

Similar presentations


Ads by Google