Objects What are they A way to pull

Objects

What are they? �A way to pull together related data A Shape Class would contain the following data: x, y, height, width �Then associate methods with that data A Shape Class would contain the following methods: move. Up(), move. Down(), move. Left(), move. Right(), grow(), shrink()

Class Syntax class Name. Of. Class { //variable – the values that define the object //methods – what they do }

Seen Before 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.

Class = Blueprint �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 j. Karel Robots. �Ex: Robot bob = new Robot(); �Ex: Color background = new Color(10, 223, 97);

A Class can contain other Objects �Smiley face � Location (x & y) � radius � Color (Red, Green, and Blue values) � Eyes (x, y, size, Color, is. Open) � Mouth(x, y, size, Color, is. Open, is. Smile) �Karel the Robot � Location (x & y) � num. Beepers � direction � Image

Methods �Have four key parts: �Name – what the method is called �Arguments – data (variables & other objects) passed into the method �Return Type – what if anything that the method returns when called �Code – what the method does

Method Syntax return. Type method. Name(arguments){ //method code }

Return Types �Can be a: � primitive (double, int, char) � another object � nothing (void)

Accessing variables & methods �Use the “. ” operator �Ex: � the. Robot. move(); � a. Circle. radius = 3;

The Constructor �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++).

Private vs Public �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.

A Reference To Something Else �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”

Equality of Objects �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.

Class Variables versus Method Variable �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.
- Slides: 15