news recap classes methods objects recap declare vs
play

News Recap: Classes, Methods, Objects Recap: Declare vs. Construct - PowerPoint PPT Presentation

University of British Columbia News Recap: Classes, Methods, Objects Recap: Declare vs. Construct Object CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Midterm location announced: FSC 1005 Class: complex data type public static


  1. University of British Columbia News Recap: Classes, Methods, Objects Recap: Declare vs. Construct Object CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 ■ Midterm location announced: FSC 1005 ■ Class: complex data type public static void main (String[] args) { ■ for both Feb 28 and Mar 22 midterms ■ includes both data and operations String firstname; Tamara Munzner firstname = new String (“Kermit"); ■ Assignment 1 out ■ programmers can define new classes } ■ many predefined classes in libraries ■ due Wed 3 Feb at 5pm, by electronic handin ■ Method: operations defined within class ■ Variable declaration does not create object Objects, Input ■ internal details hidden, you only know result ■ creates object reference ■ Constructor and new operator creates object somewhere in ■ Object: instance of class Lecture 7, Wed Jan 20 2010 memory ■ entity you can manipulate in your program ■ constructors can pass initial data to object ■ Assignment binds object reference to created object borrowing from slides by Kurt Eiselt ■ assigns address of object location to variable http://www.cs.ubc.ca/~tmm/courses/111-10 1 2 3 4 Recap: Declare vs. Construct Object Recap: Objects vs. Primitives Recap: Objects vs. Primitives Recap: API Documentation Frog object Frog object ■ references ■ references ■ Online Java library documentation at String object String object http://java.sun.com/javase/6/docs/api/ String object firstname ■ textbook alone is only part of the story Frog Frog String frogName String frogName famousFrog “Kermit” famousFrog “Kermit” ■ let’s take a look! “Kermit” boolean isMuppet boolean isMuppet ■ Everything we need to know: critical details true false Frog Frog ■ and often many things far beyond current need favoriteFrog int favoriteFrog int famousNum famousNum bind variable to 42 42 ■ Classes in libraries are often referred to as expression on right side of assignment operator Application Programming Interfaces expression on right side int int favoriteNum favoriteNum ■ or just API of assignment operator ■ vs. direct storage ■ vs. direct storage 42 999 5 6 7 8 Recap: Some Available String Methods Recap: More String Methods Recap: Methods and Parameters Recap: Return Values public String toUpperCase(); public String replace(char oldChar, char newChar); ■ Methods are how objects are manipulated ■ Methods can have return values Returns a new String object identical to this object but with Returns a new String object where all instances of oldChar have been ■ pass information to methods with parameters ■ Example: charAt method result all the characters converted to upper case. changed into newChar . ■ inputs to method call ■ return value, the character 'n', is stored in public int length(); public String substring(int beginIndex); ■ tell charAt method which character in the String object we're thirdchar Returns the number of characters in this String object. Returns new String object starting from beginIndex position interested in ■ methods can have multiple parameters public boolean equals( String otherString ); public String substring( int beginIndex, int endIndex ); Returns true if this String object is the same as Returns new String object starting from beginIndex position and ending String firstname = "kangaroo"; ■ API specifies how many, and what type otherString and false otherwise. at endIndex position char thirdchar = firstname.charAt(2); ■ two types of parameters return value object method parameter public char charAt( int index ); ■ explicit parameters given between parens  up to but not including endIndex char: Returns the character at the given index. Note that the ■ Not all methods have return values first character in the string is at index 0. ■ implicit parameter is object itself substring(4, 7) “o K” ■ No return value indicated as void String firstname = "Alphonse"; H e l l o K e r m i t F r o g char thirdchar = firstname.charAt(2); 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 9 10 11 12 object method parameter Recap: Constructors and Parameters Classes, Continued Primitive Types vs. Classes Objects Belong to Classes ■ Many classes have more than one ■ A class has a name. Primitive Types Classes ■ Just as 1, 2, and 3 are all integers, you are all objects of the class UBCStudent! constructor, taking different parameters ■ A class should describe something intuitively Pre-defined in Java Written by other ■ You each have names, ID numbers, etc. meaningful. Why did someone create this class? programmers or by you ■ use API docs to pick which one to use based ■ Each is unique person, but all are students ■ A class describes the data stored inside objects in on what initial data you have Simplest things, e.g., int Can be arbitrarily complex ■ Social organizations example: the class. (Nouns) Operators: +, -, … Methods ■ Ballroom Dance Club ■ A class describes the legal operations that can be Values belong to types. Objects belong to classes ■ Ski Club done to the data. (Verbs) E.g., 3 is an int , 3.14159 E.g., you are a UBC ■ CSSS ■ Example in Book: java.awt.Rectangle is a double Student ■ Etc. ■ Sometimes called “instances” of a class. animal = new String(); animal = new String("kangaroo"); 13 14 15 16

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend