Using Java Objects in Java

Download Report

Transcript Using Java Objects in Java

A Simple Object Oriented Program
public class Simple {
public static void main (String [] args) {
System.out.println(“howdy”);
}
}
System.out is an object of class PrintStream,
provided by the Java library
println is a method of the PrintStream class
Objects and Classes
 Object: entity that contains data and can
performs actions in a program (by invoking
methods)
 Class: describes a type of object, a class must be
defined in order to ‘create’ an object
 So, each object belongs to a class
 Class determines legal methods for an object
System.out.fill();
// Error, no fill method
System.out.println(“howdy”); // OK
Creating Objects
The System.out object has already been created, and
already exists for your use in a Java program.
Typically, you must create an object before you can
use it. In order to create an object, a class must
exist which ‘describes’ this type of object . To use
this class, you must be familiar with the creation
methods (constructors) it provides.
A constructor method always has the same name as
the class.
One class which already exists for your use in the
Java library is the Rectangle class. Let’s create a
Rectangle object………
Two of the constructors available with the
Rectangle class:
Rectangle()
Rectangle(int x, int y, int width, int height)
www.java.sun.com provides
class specs (see API specs)
Declare a rectangle reference variable:
Rectangle myrec;
Construct a rectangle:
myrec = new Rectangle(5, 10, 20, 30);
Adjust the size of this Rectangle object by
calling the setsize method of the Rectangle class:
myrec.setsize(45,15);
• Print the Rectangle object
• System.out.println(myrec);
prints
java.awt.Rectangle[x=5,y=10,width=45,height=15]
Attributes of a Rectangle Object
Syntax: Variable Definition
TypeName variableName;
TypeName variableName = expression;
–Example:
Rectangle myrec;
Rectangle box = new Rectangle();
–Purpose:
To define a new variable of a particular type
and optionally supply an initial value
Syntax: Object Construction
• new ClassName(parameters)
–Example:
• new Rectangle(5, 10, 20, 30)
• new Car(“red”,
“Ford”,”Pinto”, 4.5)
–Purpose:
• To construct a new object, initialize it with
the construction parameters, and return a
reference to the constructed object.
Object Variables
Declare
Rectangle crispyCrunchy;
Declare and initialize:
Rectangle cerealBox = new Rectangle(5, 10, 20, 30);
 Once you have an object, you can apply methods:
cerealBox.translate(15, 25);
crispyCrunchy.translate(10,5); //error !!
Uninitialized and Initialized Variables
Rectangle cerealBox;
Rectangle cerealBox = new Rectangle(5,10,20,30);
These variables are references (like pointers) to objects,
SO:
Rectangle r;
r = cerealBox;
is NOT the same as:
r = new Rectangle(5,10,20,30);
In the first case, two variables will refer to
the SAME object, in the second, an additional
object is created and referred to by
crispCrunchy.
Two Object Variables Referring to the Same Object
Writing a Test Program
 Invent a new class, say MoveTest
 Supply a main method (which makes this an
application class)
 Place instructions inside the main method
 Import any library classes you need by specifying the
package and class name:
import java.awt.Rectangle;
 You don't need to import classes in the java.lang
package such as String and System
Syntax : Importing a Class from a Package
• import packageName.ClassName ;
–Example:
•
import java.awt.Rectangle;
–Purpose:
• To import a class from a package for use in a
program
File MoveTest.java
import java.awt.Rectangle;
public class MoveTest {
public static void main(String[] args){
// declare and create the rectangle
Rectangle cerealBox = new
Rectangle(5,10,20,30);
// move the rectangle
cerealBox.translate(15, 25);
// print the moved rectangle
System.out.println(cerealBox);
}
}
Using Class Methods
In order to call a class method successfully,
you must examine it’s signature:
// the following bold signature indicates how to use this
// method of the Rectangle class
public boolean contains (int x, int y) {
//method body
}
Using Class Methods
 access specifier (such as public)
 return type (such as String or void)
 method name (such as contains or
setSize)
 list of parameters (number, type and purpose of
parameters needed in call)
public boolean contains (int x, int y) {
//method body
}
• This method is ‘callable’, because its access specifier is
public
• This method will return a true or false result, so the call
should occur within an expression
• The method is called using the name ‘contains’
• the call must provide 2 arguments
Call:
myrec.contains(6,7)