Skip to main content
← Intro to CS (Java) Study Guide

Unit 6 · Ends the course

Methods and Classes

Breaking a program into named pieces, then grouping data and behavior together into a class. This is the unit that makes larger programs possible rather than merely longer.

Topics in this unit

1

Writing Methods

Practice this topic →

Know

A method takes values, does one job, and usually hands something back. Deciding what it takes and returns is the design; the body is the easy part.

Apply

Write the name, the values it takes, and what it returns before the body.

Watch out

Writing one long method that does several jobs, which is hard to test and harder to fix.

2

Handing Values Back

Practice this topic →

Know

Returning gives a value to whoever called the method. Printing shows it on screen and hands back nothing usable.

Apply

Return the result and let the caller decide whether to print it.

Watch out

Printing inside a method and then trying to use the result, which gives nothing.

3

Classes and Objects

Practice this topic →

Know

A class describes a kind of thing; an object is one of them. The constructor sets up a new object when it is created.

Apply

Give every piece of data a value in the constructor so no object starts incomplete.

Watch out

Writing a return type on a constructor, which turns it into an ordinary method that never runs.

4

Keeping Data Safe

Practice this topic →

Know

Keeping data private and offering methods to work with it lets the object stay in a sensible state.

Apply

Make the data private and add only the operations that make sense for the thing.

Watch out

Making everything public, which lets any code put the object into a nonsense state.

5

First Algorithms

Practice this topic →

Know

Searching one item at a time works on any order. Searching by halving requires the data to be sorted first.

Apply

Check whether data is sorted before choosing the faster method.

Watch out

Halving through unsorted data, which gives a wrong answer without any error.

6

Data About People

Practice this topic →

Know

Programs that store information about people carry responsibilities, including privacy and fairness.

Apply

For any program holding personal data, say what it stores, why it needs it, and who can see it.

Watch out

Collecting data because it is easy rather than because it is needed.

Emphasized in this unit

Connections and techniques that receive extra attention in this unit.

  • • Designing a method before writing it
  • • Returning rather than printing
  • • Keeping object data private
  • • Checking that data is sorted before halving

Varies by course

Related topics some schools attach to this unit and others leave out. Covered on request rather than assumed.

  • Inheritance. Usually left to AP CSA.
  • Sorting algorithms. Some sections add a simple sort here.

Check yourself

  • • Why is designing a method harder than writing its body?
  • • What is wrong with a constructor that has a return type?
  • • What happens when you halve through unsorted data?

Modeling drill

Write a class that stores a student name and a list of scores, with a method returning the average, keeping the scores private.

MethodParameterReturnClassObjectConstructorPrivateSearch

Need help with this topic?

Work through questions with a tutor and get help with the steps you find difficult.

Get help with this topic