- 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
Premium Content - Free Preview
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.
End of Free Content Preview. Please Sign in or Sign up to buy premium content.
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.