- Objects
- Classes
- Inheritance
-
Programming in Java on Your Computer - Classes and Objects in BlueJ
- Trying out Code in BlueJ
- The Code for Creating and Using Objects
- Source Code and Methods
- Accessors and Mutators
- Constructor Code
- Scope
- Inheritance
-
BlueJ Review - Class Code
- Simple Debugging
- Interactive Picture
- Refactoring Code with Inheritance
Class Code
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
Peter Lacres
Jan 20, 8:11 AMThe acclerateMissileToSpeed method is missing a pair of curly braces but that is an compile error in two lines of code i guess.
Learneroo
Jan 20, 9:48 AM@Peter, thanks, its been fixed.
calo
Jan 28, 11:26 AMI guess it is something to do with second variable but, could I have a little help here?, thanks
Learneroo
Jan 28, 12:10 PM@calo, can you find a private instance variable that is being accessed directly from outside the class?
calo
Jan 29, 3:18 PMnow when I got it seems so obvious :)
David
Mar 25, 1:59 PMThe abstraction for this is mind blowing, at least it's starting now somehow to fall into place in my mind :)
Asım
Jan 22, 5:34 PMI 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?
apart from my question I can't figure out what is wrong with the code :S please help
I am not sure how to access the private instance variable. Does it matter if I use this. or super()?
Learneroo
Mar 16, 12:01 PMYou cannot access private instance variables from outside the object, that's why you use accessor methods.
thales
Jul 9, 6:59 AMI 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();
thales
Jul 9, 7:05 AMsolved it, i guess the mistake was that is should be
diamter=diamterc
can someone confirm this?
thales
Jul 9, 7:32 AMabout 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?
thales
Jul 9, 7:53 AMgot it
while(missile.getSpeed() < speed){
i was forgetting the capital S
Utsav
Jan 29, 12:14 PMHello,
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");):
java.lang.NullPointerException
at Canvas.setForegroundColor(Canvas.java:124)
at Canvas$ShapeDescription.draw(Canvas.java:216)
at Canvas.redraw(Canvas.java:167)
at Canvas.draw(Canvas.java:104)
at Circle.draw(Circle.java:181)
at Circle.makeVisible(Circle.java:42)
at Circle.(Circle.java:33)
Could you please tell me why this happened?
Thanks,
Utsav
Learneroo
Jan 29, 7:25 PMI 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?Utsav
Jan 30, 5:57 AMHello,
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);
Here's the code for my Circle constructor:
public Circle(int diameter, int xPosition, int yPosition, String color)
{
makeVisible();
}
Thanks,
Utsav
Learneroo
Jan 30, 7:48 AMThe 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
values. This causes an error when the code later checks the value of
colorString
but it's null. You'll need to set the values of the variables in your constructor. (See https://www.learneroo.com/modules/18/nodes/133). Good luck!