Some Standard Classes ✏ AP Computer Science A

Rucete ✏ AP Computer Science A In a Nutshell

4. Some Standard Classes

This chapter introduces key Java standard classes: Object, String, Wrapper classes (Integer, Double), the Math class, and how to use Math.random for random number generation.


The Object Class

• Object is the universal superclass in Java; every class inherits from Object directly or indirectly.

• Important inherited methods:

• toString(): Returns a String representation of the object; should be overridden for meaningful output.

• equals(Object other): Tests object equality; default checks if two references point to the same object.

• Do not use == to compare object contents; always use equals.

The String Class

• A String object represents a sequence of characters and is immutable.

• String literals (e.g., "hello") are instances of the String class.

• Strings are initialized easily: String s = "abc";

• Concatenation uses + operator; non-String operands are automatically converted to String via toString().

Comparing Strings

• equals(String other): Returns true if two strings are identical.

• compareTo(String other): Lexicographically compares two strings.

• Java string comparisons are case-sensitive; ASCII order matters (digits < capitals < lowercase).

• Always use equals, not ==, to compare String values.

Important String Methods

• length(): Returns number of characters in the string.

• substring(startIndex): Returns substring from startIndex to end.

• substring(startIndex, endIndex): Returns substring from startIndex to endIndex-1.

• indexOf(String str): Returns the index of the first occurrence of str, or -1 if not found.

• Common errors: StringIndexOutOfBoundsException if invalid indices are used.

Processing Strings in Programs

• Looping through characters using substring and indexOf is common.

• Careful handling of multiple occurrences, replacements, and removals of substrings is important.

Wrapper Classes: Integer and Double

• Java provides wrapper classes to represent primitive types as objects (e.g., Integer for int, Double for double).

• Useful when objects are required, such as in ArrayLists or method parameters expecting objects.

• Common methods:

• Integer.parseInt(String s): Converts a String to an int.

• Double.parseDouble(String s): Converts a String to a double.

• Integer.valueOf(String s): Returns an Integer object.

• Auto-boxing: Automatic conversion between primitive types and their wrapper objects (e.g., int to Integer).

• Auto-unboxing: Automatic conversion from wrapper object to primitive (e.g., Integer to int).

The Math Class

• Math provides static methods for common mathematical operations.

• Important methods:

• Math.abs(x): Absolute value.

• Math.pow(x, y): x raised to the y power.

• Math.sqrt(x): Square root.

• Math.min(x, y), Math.max(x, y): Minimum and maximum of two values.

• Math.random(): Returns a random double between 0.0 (inclusive) and 1.0 (exclusive).

Generating Random Numbers

• To generate a random integer between a and b inclusive:

• (int)(Math.random() * (b - a + 1)) + a;

• Example: To generate a random integer from 1 to 6 (like a die roll): (int)(Math.random() * 6) + 1;

Randomness in Simulations

• Random numbers are used in simulations and modeling unpredictable events (e.g., rolling dice, shuffling cards).

• Randomness in computer programs is pseudorandom, based on algorithms, but appears random enough for most purposes.

Common Errors with Math.random()

• Forgetting to cast the result to int causes compilation errors when an integer is expected.

• Incorrect scaling or shifting can cause wrong random ranges (off-by-one errors are common).

In a Nutshell

Java’s standard classes like Object, String, Integer, Double, and Math provide essential functionality for efficient programming. Understanding how to manipulate Strings, use wrapper classes for type conversions, and generate random numbers accurately with Math methods is crucial for solving a wide range of programming problems and creating dynamic, responsive Java applications.

Post a Comment

Previous Post Next Post