A way to pull together related data A Shape Class would contain the following data: x, y, height,...

15
Objects

Transcript of A way to pull together related data A Shape Class would contain the following data: x, y, height,...

Page 1: 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.

Objects

Page 2: 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.

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?

Page 3: 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 Syntax

class NameOfClass {//variable – the values that define the

object

//methods – what they do

}

Page 4: 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.

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

Page 5: 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.

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

Page 6: 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.

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

Page 7: 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.

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

Page 8: 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.

returnTypemethodName(arguments){

//method code

}

Method Syntax

Page 9: 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.

Can be a:primitive (double, int, char)another objectnothing (void)

Return Types

Page 10: 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.

Use the “.” operatorEx:

theRobot.move();aCircle.radius = 3;

Accessing variables & methods

Page 11: 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.

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

Page 12: 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.

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

Page 13: 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.

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

Page 14: 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.

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

Page 15: 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.

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