CMSC 150 CLASSES CS 150: Mon 6 Feb 2012 Images: Library of Congress.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
The Line Class Suppose you are involved in the development of a large mathematical application, and this application needs an object to represent a Line.
Topic 10 Java Memory Management. 1-2 Memory Allocation in Java When a program is being executed, separate areas of memory are allocated for each class.
Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
Fields, Constructors, Methods
Written by: Dr. JJ Shepherd
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
IMPLEMENTING CLASSES Chapter 3. Black Box  Something that magically does its thing!  You know what it does but not how.  You really don’t care how.
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.
Chapter 3 Implementing Classes. Instance Variables Instance variables store the data of an object; the fields of an object. Instance of a class: an object.
Road Map Introduction to object oriented programming. Classes
Arrays Liang, Chpt 5. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
COMP More About Classes Yi Hong May 22, 2015.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Writing Classes (Chapter 4)
Recitation 2 Main Method, API & Packages, Java Basics.
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.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CSCI 1100/1202 April 1-3, Program Development The creation of software involves four basic activities: –establishing the requirements –creating.
Objects and Classes Mostafa Abdallah
CSC 1051 M.A. Papalaskari, Villanova University Everyday objects: Strings and Wrappers CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari.
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
SEEM Java – Basic Introduction, Classes and Objects.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
CMSC 150 METHODS AND CLASSES CS 150: Wed 25 Jan 2012.
CMSC 150 METHODS AND CLASSES CS 150: Wed 25 Jan 2012.
More about Java Classes Writing your own Java Classes More about constructors and creating objects.
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
Written by: Dr. JJ Shepherd
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
UNDERSTANDING CLASSES CMSC 150: Lecture 17. String Literals  "Wilco"  String literal: a sequence of characters flanked by "  Want to operate on the.
1 Class and Object Lecture 7. 2 Classes Classes are constructs that define objects of the same type. A Java class uses instance variables to define data.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
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
CSCI 51 Introduction to Programming Dr. Joshua Stough February 24, 2009.
CMSC 150 ARRAYS CS 150: Fri 10 Feb Motivation  Consider a list of your contact addresses: String Zero = String.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Object-Oriented Concepts
GC211 Data structure Lecture 3 Sara Alhajjam.
Java Memory Management
Lecture 3 John Woodward.
Chapter 7 User-Defined Methods.
Java Memory Management
Software Development Java Classes and Methods
Road Map Introduction to object oriented programming. Classes
Chapter 4: Writing Classes
Chapter 3 Introduction to Classes, Objects Methods and Strings
Interfaces and Constructors
Classes and Objects Object Creation
CMSC 202 Constructors Version 9/10.
Presentation transcript:

CMSC 150 CLASSES CS 150: Mon 6 Feb 2012 Images: Library of Congress

First: quick review of using methods  Remember types of methods:  Function: creates and “returns” a value int score = game.getScore(); System.out.println( “You get “ + game.numPointsForWin() ); int newScore = game.getScore() – game.numPointsPerMove(); Room playerRoom = game.getRoom( playerRow, playerCol );  Remember to either:  Save the returned value in a variable of the appropriate type, or  Use the returned value in an expression.

First: quick review of using methods  Remember types of methods:  Procedure: does work & returns no value (i.e., void return type) game.updateScore(newScore); game.updateMessage( “It’s the StayPuft Marshmallow Man!”); roomForSlimer.addSlimer();  Remember:  methods with void return type don’t create a value  illogical to use them in an assignment or expression

Anatomy of a method call  game.updateScore( newScore );  An object reference is always needed to call a method Object to call the method with.

Anatomy of a method call  game.updateScore( newScore );  An object reference is always needed to call a method Name of the method to call.

Anatomy of a method call  game.updateScore( newScore );  An object reference is always needed to call a method Parameters – input values for the method.

