Presentation is loading. Please wait.

Presentation is loading. Please wait.

Review of Classes and Arrays

Similar presentations


Presentation on theme: "Review of Classes and Arrays"— Presentation transcript:

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

2 Defining the class Methods: Constructor: Properties (C#)
public, private or protected return or not Properties (C#) get set Constructor: same name as class public visibility default and overloaded Class instance variables Should normally be private 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

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

5 public string Name { get return name; }
Properties (C#) public int Points { get { return points; } set if (value > 0) points = value; } public string Name { get return name; } 4/26/2018

6 Override of the ToString method
C# public override string ToString( ) { return name + " (" + points + ")"; } 4/26/2018

7 Override of the toString method
Java @Override public string toString() { return name + ” (“+points + ”)”; } 4/26/2018

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/2018

9 Array example C# Trophy[] data_storage; int sum = 0;
Console.Write("How many trophies: "); int trophie_count; trophie_count = Int32.Parse(Console.ReadLine()); data_storage = new Trophy [trophie_count]; 4/26/2018

10 Array example Java Trophy[] data_storage; int sum = 0;
System.out.print("How many trophies: "); int trophie_count; trophie_count = scan.nextInt(); 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 } Or use a foreach loop 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 } Or use a foreach loop for (Trophy t : data_storage) { do something as long as it doesn't add or remove members of the array } 4/26/2018

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. 4/26/2018

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/2018

16 Finding the min value using a method
Method FINDMIN ( array nums) BEGIN min ← nums[0] FOR each element in nums IF nums[I] < min min = nums[I] ENDIF ENDFOR return min END FINDMIN 4/26/2018

17 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; notice that the parameters (in this case "nums") when you invoke 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

18 Java 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; notice that the parameters (in this case "nums") when you invoke 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 of elements in nums) return average END FINDAVERAGE 4/26/2018

20 Java 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

21 C# 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


Download ppt "Review of Classes and Arrays"

Similar presentations


Ads by Google