Class Code


Collapse Content

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.

You can then compile your code and Create a Circle using your new constructor in BlueJ to test it out. You should now be able to provide specific variables when creating the Circle, and the Circle should show up visually on its own.


Method Access
Above are some additional methods from Circle. The bottom two methods, draw() and erase(), are private, which means that they are only available for use within Circle's code. Other Objects cannot access those methods, so they will not show up as available on the BlueJ Object bench. Methods that are meant for internal use should be declared private, which will ensure they are not used outside of the Class.

You can create a method or variable without declaring its access-level, and then it will default to semi-public access. This means other Classes will be able to access those methods and variables.

Using methods
The methods draw() and erase() clearly play important roles in displaying the Circle, since they display and hide the visual pane. They can be used without knowing exactly how they work, as long as you know what they do, which can be determined from their name and the comment description.

For example, look at the method changeColor. It sets the Circle color with a new Color from the parameter, and then calls draw() to update the visual pane. You can write your own methods that make changes and call draw() afterwards.


Method Task
Previous methods allowed for relative movement of Circle from one point to another one, but you might want to set the exact position of Circle as you did in the Constructor. Create a new method setPosition which takes in 2 int parameters and uses them to set xPosition and yPosition of Circle. Invoke draw() at the end of your method to update the visual pane.

Challenge

You have Missile Object. What would you store properties like velocity, weight and fuelType as?

Please sign in or sign up to submit answers.

Alternatively, you can try out Learneroo before signing up.

Challenge

This is the Class Missile:

public class Missile
{
    private int speed;
    private int fuel;
    
    public Missile(int rocketFuel)
    {
       speed = 0;
       fuel = rocketFuel;
    }
    
    public void fireRocket()
    {
        fuel = fuel - 1;
        speed = speed +1;
    }
    
    public int getSpeed()
    {
        return speed;
    }    
}

The following method appears in the same project in the Class ControlMissile. One of the lines of code in it will cause a compiler error. Can you find it and type in the correct version of that line?

public void accelerateMissileToSpeed(Missile missile, int speed) {
        while(missile.speed < speed){
            missile.fireRocket();
        }  
}

Please sign in or sign up to submit answers.

Alternatively, you can try out Learneroo before signing up.

Comments

  • 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?

    cont...
  • 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){
    diamterc=diameter;
    xPosition=Xpositie;
    yPosition=Ypositie;
    color=kleur;
    makeVisible();

    }
    
  • solved it, i guess the mistake was that is should be
    diamter=diamterc

    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");):

    cont...
  • 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:

    if(colorString.equals("red"))
    graphic.setColor(Color.red);

    cont...
  • 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

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