Chapter One
Java Objects model objects from a problem domain. Objects are created from classes. The Class describe the kind of object; the object represent individual instantiations of the class. Objects can be categorized as all objects of a particular kind.
When talking in general; we are dealing with a class and when we are talking about a particular object; we are dealing with an object; an instance of a class.
in object-oriented programming we refer to a object as an instance. "Instance" is roughly a synonyms with "object"; when pointing out that the object is an instance of a particular class.
We communicate with Objects by invoking methods on them. Objects behave or do something if we invoke a method. A method represent the behavior of an object.
Methods can have parameters to provide additional information for a task. Methods may require additional information; the additional information are called parameters. Parameters define a 'type' and 'name'. Parameters have types; the type defines what kinds of values the parameter takes.
The header of a method is call signature. It provide information needed to invoke that method. For example, the method moveHorizontal(int distance). moveHorizontal is the signature of the method and the information in between parenthesis tells us what kind of information the method takes; in this example, it takes type of integer; int for integer. a numeral data type.
If method has no parameters, the method is followed by a pair of empty parenthesis.
Multiple instances can be created from single class. Instances are objects of a class. If I have class cars I can create many car objects as I like; these car objects are instances of the class cars.
State
The set of values that define an object's attributes is called the state of an object. E.g. (x-position, y-position, color, diameter, shape, visibility, etc.) The state of an object is determined by storing the values in fields, variables in some other languages.
Some methods, when called change the state of an object; for instance if I have a object named ball and called a method moveRight(). The moveRight method will change the state of the object ball; the ball's position will change.
Objects of the same class all share the same fields, the value of a particular field may vary; but the name, number and type of the field are the same because objects of the same class share the same attributes; the attributes in an object are defined in the class that they're created from and are not defined from the object itself. The same goes with methods
Date types
Int is a numerical data type; String is also a kind of data type, strings can be anything within double quotation marks: " I am a string 12444, %%$&^^"
Exercise 1.9
Choose from one of the images below and achieve the same output; write down what you needed to do in order to achieve this.
I chose the one to the right and modified the position of the sun to fit my own desires.
For this exercise I did the following:
- From the Class circle I created 4 objects
- First object: I named it groundCircle. I made the object visible and gave it a diameter or a 1000 pixels with a xposition of -270 and a yposition of 190 to center it, and I finally colored it green to make look like the above picture.
- From the same package where the Class Circle belongs to, I created 2 objects of type person; one smaller than the other. Each person object has the same fields with different values: Height, Width, Xposition, Yposition, Color and isVisible. The value differ as each person object.
- Finally, I created another object from the same Class obviously. I called is sunCircle to represent the sun. The sunCircle has the attributes as the groundCircle with different values make it appropriate for this exercise.
Here is my version:
1 comment:
Great post, keep posting on java. Java Course In Pune
Post a Comment