AP Computer Science in Java
  • Introduction
  • Introduction to Programming in Java with Karel the Dog
    • Introduction to Programming with Karel
    • More Basic Karel
    • Java Programs and the Run Method
    • Karel Can't Turn Right
    • Methods in Karel
    • Top Down Design and Decomposition in Karel
    • Commenting Your Code
    • SuperKarel
    • For Loops
    • While Loops in Karel
    • If Statements
    • If/Else Statements
    • Control Structures Example
    • How To Indent Your Code
  • Basic Java
    • Printing in Java
    • Variables and Types
    • User Input
    • Arithmetic Expressions
    • Casting
    • Booleans
    • Logical Operators
    • Comparison Operators
    • For Loops
    • While Loops
    • If Statements
    • Loop-and-a-Half
    • Short-Circuit Evaluation
    • De Morgan's Laws
    • Strings
  • Methods
    • Java Methods
    • Methods and Parameters
    • Methods and Return Values
    • Javadoc and More Methods
    • Strings Methods
    • Strings and Characters
    • Exceptions
    • String Processing
  • Classes and Object-Oriented Programming
    • Introduction To Classes and Objects
    • Classes vs. Objects
    • Using a Class as a Client
    • Writing Classes
    • Writing Classes and Instance Methods
    • Getter and Setter Methods
    • Class Methods and Class Variables
    • Method Overloading
    • Local Variables and Scope
    • Key Terms for Classes
    • Objects vs Primitives
    • Inheritance
    • Class Design and Abstract Classes
    • Polymorphism
    • Interfaces
  • Data Structures
    • What Are Data Structures?
    • Introduction to Arrays
    • Using Arrays
    • ArrayList Methods
    • Arrays vs ArrayLists
    • 2D Arrays (Matrices or Grids)
    • Hashmaps
  • Algorithms and Recursion
    • What is an Algorithm?
    • Pseudocode
    • Linear Search
    • Binary Search
    • Selection Sort
    • Insertion Sort
    • Advanced: Recursion
    • Mergesort
Powered by GitBook
On this page
  • Objects
  • State
  • Behavior
  • Classes
  • Rectangle Class
  • Animal Class
  • Vehicle Class
  • Creating Objects
  • Using Objects

Was this helpful?

  1. Classes and Object-Oriented Programming

Introduction To Classes and Objects

Classes and Objects are utilized in Java as part of the object-oriented programming model. This model focuses on objects and the data and actions associated with the objects.

Objects

Objects are structures that contain a state and behavior. Every day objects we commonly use have states and behaviors. For example, a car is an object with both a state and a behavior.

State

The state contains the information about the specific object.

Thinking back to the car in this case. An example of a state, with the car, would be how much fuel it has.

Behavior

The behavior is the actions that can be performed on the specific object.

A behavior of the car may be to get the mileage from the remaining fuel.

Classes

Classes are the templates we use for creating objects.

Rectangle Class

Here is an example of a class that lets us create a rectangle, and get its area:

public class Rectangle
{
    private double width;
    private double height;

    public Rectangle(double rectWidth, double rectHeight)
    {
        width = rectWidth;
        height = rectHeight;
    }

    public int getWidth()
    {
        return width;
    }

    public int getHeight()
    {
        return height;
    }

    public int getArea()
    {
        return width * height;
    }

    public String toString()
    {
        String rectInfo = "Rectangle with width: " + width + " and height: " + height + 
            " and area: " + getArea();

        return rectInfo;
    }
}

Animal Class

Here is another example of a class that lets us create new animal objects:

public class Animal
{
    private String name;
    private boolean isPet;
    private int age;

    public Animal(String animalName, boolean isAnimalPet, int animalAge)
    {
        name = animalName;
        isPet = isAnimalPet;
        age = animalAge;
    }

    public String getName()
    {
        return name;
    }

    public boolean getPetStatus()
    {
        return isPet;
    }

    public int getAge()
    {
        return age;
    }

    public String toString()
    {
        String aInfo = "This animal's name is: " + name + " they are currently a pet: " +
            isPet + ". The animal is " + age + " years old.";

        return aInfo;
    }
}

Vehicle Class

Here is another example of a class that takes in a vehicle type, its age, and how many miles it has:

public class Vehicle
{
    private String vehicleType;
    private int vehicleAge;
    private double vehicleMiles;

    public Vehicle(String vType, int vAge, double vMiles)
    {
        vehicleType = vType;
        vehicleAge = vAge;
        vehicleMiles = vMiles;
    }

    public String getType()
    {
        return vehicleType;
    }

    public int getAge()
    {
        return vehicleAge;
    }

    public double getMiles()
    {
        return vehicleMiles;
    }

    public double estimateMilesPerYear()
    {
        return vehicleMiles / vehicleAge;
    }
}

Creating Objects

When creating a new object from a class you will use the following format:

[Class Name] [Object Name] = new [Class Name] (params);

// Here are some examples using the classes we created earlier

Rectangle rect = new Rectangle(20, 8);

Animal pet = new Animal("Cujo", true, 7);

Vehicle myTruck = new Vehicle("Truck", 10, 173600.4);

Using Objects

Here are some examples on how to create objects with our previous defined classes, and use them.

// Rectangle Class
public class StateBehavior_Rectangle
{
    // Here we will create a new rectangle, and then print its information.
    public void run()
    {
        // Create the rectangle
        Rectangle rect = new Rectangle(20, 8);
        // This will print "Rectangle with width: 20 and height: 8 and area: 160"
        System.out.println(rect);
    }
}

// Animal Class
public class StateBehavior_Animal
{
    // Here we will create a new animal, and then print its information.
    public void run()
    {
        // Create the animal and assign its attributes
        Animal myPet = new Animal("Cujo", true, 7);
        // Print out the animal info
        System.out.println(myPet);

        // This will print: "This animal's name is: Cujo they are currently a pet: true.
        // The animal is 5 years old."
    }
}

// Vehicle Class
public class StateBehavior_Vehicle
{
    // Here we will create a new vehicle, and then calculate how many miles/year it has.
    public void run()
    {
        // Create the vehicle and assign its attributes
        Vehicle myTruck = new Vehicle("Truck", 15, 173600.4);

        // Now we will use the `estimateMilesPerYear()` method to determine how many miles
        // per year, it has been driven.
        System.out.println("Your yearly mileage is: " + myTruck.estimateMilesPerYear());
    }
}
PreviousClasses and Object-Oriented ProgrammingNextClasses vs. Objects

Last updated 5 years ago

Was this helpful?