Some Standard Classes Goals The Object class The String class Wrapper classes The Math class Random Numbers.

Slides:



Advertisements
Similar presentations
ACM/JETT Workshop - August 4-5, Classes, Objects, Equality and Cloning.
Advertisements

Primitives, References and Wrappers Java has the following types defined as part of the java language; int float double byte char boolean long short These.
SUMMARY: abstract classes and interfaces 1 Make a class abstract so instances of it cannot be created. Make a method abstract so it must be overridden.
Chapter 3 DATA: TYPES, CLASSES, AND OBJECTS. Chapter 3 Data Abstraction Abstract data types allow you to work with data without concern for how the data.
Java Programming Strings Chapter 7.
Strings An extension of types A class that encompasses a character array and provides many useful behaviors Chapter 9 Strings are IMMUTABLE.
Chapter 10 Review. Write a method that returns true is s1 and s2 end with the same character; otherwise return false. Sample Answer: public boolean lastChar(String.
AP Computer Science TOPICS TO DISCUSS.equals() == instanceof operator compareTo Interfaces Abstract Classes.
Hip Hip Array! AP Computer Science. Remember Strings? Strings are an array of characters An array is a collection of variables all of the same type. Arrays.
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
©2004 Brooks/Cole Chapter 7 Strings and Characters.
C#: Data Types Based on slides by Joe Hummel. 2 UCN Technology: Computer Science Content: “.NET is designed around the CTS, or Common Type System.
Programming 2 CS112- Lab 2 Java
1 Java Object Model Part 2: the Object class. 2 Object class Superclass for all Java classes Any class without explicit extends clause is a direct subclass.
Inheritance and interfaces A class C1 is derived from class C2, then C1 is called subclass, and C2 is called superclass Superclass-parent, base class Subclass.
Comparing Objects in Java. The == operator When you define an object, for instance Person p = new Person("John", 23); we talk about p as if its value.
String Escape Sequences
Chapter 11 Abstract Classes and Interfaces 1. Abstract method New modifier for class and method: abstract An abstract method has no body Compare: abstract.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
BPJ444: Business Programming using Java Java basics Tim McKenna
String Class in Java java.lang Class String java.lang.Object java.lang.String java.lang.Object We do not have to import the String class since it comes.
Strings.
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 6, page 1 Sun Certified Java 1.4 Programmer Chapter 6 Notes Gary Lance
String Class. Objectives and Goals Identify 2 types of Strings: Literal & Symbolic. Learn about String constructors and commonly used methods Learn several.
Java 2 More Java Then Starbucks ;-). Important Terms Primitive Data – Basic, built-in values (characters & numbers) Data Type – Used to talk about values.
APCS Java AB 2004 Review of CS1 and CS2 Review for AP test #1 Sources: 2003 Workshop notes from Chris Nevison (Colgate University) AP Study Guide to go.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
The java.lang Package chapter 8 Java Certification Study Group February 2, 1998 Seth Ladd.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Strings Carol Yarbrough AP Computer Science Instructor Alabama School of Fine Arts.
Working with string In java Four classes are provided to work with String:1) String 2)String Buffer 3)String Tokenizer 4)String Builder Note: An object.
Math Class Part of the Java.lang package. This package is from object so you do not have to import the lang package. Static: Math Class is static.
1 Building Java Programs Chapter 7: Arrays These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
 Definition: Accessing child class methods through a parent object  Example: Child class overrides default parent class methods  Example: Child class.
SE-1010 Dr. Mark L. Hornick 1 Variables & Datatypes.
Chapter 7: Characters, Strings, and the StringBuilder.
Types in programming languages1 What are types, and why do we need them?
Copyright Curt Hill Variables What are they? Why do we need them?
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
Abstract Classes and Interfaces Chapter 9 CSCI 1302.
Strings Mr. Smith AP Computer Science A. What are Strings? Name some of the characteristics of strings: A string is a sequence of characters, such as.
Coding Bat: Ends in ly Given a string of even length, return a string made of the middle two chars, so the string "string" yields "ri". The string.
U n i v e r s i t y o f H a i l 1 ICS 202  2011 spring  Data Structures and Algorithms 
Strings and ArrayLists April 23, 2012 ASFA AP CS.
Chapter 3A Strings. Using Predefined Classes & Methods in a Program To use a method you must know: 1.Name of class containing method (Math) 2.Name of.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
The ArrayList Data Structure Standard Arrays at High Speed! More Safety, More Efficient, and Less Overhead!
Java String 1. String String is basically an object that represents sequence of char values. An array of characters works same as java string. For example:
ITK 168 – More Variables 10/13/05. Another example of using instance variables and constants  Go through SimpleBot  Modify SimpleBot to “teleport”
Chapter 5 Defining Classes II Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
 Variables can store data of different types, and different data types can do different things.  PHP supports the following data types:  String  Integer.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
The ArrayList Data Structure Standard Arrays at High Speed!
Lecture 5: java.lang.Math, java.lang.String and Characters Michael Hsu CSULA.
Introduction C# program is collection of classes Classes are collection of methods and some statements That statements contains tokens C# includes five.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
2 Arrays Array is a data structure that represents a collection of the same type of data. A "fixed length list".
The AP Java Subset Topics. A Topics Primitive Types int double boolean.
The Methods and What You Need to Know for the AP Exam
Java: Base Types All information has a type or class designation
Lecture 20: Wrapper Classes and More Loops
Java: Base Types All information has a type or class designation
Programming in Java Text Books :
Java Programming: From Problem Analysis to Program Design, 4e
String Objects & its Methods
Chapter 7: Strings and Characters
Object Oriented Programming
The Language Package.
Presentation transcript:

