Creating Objects & String Class

Slides:



Advertisements
Similar presentations
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Advertisements

1 Objects, Classes, and Packages “Static” Classes Introduction to Classes Object Variables and Object References Instantiating Objects Using Methods in.
ECE122 L4: Creating Objects February 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 4 Creating and Using Objects.
Chapter Day 5. © 2007 Pearson Addison-Wesley. All rights reserved2-2 Agenda Day 5 Questions from last Class?? Problem set 1 Posted  Introduction on developing.
Chapter 3 Using Classes and Objects. Creating Objects A variable holds either a primitive type or a reference to an object A class name can be used as.
1 A Sorting Example (a start for Lab 4 problem number 3)
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
References, Aliases, Garbage Collection and Packages Packages and Importing Classes Reading for this Lecture: L&L, Familiarize yourself with.
Expressions, Data Conversion, and Input
Writing Classes (Chapter 4)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
Lecture 1 Introduction Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 4 Writing Classes Part 2. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Classes A class can contain data declarations and method declarations.
Java The Java programming language was created by Sun Microsystems, Inc. It was introduced in 1995 and it's popularity has grown quickly since A programming.
© 2004 Pearson Addison-Wesley. All rights reserved September 14, 2007 Anatomy of a Method ComS 207: Programming I (in Java) Iowa State University, FALL.
Using Classes and Objects. We can create more interesting programs using predefined classes and related objects Chapter 3 focuses on: Object creation.
CSC 1051 M.A. Papalaskari, Villanova University Everyday objects: Strings and Wrappers CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari.
Using Classes and Objects Chapters 3 Creating Objects – Section 3.1 The String Class – Section 3.2 The Scanner Class – Section 2.6 Instructor: Scott Kristjanson.
Mathematical Calculations in Java Mrs. C. Furman.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
CSE 1201 Object Oriented Programming Using Classes and Objects.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
SEEM Java – Basic Introduction, Classes and Objects.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Chapter 3: Using Classes and Objects Coming up: Creating Objects.
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.
© 2004 Pearson Addison-Wesley. All rights reserved November 12, 2007 Inheritance ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
© 2004 Pearson Addison-Wesley. All rights reserved January 23, 2006 Creating Objects & String Class ComS 207: Programming I (in Java) Iowa State University,
© 2004 Pearson Addison-Wesley. All rights reserved September 5, 2007 Packages & Random and Math Classes ComS 207: Programming I (in Java) Iowa State University,
Data and Expressions. Let's explore some other fundamental programming concepts Chapter 2 focuses on: Character Strings Primitive Data The Declaration.
© 2004 Pearson Addison-Wesley. All rights reserved3-1 Objects Declaration: String title;  title (object variable) of type String( Class )  title is just.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Chapter 5: Enhancing Classes
Variables in Java A variable holds either
Formatting Output & Enumerated Types & Wrapper Classes
Lecture 1 Introduction Richard Gesick.
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Data Conversion & Scanner Class
Multiple variables can be created in one declaration
Inheritance November 10, 2006 ComS 207: Programming I (in Java)
Inheritance April 7, 2006 ComS 207: Programming I (in Java)
Increment and Decrement
Lecture 3 Expressions Richard Gesick.
Ch 4: Writing Classes Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: Classes and Objects.
Chapter 4: Writing classes
With Assignment Operator
Chapter 4 Writing Classes.
Static Class Members March 29, 2006 ComS 207: Programming I (in Java)
Recursion (part 1) October 24, 2007 ComS 207: Programming I (in Java)
Creating Objects A variable holds either a primitive value or a reference to an object A class name can be used as a type to declare an object reference.
Data Types and Expressions
Packages & Random and Math Classes
Arrays October 6, 2006 ComS 207: Programming I (in Java)
Outline Creating Objects The String Class The Random and Math Classes
Data Types and Expressions
Data Types and Expressions
Presentation transcript:

Creating Objects & String Class September 1, 2006 ComS 207: Programming I (in Java) Iowa State University, FALL 2006 Instructor: Alexander Stoytchev © 2004 Pearson Addison-Wesley. All rights reserved

Quick review of last lecture © 2004 Pearson Addison-Wesley. All rights reserved

