Review of Classes and Arrays

Slides:



Advertisements
Similar presentations
PHP functions What are Functions? A function structure:
Advertisements

Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Language Fundamentals in brief C# - Introduction.
Alice in Action with Java
Lecture 16 Arrays: Part 2 COMP1681 / SE15 Introduction to Programming.
Static Class Members Wrapper Classes Autoboxing Unboxing.
1301 Review Part 2 More Stuff You Should Know Briana B. Morrison CSE 1302C Spring 2010.
Inheritance. Types of Inheritance Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and.
Chapter 1 Algorithm Analysis
CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
FEN 2012 UCN Technology: Computer Science1 C# - Introduction Language Fundamentals in Brief.
CSE 1302 Lecture 7 Object Oriented Programming Review Richard Gesick.
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
CS 100Lecture 131 Announcements Exam stats P3 due on Thursday.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
Simple Arrays Arrays of primitives and Strings Sections 7.1, 7.2.
CMSC 202 Arrays 2 nd Lecture. Aug 6, Array Parameters Both array indexed variables and entire arrays can be used as arguments to methods –An indexed.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Java 5 Class Anatomy. User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating.
Lecture 7: Arrays Michael Hsu CSULA 3 Opening Problem Read one hundred numbers, compute their average, and find out how many numbers are above the average.
Review for Test2. Scope 8 problems, 60 points. 1 Bonus problem (5 points) Coverage: – Test 1 coverage – Exception Handling, Switch Statement – Array of.
Lecture 14 Searching and Sorting Richard Gesick.
Andy Wang Object Oriented Programming in C++ COP 3330
Arrays 2/4 By Pius Nyaanga.
Foundations of Programming: Arrays
Counted Loops.
LinkedList Class.
Building Java Programs
Objects First with Java CITS1001
More Object Oriented Programming
Arrays An Array is an ordered collection of variables
Pass by Reference, const, readonly, struct
Selection sort Given an array of length n,
Building Java Programs
Chapter 8 Slides from GaddisText
Review of Classes and Arrays
Building Java Programs
Lecture 11 Searching and Sorting Richard Gesick.
Lecture 2: Implementing ArrayIntList reading:
Assignment 7 User Defined Classes Part 2
Arrays .
Cs212: Data Structures Computer Science Department Lecture 2: Arrays.
CS2011 Introduction to Programming I Arrays (I)
Announcements Lab 7 due Wednesday Assignment 4 due Friday.
int [] scores = new int [10];
Lecture 4 2d Arrays CSE /26/2018.
Static is one of the modifiers that determine variable and method characteristics. The static modifier associates a variable or method with its class.
Tonga Institute of Higher Education
Arrays ICS2O.
1D Arrays and Lots of Brackets
1D Arrays and Lots of Brackets
Object Oriented Programming Review
1D Arrays and Lots of Brackets
Lecture 1 Review of 1301/1321 CSE /26/2018.
Review of Searching and Sorting Algorithms
CIS 199 Final Review.
Module 8 – Searching & Sorting Algorithms
Building Java Programs
1D Arrays and Lots of Brackets
Module 8 – Searching & Sorting Algorithms
Review of Classes and Arrays
Review for Midterm 3.
Arrays Introduction to Arrays Reading for this Lecture:
CSC 205 Java Programming II
CMP 167 Programming Methods One
1D Arrays and Lots of Brackets
Ps Module 7 – Part II 2D Arrays and LISTS 8/29/2019 CSE 1321 Module 7.
Creating and Using Classes
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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