zuai-logo

Calling a Void Method

Sophie Anderson

Sophie Anderson

13 min read

Listen to this study note

Study Guide Overview

This study guide covers methods in AP Computer Science A, including:

  • Method definition, purpose, and behavior
  • Void vs. Non-Void methods and Static vs. Non-Static methods
  • Method signatures and calling methods using dot notation
  • Practice problems and exam tips focusing on object interaction and code tracing with methods

AP Computer Science A: Methods - The Night Before 🚀

Hey there, future AP Computer Science rockstar! Let's get you prepped for the exam with a super-focused review of methods. We'll make sure everything clicks, and you'll walk in feeling confident. Let's do this!

Introduction to Methods

Methods are like mini-programs within your program. They perform specific tasks, and they're essential for organizing your code. Think of them as the verbs of your code, describing what actions your objects can take. 💡

  • Definition: A method is a block of code that performs a specific task.
  • Purpose: To organize code into reusable blocks, making programs more modular and easier to manage.
  • Behavior: Methods define the actions an object can perform, also known as the object's behavior.
  • Parameters: Methods can accept input values called parameters, which modify their behavior.

Types of Methods

Methods can be classified in a few ways. Let's break them down:

Void vs. Non-Void Methods

  • Void Methods: These methods perform actions but do not return a value. They change the state of an object or perform an operation like printing to the console.

    • Example: public void printName() { System.out.println(name); }
    • Keyword: void in the method header indicates it's a void method.
    • Signature: Method name + parameter list (e.g., printName(), setName(String newName))
  • Non-Void Methods: These methods perform actions and return a value. We'll dive into these in more detail later.

Static vs. Non-Static Methods

  • Static Methods: These methods belong to the class itself, not to any specific object. Think of them as general tools that any object of the class can use. ⚙️
    • Keyword: static in the method header.
    • Example:

java public static void incrementMinimumWage() { minimumWage++; } ``` - Calling Static Methods: Use the class name and dot notation: ClassName.methodName();

  • Non-Static Methods: These methods operate on a specific object. Each object has its own version of these methods.
    • Example:

java public void printName() { System.out.println(name); } ``` - Calling Non-Static Methods: Use the object name and dot notation: objectName.methodName();

Memory Aid

Static methods are like class-wide tools (e.g., a calculator app), while non-static methods are like personal actions each object can take (e.g., a person's ability to speak).

Key Concept

Method Signatures

  • Definition: The method signature includes the method name and the parameter list (the types and names of the inputs the method takes).
  • Importance: The method signature is crucial for the compiler to identify and call the correct method.
  • Example: In public void setName(String newName), the method signature is setName(String newName). The return type (void in this case) is not part of the method signature.
Exam Tip

Pay close attention to method signatures in multiple-choice questions. Mismatched signatures are a common source of errors.

Key Concept

Calling Methods

  • Dot Notation: Use dot notation to call both static and non-static methods.
    • Static: ClassName.methodName(parameters);
    • Non-Static: objectName.methodName(parameters);
  • Execution Flow: When a method is called, the program jumps to the method's code, executes it, and then returns to the point where it was called.
Common Mistake

Forgetting the parentheses () when calling a method is a frequent error. Always include them, even if there are no parameters.

Methods Practice Problems

Okay, let's put this knowledge to the test with some practice problems similar to what you'll see on the AP exam. Remember, practice makes perfect! 💪

Practice Problem 1

Consider the following class definition.

java
public class Dog
{
  String name;
  int age;
  boolean isTrained;

  public Dog()
  {
    name = "";
    age = 0;
    isTrained = false;
  }

  public void setName(String newName)
  {
    name = newName;
  }

  public void train()
  {
    isTrained = true;
  }
}

Assume that a Dog object called myDog has been properly declared and initialized in a class other than Dog. Which of the following statements are valid?

A. myDog.setName("Buddy"); B. myDog.train(true); C. myDog.age(5); D. System.out.println( myDog.isTrained() ); E. myDog.setAge(3);

Answer: A. myDog.setName("Buddy");

Practice Problem 2

Consider the following class definition.

java
public class Car
{
  String make;
  String model;
  int year;
  boolean isNew;

  public Car()
...

Question 1 of 15

What is the primary role of a method in programming? 🚀

To store data values

To perform a specific task

To define object properties

To create new classes