More on Classes Pepper With help from rs.html.

Slides:



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

Classes and Objects. What is Design? The parts of the software including – what information each part holds – what things each part can do – how the various.
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
CSCI 1100/ , 6.2, 6.4 April 12, 15, 17.
1 Objects, Classes, and Packages “Static” Classes Introduction to Classes Object Variables and Object References Instantiating Objects Using Methods in.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Using FangEngine The FangEngine is created by Brian C. Ladd & Jam Jenkins Presentation by Pepper With much credit to: Jenkins, Jam & Brian C. Ladd. Introductory.
Lecture 3. 2 Introduction Java is a true OO language -the underlying structure of all Java programs is classes. Everything must be encapsulated in a class.
OOP Languages: Java vs C++
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
CSE 333 – SECTION 4. Overview Pointers vs. references Const Classes, constructors, new, delete, etc. More operator overloading.
Announcements  If you need more review of Java…  I have lots of good resources – talk to me  Use “Additional Help” link on webpage  Weekly assignments.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
CSC172 Intro Pepper. Goals Reminder of what a class is How to create and use classes How to design classes How to think in terms of objects How to comment.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
CIT 590 Intro to Programming First lecture on Java.
Chapter 10 Classes and Objects: A Deeper Look Visual C# 2010 for Programmers © by Pearson Education, Inc. All Rights Reserved.
Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes.
CSSE501 Object-Oriented Development. Chapter 4: Classes and Methods  Chapters 4 and 5 present two sides of OOP: Chapter 4 discusses the static, compile.
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
Chapter 8 Objects and Classes Object Oriented programming Instructor: Dr. Essam H. Houssein.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
More Methods and Arrays Material from Chapters 5 to 7 that we didn’t cover in 1226.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
This recitation 1 An interesting point about A3: Using previous methods to avoid work in programming and debugging. How much time did you spend writing.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
1 Chapter 5: Defining Classes. 2 Basics of Classes An object is a member of a class type What is a class? Fields & Methods Types of variables: –Instance:
SEEM3460 Tutorial Java Keyword – static. static Variables class XY { static int x; // class variable int y; // instance variable } XY xy1 = new XY();
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
OBJECT ORIENTED PROGRAMMING. Design principles for organizing code into user-defined types Principles include: Encapsulation Inheritance Polymorphism.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
OOP Basics Classes & Methods (c) IDMS/SQL News
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
Arrays Pepper. What is an Array A box that holds many of the exact same type in mini-boxes A number points to the mini-box The number starts at 0 String.
Comp1004: Building Better Objects II Encapsulation and Constructors.
Memory Management in Java Mr. Gerb Computer Science 4.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
Topic: Classes and Objects
OOP: Encapsulation &Abstraction
Objects as a programming concept
Game Extras Pepper.
Intro To Classes Review
More on Classes & Arrays
CS240: Advanced Programming Concepts
Encapsulation.
CSC 113 Tutorial QUIZ I.
Object Oriented Programming (OOP) LAB # 5
Encapsulation and Constructors
Tonga Institute of Higher Education
Applying OO Concepts Using Java
Tonga Institute of Higher Education
Java Programming Language
ITE “A” GROUP 2 ENCAPSULATION.
Presentation transcript:

More on Classes Pepper With help from rs.html

 Refers to the instance of the class that is running the code right now  Static methods have no “this” because they run the code from the Class, itself  skey.html skey.html This inside the class code that has an instance

 First create a variable to hold an instance  MyClass x ;  Then fill it with an instance by calling constructor  x = new MyClass(1);  Access its public variables with :  x.myvar1  Access its methods:  x.myMethod(‘a’); Using Objects (such as in your main routine)

 Just as arrays passed to methods are really just passing a pointer to the array, objects pass only pointers.  When your main method calls mymethod(player1), it is updating the Player object player1 inside the main method.  Note: Strings act like primitives and pass real copies (not under the covers, but they act as if they do) Passing Objects

 Why? When other programmers use your class, you must not change anything public or you will break their code.  Ex: Fang – if we have a game using advance(), and Fang upgrades to insist on advance(int y), all our code breaks  How?  Public – everyone  Private – no one  Protected – extended can  Nothing - your package (your bluej panel) can  Method or Variable Encapsulating

 Static – just like method static – one per class  Ex: Class variable total # of bikes  static int numbBikes = 0; (defaulting to 0)  each bike instance points to that one variable # of bikes.  Can change if your program asks it to  numbBikes = numbBikes + 1;  Changes the total number for all the bike instances, not just the one you are accessing.  Without static – a separate value for each instance  Ex: speed, color of a bike  int speed = 0;  Constants  Make it unchangeable with the word final after static  Capitalize by convention  ex: maximum number of bikes:  static final int MAXBIKES = 15;  Cannot later have MAXBIKES = MAXBIKES+1;  Class Variable

 Arrays can hold objects  If a point has a x and y, an array of 3 points holds 3 point objects, each with their own x and y.  Point[] p = new Point[2]; // makes 2 player miniboxes  p[0] = new Point(1,2); // puts a point object into p[0]  p[1] = new Point(3,4); // puts a point object into p[1]  p[0].getDistance(p[1]); // asks p[0] for its distance from p[1]  See in debug Arrays holding objects

 Do these exercises, but your deck of cards should only have the face cards so you have less coding. JQKA  ndE/creating-questions.html ndE/creating-questions.html Exercises

 Do question #1 and Exercise 1 & 2. (Skip the garbage collection questions.)  ndE/objects-questions.html ndE/objects-questions.html Exercise 2

 Shortcut for loop through an array:  for (variable to hold value : array name)  Ex: int[] arr= {1,2,3,4}; int tot = 0; for (int x : arr) { tot = tot + x;} System.out.println(tot); More on Arrays – For Each