Chapter 5
- Chapter 5 - Using Pre-Built Methods
- Understanding how to utilize pre-built classes from the Java API Library can streamline development and avoid redundant code.
- The API Library
- Pre-built classes should be leveraged to avoid unnecessary complexities.
- Example: Use the Scanner class for user input instead of creating custom input methods.
- The Math class simplifies complex mathematical calculations.
- Java's API Library (Application Programming Interface) holds these ready-to-use classes.
- Classes must be imported to be used in program, e.g.,
import java.util.Scanner;. - Packages: Groups of classes, e.g.,
java.utilincludes various utility classes like Scanner. - Some classes (like Math) are auto-imported from the
java.langpackage.
- API Headings
- Interaction with API classes requires an understanding of their methods and how to call them.
- Example: To perform input, it’s necessary to know methods like
next,nextLine,nextInt, etc. - Source Code Headings: Describe method signatures that indicate return types and parameters.
- Example:
public int nextInt()- returns an integer and takes no parameters.
- Math Class
- Key methods from the Math class include:
public static int abs(int num): Returns absolute value ofnum.public static double max(double x, double y): Returns larger ofxandy.public static double random(): Generates a random double between 0.0 and 1.0.- Methods are static (class methods) and called like
Math.abs(). - Passing an int to methods expecting a double is permitted.
- Named Constants: Math class contains
PI, a constant value for the ratio of a circle's circumference to its diameter, accessed asMath.PI.
- Wrapper Classes for Primitive Types
- Wrapper classes encapsulate primitive data types to add functionalities.
- Examples include: Integer for
int, Double fordouble. - The
java.langpackage automatically imports these classes, so explicit import is unnecessary. - In GUI applications, numbers must be converted to strings for display and read back as strings for input.
- Conversion methods include:
Integer.parseInt(<string>)for string to int conversion,Double.toString(<#>)for num to string conversion.
- Lottery Example
- Example of a lottery program using Scanner for input:
java import java.util.Scanner; public class Lottery { public static void main(String[] args) { Scanner stdIn = new Scanner(System.in); // Program logic to guess a number } } - Users guess a randomly generated number, and program evaluates and prompts based on input.
- Example of a lottery program using Scanner for input:
- String Class and Methods
- The String class includes methods for string manipulation such as:
substring(int beginIndex): Returns substring from specified index.indexOf(char ch): Finds first occurrence of a character.lastIndexOf(char ch): Finds last occurrence from right to left.- Example of string usage:
java String example = "Hello World"; int index = example.indexOf('o'); String sub = example.substring(6);
- Formatted Output with printf Method
- Use
System.out.printffor formatted console output. - Example format:
java System.out.printf("Total: $%.2f\n", value); - Format specifiers include:
%s: String%d: Decimal integer%f: Floating-point number%e: Scientific notation- Control precision and width with additional format specifiers.
- Use
- Advanced Formatting with printf
- Width and precision specifiers control output layout.
- Example:
java System.out.printf("Value: %7.2f\n", 123.456); - Flags include:
-: left justify output,0: leading zeros,,: locale-specific grouping.
- Final Example: Budget Report
- A sample program employing
printffor a budget report layout, showcasing functionality through structured output and manipulation of financial figures.java System.out.printf("Account: %-25s%13.2f\n", "Supplies", amount);
- A sample program employing