💻AP Computer Science A
8 min read•Last Updated on June 18, 2024
Avanish Gupta
user_sophia9212
Avanish Gupta
user_sophia9212
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](https://www.fiveableKeyTerm:public) class Square {
[private](https://www.fiveableKeyTerm: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
Abstraction is the process of simplifying complex systems by focusing on essential features while hiding unnecessary details. It allows programmers to work with high-level concepts without worrying about implementation specifics.
Term 1 of 13
Abstraction is the process of simplifying complex systems by focusing on essential features while hiding unnecessary details. It allows programmers to work with high-level concepts without worrying about implementation specifics.
Term 1 of 13
Abstraction is the process of simplifying complex systems by focusing on essential features while hiding unnecessary details. It allows programmers to work with high-level concepts without worrying about implementation specifics.
Term 1 of 13
A class is a blueprint or template for creating objects in object-oriented programming. It defines the properties and behaviors that an object of that class will have.
Object: An object is an instance of a class. It represents a specific entity with its own set of data and behavior.
Inheritance: Inheritance is the process by which one class inherits the properties and behaviors of another class. It allows for code reuse and promotes modularity.
Encapsulation: Encapsulation is the practice of hiding internal details of an object and providing access only through well-defined interfaces. It helps maintain code integrity and prevents unauthorized access.
Behaviors in programming refer to the actions or operations that an object can perform. They are defined as methods within a class and allow objects to interact with each other and manipulate data.
Variable Declarations: Variable declarations are statements that define a variable's name, type, and initial value. They are used to store data that can be accessed and manipulated by behaviors.
Constructors: Constructors are special methods used to initialize objects when they are created. They set the initial values of variables and prepare the object for use.
Inheritance: Inheritance is a concept where one class inherits properties and behaviors from another class. It allows for code reuse and promotes hierarchical organization of classes.
Methods are functions defined within a class that perform specific tasks or actions when called upon by an object. They can manipulate data, interact with other objects, or return values.
Parameters: Parameters are variables used in method declarations to receive input values when the method is called. They allow methods to accept and work with different data.
Return Type: The return type of a method specifies the type of value that the method will return after its execution. It can be void (no return value) or any other data type.
Overloading: Method overloading is a feature that allows multiple methods within a class to have the same name but different parameters, enabling flexibility and code reusability.
Encapsulation refers to the bundling of data and methods within a class, where the data is hidden from external access. It ensures that an object's internal state remains consistent by controlling how it can be accessed or modified.
Object-oriented programming (OOP): OOP promotes encapsulation as one of its fundamental principles. It allows for modular design by grouping related data and behaviors together within classes.
Access modifiers: Access modifiers control the visibility and accessibility of members (variables or methods) in a class. They determine whether other parts of the program can access or modify those members directly.
Information hiding: Information hiding is closely related to encapsulation and involves restricting direct access to internal details of an object. By hiding information, we protect it from being accidentally modified or misused outside its intended scope.
A default constructor is a special type of constructor that is automatically created by the compiler if no other constructors are defined in a class. It initializes the object's instance variables with default values.
Parameterized Constructor: A parameterized constructor is a constructor that takes one or more parameters to initialize the object's instance variables with specific values.
Instance Variables: Instance variables are variables declared within a class and hold data unique to each instance (object) of the class.
Object Initialization: Object initialization refers to setting initial values for an object's instance variables either through constructors or directly assigning values after creating the object.
Instance variables are variables declared within a class but outside any method. They hold unique values for each instance (object) of the class and define the state or characteristics of an object.
Methods: Methods are actions or behaviors associated with objects in a class. They define what an object can do or how it can interact with other objects.
Constructor: A constructor is a special method within a class that is automatically called when an object is created from that class. It initializes the object's state by assigning initial values to its instance variables.
Access Modifiers: Access modifiers determine the accessibility or visibility of classes, methods, and variables in object-oriented programming. They control which parts of a program can access certain elements and help enforce encapsulation and data hiding principles.