CSC 1051 M.A. Papalaskari, Villanova University Everyday objects: Strings and Wrappers CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari.

Slides:



Advertisements
Similar presentations
Chapter 3 Using Classes and Objects. © 2004 Pearson Addison-Wesley. All rights reserved3-2 Using Classes and Objects We can create more interesting programs.
Advertisements

Java Software Solutions
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)
Chapter 3 Using Classes and Objects. © 2004 Pearson Addison-Wesley. All rights reserved3-2 Using Classes and Objects We can create more interesting programs.
Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Copyright © 2012 Pearson Education, Inc. Chapter 3 Using Classes and Objects Java Software Solutions Foundations of Program Design Seventh Edition John.
Chapter 3 Using Classes and Objects 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
Reference … and Misc Other Topics Clark Savage Turner, J.D., Ph.D. Some lecture slides have been adapted from those developed.
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.
Static Class Members Wrapper Classes Autoboxing Unboxing.
COMP 110 Introduction to Programming Mr. Joshua Stough September 10, 2007.
References, Aliases, Garbage Collection and Packages Packages and Importing Classes Reading for this Lecture: L&L, Familiarize yourself with.
CSC 1051 M.A. Papalaskari, Villanova University Repetition CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing.
Class Library, Formatting, Wrapper Classes, and JUnit Testing
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Copyright © 2012 Pearson Education, Inc. Chapter 3 Using Classes and Objects Java Software Solutions Foundations of Program Design Seventh Edition John.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Copyright © 2012 Pearson Education, Inc. Chapter 3 Using Classes and Objects Java Software Solutions Foundations of Program Design Seventh Edition John.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
Programming in Java (COP 2250) Lecture 8 Chengyong Yang Fall, 2005.
CSC 1051 M.A. Papalaskari, Villanova University Conditional Statements Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 M.A. Papalaskari, Villanova University CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 3 Using Classes and Objects 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
1 Enhancing Classes  Now we can explore various aspects of classes and objects in more detail  Chapter 5 focuses on: object references and aliases passing.
Using Classes and Objects. We can create more interesting programs using predefined classes and related objects Chapter 3 focuses on: Object creation.
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.
© 2004 Pearson Addison-Wesley. All rights reserved September 7, 2007 Formatting Output & Enumerated Types & Wrapper Classes ComS 207: Programming I (in.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSE 1201 Object Oriented Programming Using Classes and Objects.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
SEEM Java – Basic Introduction, Classes and Objects.
Chapter 3 Using Classes and Objects 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
Outline Creating Objects The String Class Packages Formatting Output Enumerated Types Wrapper Classes Components and Containers Images.
Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Chapter 3: Using Classes and Objects Coming up: Creating Objects.
3-1 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.
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.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Wrapper Classes Use wrapper objects in Collections when you can’t use primitive types Primitive TypeWrapper Class byteByte shortShort intInteger longLong.
1 CMPT 126 Java Basics Using Classes and Objects.
© 2004 Pearson Addison-Wesley. All rights reserved January 23, 2006 Creating Objects & String Class ComS 207: Programming I (in Java) Iowa State University,
Using Classes and Objects We can create more interesting programs using predefined classes and related objects Chapter 3 focuses on: object creation and.
© 2004 Pearson Addison-Wesley. All rights reserved September 5, 2007 Packages & Random and Math Classes ComS 207: Programming I (in Java) Iowa State University,
© 2004 Pearson Addison-Wesley. All rights reserved January 27, 2006 Formatting Output & Enumerated Types & Wrapper Classes ComS 207: Programming I (in.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 5: Enhancing Classes
Variables in Java A variable holds either
Formatting Output & Enumerated Types & Wrapper Classes
Using Classes and Objects
CSC 1051 – Data Structures and Algorithms I
Creating Objects & String Class
Wrapper Classes The java.lang package contains wrapper classes that correspond to each primitive type: Primitive Type Wrapper Class byte Byte short Short.
Using Classes and Objects
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.
Packages & Random and Math Classes
Outline Creating Objects The String Class The Random and Math Classes
Presentation transcript:

CSC 1051 M.A. Papalaskari, Villanova University Everyday objects: Strings and Wrappers CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: Some slides in this presentation are adapted from the slides accompanying Java Software Solutions by Lewis & Loftus

Overview Review what we know about objects: –classes –methods –object creation String class –useful methods and examples Wrapper classes CSC 1051 M.A. Papalaskari, Villanova University

Some everyday Objects… Strings - defined by the String class: "This is a string literal." "123 Main Street" "X” System.out is also an object - it represents a destination (the monitor screen) to which we can send output CSC 1051 M.A. Papalaskari, Villanova University

Methods Objects can have methods associated with them In Lincoln.java we invoked the println method System.out.println ("Whatever you are, be a good one."); object method name information provided to the method (parameters) CSC 1051 M.A. Papalaskari, Villanova University

Invoking Methods We use the dot operator to invoke an object’s methods CSC 1051 M.A. Papalaskari, Villanova University int numOfCharsInName = name. length(); length() is one of the methods of String objects (defined in String class) String name = scan. nextLine(); nextLine() is one of the methods of Scanner objects (defined in Scanner class) B B y y s s t t e e

More String Methods int numOfCharsInName = name. length(); char initial = name. charAt(0); B B y y s s t t e e String newName = name. replace('s', 't'); String capsName = name. toUpperCase(); String nickName = name. substring(1, 4); name newName capsName nickName See also textbook example StringMutation.java

Palindrome tester Input a string, determine whether it is a palindrome, i.e.: –first char is the same as last char –2nd char is the same as 2nd last char –and so on… How to express this as an algorithm? How to implement it? CSC 1051 M.A. Papalaskari, Villanova University R R R R A A D D A A str

CSC 1051 M.A. Papalaskari, Villanova University System.out.println ("Enter a potential palindrome:"); str = scan.nextLine(); left = 0; right = str.length() - 1; while (str.charAt(left) == str.charAt(right) && left < right) { left++; right--; } if (left < right) System.out.println ("NOT a palindrome"); else System.out.println ("palindrome"); PalindromeTester.javaPalindromeTester.java (Example from Chapter 5)

CSC 1051 M.A. Papalaskari, Villanova University System.out.println ("Enter a potential palindrome:"); str = scan.nextLine(); left = 0; right = str.length() - 1; while (str.charAt(left) == str.charAt(right) && left < right) { left++; right--; } if (left < right) System.out.println ("NOT a palindrome"); else System.out.println ("palindrome"); PalindromeTester.javaPalindromeTester.java (Example from Chapter 5) Sample Run Enter a potential palindrome: radar palindrome Test another palindrome (y/n)? y Enter a potential palindrome: able was I ere I saw elba palindrome. Test another palindrome (y/n)? y Enter a potential palindrome: abracadabra NOT a palindrome. Test another palindrome (y/n)? n

Declaring Variables, revisited Examples of variable declarations: int count = 0; double mpg; String title; Graphics page; Color aquamarine; Scanner scan; A class name can be used as a type to declare an object reference variable The object itself must be created separately CSC 1051 M.A. Papalaskari, Villanova University

Creating Objects We have already seen something like this: Scanner scan = new Scanner (System.in); The new operator calls the Scanner constructor, which is a special method that sets up the object CSC 1051 M.A. Papalaskari, Villanova University Variable refers to a Scanner object Constructing a new object is called instantiation an instance of the Scanner class

Creating Objects Another example: The new operator calls the String constructor, which is a special method that sets up the object CSC 1051 M.A. Papalaskari, Villanova University Variable refers to a String object Constructing a new object is called instantiation an instance of the String class String title = new String ("Java Software Solutions");

The String Class is SPECIAL! Exception to the use of new operator: Because strings are so common, we don't have to use the new operator to create a String object This is special syntax that works only for strings CSC 1051 M.A. Papalaskari, Villanova University String title = new String ("Java Software Solutions"); String title = "Java Software Solutions";

Wrapper Classes The java.lang package contains wrapper classes that correspond to each primitive type: Primitive TypeWrapper Class byteByte shortShort intInteger longLong floatFloat doubleDouble charCharacter booleanBoolean

Wrapper Classes The following declaration creates an Integer object which represents the integer 40 as an object CSC 1051 M.A. Papalaskari, Villanova University Integer age = new Integer(25); age

Wrapper Class methods and constants Integer.parseInt(): convert String to int Double.parseDouble(): convert String to double Integer. MIN_VALUE: smallest int value Integer. MAX_VALUE : largest int value Examples: CSC 1051 M.A. Papalaskari, Villanova University String str = scan.nextLine(); int num = Integer.parseInt(str); int currentMin = Integer.MAX_VALUE;

Autoboxing Autoboxing is the automatic conversion of a primitive value to a corresponding wrapper object: Integer obj; int num = 42; obj = num; The assignment creates the appropriate Integer object The reverse conversion (called unboxing) also occurs automatically as needed CSC 1051 M.A. Papalaskari, Villanova University

Quick Check CSC 1051 M.A. Papalaskari, Villanova University Are the following assignments valid? Double value = 15.75; Character ch = new Character('T'); char myChar = ch;

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 CSC 1051 M.A. Papalaskari, Villanova University

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 38 After: CSC 1051 M.A. Papalaskari, Villanova University int num1 = 38; int num2 = 96;

Reference Assignment For objects, the same is true, but what is copied is the reference to the object (i.e., its address): For objects: num1 num2 After: 38 CSC 1051 M.A. Papalaskari, Villanova University num1 num2 Before: num2 = num1; Integer num1 = 38; Integet num2 = 96;

Another example For object references, assignment copies the address: name2 = name1; name1 name2 Before: "Steve Jobs" "Steve Wozniak" name1 name2 After: "Steve Jobs" CSC 1051 M.A. Papalaskari, Villanova University

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 CSC 1051 M.A. Papalaskari, Villanova University

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 CSC 1051 M.A. Papalaskari, Villanova University