The way my teacher writes and organizes code is very different than what is proposed in my supplementary textbook. In a recent lab, we were required to use the wheels library to display several objects in a single window. Here is the Snowman class that was written by my teacher (code for other objects have not been included for convenience):
public class Snowman {
private Ellipse _top;
private Ellipse _middle;
private Ellipse _bottom;
public Snowman() {
_top = new Ellipse();
_top.setColor(Color.WHITE);
_top.setFrameColor(Color.BLACK);
_top.setFrameThickness(1);
_top.setSize(80, 80);
_middle = new Ellipse();
_middle.setColor(Color.WHITE);
_middle.setFrameColor(Color.BLACK);
_middle.setFrameThickness(1);
_middle.setSize(120, 120);
_bottom = new Ellipse();
_bottom.setColor(Color.WHITE);
_bottom.setFrameColor(Color.BLACK);
_bottom.setFrameThickness(1);
_bottom.setSize(160, 160);
}
public void setLocation(int x, int y) {
_top.setLocation(x + 40, y - 170);
_middle.setLocation(x + 20, y - 100);
_bottom.setLocation(x, y);
}
}
This object, among others, is later instantiated in the SnowmanCartoon class:
public class SnowmanCartoon extends Frame{
private Snowman _snowman;
private Eyes _eyes;
private Hat _hat;
private Bubble _bubble;
public SnowmanCartoon() {
_snowman = new Snowman();
_snowman.setLocation(100, 300);
_eyes = new Eyes();
_eyes.setLocation(165, 150);
_hat = new Hat();
_hat.setLocation(152, 98);
_bubble = new Bubble();
_bubble.setLocation(280, 60);
}
public static void main(String[] args) {
new SnowmanCartoon();
}
}
Here are my concerns:
-
In both of these classes, why is there a method of the same name as the class, and what is its purpose?
-
Why is the method
setLocation()a void method while the methodSnowman()is not, even thoughSnowman()does not return anything? -
In the
SnowmanCartoonclass, when it saysprivate Snowman _snowman;and_snowman = new Snowman();, isSnowmanreferring to theSnowmanclass, or rather theSnowman()method? -
If the instantiation of the
Snowmanobject is referring to theSnowman()method to set all of its properties, why don’t I need to use the dot operator:Snowman.Snowman()? -
In my textbook, instance variables and methods are declared in a one class and are instantiated in another. However, they seem to occur simultaneously in my teacher’s code.
The method that is named the same as a class is called “constructor” of the class. It is used to instantiate an object.
Example:
Note that constructor is just like any other method, however, it does not have a return type. It basically returns “this” object.
When you call
this will actually invoke the constructor and create the object.
Note that you can have multiple constructors in a class by using different number and types of parameters.
In java, if you do not include any constructor, then any class will by default have the default parameterless constructor.
Tons of information available on constructors online. They are the basic concepts to start learning object oriented programming. Some more information here.
–EDIT–
answering question about how to call specific question.
Take these two constructors
Then when you instantiate object