Objects.

Slides:



Advertisements
Similar presentations
Kernighan/Ritchie: Kelley/Pohl:
Advertisements

1 More on Decisions Overview l The selection Operator l Grouping of Statements l Multiple branches (if else if) l Switch Statement.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
SCOPE & I/O CSC 171 FALL 2004 LECTURE 5. CSC171 Room Change Thursday, September 23. CSB 209 THERE WILL BE A (group) QUIZ! - topic: the CS department at.
1 Methods Overview l Closer Look at Methods l Parameter passing l Passing parameters by value l Passing parameters by reference.
1 Memory Model of A Program, Methods Overview l Closer Look at Methods l Memory Model of JVM »Method Area »Heap »Stack l Preview: Parameter Passing.
Introduction to Computers and Programming Strings Professor: Evan Korth New York University.
1 Methods Overview l Closer Look at Methods l Parameter passing l Passing parameters by value l Passing parameters by reference.
Programming in Java; Instructor:Alok Mehta Objects, Classes, Program Constructs1 Programming in Java Objects, Classes, Program Constructs.
1 Introduction to Console Input  Primitive Type Wrapper Classes  Converting Strings to Numbers  System.in Stream  Wrapping System.in in a Buffered.
CS 225 Java Review. Java Applications A java application consists of one or more classes –Each class is in a separate file –Use the main class to start.
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.
Chapter 3b Standard Input and Output Sample Development.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.3 The Class String.
Chapter 2 types, variables, methods Pages Horstmann.
1 One-Dimensional Arrays  What are and Why 1-D arrays?  1-D Array Declaration  Accessing elements of a 1-D Array  Initializer List  Passing Array.
1 Using Objects Chapter 3 Fall 2005 CS 101 Aaron Bloomfield.
1 Course Lectures Available on line:
SOFTWARE TECHNOLOGY - I CONSTANTS VARIABLES DATA TYPES.
1 Java Console I/O Introduction. 2 Java I/O You may have noticed that all the I/O that we have done has been output The reasons –Java I/O is based on.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value.
Console Input. So far… All the inputs for our programs have been hard-coded in the main method or inputted using the dialog boxes of BlueJ It’s time to.
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
Streams Reading: 2 nd Ed: , rd Ed: 11.1, 19.1, 19.4
1 Using Objects Chapter 2 (part 2 of 2) Spring 2007 CS 101 Aaron Bloomfield.
The basics of the array data structure. Storing information Computer programs (and humans) cannot operate without information. Example: The array data.
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.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Java basics. Task  Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943.
1 Using Objects Chapter 3 Fall 2006 CS 101 Aaron Bloomfield.
Introducing Objects and stuff GABY and MATT Definition: Object: “a class that is the root of the hierarchy tree for all classes in JAVA.” –An object.
Introduction to Programming
Java 1.5 The New Java Mike Orsega Central Carolina CC.
Objects. Getting classy Your current job –Gain experience creating and manipulating objects from the standard Java types Why –Prepares you for defining.
Department of Computer Engineering Using Objects Computer Programming for International Engineers.
1 Using Objects Chapter 3 Spring 2006 CS 101 Aaron Bloomfield.
CSI 3125, Preliminaries, page 1 Java I/O. CSI 3125, Preliminaries, page 2 Java I/O Java I/O (Input and Output) is used to process the input and produce.
Arrays. Background  Programmer often need the ability to represent a group of values as a list List may be one-dimensional or multidimensional  Java.
Exceptions. Exception  Abnormal event occurring during program execution  Examples Manipulate nonexistent files FileReader in = new FileReader("mumbers.txt“);
Java Input and Output. Java Input  Input is any information needed by your program to complete its execution  So far we have been using InputBox for.
Programming Principles Operators and Expressions.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Introduction to programming in java Lecture 21 Arrays – Part 1.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
IAS 1313: OBJECT ORIENTED PROGRAMMING Week 3: Data Type, Control Structure and Array Prepared by: Mrs Sivabalan1.
1 reading: 3.3 Using objects. Objects So far, we have seen: methods, which represent behavior variables, which represent data (categorized by types) It.
Chapter 8 Arrays and the ArrayList Class Arrays of Objects.
Array in C# Array in C# RIHS Arshad Khan
Chapter 3 Spring 2005 CS 101 Aaron Bloomfield
Chapter 7 User-Defined Methods.
Arrays in JAVA Visit for more Learning Resources.
Objects.
Elementary Programming
Math and String Examples
Yanal Alahmad Java Workshop Yanal Alahmad
Objects, Classes, Program Constructs
I/O Basics.
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2: Basic Elements of Java
Introduction to Java Programming
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
Chapter 3 Spring 2006 CS 101 Aaron Bloomfield
Arrays in Java.
Exceptions.
Announcements & Review
Exceptions.
More on iterations using
Presentation transcript:

Objects

Getting classy Your current job Gain experience creating and manipulating objects from the standard Java types Why Prepares you for defining your own classes and creating and manipulating the objects of those classes

Values versus objects Numbers Have values but they do not have behaviors Objects Have attributes and behaviors System.in References an InputStream Attribute: keyboard Behaviors: reading System.out References an OutputStream Attribute: monitor Behaviors: printing Java treats object variables differently. Although an object variable is the symbolic name for a memory location being used by the program, the memory location for an object variable does not store a value of that object type. Instead the value in the memory location tells the program where to find a value (object) of that type. We say that an object variable references or points to an object of the object type. Thus, a String variable references a memory location that holds a value of type String. Similarly, a BufferedReader variable references a memory location holding a BufferedReader value.

Other Java object types String Rectangle Color JFrame

Consider Statements int peasPerPod = 8; String message = "Don't look behind the door!“ How show we represent these definitions according to the notions of Java? Java treats object variables differently. Although an object variable is the symbolic name for a memory location being used by the program, the memory location for an object variable does not store a value of that object type. Instead the value in the memory location tells the program where to find a value (object) of that type. We say that an object variable references or points to an object of the object type. Thus, a String variable references a memory location that holds a value of type String. Similarly, a BufferedReader variable references a memory location holding a BufferedReader value.

Representation

Shorthand representation

Examples Consider String a = "excellence“; String b = a; What is the representation?

Examples Consider String a = "excellence“; String b = a; What is the representation?

Uninitialized versus null Consider String dayOfWeek; BufferedReader inStream; What is the representation?

Uninitialized versus null Consider String dayOfWeek; BufferedReader inStream; What is the representation?

Uninitialized versus null Consider String fontName = null; BufferedReader fileStream = null; What is the representation?

Uninitialized versus null Consider String fontName = null; BufferedReader fileStream = null; What is the representation?

Assignment Consider String word1 = "luminous"; String word2 = "graceful"; Word1 = word2; Initial representation

Assignment Consider String word1 = "luminous"; String word2 = "graceful"; Word1 = word2; After assignment

Using objects Consider BufferedReader stdin = new BufferedReader( new InputStreamReader(System.in)); System.out.print("Enter your account name: "); String response = stdin.readLine();

Using objects Consider BufferedReader stdin = new BufferedReader( new InputStreamReader(System.in)); System.out.print("Enter your account name: "); String response = stdin.readLine();

Using objects Consider BufferedReader stdin = new BufferedReader( new InputStreamReader(System.in)); System.out.print("Enter your account name: "); String response = stdin.readLine(); Suppose the user interaction is Enter your account name: artiste

String representation Consider String alphabet = "abcdefghijklmnopqrstuvwxyz"; Standard shorthand representation Truer representation Another useful String member method is charAt(). This method expects a single index as its parameter and returns the value of the character at that position in its String. While such behavior seems relatively natural given the method name and its parameter, what is not natural is that in Java, as in several other programming languages, the first character of a String is located at index 0.

String representation Consider String alphabet = "abcdefghijklmnopqrstuvwxyz"; char c1 = alphabet.charAt(9); char c2 = alphabet.charAt(15); char c3 = alphabet.charAt(2); What are the values of c1, c2, and c3? Why? Another useful String member method is charAt(). This method expects a single index as its parameter and returns the value of the character at that position in its String. While such behavior seems relatively natural given the method name and its parameter, what is not natural is that in Java, as in several other programming languages, the first character of a String is located at index 0.

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

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?

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.

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?

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

More String methods Consider int v1 = -12; double v2 = 3.14; char v3 = 'a'; String s1 = String.valueOf(v1); String s2 = String.valueOf(v2); String s3 = String.valueOf(v3);

Final variables Consider final String POEM_TITLE = “Appearance of Brown"; final String WARNING = “Weather ball is black"; What is the representation? In Chapter we used modifier final to define primitive type constants. We can also use this modifier to define object constants. However, when an object constant is defined—be it String or any other object type—we are specifying only that the reference cannot be changed. That is, the constant must always refer to the same memory location. Although an object constant must always refer to the same memory location, it does not mean necessarily that the value stored at that location cannot be modified through its member methods.

Final variables In Chapter we used modifier final to define primitive type constants. We can also use this modifier to define object constants. However, when an object constant is defined—be it String or any other object type—we are specifying only that the reference cannot be changed. That is, the constant must always refer to the same memory location. Although an object constant must always refer to the same memory location, it does not mean necessarily that the value stored at that location cannot be modified through its member methods.

Rectangle

Rectangle

Rectangle Consider final Rectangle BLOCK = new Rectangle(6, 9, 4, 2); BLOCK.setLocation(1, 4); BLOCK.resize(8, 3); In Chapter we used modifier final to define primitive type constants. We can also use this modifier to define object constants. However, when an object constant is defined—be it String or any other object type—we are specifying only that the reference cannot be changed. That is, the constant must always refer to the same memory location. Although an object constant must always refer to the same memory location, it does not mean necessarily that the value stored at that location cannot be modified through its member methods.

Rectangle Consider final Rectangle BLOCK = new Rectangle(6, 9, 4, 2); BLOCK.setLocation(1, 4); BLOCK.resize(8, 3); In Chapter we used modifier final to define primitive type constants. We can also use this modifier to define object constants. However, when an object constant is defined—be it String or any other object type—we are specifying only that the reference cannot be changed. That is, the constant must always refer to the same memory location. Although an object constant must always refer to the same memory location, it does not mean necessarily that the value stored at that location cannot be modified through its member methods.

Final variables Consider final String LANGUAGE = "Java";