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.
Lecture 2 Basics of C#. Members of a Class A field is a variable of any type that is declared directly in a class. Fields are members of their containing.
CSCI 1100/1202 April 3, Testing A program should be executed multiple times with various input in an attempt to find errors Debugging is the process.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
Alice in Action with Java
Static Class Members Wrapper Classes Autoboxing Unboxing.
1301 Review Part 2 More Stuff You Should Know Briana B. Morrison CSE 1302C Spring 2010.
Multiple Choice Solutions True/False a c b e d   T F.
CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
CSE 1302 Lecture 7 Object Oriented Programming Review Richard Gesick.
Static class members.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
CSE 1301 Lecture 6 Writing Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
Chapter 6 Object-Oriented Design Part 2. © 2004 Pearson Addison-Wesley. All rights reserved2/20 The this Reference The this reference allows an object.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
1 CS161 Introduction to Computer Science Topic #16.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
COMPUTER PROGRAMMING 2 ArrayLists. Objective/Essential Standard Essential Standard 3.00Apply Advanced Properties of Arrays Essential Indicator 3.02 Apply.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Functions Modules in C++ are called functions and classes. Main reason to use functions is : – get aid in conceptual organization.
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.
COMP 14 Introduction to Programming Mr. Joshua Stough March 23, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
More About Objects and Methods
Haskell Chapter 2.
Lecture 14 Searching and Sorting Richard Gesick.
Intro To Classes Review
More About Objects and Methods
Lecture 14 Writing Classes part 2 Richard Gesick.
LinkedList Class.
This pointer, Dynamic memory allocation, Constructors and Destructor
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
Lecture 11 B Methods and Data Passing
More Object Oriented Programming
Object Oriented Programming
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays An Array is an ordered collection of variables
Pass by Reference, const, readonly, struct
Selection sort Given an array of length n,
Classes & Objects: Examples
© A+ Computer Science - Arrays and Lists © A+ Computer Science -
Lecture 11 Searching and Sorting Richard Gesick.
An "enum" is a type with a fixed set of elements.
More About Objects and Methods
Arrays, Part 1 of 2 Topics Definition of a Data Structure
int [] scores = new int [10];
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, Part 1 of 2 Topics Definition of a Data Structure
1D Arrays and Lots of Brackets
Object Oriented Programming Review
1D Arrays and Lots of Brackets
Review of Classes and Arrays
§ § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊
CIS 199 Final Review.
1D Arrays and Lots of Brackets
Review of Classes and Arrays
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Functions, Part 2 of 3 Topics Functions That Return a Value
Review for Midterm 3.
CSC 205 Java Programming II
§ § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊
1D Arrays and Lots of Brackets
Creating and Using Classes
Presentation transcript:

Review of Classes and Arrays Lecture 3 Review of Classes and Arrays Richard Gesick

Defining the class Methods: Constructor: Properties public, private or protected return or not Properties get set Constructor: same name as class public visibility default and overloaded Class instance variables Should normally be private

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; }

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

public string Name { get return name; } Properties public int Points { get { return points; } set if (value > 0) points = value; } public string Name { get return name; }

Override of the toString method public override string ToString( ) { return name + " (" + points + ")"; }

Arrays Trophy [] troph; … troph = new Trophy[10]; or Trophy [] troph = new Trophy[10]; can't use until memory is allocated with new.

Array example 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];

Traversing an array 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 }

program sample 1

Enumerated types public enum <typeName>{constant1, constant2, … , constantN}; public enum PowerType {Strength, Fly, Speed, MindControl, BugVision}; enum allows us to define a new "type", but this is actually just a place holder for integers.

program sample 2

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.

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.

finding the 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; 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.

finding a sum and or average private static float FindAverage (int [] B) { int sum = 0; for (int i = 0; i < 10; i++) { sum += B[i]; } return sum / 10f;

program sample 3