3. Class Creation

Writing Methods

Writing Methods in AP Computer Science A

When you write a class, you are creating a blueprint for objects. But a blueprint is only useful if it can do something. That is where methods come in, students. Methods describe the actions an object can perform, and they help turn a class from a static container of data into a program with behavior 💡. In AP Computer Science A, writing methods is a major part of Class Creation because it shows how data and actions work together.

Why methods matter

A method is a block of code that performs a task. In a class, methods define the behavior of objects created from that class. For example, a BankAccount class might have methods to deposit money, withdraw money, and check the balance. A Dog class might have methods like bark() or eat(). These methods make the object useful in a real program.

Learning to write methods helps you break a big problem into smaller parts. Instead of writing one giant block of code, you create separate methods for each job. This makes code easier to read, test, and reuse. For the AP exam, you need to understand how methods are written, how they use parameters, how they return values, and how they interact with instance variables.

Method structure and terminology

A method has a few important parts:

  • Access modifier such as public or private
  • Return type such as void, int, or String
  • Method name
  • Parameter list inside parentheses
  • Method body inside braces

A common method header looks like this:

$$\texttt{public int getScore()}$$

This means the method is public, returns an int, is named getScore, and takes no parameters.

If a method does not return a value, its return type is void. For example:

$$\texttt{public void displayInfo()}$$

This method performs an action but does not send back a value.

If a method returns a value, it must use the return statement. For example, a method that computes and returns an average might look like:

$$\texttt{public double getAverage()}$$

and inside the method body it would include a statement like:

$$\texttt{return total / count;}$$

The returned value must match the declared return type.

Writing methods that use instance variables

Methods in a class often work with instance variables, which store the state of each object. Instance variables are usually declared at the top of the class, outside all methods. Methods can read or change these variables.

Consider a Car class with an instance variable named speed. A method named accelerate() might increase the speed by a certain amount. If the method is inside the same class, it can access the instance variable directly.

Example idea:

$$\texttt{speed = speed + 5;}$$

This statement increases the car’s speed by $5$. If a method updates an instance variable, it changes the object’s state.

This is important because AP Computer Science A often asks whether a method changes object data or simply calculates something. If a method changes instance variables, the effect remains after the method ends. If it only uses local variables, the changes may disappear when the method finishes.

Parameters, local variables, and scope

Methods often receive information through parameters. A parameter is a variable listed in the method header. The value passed into the method when it is called is called an argument.

For example:

$$\texttt{public void addPoints(int amount)}$$

Here, amount is a parameter. If the method is called as addPoints(10), then $10$ is the argument.

Methods can also create local variables inside the method body. Local variables exist only while the method runs. This is part of scope, which means where a variable can be used.

A variable declared inside a method cannot be used outside that method. Likewise, a parameter is only available inside its own method. Instance variables, however, can be used by all methods in the class unless a local variable with the same name hides them.

Example:

$$\texttt{public void setAge(int age)}$$

In this method, the parameter age has the same name as a possible instance variable age. To avoid confusion, programmers often use this to refer to the instance variable:

$$\texttt{this.age = age;}$$

This statement means “set the object’s instance variable age equal to the parameter age.”

Understanding scope is essential because many AP questions ask which variables are visible at a certain line of code.

Return values and helper methods

Some methods are designed to produce a value, while others are designed to perform an action. A method with a return type such as int, double, or String must return a value of that type. A method with return type void does not return anything.

Example of a method that returns a value:

$$\texttt{public int getArea()}$$

If a rectangle has length and width, the method could return:

$$\texttt{return length * width;}$$

The method is useful because other parts of the program can use the result.

Sometimes a method is broken into smaller helper methods. A helper method supports another method by handling part of the task. This is a strong example of breaking problems into subproblems. For example, a Game class might have a method that checks whether a player has won, and helper methods that check rows, columns, and diagonals separately. This design makes the code easier to manage and less likely to contain mistakes.

Writing methods in constructors and class design

Methods are closely connected to constructors and other class features. A constructor initializes a new object, while methods allow the object to act after it is created. In a well-designed class, constructors set up the initial state, and methods update or use that state.

For example, a Student class might use a constructor to set the student’s name and grade level. Then methods like promoteGrade() or getGPA() provide behavior. This shows the broader idea of Class Creation: a class combines data, constructors, and methods into one complete design.

A class may also have class variables and class methods using the static keyword. However, in many AP Computer Science A class examples, methods focus on instance behavior, meaning each object has its own data. Knowing the difference helps you decide whether a method should use instance variables or shared class data.

Example walkthrough: a simple class method

Imagine a LightBulb class with an instance variable isOn, which stores whether the bulb is on or off.

A method named turnOn() could look like this conceptually:

$$\texttt{public void turnOn()}$$

Inside the method:

$$\texttt{isOn = true;}$$

This method changes the object’s state to on.

A method named toggle() might switch the value of isOn.

If the bulb is currently on, the method makes it off. If it is currently off, the method makes it on.

This is a good example of a method using a conditional statement such as if / else.

Another method, isLit(), might return a boolean value:

$$\texttt{public boolean isLit()}$$

and then:

$$\texttt{return isOn;}$$

This method does not change the object. It simply reports information. AP questions often ask you to predict whether a method mutates data or only accesses it.

Common method-writing mistakes

When writing methods, students often make a few predictable errors:

  • Using the wrong return type
  • Forgetting to include return in a non-void method
  • Trying to use a local variable outside its scope
  • Confusing parameters with instance variables
  • Changing the wrong variable when names are similar
  • Forgetting that a method call must match the method header’s parameter list

A useful habit is to check the method header first. Ask: Does this method return a value? Does it need parameters? Does it read or change instance variables? Answering those questions helps you write the body correctly.

How this fits AP Computer Science A

Writing methods is a core skill because AP Computer Science A expects you to build and read classes that follow good object-oriented design. You need to know how a method fits into the larger class structure, how it uses access modifiers, and how it works with constructors and instance variables.

On the AP exam, you may be asked to:

  • Write a method from a description
  • Trace what a method does
  • Determine the value returned by a method
  • Identify variables in scope
  • Explain how a method affects object state

To succeed, students, focus on the connection between method purpose and method structure. A method is not just code inside braces. It is a carefully designed piece of a class that either performs an action, returns information, or helps another method solve a larger problem.

Conclusion

Writing methods is one of the most important parts of Class Creation because methods give objects behavior. They work with instance variables, use parameters, follow scope rules, and sometimes return values. They also support good program design by breaking large problems into smaller pieces. In AP Computer Science A, being able to write and understand methods shows that you can build useful classes that model real-world situations effectively 🌟.

Study Notes

  • A method is a block of code that performs a task in a class.
  • Method headers include an access modifier, return type, name, and parameter list.
  • void methods do not return a value.
  • Non-void methods must return a value that matches the declared type.
  • Parameters are listed in the method header; arguments are the values passed in.
  • Local variables exist only inside the method where they are declared.
  • Scope tells where a variable can be used.
  • Instance variables store object state and can be used by methods in the same class.
  • Methods can change object state or simply access and report it.
  • Helper methods break a large task into smaller subproblems.
  • Writing methods connects directly to constructors, instance variables, and class design in AP Computer Science A.

Practice Quiz

5 questions to test your understanding