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.

What a strong answer looks like

A strong Unit 6 answer says what a method is given, what it hands back, and what it leaves alone.

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.

Study move

Split a working program into two methods without changing what it does.

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.

Study move

Change a printing method into a returning one and update the caller.

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.

Study move

Write a small class and create two objects with different values.

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.

Study move

Write a class where a value can never be set to something invalid.

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.

Study move

Search a sorted list by halving and write down the range at each step.

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.

Study move

List what a class attendance program truly needs to store and what it should not.

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.

Mastery checklist

  • Write a method with a clear job, inputs, and output.
  • Explain the difference between returning and printing.
  • Write a constructor that fills in every value.
  • Say what must be true before halving a search.

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