- 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
Interactive Picture
The Picture Class you created may look nice, but it doesn't do much besides create a static picture. In Object-Oriented Programming, you can have Objects interact with each other to do more exciting things than just sit there. For example, it would be cool if you could change the colors or move multiple Objects with one action. Currently, your code probably looks like the code below, where each Object is created and then forgotten:
public class Picture
{
public Picture()
{
Circle circle1 = new Circle(30, 30, 60, "blue");
Circle circle2 = new Circle(30, 60, 60, "blue");
Triangle triangle1 = new Triangle();
//continued code...
}
}
The Circles are created, but they won't be available to other methods in your program. In fact, if you inspect your Picture Object on the bench, you'll see it has nothing to show. This is the first thing that should be fixed.
Instance Variable Task
- Declare all the shapes that you will use as instance variables at the beginning of your Class code. You might as well give them descriptive names this time. For example:
Circle eye1;
- Since you just declared the types, your constructor shouldn't repeat that. Remove the type declarations from the constructor so it just assigns each variable to a new Object. For example:
eye1 = new Circle(30, 30, 60, "blue");
The Picture cannot do anything more yet, but at least it now has some accessible data. If you inspect a Picture Object again, you'll now be able to see (and inspect) each instance variable you created:
Simple Method
Now that Picture's shapes are accessible, you can create a method that changes the color of all its Circles:
- Create a method
changeCirclesColor
that takes in a Stringcolor
as a parameter. - Invoke the
changeColor
method on each one of Picture's Circles and pass incolor
as a parameter.
public void changeCirclesColor(String color){ eye1.changeColor(color); //etc... }
You should now be able to create a Picture and change the colors of all its circles at once.
Holding the Circles Together
The above code works fine, but its too repetitive. You shouldn't have to call each Circle individually every time you want to do something with all of then. To solve this problem, it would help if you could hold all of the Circles in one "container" so that you could just iterate through the container and do something to all of them.
The array will work to hold the Circles. Previously, you dealt with arrays that held simple integers, but arrays can hold Objects too. You just need to declare the array of the Class type that you want to hold. For example, this is the code to create a 5-cell array named blobBox
to hold Objects of type Blob
:
Blob[] blobBox;
blobBox = new Blob[5];
Improving the method
You can now update your Picture class so that all of the Circles are put into one array:
- Declare a Circle array
circles
as an instance variable. - In your constructor, assign
circles
to a new array of size 3. - At the end of your constructor, "fill in" each cell of
circles
with a Circle object. For example:circles[0] = eye1;
A Circles array should be available in your code, and again you can inspect it in the Object bench.
You can now improve the changeCirclesColor
method. Instead of calling each Circle individually, you can just iterate through circles
. As in arrays and loops, you will go through the array to access each element in it, but here you will call the changeColor
method on each Object accessed.
public void changeCirclesColor(String color) { for(int i=0; i < circles.length; i=i+1){ circles[i].changeColor(color); } }
This code allows you to write only one line that calls changeColor
and avoids the great programming sin of code duplication. While it may not seem to have saved much, imagine if you had more than 3 items to deal with, such as 10 or 500!
Creating Additional Methods
You can create similar methods to do any other Circle action to all of the Circles. For example, you can create a method to move them all in the same direction.
This may not be Angry Birds, but it's a start!
Challenge
Did you get the changeCirclesColor
method to work?
Please sign in or sign up to submit answers.
Alternatively, you can try out Learneroo before signing up.
Comments
Honzis
Dec 23, 3:08 PMCircle[] circles = new Circle[3];
circles[0] = eye1;
circles[1] = eye2;
Result in ... ']' expected ... error in circles[0] definition ?
Learneroo
Dec 23, 10:25 PM@Honzis, you should move your code to the constructor.
David
Mar 27, 4:29 PMNot Angry Birds, but... Angry Doggys here we go!
Brandon
Jul 21, 4:48 PMReally cool stuff. At first, I wasn't crazy about using BlueJ, but everything is starting to piece together very nicely. Thanks!
Sarick Shah
Oct 9, 12:24 AMjava.lang.NullPointerException
at circles[i].moveRight();
Stanixlav
Aug 7, 12:47 PMHow do I get a check on this? You've turned me into a slave for completion checks...
boolean lessonComplete;
public void gimmeCheck(){
lessonComplete = true;
}
interactivePicture.gimmeCheck();
Learneroo
Aug 8, 10:09 PMCurrently there's no online question/check for this page since you do the coding on your computer.
catypus
Jun 12, 12:21 AMHow do I get the checkbox for this page? I'm finished the unit... but I can't find any quiz on this page to finish. I just can't stand not having my checkbox checked!!!
Learneroo
Jun 12, 7:21 AMJust added a simple question so you can mark your progress.