join the ap-comp-sci-a course

๐ŸŽ–๏ธfiveable certificate upon completion

โฑ๏ธ 4 min read

๐Ÿ“ย ย 

ap comp sci a

ย โ€ขย 

study guide

ย โ€ขย 

Unit 5

5.1 Overview of Unit 5: Writing Classes and 5.1 Anatomy of a Class


written by

Peter Cao

peter cao

published on september 20, 2020

Last updated on September 20, 2020

The Big Takeaway Of This Unit

Classes and Object-Oriented Programming Classes are what run Java and object-oriented programming. A class sets the specifications for a type of object and by making them, we are incorporating abstraction and encapsulation.

Unit Overview

Exam Weighting

  • 5-7.5% of the test
  • Roughly 2 to 3 multiple-choice questions
  • A part of FRQ #2, which tests your ability to write a class according to specifications.

Enduring Understanding

We learned a little bit about abstraction, the first of the object-oriented programming principles back in Unit 2. Now it's time to review abstraction and learn about encapsulation, the second principle in this unit. Here, we will go into classes more in-depth. Here, we will make and use classes for what they were intended for: to make representations of real-world objects that we can manipulate as objects.

Building Computational Thinking

This unit will be mainly centered on the creation of classes from scratch. Thus, we need to know how to make the parts of a class: the variables, the constructors, and the methods. Thankfully, we will have the help of Java comments to help us organize our code. Most of this unit guide will be "project-based" where we discuss the creation of one or multiple classes from beginning to end and have it span the entire guide.

Main Ideas for This Unit

  • Instance Variables
  • Constructors
  • Methods
    • Accessor Methods
    • Mutator Methods
    • Other Methods
  • Comments and Documentation
  • Access and Scope

5.1: Anatomy of a Class

Introduction to Class Writing

Class writing is something you will be doing a lot throughout the rest of AP CSA, and, in fact, there will be one FRQ dedicated to this topic! Class writing involves not just coding a class, but thinking through what needs to be done and how to achieve it as well.

The Parts of a Class

Fiveable

join more students

create an account on fiveable

A class has three main parts: variable declarations, constructors, and methods.

Variable declarations list the variables that are used to characterize objects. Constructors are what create objects, and a class can have multiple constructors. Finally, methods create an effect, either changing other objects, changing itself, or doing something else. Each class is in its own file with the same name. Here is an example of a class:

public class Square { private int side;

This one line after the class header is the variable declaration. In this class, we have a private integer named side, which represents the side length of the square. We have set this variable to private because we want to restrict access to the data in the class from other classes. Otherwise, anyone could change the data without permission. This has the potential for other classes and programmers to break the code. This the principle of encapsulation, keeping information private and only allowing approved methods to access such data.

public Square(int sideLength) { side = sideLength; } public Square() { side = 0; }

These are the two constructors for the Square class which makes Square objects. The first one is the full constructor where the client (other users and classes) can input parameters into the constructor. The other constructor overloads the first constructor and is the default constructor with default values.

public int getSide() { return side; } public void setSide(int sideLength) { side = sideLength; } public int getPerimeter() { return side * 4; } public int getArea() { return side * side; } public Square duplicate() { return new Square(side); } }

These are the methods of the class. These either make a new object, change an object, or return the value. We will also learn how to write methods later in the unit.

We will discuss these parts in more detail in the following topics!

This Guide's Design Constraints and Introduction to Software Programming

This guide is different than other guides where we will have a design situation and make one or several classes from this situation.

The constraints for this are as follows:

We have students, which each have a grade level, name, and age. Every so often, they have an assignment, which consists of a correct answer. A student can submit an assignment that has one true/false question which can be graded. Based on past assignments completed, a student can receive a grade and determine if the student is passing.

Wow, that's a lot to take in! Let's break it down a little bit. Our two main nouns are students and assignments. These will be our classes. The student has a grade level, name, age, assignments, and a grade. These will be our instance variables. Instance variables are the characteristics that each You can figure out instance variables from what the class has as characteristics. From there, we can get the instance variable of the assignments to be the answer which is true or false.

The methods are the actions that objects from the class can do or ways to access data about the object. For the assignment class, we don't want the answer to be set or viewed (no cheating! ๐Ÿคง), but we do want to check the answer to an assignment. For the student class, we want to access the grade level, name, and age. We also only want to set the grade level and name manually, but we need a way for a student to turn in an assignment and also to calculate their grade.

Finally, we want a method to see if a student is passing or failing. With the specifications defined, let's get started on writing our classes!

Fiveable Logo

2550 north lake drive
suite 2
milwaukee, wi 53211

92% of Fiveable students earned a 3 or higher on their 2020 AP Exams.

*apยฎ and advanced placementยฎ are registered trademarks of the college board, which was not involved in the production of, and does not endorse, this product.

ยฉ fiveable 2020 | all rights reserved.