WC - Writing Methods (Lesson)

APCompSci_LessonTopBanner.png

Writing Methods

Introduction

Click below to watch the Writing Methods intro video.

 

 Think of the instance variables and methods for a Zipper class:

Zipper

length
zip()
unzip() 

 Think of the instance variables and methods for a Clock class:

Clock
hours
minutes
setTime()
setAlarm()
setTimer() 

Methods

The methods are the meat of the class. That is where most of your code will be located and where the logic and calculations take place. Methods can use the instance variables of the class. Recall the Triangle class method which used the base and height instance variables to calculate the area:

public double computeArea ()
{
double area;
area = .5*base*height;
return area;
} 

 Consider the following method:

public int method (int n)
{
int count = 0;
for (int i = 0; i < n; i++)
count++;
return count;
} 

Click on Question and Answer below to see an example question.

 

Consider the following method: 

Duck saying "All methods will be public!"

public int difference (int first, int second)
{
int difference = first - second;
return difference;
}  

This method returns an int that is the difference from subtracting the two parameters.

Static Methods

Static methods are methods which are associated with the class, not objects of the class. They must have the keyword static in their method heading before the method name. Static methods cannot access or change the values of instance variables. When a static method is called it will use the class name, not the dot operator, since they are associated with a class, not objects of a class. Recall the Math class methods that we learned previously. The Math class methods are all static which is why we can call them using Math.methodName(). This works for variables and constants as well. For example, to access numerical PI from the Math class we use Math.PI.

Click below to begin the Writing Methods Try it! Activity.

 

Try it!

Complete the 2018 AP Computer Science Free Response Question 1: Click on the Frog Simulation link below.

Frog Simulation Link Links to an external site.

Once you have written your answer, view the video below to check your work.

 

Book Icon Click on "Introduction to Computer Science Using Java" below to open the required reading that is listed.

Introduction to Computer Science Using Java Links to an external site.

READ:

  • Chapter 49: Class Design Example: Checking Account
  • Chapter 51: Parameters, Overloading, Local Variables

Complete the reviews for practice.

Methods Practice

Practice-It! Practice

Practice Icon Go to the PracticeIt website Links to an external site..

Log into account.

Click on Start Practicing!

Go to the most recent edition.

Click on Chapter 3: Parameters and Objects.

Complete Exercises 3.1 – 3.22.

At this point you should be able to successfully complete all these exercises. (Be sure you complete the Exercises, not the Self-Checks.) Contact your teacher if you need help.

CodingBat Practice

Practice Icon Go to CodingBat website Links to an external site..

  • Create an account (your work will be saved when you are logged in).
  • Click on the Java tab.
  • Complete String 1 Exercises.
  • Complete String 2 Exercises.

At this point you should be able to successfully complete all these exercises. Contact your teacher if you need help.

 

APCompSci_LessonBottomBanner.pngIMAGES CREATED BY GAVS