Objects.

Slides:



Advertisements
Similar presentations
4-1 Chapter 4 (a) Defining Classes. 4-2 The Contents of a Class Definition A class definition specifies the data items and methods that all of its objects.
Advertisements

Kernighan/Ritchie: Kelley/Pohl:
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
Introduction to Computers and Programming Strings Professor: Evan Korth New York University.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.3 The Class String.
Chapter 2 types, variables, methods Pages Horstmann.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
1 Using Objects Chapter 3 Fall 2005 CS 101 Aaron Bloomfield.
1 BUILDING JAVA PROGRAMS CHAPTER 3 THE SCANNER CLASS AND USER INPUT.
CS0007: Introduction to Computer Programming Scope, Comments, Code Style, Keyboard Input.
Week 2 - Friday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
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.
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 4 – Fundamental Data Types. Chapter Goals To understand integer and floating-point numbers To understand integer and floating-point numbers To.
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.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) public static void main(String[]
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.
1 Even even more on being classy Aaron Bloomfield CS 101-E Chapter 4+
Introduction to Programming
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.
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.
Strings and I/O. Lotsa String Stuff… There are close to 50 methods defined in the String class. We will introduce several of them here: charAt, substring,
COP 2551 Introduction to Object Oriented Programming with Java Topics –Introduction to the Java language –Code Commenting –Java Program Structure –Identifiers.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
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.
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.
COMP Review of Chapter 1 & 2
Chapter 3 Spring 2005 CS 101 Aaron Bloomfield
Chapter 2 Clarifications
Chapter 7 User-Defined Methods.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 7 User-Defined Methods.
Elementary Programming
Chapter 2 Elementary Programming
Yanal Alahmad Java Workshop Yanal Alahmad
Java basics – part 3.
Data types, Expressions and assignment, Input from User
Java Programming: From Problem Analysis to Program Design, 4e
Scope, Comments, Code Style, Keyboard Input
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 2: Basic Elements of Java
Introduction to Computer Science and Object-Oriented Programming
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.
Announcements & Review
Objects.
Review for Midterm 3.
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.

Some other Java object types Scanner 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; Scanner inStream; What is the representation?

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

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

Uninitialized versus null Consider String fontName = null; Scanner 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 Scanner stdin = new Scanner(System.in); System.out.print("Enter your account name: "); String response = stdin.nextLine();

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

Using objects Consider Scanner stdin = new Scanner(System.in); System.out.print("Enter your account name: "); String response = stdin.nextLine(); 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) { Scanner stdin = new Scanner(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";