AP Computer Science in Java
  • Introduction
  • Introduction to Programming in Java with Karel the Dog
    • Introduction to Programming with Karel
    • More Basic Karel
    • Java Programs and the Run Method
    • Karel Can't Turn Right
    • Methods in Karel
    • Top Down Design and Decomposition in Karel
    • Commenting Your Code
    • SuperKarel
    • For Loops
    • While Loops in Karel
    • If Statements
    • If/Else Statements
    • Control Structures Example
    • How To Indent Your Code
  • Basic Java
    • Printing in Java
    • Variables and Types
    • User Input
    • Arithmetic Expressions
    • Casting
    • Booleans
    • Logical Operators
    • Comparison Operators
    • For Loops
    • While Loops
    • If Statements
    • Loop-and-a-Half
    • Short-Circuit Evaluation
    • De Morgan's Laws
    • Strings
  • Methods
    • Java Methods
    • Methods and Parameters
    • Methods and Return Values
    • Javadoc and More Methods
    • Strings Methods
    • Strings and Characters
    • Exceptions
    • String Processing
  • Classes and Object-Oriented Programming
    • Introduction To Classes and Objects
    • Classes vs. Objects
    • Using a Class as a Client
    • Writing Classes
    • Writing Classes and Instance Methods
    • Getter and Setter Methods
    • Class Methods and Class Variables
    • Method Overloading
    • Local Variables and Scope
    • Key Terms for Classes
    • Objects vs Primitives
    • Inheritance
    • Class Design and Abstract Classes
    • Polymorphism
    • Interfaces
  • Data Structures
    • What Are Data Structures?
    • Introduction to Arrays
    • Using Arrays
    • ArrayList Methods
    • Arrays vs ArrayLists
    • 2D Arrays (Matrices or Grids)
    • Hashmaps
  • Algorithms and Recursion
    • What is an Algorithm?
    • Pseudocode
    • Linear Search
    • Binary Search
    • Selection Sort
    • Insertion Sort
    • Advanced: Recursion
    • Mergesort
Powered by GitBook
On this page

Was this helpful?

  1. Methods

Java Methods

To teach Karel a new word, we had to create a method that contained instructions for Karel to follow. Though we are no longer programming with Karel, we can still use methods to organize our code in a similar way.

Methods in Java

Methods allow us to break our program down into smaller parts by grouping commands together. Methods are useful because they help us avoid having to repeat the same sequence of commands over and over. As you recall, to have Karel turn right, you could give Karel three turnLeft(); commands. But it was much easier to create a turnRight method to "teach" Karel how to turn right instead of writing out turnLeft(); three times over and over.

Let's say we want to write a program that prints out your favorite skateboard tricks and draws ASCII art of a skateboarder between every word. Your program might look like this:

public class SkateVocab extends ConsoleProgram
{
    public void run()
    {
        System.out.println("Nollie big spin");
        System.out.println("      |    ");
        System.out.println("      /o   ");                                                                                  
        System.out.println("     /     ");
        System.out.println("   _/o     ");

        System.out.println("Tre flip");
        System.out.println("      |    ");
        System.out.println("      /o   ");                                                                                  
        System.out.println("     /     ");
        System.out.println("   _/o     ");

        System.out.println("A super clean kickflip");
        System.out.println("      |    ");
        System.out.println("      /o   ");                                                                                  
        System.out.println("     /     ");
        System.out.println("   _/o     ");

        System.out.println("180 no-comply");
        System.out.println("      |    ");
        System.out.println("      /o   ");                                                                                  
        System.out.println("     /     ");
        System.out.println("   _/o     ");
    }
}

Drawing out the skateboard so many times becomes quite tedious. Instead of writing out four lines of System.out.println(), it would be much easier to create a "draw skateboard" method.

public class SkateVocab extends ConsoleProgram
{
    public void run()
    {
        System.out.println("Nollie big spin");
        drawSkateboard();

        System.out.println("Tre flip");
        drawSkateboard();

        System.out.println("A super clean kickflip");
        drawSkateboard();

        System.out.println("180 no-comply");
        drawSkateboard();
    }

    private void drawSkateboard()
    {
        System.out.println("      |    ");
        System.out.println("      /o   ");                                                                                  
        System.out.println("     /     ");
        System.out.println("   _/o     ");
    }
}

Using a method makes the program easier to read and understand. Also, if you ever want to change the ASCII art picture of the skateboard, you would only need to do it once in the method instead of changing it througout the program.

PreviousMethodsNextMethods and Parameters

Last updated 5 years ago

Was this helpful?