Download presentation
Presentation is loading. Please wait.
1
Review of Classes and Arrays
Lecture 2 Review of Classes and Arrays CSE 1322 4/26/2018
2
Defining the class Constructor: Class instance variables Methods:
same name as class public visibility default and overloaded Class instance variables Should normally be private Methods: public, private or protected return or not Properties (C#) get set 4/26/2018
3
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 Note: Java is String, C# is string
4
The Overloaded Constructor
public Trophy (string n, int p) { name = n; points = p; } 4/26/2018
5
Properties (C#) public string Name { get { return name; } 4/26/2018
6
Properties (C#) public int Points { get { return points; } set
if (value > 0) points = value; } 4/26/2018
7
Override of the ToString method
public override string ToString( ) { return name + " (" + points + ")"; } 4/26/2018
8
Override of the toString method
@Override public string toString() { return name + ” (“+points + ”)”; } 4/26/2018
9
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/2018
10
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/2018
11
Traversing an array Use a for, while or do-while starting at index 0 and going to the last element FOR each element in the array do something ENDFOR 4/26/2018
12
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/2018
13
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/2018
14
Note about Capitalization
Java uses .length C# uses .Length 4/26/2018
15
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
16
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/2018
17
Finding the min value using a method
Ps Method FINDMIN ( array nums) BEGIN min ← nums[0] FOR each element in nums IF element < min min = element ENDIF ENDFOR return min END FINDMIN 4/26/2018
18
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/2018
19
Finding the sum or average using a method
Method FINDAVERAGE (array nums) BEGIN sum ← 0 FOR each element in nums sum = sum + nums[I] ENDFOR average = (sum*1.0)/(number elements in nums) return average END FINDAVERAGE 4/26/2018
20
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.