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()
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.
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);
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
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
returnTypemethodName(arguments){ //method code }
Can be a: primitive (double, int, char) another object nothing (void)
Use the “.” operator Ex: theRobot.move(); aCircle.radius = 3;
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++).
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.
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”
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.
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.