Assignment Revisited The right and left hand sides of an assignment statement can contain the same variable First, one is added to the original value of count count = count + 1; Then the result is stored back into count (overwriting the original value) © 2004 Pearson Addison-Wesley. All rights reserved

Increment and Decrement The increment and decrement operators use only one operand The increment operator (++) adds one to its operand The decrement operator (--) subtracts one from its operand The statement count++; is functionally equivalent to count = count + 1; © 2004 Pearson Addison-Wesley. All rights reserved

Increment and Decrement The increment and decrement operators can be applied in postfix form: count++ or prefix form: ++count When used as part of a larger expression, the two forms can have different effects Because of their subtleties, the increment and decrement operators should be used with care © 2004 Pearson Addison-Wesley. All rights reserved

Assignment Operators Often we perform an operation on a variable, and then store the result back into that variable Java provides assignment operators to simplify that process For example, the statement num += count; is equivalent to num = num + count; © 2004 Pearson Addison-Wesley. All rights reserved

Assignment Operators There are many assignment operators in Java, including the following: Operator += -= *= /= %= Example x += y x -= y x *= y x /= y x %= y Equivalent To x = x + y x = x - y x = x * y x = x / y x = x % y © 2004 Pearson Addison-Wesley. All rights reserved

Widening Conversions © 2004 Pearson Addison-Wesley. All rights reserved

Narrowing Conversions © 2004 Pearson Addison-Wesley. All rights reserved

Conversion Techniques 1) Assignment conversion Value of one type is assigned to a variable of another type during which the value is converted to the new type. 2) Promotion Occurs automatically when certain operators need to modify their operands. 3) Casting (a.k.a. type casting) Specified explicitly by the programmer © 2004 Pearson Addison-Wesley. All rights reserved

Assignment conversion float money; int dollars; dollars=5; money = dollars; // OK, money is now equal to 5.0 dollars= money; //Compile error © 2004 Pearson Addison-Wesley. All rights reserved

(automatic) promotion float sum, result; int count; sum= 12.0; count=5; result = sum/count; // count promoted to float // before the division © 2004 Pearson Addison-Wesley. All rights reserved

(automatic) promotion // the number ‘5’ is first promoted to a string and then // the two strings are concatenated System.out.printlf(“Five is equal to ” + 5); © 2004 Pearson Addison-Wesley. All rights reserved

Type Casting float money; int dollars; dollars=5; money = dollars; // OK, money is now equal to 5.0 dollars= (int) money; //Compile error OK © 2004 Pearson Addison-Wesley. All rights reserved

Type Casting + Promotion float result; int total, count; total= 12; count=5; result = (float) total / count; // result = 2.4 // 1. total is cast to float // 2. count is promoted to float // 3. the division is performed © 2004 Pearson Addison-Wesley. All rights reserved

Type Casting + Promotion float result; int total, count; total= 12; count=5; result = (float) (total / count); // result = 2.0 // 1. total and count a divided using integer division // 2. the intermediary result is cast to a float // 3. this float value is assigned to result © 2004 Pearson Addison-Wesley. All rights reserved

Scanner Class © 2004 Pearson Addison-Wesley. All rights reserved

Scanner scan = new Scanner (System.in); answer = scan.nextLine(); Reading Input The following line creates a Scanner object that reads from the keyboard: Scanner scan = new Scanner (System.in); The new operator creates the Scanner object Once created, the Scanner object can be used to invoke various input methods, such as: answer = scan.nextLine(); In order to use the Scanner object you must put this line at the top of your Java program import java.util.Scanner; © 2004 Pearson Addison-Wesley. All rights reserved

Using Classes and Objects Chapter 3 Using Classes and Objects

Section 1.6 Sections 3.1 & 3.2

Problem Solving The purpose of writing a program is to solve a problem Solving a problem consists of multiple activities: Understand the problem Design a solution Consider alternatives and refine the solution Implement the solution Test the solution These activities are not purely linear – they overlap and interact © 2004 Pearson Addison-Wesley. All rights reserved

Problem Solving The key to designing a solution is breaking it down into manageable pieces When writing software, we design separate pieces that are responsible for certain parts of the solution An object-oriented approach lends itself to this kind of solution decomposition We will dissect our solutions into pieces called objects and classes © 2004 Pearson Addison-Wesley. All rights reserved

