190 likes | 340 Views
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)
E N D
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) Access (public, protected, private)
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)
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; } }
class Trophy { // properties public string Name { get { return _Name; } } public int Points { get { return _Points; } set { if (value > 0) _Points = value; } } }
class Trophy { // overloaded ToString public override string ToString() { return _Name + " (" + _Points + ")"; } }
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
// declare and instantiate instance Trophy t = new Trophy("100 kills", 10); // print values Console.WriteLine(t);
// 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); }
// 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);
Another Class Example How about a superhero class Name Superhero ability
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.
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); } } }
Use the Hero Class Create 2 heroes with powers and have them fight.
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(); } }
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); } }
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.