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
makeVisible
from the menu. Observe what happens…
Try:moveRight
moveDown
makeInvisible
makeVisible
- Experiment: What happens if you call
moveDown
twice? Or three times? What happens if you callmakeInvisible
twice? - Invoke the
moveHorizontal
method. 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 frommoveRight
ormoveLeft
? - Try invoking the
moveVertical
,slowMoveVertical
, andchangeSize
methods before you move on. - Challenge: Find out how you can use
moveHorizontal
to move the circle 70 pixels to the left. Experiment ifmoveVertical
moves the circle up in the same way. - Try invoking the
changeColor
method and see what happens when you specify a color that is not known. - Invoke the
changeColor
method, 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 theCircle
class. 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
Picture
and invoke itsdraw
method. Also, try out thesetBlackAndWhite
andsetColor
methods. How do you think thePicture
class draws the picture?
Observing Picture class
- Look at the pop-up menu of class
Picture
again. 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
Day
class creates aDay
instance with today’s date, month, and year? - Can you create a
Day
instance of any given year, month, and date? Try giving values like99
foraDayOfMonth
oraMonth
oraYear
. Does it accept? - Find a way to calculate age of a person using
Day
object’s methods. - Can the same process be used to find how long a person lived?
- Try checking the new
Day
value 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
Rectangle
and draw it. What do you observe? Is thedraw()
method ofRectangle
in this project different from thedraw()
method ofRectangle
in figures project? - Create an instance of
Rectangle
usingnew Rectangle()
and create anotherRectangle
instance 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 theRectangle
class in this project different from that of house project? - What happens when you invoke
fill()
method on yourRectangle
instance? - Try
translate()
andgrow()
methods onRectangle
instances 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
Picture
instances. 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 typeString
must be written within double quotes.) - Create some
Student
objects. Call thegetName
method 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
numberOfStudents
method of that class. What does it do? - Look at the signature of the
enrollStudent
method. You will notice that the type of the expected parameter isStudent
. Make sure you have two or three students and aLabClass
object on the object bench, then call theenrollStudent
method of theLabClass
object. 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 theenrollStudent
method. Click OK and you have added the student to theLabClass
. Add one or more other students as well. - Call the
printList
method of theLabClass
object. 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: 6
Then enter all three into a lab and print a list to the screen.
- Use the inspector on a
LabClass
object 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.