Object-Oriented Programming Java is an object-oriented programming language As the term implies, an object is a fundamental entity in a Java program Objects can be used effectively to represent real- world entities For instance, an object might represent a particular employee in a company Each employee object handles the processing and data management related to that employee © 2004 Pearson Addison-Wesley. All rights reserved

Objects An object has: state - descriptive characteristics behaviors - what it can do (or what can be done to it) The state of a bank account includes its account number and its current balance The behaviors associated with a bank account include the ability to make deposits and withdrawals Note that the behavior of an object might change its state © 2004 Pearson Addison-Wesley. All rights reserved

Classes An object is defined by a class A class is the blueprint of an object The class uses methods to define the behaviors of the object The class that contains the main method of a Java program represents the entire program A class represents a concept, and an object represents the embodiment of that concept Multiple objects can be created from the same class © 2004 Pearson Addison-Wesley. All rights reserved

Objects and Classes A class An object (the concept) (the realization) Bank Account A class (the concept) John’s Bank Account Balance: $5,257 An object (the realization) Bill’s Bank Account Balance: $1,245,069 Mary’s Bank Account Balance: $16,833 Multiple objects from the same class © 2004 Pearson Addison-Wesley. All rights reserved

Inheritance One class can be used to derive another via inheritance Classes can be organized into hierarchies Bank Account Account Charge Account Savings Account Checking Account © 2004 Pearson Addison-Wesley. All rights reserved

Classes A class can contain data declarations and method declarations int size, weight; char category; Data declarations Method declarations © 2004 Pearson Addison-Wesley. All rights reserved

Bank Account Example acct1 acctNumber 72354 balance 102.56 name “Ted Murphy” acct2 69713 acctNumber 40.00 balance name “Jane Smith” © 2004 Pearson Addison-Wesley. All rights reserved

Creating Objects A variable holds either a primitive type or a reference to an object A class name can be used as a type to declare an object reference variable String title; No object is created with this declaration An object reference variable holds the address of an object The object itself must be created separately © 2004 Pearson Addison-Wesley. All rights reserved

Creating Objects Generally, we use the new operator to create an object title = new String ("Java Software Solutions"); This calls the String constructor, which is a special method that sets up the object Creating an object is called instantiation An object is an instance of a particular class © 2004 Pearson Addison-Wesley. All rights reserved

Invoking Methods We've seen that once an object has been instantiated, we can use the dot operator to invoke its methods count = title.length() A method may return a value, which can be used in an assignment or expression A method invocation can be thought of as asking an object to perform a service © 2004 Pearson Addison-Wesley. All rights reserved

References Note that a primitive variable contains the value itself, but an object variable contains the address of the object An object reference can be thought of as a pointer to the location of the object Rather than dealing with arbitrary addresses, we often depict a reference graphically "Steve Jobs" name1 num1 38 © 2004 Pearson Addison-Wesley. All rights reserved

Assignment Revisited The act of assignment takes a copy of a value and stores it in a variable For primitive types: num1 38 num2 96 Before: num2 = num1; num1 38 num2 After: © 2004 Pearson Addison-Wesley. All rights reserved

Reference Assignment For object references, assignment copies the address: name1 name2 Before: "Steve Jobs" "Steve Wozniak" name2 = name1; name1 name2 After: "Steve Jobs" © 2004 Pearson Addison-Wesley. All rights reserved

Aliases Two or more references that refer to the same object are called aliases of each other That creates an interesting situation: one object can be accessed using multiple reference variables Aliases can be useful, but should be managed carefully Changing an object through one reference changes it for all of its aliases, because there is really only one object © 2004 Pearson Addison-Wesley. All rights reserved

Garbage Collection When an object no longer has any valid references to it, it can no longer be accessed by the program The object is useless, and therefore is called garbage Java performs automatic garbage collection periodically, returning an object's memory to the system for future use In other languages, the programmer is responsible for performing garbage collection © 2004 Pearson Addison-Wesley. All rights reserved

Bank Example Code © 2004 Pearson Addison-Wesley. All rights reserved

THE END © 2004 Pearson Addison-Wesley. All rights reserved