1 Using Objects Chapter 3 Spring 2006 CS 101 Aaron Bloomfield.

Slides:



Advertisements
Similar presentations
Today’s lecture Review of Chapter 1 Go over homework exercises for chapter 1.
Advertisements

1 Value vs. reference semantics. Recall: Value semantics value semantics: Behavior where variables are copied when assigned to each other or passed as.
CS324e - Elements of Graphics and Visualization A Little Java A Little Python.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Objects, Methods, Parameters, Input Lecture 5, Thu Jan
Introduction to Computer Programming Stringing Along – Using Character and String Data.
03 Data types1June Data types CE : Fundamental Programming Techniques.
Introduction to Computers and Programming Strings Professor: Evan Korth New York University.
Strings as objects Strings are objects. Each String is an instance of the class String They can be constructed thus: String s = new String("Hi mom!");
Using Objects CS 101-E Aaron Bloomfield. Announcements Midterm 1 is a week from this Wednesday Midterm 1 is a week from this Wednesday TESTS ARE IN CHM.
1 Using Objects Chapter 3 Fall 2005 CS 101 Aaron Bloomfield.
MSc IT Programming Methodology (2). MODULE TEAM Dr Aaron Kans Dr Sin Wee Lee.
Week 2 - Friday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapters 3-4: Using Objects.
Methods and You. Up to this point, I have covered many different data types with you. Variables can be considered the nouns of an English sentence. If.
Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value.
Chapter 8. About the Midterm Exam.. Exam on March 12 Monday (Tentatively) Review on March 7 Wednesday Cover from Chapter 6 Grades will be out before spring.
Objects and Classes; Strings. 2 Classes and objects class: A program entity that represents either 1.A program / module, or 2.A type of objects* –A class.
ArrayList, Multidimensional Arrays
1 Using Objects Chapter 2 (part 2 of 2) Spring 2007 CS 101 Aaron Bloomfield.
Week 2 - Wednesday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
CS107 References and Arrays By Chris Pable Spring 2009.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) public static void main(String[]
1 Using Objects Chapter 3 Fall 2006 CS 101 Aaron Bloomfield.
1 Even even more on being classy Aaron Bloomfield CS 101-E Chapter 4+
Programming Review. Java Class Structure All Java statements are part of a class public class ClassName { }
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Lecture 7: Whiles - Indefinite Loops Input and while loops Katie Coons CS 305J February 2, 2007.
Java 1.5 The New Java Mike Orsega Central Carolina CC.
CSC1401 Strings (text). Learning Goals Working with Strings as a data type (a class) Input and output of Strings String operations.
String and Scanner CS 21a: Introduction to Computing I First Semester,
VARIABLES Programmes work by manipulating data placed in memory. The data can be numbers, text, objects, pointers to other memory areas, and more besides.
CS 11 java track: lecture 2 This week: more on object-oriented programming (OOP) objects vs. primitive types creating new objects with new calling methods.
Objects. Getting classy Your current job –Gain experience creating and manipulating objects from the standard Java types Why –Prepares you for defining.
Vladimir Misic: Characters and Strings1Tuesday, 9:39 AM Characters and Strings.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Department of Computer Engineering Using Objects Computer Programming for International Engineers.
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING String and Scanner Objects.
1 Review for exam 1 CS 101 Aaron Bloomfield. 2 Today’s lecture An overview of the “review” sections of chapters 1-3 Stop me if you want me to go over.
CSC 142 F 1 CSC 142 References and Primitives. CSC 142 F 2 Review: references and primitives  Reference: the name of an object. The type of the object.
College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
A Introduction to Computing II Lecture 1: Java Review Fall Session 2000.
Java Scanner Class Keyboard Class. User Interaction So far when we created a program there was no human interaction Our programs just simply showed one.
Programming Fundamentals 2: Libraries/ F II Objectives – –utilize some useful Java libraries e.g. String, Scanner, HashMap, and Random.
Review for exam 1 CS 101 Aaron Bloomfield. 2 Today’s lecture An overview of the “review” sections of chapters 1- 3 and 5 Stop me if you want me to go.
Chapter 9 Introduction to Arrays Fundamentals of Java.
1 reading: 3.3 Using objects. Objects So far, we have seen: methods, which represent behavior variables, which represent data (categorized by types) It.
COMP Review of Chapter 1 & 2
Chapter 3 Spring 2005 CS 101 Aaron Bloomfield
Chapter 2 Clarifications
Objects.
Elementary Programming
Programming in Java Text Books :
Methods and Parameters
Review for Exam 1 Spring 2007 CS 101/CS 101-E.
CS 200 Primitives and Expressions
F II 6. Using Libraries Objectives
Chapter 3 Spring 2006 CS 101 Aaron Bloomfield
CS2011 Introduction to Programming I Strings
Peer Instruction 4 Control Loops.
Announcements & Review
CS Week 2 Jim Williams, PhD.
Objects.
What is a String? String s = "compsci"; s c o m p s i
More on iterations using
Presentation transcript:

1 Using Objects Chapter 3 Spring 2006 CS 101 Aaron Bloomfield

2 Variables vs. Types  The type is the recipe or template for how to create a variable Examples: int, double, char, boolean, etc.  There are only 8 primitive types There are only a few things you can do with a type:  Declare a variable int x;  Use it as a cast x = (int) 3.5; There is only one of each type  The variable is the actual instance of a type in memory It’s a spot in memory where you store a value You choose the name: width, x, thatThemThereValue, etc. You can have as may variables as you want – but only one type!  Like the difference between a recipe and a bunch of cookies

3 Values versus objects  Numbers Have values but they do not have behaviors  Objects Have attributes and behaviors

4 Classes vs. Objects  A class is a user-defined “thing” Examples: String, Scanner, Rectangle, etc.  We’ll start defining our own classes later this semester Classes are more complex than the primitive types A class is analogous to a type  It’s just more complex and user-defined There can be only one class of each name  An object is an instance of a class There is only one String class, but you can have 100 String objects An object is analogous to a variable  It just is a reference instead  A class is a “template” used for creating objects

5 Using objects  First, we create an object: Scanner stdin = new Scanner (System.in);  Most object creation lines look like this  Then we use the object stdin.nextInt(); stdin.nextDouble();  Note that we could have called the object foo, bar, or anything stdin is just what we chose to call it

6 Using Rectangle objects  Let’s create some Rectangle objects  Rectangle creation: Rectangle r = new Rectangle (10, 20);  Objects have attributes (or properties): System.out.println (r.length); System.out.println (r.width);  Objects have behaviors (or methods): r.grow (10, 20) r.isEmpty() r.setLocation (5,4)

7 Using String objects  Let’s create some String objects  String creation: String s = new String (“Hello world”);  Objects have attributes (or properties): But we can’t access them…  Objects have behaviors (or methods): s.substring(0,6) s.indexOf (“world”) s.toLowerCase()

8 The lowdown on objects  Objects are “things” that have properties (attributes) and behaviors (methods)  We first create one or more objects  We then manipulate their properties and call their methods

9 So why bother with objects?  Let’s say you want to do a lot of String manipulation  Once you create a String object, all the manipulation methods are contained therein Sun already wrote the methods for us  So we can use String objects instead of writing our own code to get the substring, indexOf, etc.

10 Visualizing objects  Class (type) name  Attributes (properties)  Methods (behaviors) + grow (int, int) : void + isEmpty ( ) : void + setLocation ( int, int ) : void + resize ( int, int ) : void +... Rectangle - width = 10 - height =

11 How well do we understand using objects?

12 Review  Variables of primitive types int, double, char, boolean, etc. Can assign a value to it Can read a value from it Can’t do much else!  Objects String, Rectangle, etc. Have many parts  Rectangle has width, length, etc. Like a complex type Have methods  String has length(), substring(), etc.

13 String methods  length(): returns the String’s length (duh!) String s = “hello world”; String t = “goodbye”; System.out.println (s.length()); System.out.println (t.length());  Prints 11 and 7  Note that calling s.length() is different than calling t.length()! Both return the length But of different Strings

14 More String methods  Consider String weddingDate = "August 21, 1976"; String month = weddingDate.substring(0, 6); System.out.println("Month is " + month + ".");  What is the output? Month is August.

15 More String methods  Consider String fruit = "banana"; String searchString = "an"; int n1 = fruit.indexOf(searchString, 0); int n2 = fruit.indexOf(searchString, n1 + 1); int n3 = fruit.indexOf(searchString, n2 + 1); System.out.println("First search: " + n1); System.out.println("Second search: " + n2); System.out.println("Third search: " + n3);  What is the output? First search: 1 Second search: 3 Third search: -1

16 These images are not animated…

17 Program WordLength.java public class WordLength { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); System.out.print("Enter a word: "); String word = stdin.next(); int wordLength = word.length(); System.out.println("Word " + word + " has length " + wordLength + "."); }

18 More String methods  trim() Returns the String without leading and trailing whitespace Whitespace is a space, tab, or return

19 Reading Javadocs  What is a Javadoc? Documentation of Java classes  Where are Sun’s Javadocs?