Some Standard Classes Goals The Object class The String class Wrapper classes The Math class Random Numbers

The Object Class The Universal Superclass – Every class automatically extends Object class. Methods in Object – There are many methods in Object class, all of them inherited by every other class. – Methods are NOT abstract and all of it’s methods have code. – The expectation is that Object method will be overridden when not suitable. – 2 methods in the Object class of AP Java Subset toString() equals()

The toString Method public String toString() – Returns a version of your object in String form – When you try to print using the Object toString(), you will see the memory address – Example: You will need to override the toString method by creating your own toString(). NOTE: Array objects are unusual in that they do not have a toString method. To print the elements of an array, the array must traversed and each element must explicitly be printed.

The equals Method public boolean equals(Object other) All classes inherit this method from the Object class. Returns true or false depending if objects are equal or not. Being equal means referencing the same memory slot. If you want the objects contents (fields etc) to be the same, then you will have to override the equals method. Notes: – The default implementation of equal (Object class) is equivalent to the == relational operator for objects. Return true if it is the same Address, false otherwise. – The operators, and so on, are not overloaded like the + symbol. To compare objects use compareTo method or equals method.

String Class String Objects – Sequence of characters Literal Strings – enclosed in double quotes “Yikes!” – Considered to be an object with all the methods of the String Class. Strings are immutable – meaning once created you can’t make changes to it, but you can reassign it to a new string.

Constructing String Objects Strings are unusual because they can be initialized like a primitive type: – String s1 = “Bob”; Most object instances use the new operator to be created. – String s1 = new String(“Bob”); Strings are immutable. They can be reassigned. String s1 = “Bob”; s1 = “Bobby”; //s1 gets reassigned to Bobby //and Bob get recycled into memory.

Null String String s1; – Has no size and not initialized (Given a value) String s1 = null; – Same as above.

Empty String String s1 = “”; String s1 = new String(); – Length  0

The Concatenation Operator Concatenation Operator “+” symbol. Joins two strings into one – Example: – s1 = “sun”; s2 = “shine”; – s3 = s1 + s2; //s3 will point to “sunshine” If trying to concatenate mixed data type (primitive), then the primitive data type is promoted up to a String. s3 = “Hello” + 5; //s3  Hello5 – 5 is promoted to String If trying to store a nonString value into a String will cause an error. – int x = 6, y = 3; – String s1 = x + y; // error

Comparison of String Objects Java is case sensitive String class implements the Comparable class and overrides the equals and compareTo methods. Methods compare strings in lexicographical order (dictionary order). Characters are compared according to their position in the ACSII chart. You need to remember Digits precedes all capital letters, which precedes all lowercase letters. “5” <“R”<“a” Two strings are compared as follows: Starts at the left end of each string and do a character-by-character comparison until you reach characters that are different. If one string is the same excepts that it terminates before the other it is less than the longer one.

String Comparison Notes Don’t use == to compare strings. It compares addresses. Use equals or compareTo methods to compare strings.

Other String Methods AP Subset – length() – substring(int startIndex) Returns a string starting at the startIndex to the end of string. Throws StringIndexOutOfBoundsException if int is out of range of String. – substring(int startIndex, int endIndex) Returns a string starting at the startIndex position to the endIndex but does not include endIndex. Throws StringIndexOutOfBoundsException if int is out of range of String. – int indexOf(String str) Returns -1 if not in string Throws StringIndexOutOfBoundsException if str is null

Wrapper Classes A wrapper class takes either an existing object or a value of primitive type, “wraps” or boxes it in an object, and provides a new set of methods for that type. – Construction of an object from a single value (mainly primitive data types) – Retrieval of the primitive value (unwrapping or unboxing from the wrapper object). AP you should know Integer & Double Wrapper class.

Integer Class AP EXAM – Integer(int value) Constructs an Integer object from an int (Boxing) – int compareTo(Object other) Returns 0, positve, or negative – int intValue() Returns the value of this Integer as an int (Unboxing) – Boolean equals(Object obj) Returns true if and only if this Integer has the same int value as obj. This method overrides equals in class Object. This method throws a ClassCastException if obj is not an Integer. – toString() Returns a String representing the value of this Integer

The Double Class The Double class wraps a value of type double in an object AP Exam – Double(double value) Constructs a Double object from a double. (Boxing) – double doubleValue() Returns the value of this Double as a double. (Unboxing) – int compareTo(Object other) Implements Comparable Interface. Returns 0, negative, and positive ClassCastException if not a Double. – boolean equals(Object obj) Overrides the Object equals method. ClassCastException if not a Double. – String toString() Returns a String representing the value of this Double.

Integer & Double Classes Integer and Double objects are immutable: There are no mutator methods in the classes. Integer, Double, and String all implements the Comparable class.

Math Class AP Math Class methods – static int abs(int x) Returns the absolute value of integer x. – static double abs(double x) Returns the absolute value of real number x. – static double pow(double base, double exp) Returns base exp. Assumes base > 0, or base = 0 and exp > 0, or base < 0 and exp is an integer. – static double sqrt(double x) Returns, x >= 0 – static double random() Returns a random number r, where 0.0 <= r <1.0

Math Class All methods and variables are static so there is no instances of the Math class. – Example: X = Math.pow(3,2); // no need to create an object //of the Math Class PI is Math Class constant – Example: circle = (Math.PI)* Math.pow(radius, 2);

Random Numbers Random Reals On AP Test you may have to create a range of random numbers; Like 6 to 24 (int)((High Num – Low Num + 1) * Math.random() + Low Num) Produce random integers from 0 to 99 – (int)(Math.random() * 100);