Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

AP Computer Science Classes, Objects, Methods

Code Structure in Java Source File


Class File
Method1

Method1

When designing a CLASS, think about the objects it will CREATE.

What things will the OBJECT know? What things will the OBJECT do? Example: An object is like ONE entry in an address book. Each entry has the same structure, but different data. The data can be changed (e.g., phone number change, address change, etc.) An object consists of METHODS that contain a set of instructions that use the data (what the object knows) to perform an action (what the object does).

Two Familiar Classes

PrintStream

String

println print

length
toUpperCase

replace

String greeting = "Hello, World!";

String river = "Mississippi";

Examples:

int n = greeting.length();

String bigRiver = river.toUpperCase();

Output = ?? How would you print the uppercase version of Hello, World! System.out.println(greeting.toUpperCase());

Method Parameters and Return Values

A parameter is an INPUT to the method. Some methods require multiple parameters, others require none at all, e.g., the length() method.

System.out.println(greeting.length()); see animation online

What does this method of the String class do?

public String replace(String target, String replacement) river.replace("issipp", "our");

What is the result of the call river.replace(p, s)?

Try Programming Exercises P.9 and P.10

You might also like