One Exception public class GhostBusters { … public boolean handleMove( ) { … boolean result = checkForLoss( ); } public boolean checkForLoss( ) { … } }  EXCEPT: if the method definition and the method call are in the same class, the object reference is “implicit”  Supplied automatically by Java

One other exception  Static methods are a little different int jenny = Integer.parseInt(“ ”); This is the name of a class, not a reference to an object.

What is a Class?  A “blueprint” of your entity to be implemented  Defines and encapsulates your entity’s  Characteristics (data)  Behaviors (methods)  Example:

Example: Dog  What characteristics do dogs have in general?   Your class’s data (instance variables)  What behaviors do dogs exhibit?   Your class’s methods  But do these identify a specific dog?

Example: Dog  Do these identify a specific dog?

Example: Dog

 Class: Dog  Characteristics: breed, eye color, weight  Behaviors: fetch(), lick(), sit(), stay(), poop()  Science Lab Lilly: an instance (object) of class Dog  chocolate lab, golden eyes, 65 lbs.  can perform Dog behaviors  Darth Bailey: an instance (object) of class Dog  black lab, brown eyes, 50 lbs.  can perform Dog behaviors

In Summary  Write the class “blueprint” once  Create a specific object of that class  Create another object of that class  And another…  Consider a familiar example

