LEARN JAVA: METHODS

Introduction

In the last lesson/article, we created an instance of the Store class in the main method. We learned that objects have state and behavior:

We have seen how to give objects state through instance fields. Now, we’re going to learn about behavior. Remember our example of a Savings Account.
A savings account should know:
  • The balance of money available
It should be able to perform:
  • Depositing
  • Increasing the amount available
  • Withdrawing
  • Decreasing the amount available
  • Checking the balance
  • Displaying the amount available.
You have defined and called constructor methods, which create an instance of a class. You have also defined main methods, which are the tasks that execute when the program is run. These are specific examples of methods. We can also define our own that will take input, do something with it, and return the kind of output we want.
Java
What if it took 20 lines of code to make a sandwich? Our program would become very long very quickly if we were making multiple sandwiches. Methods are powerful because they allow us to create blocks of code that are repeatable and modular.

Defining Methods

Remember our Car example from the last lesson? Let’s add a method to this Car called startEngine() that will print:
Starting the car! Vroom!
This method looks like:
public void startEngine() { System.out.println("Starting the car!"); System.out.println("Vroom!"); }
The first line, public void startEngine(), is the method signature. It gives the program some information about the method:
  • public means that other classes can access this method. We will learn more about that later in the course.
  • The void keyword means that there is no specific output from the method. We will see methods that are not void later in this lesson, but for now all of our methods will be void.
  • startEngine() is the name of the method.
checkBalance() method for the Savings Account we talked about earlier looks something like:
public void checkBalance(){ System.out.println("Hello!"); System.out.println("Your balance is " + balance); }
The two print statements are inside the body of the method, which is defined by the curly braces.
Anything we can do in our main() method, we can do in other methods! All of the Java tools you know, like the math and comparison operators, can be used to make interesting and useful methods.

Calling Methods

Great! When we add the startEngine() method to the Car class, it becomes available to use on any Car object. We can do this by calling the method on the Car object we created, for example.
Here is an example of calling a method on an object using the Car class:
class Car { String color; public Car(String carColor) { color = carColor; } public void startEngine() { System.out.println("Starting the car!"); System.out.println("Vroom!"); } public static void main(String[] args){ Car myFastCar = new Car("red"); myFastCar.startEngine(); } }
In the example above, we call the startEngine() method on the myFastCar object. This method call occurs inside the main() method. Running the program results in printing Vroom! to the output terminal.

Scope

A method is a task that an object of a class performs.
We mark the domain of this task using curly braces: {, and }. Everything inside the curly braces is part of the task. This domain is called the scope of a method.
We can’t access variables that are declared inside a method in code that is outside the scope of that method.
Looking at the Car class again:
class Car { String color; int milesDriven; public Car(String carColor) { color = carColor; milesDriven = 0; } public void drive() { String message = "Miles driven: " + milesDriven; System.out.println(message); } public static void main(String[] args){ Car myFastCar = new Car("red"); myFastCar.drive(); } }
The variable message, which is declared and initialized inside of drive, cannot be used inside of main()! It only exists within the scope of the drive() method.
However, milesDriven, which is declared at the top of the class, can be used inside all methods in the class, since it is in the scope of the whole class.

Adding Parameters

We saw how a method’s scope prevents us from using variables declared in one method in another method. What if we had some information in one method that we needed to pass into another method?
Similar to how we added parameters to constructors, we can customize all other methods to accept parameters.
class Car { String color; public Car(String carColor) { color = carColor; } public void startRadio(String station) { System.out.println("Turning on the radio to " + station + "!"); System.out.println("Enjoy!"); } public static void main(String[] args){ Car myCar = new Car("red"); myCar.startRadio("Meditation Station"); } }
In this code, we create a startRadio() method that accepts an String parameter called station. In the main() method, we call the startRadio() method on the myCar object and provide an String argument of "Meditation Station".
A call to the startRadio() method on myCar results in printing:
Turning on the radio to Meditation Station! Enjoy!

Reassigning Instance Fields

Earlier, we thought about a Savings Account as a type of object we could represent in Java.
Two of the methods we need are depositing and withdrawing:
public SavingsAccount{ double balance; public SavingsAccount(double startingBalance){ balance = startingBalance; } public void deposit(double amountToDeposit){ //Add amountToDeposit to the balance } public void withdraw(double amountToWithdraw){ //Subtract amountToWithdraw from the balance } public static void main(String[] args){ } }
These methods would change the value of the variable balance. We can reassign balance to be a new value by using our assignment operator, =, again.
public void deposit(double amountToDeposit){ double updatedBalance = balance + amountToDeposit; balance = updatedBalance; }
Now, when we call deposit(), it should change the value of the instance field balance:
public static void main(String[] args){ SavingsAccount myAccount = new SavingsAccount(2000); System.out.println(myAccount.balance); myAccount.deposit(100); System.out.println(myAccount.balance); }
This code first prints 2000, the initial value of myAccount.balance, and then prints 2100, which is the value of myAccount.balance after the deposit() method has run.
Changing instance fields is how we change the state of an object and make our objects more flexible and realistic.

Returns

Remember, variables can only exist in the scope that they were declared in.
We can use a value outside of the method it was created in if we return it from the method.
We return a value by using the keyword return:
public int numberOfTires() { int tires = 4; return tires; }
This method, called numberOfTires, returns 4. In past exercises, when creating new methods, we used the keyword void. Here, we are replacing void with int, to signify that the return type is an int.
The void keyword (which means “completely empty”) indicates to the method that no value is returned after calling that method.
Alternatively, we can use datatype keywords (such as int, char, etc.) to specify that a method should return a value of that type.
public static void main(String[] args){ Car myCar = new Car("red"); int numTires = myCar.numberOfTires(); }
Within main(), we called the numberOfTires() method on myCar. Since the method returns an int value of 4, we store the value 4 in an integer variable called numTires. If we printed numTires, we would see 4.

The toString() Method

When we print out Objects, we often see a String that is not very helpful in determining what the Object represents. 
 We can add a method to our classes that makes this printout more descriptive.
When we define a toString() method for a class, we can return a String that will print when we print the object:
class Car { String color; public Car(String carColor) { color = carColor; } public static void main(String[] args){ Car myCar = new Car("red"); System.out.println(myCar); } public String toString(){ return "This is a " + color + " car!"; } }
When this runs, the command System.out.println(myCar) will print This is a red car!, which tells us about the Object myCar.

Review

Let’s review everything we have learned about methods so far.

  • Defining a method : Methods have a method signature that declares their return type, name, and parameters
  • Calling a method : Methods are invoked with a . and ()
  • Parameters : Inputs to the method and their types are declared in parentheses in the method signature
  • Changing Instance Fields : Methods can be used to change the value of an instance field
  • Scope : Variables only exist within the domain that they are created in
  • Return : The type of the variables that are output are declared in the method signature
  • As you move through more Java material, it will be helpful to frame the tasks you create in terms of methods. This will help you think about what inputs you might need and what output you expect.

No comments:

Powered by Blogger.