0% found this document useful (0 votes)
40 views4 pages

AP Computer Science - Constructing Objects: Int Double

This document provides information on constructing objects in Java, including: - The code structure of a Java class with a main method - Debugging tips like checking syntax and variable definitions - Primitive data types like int and double - The difference between classes and objects, with rectangles used as an example - The syntax for creating a rectangle object with specified x, y, width, and height values - Available rectangle class methods like getX() and how to import packages

Uploaded by

jeongcho96
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
40 views4 pages

AP Computer Science - Constructing Objects: Int Double

This document provides information on constructing objects in Java, including: - The code structure of a Java class with a main method - Debugging tips like checking syntax and variable definitions - Primitive data types like int and double - The difference between classes and objects, with rectangles used as an example - The syntax for creating a rectangle object with specified x, y, width, and height values - Available rectangle class methods like getX() and how to import packages

Uploaded by

jeongcho96
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 4

AP Computer Science Constructing Objects

Code Structure of a Class public class ClassName { public static void (String[] args) { --------; --------; CONTENTS OF METHOD ; --------; --------; } }

Debugging Tips

Try to cut/paste as much code as you can from previous programs. Check syntax in book (or appendix) and code examples in textbook. Look specifically for capitalization errors, punctuation (, (), {}, ;) Check each variable to be sure it is defined (type and value) Retype (sometimes minor errors are so hard to spot its faster to just retype.) Read error messages from the compiler Know the expected result before you start and print a line (Expected result is xxx) so you will know if your program worked properly.

Number Types (Primitive types)

int use for integer values (e.g., 1,2,3,4,) double use for floating-point numbers (e.g. 1.2, 1.456, 3.14, ) These types are called PRIMITIVE TYPES. They are NOT classes (like String), NOT objects, and NOT methods. These types can be used with arithmetic operators ( + - * /) and PEMDAS applies.

Constructing Objects

So far we have only used 2 types of objects: String objects and System.out objects

Classes vs. Objects

A class defines the blueprint for an object it determines what an object KNOWS.

Ex:

The Rectangle class creates objects that are rectangles. What does it take to DEFINE a unique rectangular object? In each group below, what do the rectangles have in common? How are they different?

1)

2)

3)

PROGRAMMER View

COMPUTER View

Syntax

Rectangle myRectangle = new Rectangle(int x, int, int w, int h)

You must CREATE the object and NAME the object.

Methods for the Check the appendix for the list of methods, but the primary ones you will use Rectangle class are: getX() getY() getWidth() getHeight() translate(int x, int y)

Write the statement that will create a square with its center ad (100,100) and side length of 20.

Consider the programmers view first, then determine the computers view.

Packages

There are many classes defined in java (your appendix only shows a subset of them). Some of the classes are always available to use (like String), however others must be imported before you can use them. Classes are grouped together into packages, so you import that package at the beginning of your program and you can use all the methods in that package.

import java.awt.Rectangle (see page 780)


Sample Program

Type and run the program on page 48. You can then use it as a reference for proper syntax and structure.

Assignment:

Programming Exercises Chapter 2: Do 2.1 and 2.2 in the same program, 2.3 Also start working on the assignments in WileyPlus.

You might also like