Experiments on Objects
Table of contents
Experiments on figures project
- Start BlueJ and open the project named figures. Right-click on one of the circle objects (not the class!), Choose
makeVisiblefrom the menu. Observe what happens…
Try:moveRightmoveDownmakeInvisiblemakeVisible
- Experiment: What happens if you call
moveDowntwice? Or three times? What happens if you callmakeInvisibletwice? - Invoke the
moveHorizontalmethod. You will see a dialog appear that prompts you for some input. Type in 50 and click OK. Observe what happens…
Discuss: How is it different frommoveRightormoveLeft? - Try invoking the
moveVertical,slowMoveVertical, andchangeSizemethods before you move on. - Challenge: Find out how you can use
moveHorizontalto move the circle 70 pixels to the left. Experiment ifmoveVerticalmoves the circle up in the same way. - Try invoking the
changeColormethod and see what happens when you specify a color that is not known. - Invoke the
changeColormethod, and write the color into the parameter field without the quotes. What happens? - Create several circle objects on the object bench. You can do so by selecting
new Circle()from the pop-up menu of theCircleclass. Make them visible, then move them around on the screen using the “move” methods. Make one big and yellow; make another one small and green. Try the other shapes too: create a few triangles, squares, and persons. Change their positions, sizes, and colors.
Discuss: Does changing an attribute of one object affect another object? - Experiment: Make sure you have several objects on the object bench, and then inspect each of them in turn. Try changing the state of an object (for example, by calling the moveLeft method) while the object inspector is open. You should see the values in the object inspector change.

- Select Show Terminal from the View menu. This shows another window that BlueJ uses for text output. Then select Record method calls from the terminal’s Options menu. This function will cause all our method calls (in their textual form) to be written to the terminal. Now create a few objects, call some of their methods, and observe the output in the terminal window.
- Let’s now save this code somewhere safe and we will revisit it later.
- Select Show Code Pad from the View menu. This should display a new pane next to the object bench in your main BlueJ window. This pane is the Code Pad. You can type Java code here.
- Try copying the recorded lines one-by-one and paste in Code Pad and observe how those instructions are doing the same thing what we did using mouse-clicks.
Figures Challenge
Go to Practice to find the challenges.
Experiments on house project
- Go to Learn, download the house project ==> extract it to your projects folder.
- Open the house project using BlueJ. Create an instance of class
Pictureand invoke itsdrawmethod. Also, try out thesetBlackAndWhiteandsetColormethods. How do you think thePictureclass draws the picture?
Observing Picture class
- Look at the pop-up menu of class
Pictureagain. You will see an option labeled Open Editor. Select it. This will open a text editor displaying the source code of the class. - In the source code of class
Picture, find the part that actually sets the picture to black and white and change it to red and yellow.
Practice: Change them to any colors of your choice. Once satisfied and understood how it’s working, then turn it back to black and white. - In the source code of class
Picture, find the part that actually draws the picture. Change it so that the sun will be blue rather than yellow. -
Add a second sun to the picture. To do this, pay attention to the field definitions close to the top of the class. You will find this code:
private Square wall; private Square window; private Triangle roof; private Circle sun;You need to add a line here for the second sun. For example:
private Circle sun2;
Then write the appropriate code in two different places for creating the second sun and making it visible when the picture is drawn.
Download objects-practice projects
Experiments on objects-practice project
- Download and extract days project in your bluej_projects folder. Open in BlueJ. There are two options to create instances. Explore all available methods.
- Which option of
Dayclass creates aDayinstance with today’s date, month, and year? - Can you create a
Dayinstance of any given year, month, and date? Try giving values like99foraDayOfMonthoraMonthoraYear. Does it accept? - Find a way to calculate age of a person using
Dayobject’s methods. - Can the same process be used to find how long a person lived?
- Try checking the new
Dayvalue usingString toString()method after adding some number of days. Verify if it is correct by referring to the calendar. - Download and extract rectangles project in your bluej_projects folder. Open in BlueJ. Create an instance of
Rectangleand draw it. What do you observe? Is thedraw()method ofRectanglein this project different from thedraw()method ofRectanglein figures project? - Create an instance of
Rectangleusingnew Rectangle()and create anotherRectangleinstance usingnew Rectangle(double x, double y, double width, double height). What do you observe when you try drawing both of these rectangles? What do you observe? What do you think is the reason behind the result? - How is
setColor()method of theRectangleclass in this project different from that of house project? - What happens when you invoke
fill()method on yourRectangleinstance? - Try
translate()andgrow()methods onRectangleinstances and observe the difference in behaviors. What is the difference? - Download and extract pictures project in your bluej_projects folder. Open in BlueJ. Try different ways of creating
Pictureinstances. Try calling various methods available and see how many methods you are able to use as expected.
Experiments on lab-classes project
- Go to Learn, download the lab-classes project ==> extract it to your projects folder.
- Create an object of class
Student. You will notice that this time you are prompted not only for a name of the instance, but also for some other parameters. Fill them in before clicking OK. (Remember that parameters of typeStringmust be written within double quotes.) - Create some
Studentobjects. Call thegetNamemethod on each object. Explain what is happening. - Create an object of class
LabClass. As the signature indicates, you need to specify the maximum number of students in that class (an integer). - Call the
numberOfStudentsmethod of that class. What does it do? - Look at the signature of the
enrollStudentmethod. You will notice that the type of the expected parameter isStudent. Make sure you have two or three students and aLabClassobject on the object bench, then call theenrollStudentmethod of theLabClassobject. With the input cursor in the dialog entry field, click on one of the student objects; this enters the name of the student object into the parameter field of theenrollStudentmethod. Click OK and you have added the student to theLabClass. Add one or more other students as well. - Call the
printListmethod of theLabClassobject. You will see a list of all the students in that class printed to the BlueJ terminal window -
Create three students with the following details:
Suneetha, student ID: A00234, credits: 24 Lata Mangeshkar, student ID: C22044, credits: 56 Papon, student ID: A12003, credits: 6Then enter all three into a lab and print a list to the screen.
- Use the inspector on a
LabClassobject to discover what fields it has. - Set the instructor, room, and time for a lab, and print the list to the terminal window to check that these new details appear.
BlueJ Challenges
Go to Practice to find the challenges.