A way to pull together related data A Shape Class would contain the following data: x, y, height,...
-
Upload
julie-rogers -
Category
Documents
-
view
213 -
download
1
Transcript of A way to pull together related data A Shape Class would contain the following data: x, y, height,...
Objects
A way to pull together related dataA Shape Class would contain the following data:x, y, height, width
Then associate methods with that dataA Shape Class would contain the following methods:moveUp(), moveDown(), moveLeft(), moveRight(), grow(), shrink()
What are they?
Class Syntax
class NameOfClass {//variable – the values that define the
object
//methods – what they do
}
Haven’t we already been using classes?
The answer is yes because JAVA is exclusively an object oriented programming language. So all code must be placed in a class declaration. The first method called is the main method.
Seen Before
A class only tells the computer what goes into the “object” when constructed and how it behaves. Kind of like a blue print.
In order to use it, we have to use the new operator similar to how we used it when we declared our jKarel Robots.
Ex: Robot bob = new Robot();Ex: Color background = new Color(10, 223,
97);
Class = Blueprint
Smiley faceLocation (x & y)radiusColor (Red, Green, and Blue values)Eyes (x, y, size, Color, isOpen)Mouth(x, y, size, Color, isOpen, isSmile)
Karel the RobotLocation (x & y)numBeepersdirection Image
A Class can contain other Objects
Have four key parts:Name – what the method is calledArguments – data (variables & other objects)
passed into the methodReturn Type – what if anything that the
method returns when calledCode – what the method does
Methods
returnTypemethodName(arguments){
//method code
}
Method Syntax
Can be a:primitive (double, int, char)another objectnothing (void)
Return Types
Use the “.” operatorEx:
theRobot.move();aCircle.radius = 3;
Accessing variables & methods
The Constructor is a method that is called whenever an object is created using the new operator.
Has the (exact) same name as the class.In it you should set up variables and whatever
else is needed any time an object of this class is created.
Just like any other method, it can take in variables.
Even if you do not provide one, JAVA will provide one for you even if it does nothing(just like C++).
The Constructor
All class variables and methods are either public or private or protected.
All variables should be made either private or protected
This is needed so that objects do not behave in such a way that is not programmer defined.
It also helps protect access to various information.
Variables can still be changed but they should changed in methods that you define.
Private vs Public
When you declare an object in JAVA, you are only creating a reference to an Object. It is kind of like declaring an address that gives a location for something. That is why you have to create an object using the new operator.
If you create another object and assign it to the Object name then the old object is no longer reference and JAVA will delete it.
An object can refer to itself using the keyword “this”
A Reference To SomethingElse
Because Objects are complex things, you can not compare them using ==. JAVA will let you but == will not compare the objects themselves but instead check that the reference the same location in memory.
When checking for equality, you will want to define the .equals method.
Equality of Objects
Variables declared inside the class can be accessed by anywhere inside of the class.
Variables declared inside of a method can only be accessed inside of that method and no longer exist once the method finishes executing
There can be a class variable and a method variable by the same name. That should be avoided but if it does happen, the class variable can be referenced using “this.” followed by the class variable name.
Class Variables versus Method Variable