Chapter 2 - Using Objects Study Notes

Chapter Goals

  • Understand the concepts of classes and objects

  • Ability to call methods

  • Learn about arguments and return values

  • Browse the API documentation

  • Implement test programs

  • Understand the difference between objects and object references

  • Write programs that display simple shapes

Objects and Classes

  • Objects and Functions: Just as a home builder uses parts (e.g., furnace, water heater) that fulfill specific functions, a programmer builds applications from objects, each with specific behaviors.

    • In Java, programs are constructed using objects.

    • Each object has defined behaviors that can be manipulated to achieve specific outcomes.

Using Objects

  • Definition of Object: An entity in a program that you can manipulate by calling one or more of its methods.

  • Definition of Method: A sequence of instructions that can access the data of an object.

    • The instructions of a method are often not visible to the user, but its behavior is clearly defined.

    • Example: System.out has a println method that performs a specific action without the user needing to understand its internal workings.

Classes

  • Definition of Class: A class describes a set of objects with the same behavior.

    • Examples of String Objects include:

    • "Hello World"

    • "Goodbye"

    • "Mississippi"

    • All string objects can invoke the same methods.

  • Example of System.out: It is a member of the PrintStream class, which writes output to the console.

    • Other objects of the PrintStream class can write to different destinations.

    • All PrintStream objects share the methods println and print.

  • Behavior of Different Classes:

    • Objects of the PrintStream class have different behaviors compared to objects of the String class.

    • Responsibilities of Different Classes:

    • A string object understands its contents, such as the letters it contains.

    • A string does not have the capability to send itself to a console window or file.

    • For example, all objects of the Window class share common behaviors

Calling Methods

  • Using Methods: You interact with an object by calling its methods.

    • All objects of a specific class share a common set of methods.

  • Example of PrintStream Methods:

    • println

    • print

Example of String Methods

  • Methods Applicable to All String Objects:

    • Example: length method

    • String initialization: String greeting = "Hello, World!";

    • Method call: int numberOfCharacters = greeting.length();

    • Example: toUpperCase method

    • String initialization: String river = "Mississippi";

    • Method call: String bigRiver = river.toUpperCase();

Method Arguments

  • Most methods require input values to perform their tasks.

    • Technical Term for Inputs: Arguments.

    • Example using Java: To call println, the string being printed is an argument:

    • System.out.println(greeting);

  • Multiple Arguments: Some methods may require more than one argument, while others do not need any arguments at all.

    • Example of a method that requires no arguments: length() method of the String class, as it operates on the object itself and doesn't require additional input.

Return Values

  • Method Actions:

    • Some methods perform actions and do not return any values, like println.

    • Other methods compute and return values.

    • Example: The length() method returns the count of characters in a string:

      • int numberOfCharacters = greeting.length();

  • Using Return Values:

    • The return value computed by one method can serve as an argument for another method:

    • System.out.println(greeting.length());

      • Here, greeting.length() returns the integer value 13, which is then used as the argument in println.

Return Value Examples

  • Example using replace Method:

    • Implementation:

    • Assume String river = "Mississippi";

    • The following statement constructs a new string:

      • river = river.replace("issipp", "our");

    • This method:

    • Replaces all occurrences of "issipp" with "our"

    • Returns the new String object "Missouri"

    • Saves the returned value in the variable river

    • The result can also be used in another method call:

    • System.out.println(river.replace("issipp", "our"));

Details of Method Call to replace

  • Detailed Analysis of Method Call:

    • The method call river.replace("issipp", "our"); does the following:

    • Invokes the method on the String object "Mississippi"

    • Accepts two arguments: "issipp" and "our"

    • Returns the newly constructed string "Missouri".

Method Arguments and Return Values

Table of Examples

  • Example Method Call: System.out.println(greeting)

    • Comments: greeting is the argument for println.

  • Example Method Call: greeting.replace("e", "3")

    • Comments: The replace method has two arguments: "e" and "3".

  • Example Method Call: greeting.length()

    • Comments: The length method does not take arguments.

  • Assignment Example: int n = greeting.length();

    • Comments: length returns an integer value.

  • Method Call: System.out.println(n);

    • Comments: println does not return a value; its return type is void per API documentation.

  • Example Method Chain: System.out.println(greeting.length());

    • Comments: The return value of a method can be passed as an argument to another method.

Self Check Questions

  • Self Check 2.3: How can you compute the length of the string "Mississippi"?

    • Answer: river.length() or "Mississippi".length()

  • Self Check 2.4: How can you print out the uppercase version of "Hello, World !"?

    • Answer: System.out.println(greeting.toUpperCase()); or System.out.println("Hello, World!".toUpperCase());

  • Self Check 2.5: Is it legal to call river.println()? Why or why not?

    • Answer: Not legal; river is a String and does not have println as a method.

  • Self Check 2.6: What are the arguments in the method call river.replace("p", "s")?

    • Answer: The arguments are the strings "p" and "s".

  • Self Check 2.7: What is the result of the call river.replace("p", "s"), where river is "Mississippi"?

    • Answer: "Missississi"

  • Self Check 2.8: What is the result of the call greeting.replace("World", "Dave").length(), where greeting is "Hello, World!"?

    • Answer: 12