Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1

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 Class would contain the following methods: moveUp(), moveDown(), moveLeft(), moveRight(), grow(), shrink()

3 class NameOfClass { //variable – the values that define the object //methods – what they do }

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

5 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);

6 Smiley face Location (x & y) radius Color (Red, Green, and Blue values) Eyes (x, y, size, Color, isOpen) Mouth(x, y, size, Color, isOpen, isSmile) Karel the Robot Location (x & y) numBeepers direction Image

7 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

8 returnTypemethodName(arguments){ //method code }

9 Can be a: primitive (double, int, char) another object nothing (void)

10 Use the “.” operator Ex: theRobot.move(); aCircle.radius = 3;

11 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++).

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

13 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”

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

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


Download ppt "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."

Similar presentations


Ads by Google