cs ch3

Chapter Overview

  • Chapter 3 focuses on utilizing classes and objects in Java to create more engaging programs.

  • Topics covered include:

    • Object creation and references

    • The String class and its methods

    • Java API class library

    • Random and Math classes

    • Formatting output

    • Enumerated types

    • Wrapper classes

    • JavaFX graphics API

    • Shape classes

Creating Objects

  • Variable Types:

    • Variables can hold primitive values or references to objects.

    • Example: String title; declares an object reference variable of type String without creating an object.

    • To create the object, use the new operator.

  • Instantiation:

    • Creation of an object is called instantiation.

    • Example: title = new String("Java Software Solutions");

    • This invokes the String constructor, initializing the object.

Invoking Methods

  • After instantiation, methods can be invoked using the dot operator:

    • Example: numChars = title.length();

  • A method may return a value which can be utilized in an expression or assignment.

  • Conceptually, invoking a method is like asking the object to perform a service.

References

  • Primitive variables store their values, while object variables hold references to memory addresses of objects.

  • Object references can be illustrated graphically as pointers to memory locations.

    • Example: Katherine Johnson refers to an object holding a name.

Assignment Revisited

  • Primitive Types: Assignment copies actual values.

    • Example: num2 = num1; leads to both holding the same value.

  • Object References: Assignment copies the reference address,

    • Example: name2 = name1; makes both refer to the same object.

Aliases

  • Multiple references to the same object are called aliases.

  • Changes via one reference affect the object through all other aliases since there is essentially one object.

Garbage Collection

  • Objects with no valid references become inaccessible and are termed "garbage".

  • Java handles garbage collection automatically, reclaiming memory used by objects that are no longer referenced.

  • In contrast, manual garbage collection is required in other programming languages.

The String Class

  • Strings are ubiquitous in Java and can be created without the new operator:

    • Example: title = "Java Software Solutions";

  • String literals automatically create String objects.

  • Immutability: String objects cannot change value or length after creation, although methods can produce modified versions.

String Methods

  • String objects provide various methods, but their values can't change in place.

  • String manipulation results in the creation of new String objects.

String Indexing

  • Characters in a string can be accessed via numeric indexes, starting from zero.

    • Example: In "Hello", 'H' is at index 0, 'o' is at index 4.

Class Libraries

  • A class library is a compilation of classes available for program development, while the Java standard library is heavily used.

  • Familiar classes include System, Scanner, and String.

The Java API

  • The Java API serves as the Application Programming Interface, offering clusters of related classes.

  • Encourages use of online documentation for exploring libraries like the JavaFX API and Database API.

The Random Class

  • The Random class from java.util provides methods for generating pseudorandom numbers.

    • Example: int num1 = generator.nextInt(); generates random integers.

    • Methods can generate numbers in specific ranges like generator.nextInt(10) for values 0-9.

The Math Class

  • Located in the java.lang package, the Math class includes methods for performing mathematical operations.

  • Example: To find the cosine of an angle or the square root of a number, static methods are invoked directly through the class.

Formatting Output

  • Purpose: Proper output formatting enhances readability.

  • Classes: The NumberFormat and DecimalFormat classes in java.text assist in formatting numbers as currency or percentages.

Enumerated Types

  • Enumerated types can be defined to declare variables with restricted values.

    • Example declaration: enum Season { winter, spring, summer, fall };

  • Each enumerated type is type-safe, ensuring values assigned match predefined identifiers.

Wrapper Classes

  • Each primitive type has a corresponding wrapper class in java.lang which allows primitive types to be managed as objects.

    • Examples of wrappers include Integer for int, Double for double, etc.

  • Autoboxing automatically converts primitives to wrapper objects and vice versa during assignments.

Intro to JavaFX

  • Shift from text-based command-line applications to graphical user interfaces through JavaFX.

  • JavaFX utilizes an Application class and a start method to manage GUI operations.

Basic Shapes

  • JavaFX provides classes for creating various shapes: Line, Rectangle, Circle, and Ellipse, all customizable in terms of positioning and appearance.

  • Nested groups allow for complex scene structures and transformations like translations and rotations.

Representing Color

  • Colors in Java are represented as RGB values in Color objects, with methods to create a color from RGB or percentage.

  • Predefined Color objects include common colors like Color.BLACK and Color.WHITE.

Summary of Chapter 3

  • Key topics included:

    • Object creation and object references

    • The String class and its methods

    • The Java standard class library

    • The Random and Math classes

    • Formatting output

    • Enumerated types

    • Wrapper classes

    • JavaFX graphics API

    • Shape classes

robot