Write Our Own String Class public class SimpleString { // instance variables (data) // methods }  Notice no main() method  Only when you want to directly execute that class

Writing Your Own String Class public class SimpleString { // instance variables (data) private char myFirstCharacter; private char mySecondCharacter; private int myLength; // methods }

Methods  Constructor  must have same name as class  syntax: public ClassName( parameters… )  creates the object of this class type SimpleString str = new SimpleString(‘Z’,’a’);  no return type in method definition (implicit)  All other methods

Writing Your Own String Class public class SimpleString { // instance variables (data) private char myFirstCharacter; private char mySecondCharacter; private int myLength; // constructor public SimpleString( char charOne, char charTwo ) { myFirstCharacter = charOne; mySecondCharacter = charTwo; myLength = 2; }

In Another Class… public class Tester { public static void main( String[] args ) { SimpleString str1 = new SimpleString( ‘O’, ‘y’ ); SimpleString str2 = new SimpleString( ‘Z’, ‘a’ ); }

public class SimpleString { private char myFirstChar; private char mySecondChar; private int myLength; public SimpleString(char char1, char char2) { myFirstChar = char1; mySecondChar = char2; myLength = 2; } public class Tester { public static void main( String[] args ) { SimpleString str1 = new SimpleString(‘O’,’y’); SimpleString str2 = new SimpleString(‘Z’,’a’); } SimpleString.java Tester.java

public class Tester { public static void main( String[] args ) { SimpleString str1 = new SimpleString(‘O’,’y’); SimpleString str2 = new SimpleString(‘Z’,’a’); } SimpleString.java Tester.java Variables are of the class type SimpleString public class SimpleString { private char myFirstChar; private char mySecondChar; private int myLength; public SimpleString(char char1, char char2) { myFirstChar = char1; mySecondChar = char2; myLength = 2; }

public class Tester { public static void main( String[] args ) { SimpleString str1 = new SimpleString(‘O’,’y’); SimpleString str2 = new SimpleString(‘Z’,’a’); } SimpleString.java Tester.java Objects will be constructed using the constructor from that class public class SimpleString { private char myFirstChar; private char mySecondChar; private int myLength; public SimpleString(char char1, char char2) { myFirstChar = char1; mySecondChar = char2; myLength = 2; }

The Two Programs Together  SimpleString.java:  defines blueprint for a generic "SimpleString" object  we do not directly run this  Tester.java:  we run this (because it contains main() )  uses SimpleString as a variable type  creates objects of the class SimpleString  Let's see what happens in memory as we run Tester…

public class Tester { public static void main( String[] args ) { SimpleString str1 = new SimpleString(‘O’,’y’); SimpleString str2 = new SimpleString(‘Z’,’a’); } SimpleString.java Tester.java str1 declare… public class SimpleString { private char myFirstChar; private char mySecondChar; private int myLength; public SimpleString(char char1, char char2) { myFirstChar = char1; mySecondChar = char2; myLength = 2; }

public class Tester { public static void main( String[] args ) { SimpleString str1 = new SimpleString(‘O’,’y’); SimpleString str2 = new SimpleString(‘Z’,’a’); } SimpleString.java Tester.java ‘O’ ‘y’ character literals stored elsewhere automatically public class SimpleString { private char myFirstChar; private char mySecondChar; private int myLength; public SimpleString(char char1, char char2) { myFirstChar = char1; mySecondChar = char2; myLength = 2; } str

public class Tester { public static void main( String[] args ) { SimpleString str1 = new SimpleString(‘O’,’y’); SimpleString str2 = new SimpleString(‘Z’,’a’); } SimpleString.java Tester.java reserve enough space for a SimpleString object… public class SimpleString { private char myFirstChar; private char mySecondChar; private int myLength; public SimpleString(char char1, char char2) { myFirstChar = char1; mySecondChar = char2; myLength = 2; } ‘O’ ‘y’ str

public class SimpleString { private char myFirstChar; private char mySecondChar; private int myLength; public SimpleString(char char1, char char2) { myFirstChar = char1; mySecondChar = char2; myLength = 2; } public class Tester { public static void main( String[] args ) { SimpleString str1 = new SimpleString(‘O’,’y’); SimpleString str2 = new SimpleString(‘Z’,’a’); } SimpleString.java Tester.java reserve enough space for a SimpleString object… mySecondChar myLength DATA METHOD STUFF myFirstChar ‘O’ ‘y’ str

public class SimpleString { private char myFirstChar; private char mySecondChar; private int myLength; public SimpleString(char char1, char char2) { myFirstChar = char1; mySecondChar = char2; myLength = 2; } public class Tester { public static void main( String[] args ) { SimpleString str1 = new SimpleString(‘O’,’y’); SimpleString str2 = new SimpleString(‘Z’,’a’); } SimpleString.java Tester.java mySecondChar myLength METHOD STUFF myFirstChar ‘O’ ‘y’ str invoke the constructor…

public class SimpleString { private char myFirstChar; private char mySecondChar; private int myLength; public SimpleString(char char1, char char2) { myFirstChar = char1; mySecondChar = char2; myLength = 2; } public class Tester { public static void main( String[] args ) { SimpleString str1 = new SimpleString(‘O’,’y’); SimpleString str2 = new SimpleString(‘Z’,’a’); } SimpleString.java Tester.java mySecondChar myLength METHOD STUFF myFirstChar ‘O’ ‘y’ str which reserves space for the parameters (like variables)… which reserves space for the parameters (like variables)… char1 char2

public class SimpleString { private char myFirstChar; private char mySecondChar; private int myLength; public SimpleString(char char1, char char2) { myFirstChar = char1; mySecondChar = char2; myLength = 2; } public class Tester { public static void main( String[] args ) { SimpleString str1 = new SimpleString(‘O’,’y’); SimpleString str2 = new SimpleString(‘Z’,’a’); } SimpleString.java Tester.java mySecondChar myLength METHOD STUFF myFirstChar ‘O’ ‘y’ str char1 char2 parameters arguments ‘O’

public class SimpleString { private char myFirstChar; private char mySecondChar; private int myLength; public SimpleString(char char1, char char2) { myFirstChar = char1; mySecondChar = char2; myLength = 2; } public class Tester { public static void main( String[] args ) { SimpleString str1 = new SimpleString(‘O’,’y’); SimpleString str2 = new SimpleString(‘Z’,’a’); } SimpleString.java Tester.java mySecondChar myLength METHOD STUFF myFirstChar ‘O’ ‘y’ str char1 char2 parameters arguments ‘O’ ‘y’

public class SimpleString { private char myFirstChar; private char mySecondChar; private int myLength; public SimpleString(char char1, char char2) { myFirstChar = char1; mySecondChar = char2; myLength = 2; } public class Tester { public static void main( String[] args ) { SimpleString str1 = new SimpleString(‘O’,’y’); SimpleString str2 = new SimpleString(‘Z’,’a’); } SimpleString.java Tester.java mySecondChar myLength METHOD STUFF myFirstChar ‘O’ ‘y’ str char1 char2 ‘O’ ‘y’ execute the first statement in the constructor… ‘O’

public class SimpleString { private char myFirstChar; private char mySecondChar; private int myLength; public SimpleString(char char1, char char2) { myFirstChar = char1; mySecondChar = char2; myLength = 2; } public class Tester { public static void main( String[] args ) { SimpleString str1 = new SimpleString(‘O’,’y’); SimpleString str2 = new SimpleString(‘Z’,’a’); } SimpleString.java Tester.java mySecondChar myLength METHOD STUFF myFirstChar ‘O’ ‘y’ str char1 char2 ‘O’ ‘y’ execute the next statement in the constructor… ‘O’ ‘y’

public class SimpleString { private char myFirstChar; private char mySecondChar; private int myLength; public SimpleString(char char1, char char2) { myFirstChar = char1; mySecondChar = char2; myLength = 2; } public class Tester { public static void main( String[] args ) { SimpleString str1 = new SimpleString(‘O’,’y’); SimpleString str2 = new SimpleString(‘Z’,’a’); } SimpleString.java Tester.java mySecondChar myLength METHOD STUFF myFirstChar ‘O’ ‘y’ str char1 char2 ‘O’ ‘y’ execute the next statement in the constructor… ‘O’ ‘y’ 2

public class SimpleString { private char myFirstChar; private char mySecondChar; private int myLength; public SimpleString(char char1, char char2) { myFirstChar = char1; mySecondChar = char2; myLength = 2; } public class Tester { public static void main( String[] args ) { SimpleString str1 = new SimpleString(‘O’,’y’); SimpleString str2 = new SimpleString(‘Z’,’a’); } SimpleString.java Tester.java mySecondChar myLength METHOD STUFF myFirstChar ‘O’ ‘y’ str as the constructor finishes, space for the parameters is reclaimed… ‘O’ ‘y’ 2

public class SimpleString { private char myFirstChar; private char mySecondChar; private int myLength; public SimpleString(char char1, char char2) { myFirstChar = char1; mySecondChar = char2; myLength = 2; } public class Tester { public static void main( String[] args ) { SimpleString str1 = new SimpleString(‘O’,’y’); SimpleString str2 = new SimpleString(‘Z’,’a’); } SimpleString.java Tester.java mySecondChar myLength METHOD STUFF myFirstChar ‘O’ ‘y’ str constructor call returns the memory address of the new object, which is assigned to str1… ‘O’ ‘y’ 2 130

public class SimpleString { private char myFirstChar; private char mySecondChar; private int myLength; public SimpleString(char char1, char char2) { myFirstChar = char1; mySecondChar = char2; myLength = 2; } public class Tester { public static void main( String[] args ) { SimpleString str1 = new SimpleString(‘O’,’y’); SimpleString str2 = new SimpleString(‘Z’,’a’); } SimpleString.java Tester.java mySecondChar myLength METHOD STUFF myFirstChar ‘O’ ‘y’ str str1 variable now “references” the SimpleString object ‘O’ ‘y’ 2 130

public class SimpleString { private char myFirstChar; private char mySecondChar; private int myLength; public SimpleString(char char1, char char2) { myFirstChar = char1; mySecondChar = char2; myLength = 2; } public class Tester { public static void main( String[] args ) { SimpleString str1 = new SimpleString(‘O’,’y’); SimpleString str2 = new SimpleString(‘Z’,’a’); } SimpleString.java Tester.java mySecondChar myLength METHOD STUFF myFirstChar ‘O’ ‘y’ str ‘O’ ‘y’ object (instance) of class SimpleString variable of type SimpleString (containing reference) variable of type SimpleString (containing reference)

public class SimpleString { private char myFirstChar; private char mySecondChar; private int myLength; public SimpleString(char char1, char char2) { myFirstChar = char1; mySecondChar = char2; myLength = 2; } public class Tester { public static void main( String[] args ) { SimpleString str1 = new SimpleString(‘O’,’y’); SimpleString str2 = new SimpleString(‘Z’,’a’); } SimpleString.java Tester.java mySecondChar myLength METHOD STUFF myFirstChar str ‘Z’ ‘a’ next statement results in similar picture elsewhere in memory

public class MyString { // instance variables (data) private char myFirstCharacter; private char mySecondCharacter; private int myLength; // default constructor public SimpleString() { myFirstCharacter = ‘ ‘; mySecondCharacter = ‘ ‘; myLength = 0; } // constructor with two parameters public SimpleString( char charOne, char charTwo ) { myFirstCharacter = charOne; mySecondCharacter = charTwo; myLength = 2; }

Methods  Constructor  must have same name as class  syntax: public ClassName( arguments… )  creates the object of this class type MyString str = new MyString();  no return type (implicit)  All other methods  use any non-reserved name  two types: perform action, return a value

Methods  All other methods  use any non-reserved name  two types: perform action, return a value 1. perform an action: don't return anything public void doSomething() { … public void doSomethingElse() { … 2. return a value: provide a specific return type public int length() { … public String substring( int …

 All other methods  use any non-reserved name  two types: perform action, return a value 1. perform an action: don't return anything public void doSomething( … ) { … public void doSomethingElse( … ) { … 2. return a value: provide a specific return type public int length() { … public String substring( int … Methods

public class SimpleString { … // these methods do something without // needing to return any sort of value public void printTheString() { System.out.println( “” + myFirstCharacter + mySecondCharacter ); } public void clearTheString() { myFirstCharacter = ‘ ‘; mySecondCharacter = ‘ ‘; myLength = 0; }

return type of method public class SimpleString { … // these methods do something without // needing to return any sort of value public void printTheString() { System.out.println( “” + myFirstCharacter + mySecondCharacter ); } public void clearTheString() { myFirstCharacter = ‘ ‘; mySecondCharacter = ‘ ‘; myLength = 0; }

public class SimpleString { … // these methods do something without // needing to return any sort of value public void printTheString() { System.out.println( “” + myFirstCharacter + mySecondCharacter ); } public void clearTheString() { myFirstCharacter = ‘ ‘; mySecondCharacter = ‘ ‘; myLength = 0; } void: no return statement

public class SimpleString { … // these methods return some sort of info public int length() { return myLength; // return instance variable } public char charAt(int index) { char returnChar = ‘ ‘; if (index == 0) { returnChar = myFirstCharacter; } else if (index == 1) { returnChar = mySecondCharacter; } return returnChar; }

public class SimpleString { … // these methods return some sort of info public int length() { return myLength; // return instance variable } public char charAt(int index) { char returnChar = ‘ ‘; if (index == 0) { returnChar = myFirstCharacter; } else if (index == 1) { returnChar = mySecondCharacter; } return returnChar; } return type of method

public class SimpleString { … // these methods return some sort of info public int length() { return myLength; // return instance variable } public char charAt(int index) { char returnChar = ‘ ‘; if (index == 0) { returnChar = myFirstCharacter; } else if (index == 1) { returnChar = mySecondCharacter; } return returnChar; } return statement using a variable of type int return statement using a variable of type int return statement using a variable of type char return statement using a variable of type char