Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Some Standard Classes Goals The Object class The String class Wrapper classes The Math class Random Numbers."— Presentation transcript:

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

2 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()

3 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 classname @ memory address – Example: SavingAccount@fea485c4 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.

4 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.

5 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.

6 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.

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

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

9 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

10 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.

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

12 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

13 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.

14 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

15 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.

16 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.

17 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

18 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);

19 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);


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

Similar presentations


Ads by Google