Lesson 1:
Three big concepts:
An object is an entity that contains data and its own methods
for manipulating that data.
A class is a collection of objects. The class definition provides a
prototype for all of its objects.
A class can inherit all or most of its definition from another
class. In Java we say that a class
extends another and objects in
the extended class will inherit the data and methods from the original class
plus will probably have additional data and/or methods.
- Creating an Object
The constructor for a class has the same name as the class. We create an
object as follows:
ClassName objectName = new ClassName(argument list);
- Using an Object
We use the methods of an object as follows:
objectName.methodName(actual arguments);
Try this:
- Look up the Frame class. Construct a Frame object called
myFrame
with a title "My first frame".
The frame class inherits the method setSize(int width, int height)
from the Component class which can be used to change the size of the
Frame. Use this method to change the size of myFrame to a width
of 100 and a height of 200.
The Frame class inherits the method setVisible(boolean b) from the Component
class which allows us to make the Frame visible or not depending on
whether we set b to true or false. Use this method to make
myFrame
visible.
Look up the method setLocation and use this method to move the Frame
on the screen to position (10,20).
- Look up the Color class. Construct a Color object called
myColor
with a red value of 10, a green value of 100, and a blue value of
50. How would you make a brighter or darker version of this color?
- Look up the Button class. Construct a Button object called
myButton
with a label "Push me". How would you change the label on myButton
to "Push me, again" ?
- Look up the Graphics class. Construct a Graphics object
called g. How would you draw a line that starts at point
(10,20) and ends at point (50,60) in the graphics object?