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

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

Embed Size (px)

Citation preview

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

Some Standard ClassesGoalsThe Object classThe String classWrapper classesThe Math classRandom Numbers

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

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

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.

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

The equals Methodpublic 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.

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

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.

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

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.

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

Null String• String s1; – Has no size and not initialized (Given a value)

• String s1 = null;– Same as above.

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

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

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

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

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

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.

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

String Comparison Notes• Don’t use == to compare strings. It compares

addresses.• Use equals or compareTo methods to

compare strings.

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

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

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

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.

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

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

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

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.

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

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.

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

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

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

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

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

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