A class in Java can be thought of as a blueprint for an object. Just as a blueprint for a house outlines the design and features of the house, a class in Java outlines the attributes and behaviors of an object. For example, a class for a car might include attributes such as the make, model, and year, as well as behaviors such as driving and honking the horn. When you create an object based on a class, it is like using the blueprint to build a specific instance of the house or car. The object has all of the attributes and behaviors defined in the class, but can also have its own unique characteristics and actions.
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.
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.
After declaring a class and defining the body of the class, you can create objects using this format: Classname objectname = new Classname();
For example, for our Square class, creating a new object could look something like this:
Square mySquare = new Square();
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 Square(int sideLength) {
side = sideLength;
}
public Square() {
side = 0;
}
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.
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);
}
}
If youβre still a little confused, itβs helpful to think of a Java class as a recipe for a dish. The class header (e.g. "public class Recipe") is like the title of the recipe and specifies the name of the class and its visibility. The class body (enclosed in curly braces) contains the ingredients and instructions for the recipe, just like the body of a Java class contains the variables and methods that make up the class.
The variables in a Java class can be thought of as the ingredients needed for the recipe. Just like different ingredients have different properties (such as size, shape, and type), variables also have different data types and attributes.
The methods in a Java class can be thought of as the steps in the recipe instructions. Just like a recipe may have multiple steps (such as mixing, chopping, and baking), a Java class may have multiple methods to perform different tasks. The method header specifies the name of the method and its parameters, similar to how the instructions in a recipe specify the ingredients and tools needed for each step. The method body contains the code that is executed when the method is called, similar to how the instructions in a recipe detail the exact steps to be taken in each step.
Finally, the main method, which is typically present in every Java class, is like the main course in a meal. It is the primary function of the class and is the entry point for the program. Just like the main course is the star of the meal, the main method is the central part of a Java program.
We will discuss these parts in more detail in the following guides!
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!
Reminder: The instance variables are private and the constructor is public. Methods should also be public.
Consider the Plant class which will contain a String and a double attribute for a plant's species and height and a constructor.
public class Plant
{
Β Β /* missing code */
}
Which of the following replacements for /* missing code */ is the most appropriate implementation of the class?
Β
A.
private String species;
private double height;
private Plant(String species, double height)
{ /* implementation not shown */ }
Β
B.
private String species;
private double height;
public Plant(String species, double height)
{ /* implementation not shown */ }
Β
C.
public String species;
public double height;
public Plant(String species, double height)
{ /* implementation not shown */ }
Β
D.
public String species;
private double height;
private Plant(String species, double height)
{ /* implementation not shown */ }
Β
E.
public String species;
public double height;
private Plant(String species, double height)
{ /* implementation not shown */ }
Β
Answer: B
Β
Consider the House class which will contain a String and an int attribute for a house's address and number of rooms and a constructor.
public class House
{
Β Β /* missing code */
}
Which of the following replacements for /* missing code */ is the most appropriate implementation of the class?
Β
A.
private String address;
private int rooms;
public House(String address, int rooms)
{ /* implementation not shown */ }
Β
B.
private String address;
private int rooms;
private House(String address, int rooms)
{ /* implementation not shown */ }
Β
C.
public String address;
public int rooms;
public House(String address, int rooms)
{ /* implementation not shown */ }
Β
D.
public String address;
private int rooms;
private House(String address, int rooms)
{ /* implementation not shown */ }
Β
E.
public String address;
public int rooms;
private House(String address, int rooms)
{ /* implementation not shown */ }
Β
Answer: A
Β
Consider the Car class which will contain a String and a boolean attribute for a car's make and whether it is electric or not and a constructor.
public class Car
{
Β Β /* missing code */
}
Which of the following replacements for /* missing code */ is the most appropriate implementation of the class?
Β
A.
public String make;
public boolean electric;
private Car(String make, boolean electric)
{ /* implementation not shown */ }
Β
B.
private String make;
private boolean electric;
private Car(String make, boolean electric)
{ /* implementation not shown */ }
Β
C.
public String make;
public boolean electric;
public Car(String make, boolean electric)
{ /* implementation not shown */ }
Β
D.
public String make;
private boolean electric;
private Car(String make, boolean electric)
{ /* implementation not shown */ }
Β
E.
private String make;
private boolean electric;
public Car(String make, boolean electric)
{ /* implementation not shown */ }
Β
Answer: E
Consider the Phone class below which will contain three String attributes for brand, model, and color, a constructor, and a makeCall method. The makeCall method is intended to be accessed outside the class.
public class Phone
{
/* missing code */
}
Β
Which of the following replacements for /* missing code */ is the most appropriate implementation of the class?
Β
A.
private String brand;
private String model;
private String color;
public Phone()
{ /* implementation not shown / }
private void makeCall()
{ / implementation not shown */ }
Β
B.
private String brand;
private String model;
private String color;
public Phone()
{ /* implementation not shown / }
public void makeCall()
{ / implementation not shown */ }
Β
C.
public String brand;
public String model;
public String color;
public Phone()
{ /* implementation not shown / }
public void makeCall()
{ / implementation not shown */ }
Β
D.
private String brand;
private String model;
private String color;
private Phone()
{ /* implementation not shown / }
private void makeCall()
{ / implementation not shown */ }
Β
Answer: B