Sie sind auf Seite 1von 24

Java.

Lang
Class
The Math Class

Math Class
 Provides predefined constants and methods for
performing different mathematical operations.
Math Methods
public static void double abs(double e)
Returns the positive value of the parameter. Can also take in a float, or an integer or a
long integer as a parameter, in which case the return type is either a float or an integer
or a long integer, respectively.
2
The Math Class (cont…)

public static double random()


Returns a random positive value greater than or equal to 0.0 but less than 1.0.
public static double max(double a, double b)
Returns the larger value between two double values, a and b. Can also take in a float, or
an integer or a long integer as a parameter, in which case the return type is either a float
or an integer or a long integer, respectively.
public static double min(double a, double b)
Returns the smaller value between two double values, a and b. Can also take in a float,
or an integer or a long integer as a parameter, in which case the return type is either a
float or an integer or a long integer, respectively.
3
The Math Class (cont…)

public static double ceil(double a)


Returns the smallest integer that is greater than or equal to the specified parameter a.
public static double floor(double a)
Returns the largest integer that is lesser than or equal to the specified parameter a.
public static double exp(double a)
Returns the Euler’s number e raised to the power of the passed argument a.
public static double log(double a)
Returns the natural logarithm (base e) of a, then double value parameter.

4
The Math Class (cont…)

public static double pow(double a, double b)


Returns the double value of a raised to double value of b.
public static double round(double a)
Returns the nearest long to the given argument. Can also take in a float as an argument
and returns the nearest int in this case.
public static double sqrt(double a)
Returns the square root of the argument a.
public static double sin(double a)
Returns the trigonometric sine of the given angle a.
5
The Math Class (cont…)

public static double toDegrees(double angrad)


Returns the degree value approximately equivalent to the given radian value.
public static double toRadians(double angdeg)
Returns the radian value approximately equivalent to the given degree value.

6
The Math Class (cont…)

7
The Math Class (cont…)

8
The String Class

String Class
 Represents combination of character literals
 Using Java, strings can be represented using:
- Array of characters
Note: A String object is different from an array of characters!
 String constructor
- 11 constructors
9
The String Class (cont…)

10
The String Class (cont…)

11
The String Class (cont…)

String Methods
public char charAt(int index)
Returns the character located in the specified index.
public int compareTo(String anotherString)
Compares this String with the specified parameter. Returns a negative value if this
String comes lexicographically before other String, 0 if both of the Strings have the
same value and a positive value if this String comes after the other String
lexicographically.

12
The String Class (cont…)

public int compareToIgnoreCase(String str)


Like compareTo but ignores the case used in this String and the specified String.
public boolean equals(Object anoObject)
Returns true if this String has the same sequence of characters as that of the Object
specified, which should be a String Object. Otherwise, if the specified parameter is not a
String object or if it doesn’t match the sequence of characters in this String, the method
will return false.
public boolean equalsIgnoreCase(String anotherString)
Like equals but ignores the case used in this String and the specified String.

13
The String Class (cont…)

public void getChars(int srcBegin, int srcEnd, char[ ] dst, int dstBegin)
Gets the characters from this String starting at the srcBegin index up to the srcEnd
index and copies these characters to the dst array starting at the dstBegin index.
public int length()
Returns the length of this String.
public String replace(char oldChar, char newChar)
Returns the String wherein all occurences of the oldChar in this String is replaced with
newChar.

14
The String Class (cont…)

public String substring(int beginIndex, int endIndex)


Returns the substring of this String starting at the specified beginIndex index up to the
endIndex index.
public char[ ] toCharArray()
Returns the character array equivalent to this String
public String trim()
Returns a modified copy of this String wherein the leading and trailing white space are
removed.

15
The String Class (cont…)

public static String valueOf(Object object)


Takes in a simple data type such as boolean, integer or character, or it takes in an
object as a parameter and returns the String equivalent of the specified parameter.

16
The String Class (cont…)

17
The String Class (cont…)

18
The String Class (cont…)

19
The Wrapper Classes

Wrapper Classes
 Some Facts:
- Primitive data types are not Objects
- Cannot access methods of the Object Class
- Only actual Objects can access methods of the
Object Class

20
The Wrapper Classes (cont…)

Wrapper Classes
 Some Facts:
- Why wrapper classes?
- Need an object representation for the primitive type
variables to use Java built-in methods.
 Definition:
- Object representations of simple non-object
variables. 21
The Wrapper Classes (cont…)
Primitive Data Type Corresponding Wrapper Classes
boolean Boolean
char Character
byte Byte
short Short
int Integer
long Long
float Float
double Double
22
The Wrapper Classes (cont…)

23
The Wrapper Classes (cont…)

24

Das könnte Ihnen auch gefallen