AP Computer Science - Constructing Objects: Int Double
AP Computer Science - Constructing Objects: Int Double
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.
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
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
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.
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.