Class Code

Premium Content - Free Preview

In the previous node, you used the Circle Class without touching any of its code. In this node, we'll look at its actual code and make some changes.

This is the beginning of the Circle Class:

Below the Class declaration, the instance variables, which hold data about the Object and are available to all its code, are declared. In this case, they store information about the position and appearance of the Circle.

The constructor appears under the instance variables and is used to set up their initial values. A constructor is just a method with the same name as the Class that runs when an instance of the Class is created.

Currently, Circle's constructor just assigns hard-wired values (like a diameter of 30) to the variables, instead of allowing the variables to be set. Also, the Circle is not visible when created, but requires another call to the method makeVisible() to show up.

Constructor Task
It would be useful if you could set different initial values for the Circle. Create a new constructor that takes in parameters to set the values of diameter, xPosition, yPosition and color. To make the Circle visible on creation, call makeVisible() at the end of the constructor.

End of Free Content Preview. Please Sign in or Sign up to buy premium content.


  • The acclerateMissileToSpeed method is missing a pair of curly braces but that is an compile error in two lines of code i guess.

  • @Peter, thanks, its been fixed.

  • I guess it is something to do with second variable but, could I have a little help here?, thanks

  • @calo, can you find a private instance variable that is being accessed directly from outside the class?

  • now when I got it seems so obvious :)

  • The abstraction for this is mind blowing, at least it's starting now somehow to fall into place in my mind :)

  • I don't get this, and I have a question: Public Missile(int rocketFuel) is our constructor which takes in a parameter, and it has two variables speed and fuel.
    with this line of code:
    public void accelerateMissileToSpeed(Missile missile, int speed)
    we created a new variable with the type of Missile so does missile has the same properties of the constructor? but as a variable? can we use this technique to store in arrays? create our own type and store various information in one slot of an array?

  • I am not sure how to access the private instance variable. Does it matter if I use this. or super()?

  • You cannot access private instance variables from outside the object, that's why you use accessor methods.

  • I have the following but i dont see a circle, it gives no errors???

    public Circle(int diamterc,int Xpositie,int Ypositie, String kleur){

  • solved it, i guess the mistake was that is should be

    can someone confirm this?

  • about the last challenge. the variable speed is private, so millile cant reach that i guess. So i think thats the problem..

    i cant figure out the solution doh, so what is the solution?

  • got it
    while(missile.getSpeed() < speed){

    i was forgetting the capital S

  • Hello,

    I got the following error while attempting the constructor task (Note: I made a new constructor with the parameters, which I compiled and had no errors for but after I called(?) this constructor with my parameters which were Circle circle1 = new Circle(60, 89, 98, "red");):

  • I can't see the exact code you used. According to your message, the problem happened at line 124 of the Canvas code file, when it was trying to setForegroundColor. Do you see anything that could have gone wrong there?

  • Hello,

    I haven't made any changes to the Canvas class, but here's the code for line 124:


  • The default circle constructor sets the value of various instance variables that are used afterwards. Looks like your constructor takes in multiple parameters but then it doesn't assign their

Contact Us
Sign in or email us at [email protected]