1 / 19

1301 Review Part 2

1301 Review Part 2. More Stuff You Should Know. Briana B. Morrison CSE 1302C Spring 2010. Topics. Defining classes Using objects. Defining a Class. Contain members Attributes (data) Instance Class (static) Methods (behaviors) Instance Class (static)

Download Presentation

1301 Review Part 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Topics Defining classes Using objects

  3. Defining a Class Contain members Attributes (data) Instance Class (static) Methods (behaviors) Instance Class (static) Access (public, protected, private)

  4. 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. 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. class Trophy { // properties public string Name { get { return _Name; } } public int Points { get { return _Points; } set { if (value > 0) _Points = value; } } }

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

  8. 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. // declare and instantiate instance Trophy t = new Trophy("100 kills", 10); // print values Console.WriteLine(t);

  10. // 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. // 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. Another Class Example How about a superhero class Name Superhero ability

  13. 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. 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. Use the Hero Class Create 2 heroes with powers and have them fight.

  16. 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. 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. 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. Questions?

More Related