/
Lecture 2 Review of Classes and Arrays Lecture 2 Review of Classes and Arrays

Lecture 2 Review of Classes and Arrays - PowerPoint Presentation

min-jolicoeur
min-jolicoeur . @min-jolicoeur
Follow
342 views
Uploaded On 2019-11-28

Lecture 2 Review of Classes and Arrays - PPT Presentation

Lecture 2 Review of Classes and Arrays 4262018 2 1 CSE 1322 Defining the class Constructor same name as class public visibility default and overloaded Class instance variables Should normally be private ID: 768427

trophy int array 20182 int trophy 20182 array sum nums public points return length method 2018 string data finding

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Lecture 2 Review of Classes and Arrays" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Lecture 2 Review of Classes and Arrays 4/26/2018 2-1 CSE 1322

Defining the class Constructor:same name as class public visibilitydefault and overloaded Class instance variablesShould normally be privateMethods: public, private or protected return or not Properties (C#)getset 4/26/2018 2- 2

Class definition and default constructor public class Trophy{   private string name;   private int points = 10;   public Trophy(){     name = "Yeah that's a problem";    points = 0;   }   public Trophy (string n, int p){     name = n;     points = p;   } } 4/26/2018 2-3 Note: Java is String, C# is string

The Overloaded Constructor public Trophy ( string n, int p) { name = n; points = p; } 4/26/2018 2- 4

Properties (C#) public string Name { get { return name; } }4/26/20182-5

Properties (C#) public int Points { get { return points; } set { if (value > 0) points = value; } } 4/26/20182-6

Override of the ToString method public override string ToString( ) { return name + " (" + points + ")" ; } 4/26/20182-7

Override of the toString method @Override public string toString() { return name + ” (“ +points + ”)” ; }4/26/20182-8

Arrays Trophy[ ] troph ; troph = new Trophy[10]; or Trophy [] troph = new Trophy[10];can't use until memory is allocated with new.In C#, the [] must be between the data type and the object reference while in java the [] can be between the or after the name.4/26/20182-9

Array example Trophy[] data_storage ; int sum = 0; PRINT("How many trophies: "); int trophie_count ; trophie_count = READ();data_storage = new Trophy [trophie_count]; 4/26/20182-10

Traversing an array Use a for, while or do-while starting at index 0 and going to the last elementFOR each element in the array do something ENDFOR 4/26/2018 2- 11

Traversing an array - C# Use a for, while or do-while starting at index 0 and going thru .Length-1 for (int i =0; i< data_storage.Length ; i++) { do something }foreach(Trophy t in data_storage){ do something as long as it doesn't add or remove members of the array}4/26/20182-12

Traversing an array – Java Use a for, while or do-while starting at index 0 and going thru .length-1 for (int i =0; i< data_storage.length ; i++) { do something }for (Trophy t : data_storage){ do something as long as it doesn't add or remove members of the array}4/26/20182-13

Note about Capitalization Java uses . lengthC# uses . Length 4/26/2018 2- 14

Array processing The simplest processes involve searching ( finding a value, finding the largest or smallest ),or finding a sum, product or average.Remember to initialize properly.Note the pattern in the next examples: we use a loop! 4/26/2018 2- 15

Array processing 2 int[] nums = new int[10]; … float average = FindAverage ( nums ); … int min = FindMin(nums); …remember to use "new" to allocate memory for your object. 4/26/20182-16

Finding the min value using a method Method FINDMIN ( array nums) BEGIN min ← nums[0] FOR each element in nums IF element < min min = element ENDIF ENDFOR return minEND FINDMIN4/26/20182-17Ps

C# finding the min private static int FindMin (int [] A) { int temp = A[0]; for ( int i = 1; i < A.Length ); i++) { if (temp > A[i]) temp = A[i]; } return temp; } Note the parameters (in this case "nums ") when you call a method can be called something else in the method itself (in this case "A").  Parameters match in position and type, so the name doesn't matter. 4/26/20182-18

Finding the sum or average using a method Method FINDAVERAGE (array nums) BEGIN sum ← 0FOR each element in nums sum = sum + nums [I]ENDFOR average = (sum*1.0)/(number elements in nums )return averageEND FINDAVERAGE4/26/20182-19

Finding a sum and/or average private static float FindAverage (int [] B) { int sum = 0; for (int i = 0; i < B.length; i++) { sum += B[i]; } return (float)sum / B.length ; } 4/26/2018 2-20