Tino APCS

Summary

In this lesson you learned how to create and use objects such as SketchPad and DrawingTool. One can think of an OOP application as a simulated world of active objects. Each object has a set of constructors for its creation and a set of methods that can perform tasks. In the next lesson you will learn how to create your own classes.

Reflect on your understanding of these Lesson 1 ideas - if any of them is unclear, do something about it. Ask your teacher, search the Internet, watch a video, ask a friend or parent, etc.

A class is a blueprint that defines an object and can be used to create objects of that type. So far, you have created and used objects of type DrawingTool, SketchPad, and Color.

Ignoring special cases, classes have three important parts:

  • Attributes - Also known as instance variables, attributes are variables that describe an object and keep track of its current state.
  • Methods - Code that defines the behaviors an object can perform.
  • Constructors - Methods with the same name as the class that are used to create and initialize objects.

A class API tells you everything you need to know in order to create objects of that class and run its methods. The DrawingTool API is a good example.

Java includes thousands of built-in classes, many of which need to be imported because they're not inside the default java.lang package. The Color class is an example of this and is located in the java.awt package. To use the Color class you need to import java.awt.Color

You can add external classes, such as gpdraw.jar, that do not come with Java.

To create and use an object, you must: - Import the class, if needed.
Ex: import gpdraw.SketchPad; - Declare an object of that class.
Ex: SketchPad pad; - Create and initialize the object using a constructor
Ex: pad = new SketchPad(800, 600); - Command the object using its methods
Ex: pen.drawRect(200, 100);

Program comments should be used to add clarity to your code
Ex: pen.move(578, -25); // Move to base of tree

The general process that leads to a running Java program is:

  • You write source code in a human-readable .java file.
  • The .java file is compiled to produce a machine-readable .class file in bytecode format.
  • The Java Virtual Machine reads the bytecode and translates it as-the-program-runs into instructions specific to your machine (Mac, Windows, Linux, etc)

Every Java application starts execution in a main() method.

Dark Mode

Outline