- 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
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.
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!