Chapter 1:
![]()
Chapter 1 |
The basic concepts and terms of solving problems using a computer are covered, including use of the BlueJ application and object oriented modeling of problems.
Problems solved on computers model some aspect of the real world. For example, we calculate the date when the distance between Earth and Mars is a minimum (August 27) by modeling the planetary orbits numerically. The line between reality and computer modeling apparently grows less distinct when money is earned and spent electronically or the plane we ride is often piloted by a computer, but even in these cases the computer programmer has implemented a model solution for the computer to follow.
Object-oriented programming is one approach to modeling problems based upon how we humans naturally organize our environment - by classifying things with similar attributes together. For example, on entering a room we unconsciously classify individual objects in the room as desks, students, etc. We can then treat desks as setting objects without thinking much about the details of an individual desk.
| Class - A class defines common fields (attributes) and methods (actions) to objects. |
All objects classified as desks have height, color, location, etc. fields. The desk class can be defined by its fields as:
class Desk
height
color
location
| Object - An object is one specific instance of a class where the fields have a value. |
A room may have 20 objects classified as desks, each desk has color, height, location, etc. fields. Two desk objects, different instances of desk classification, can then have field values of:
height 30 inches
color black
location row 3, position 2
height 30 inches
color tan
location row 4, position 1
Exercise 1
|
Create object - To create a new instance of a real desk requires lumber, nails, etc. but since we are modeling a desk on a computer two new desk instances are created in Java by:
|
|
Name object - Naming each desk allows us to distinguish one desk from another by:
|
|
Exercise 2
|
Getting Started.The following assumes that textbook projects has been installed, if not, see
You should now see the following indicating that shapes project has been opened:
Exercise 3 - Creating objects in BlueJ
- Right-click on the Circle class icon
and choose:
- new Circle()
- Create another circle.
- Create a square.
Exercise 3 created two circles that included an isVisible field that when false the circle is not visible and when true the circle is visible.
| circle_1 | circle_2 | |||
|
|
| Methods - A method performs action on the object by using the values of the fields. |
Method makeVisible() sets the isVisible field to true.
Exercise 4 - Calling methods in
BlueJ
|
Two circles have been created with field values of:
| circle_1 | circle_2 | |||
|
|
| Parameters - Parameters specify values to be used by methods. |
The method call changeSize(50) specifies 50 as the parameter to the method.
Exercise 5 - Calling methods with
parameters in BlueJ
|
Describing real problems as a computer model requires defining the problem data.
The following fields describe circle_1's diameter, visibility, and color.
| circle_1 | ||
|
| Data type - Specifies the type of data value of a field or parameter. |
Exercise 6 - Calling methods with String parameters
- Click on the taskbar icon
.
- Right-click on circle_1 icon in BlueJ.
- Call changeColor method with parameter "red".
- Call changeColor method with parameter "Mary".
- Call changeColor method with parameter red.
- Call changeColor method with parameter 14.
- Call changeColor method with parameter 'red'.
circle_1 circle_2 Two instances of the Circle class
diameter 30
isVisible false
diameter 30
isVisible false
| Class - Defines the fields
and methods of objects. An object (instance of the class) can be
created.
Objects - An instance of a class. The fields have values and the methods can be called for the object. |
Exercise 7 - Multiple Instances
- Create three Circle objects by right-clicking on
.
- Make each visible.
- Move each around.
- Make each a different color.
- Create two Triangle objects.
- Make each visible.
- Move each around.
- Make each a different color.
| State - The values for all fields of an object. |
The following field values completely define circle_1's state. If the state does not change, the object does not change. Changing field diameter to 90 changes the state of the object (and triples the diameter).
| circle_1 | ||
|
Exercise 8 - Inspecting Object
State
|
| Objects - An instance of a class. The fields have values and the methods can be called for the object. |
All objects of a class have identical fields; the fields of one object can hold different values from other objects.
| Circle class | circle_1 object | circle_2 object | |||||
|
|
|
| Methods - A method performs action on the object by using the values of the fields. |
All objects of a class have identical methods; methods are called on one, designated object.
| Circle methods | ||
|
Exercise 9 - Using
Objects
a. Circle circle_1 = new Circle()
|
The tasks of Exercise 9 would normally be written as Java instructions that could be performed by the computer rather than entering by hand each time a house is to be drawn. In Exercise 10, the tasks have been listed in the picture project.
Exercise 10 - Object
Interaction
|
Tasks to create objects and call methods can be written in Java, saved to a file, and reused over and over. The list of Java statements define a new class, the written text of the statements are the source code for the class.
Exercise 11 - Source
Code
|
Java statements are written and read by humans but must be compiled (translated from Java to computer code) before performed by computer.
| Compile - Translating Java statements to computer instructions. |
Exercise 12 - Compiling ![]()
- Open Editor to see the source code text.
- Find the public void draw() method.
- Change the color of the sun to blue.
- Compile by clicking the Compile button (see figure at right).
- What happened to picture_1 object?
- Was picture_1 object using the most recent definition of Picture class?
- Create an instance of class Picture named picture_1.
- Call the draw method.
Exercise 13 - Editing
- Open Editor to see the source code text.
- Change the Picture class to have two suns.
- Add another Sun field named sun2.
- Compile by clicking the Compile button.
- Create an instance of class Picture named picture_1.
- Call the draw method.
- Did two sun's appear? What happened?
- Change the draw method appropriately for sun2.
- Copy and paste sun source code.
- Make necessary changes.
- Make sun2 a color different than sun.
- Position sun2 below sun.
- Compile by clicking the Compile button.
- Create an instance of class Picture named picture_1.
- Call the draw method.
- Did two sun's appear? What happened?
Exercise 14 - Challenge
- Open Editor to see the source code text.
- Change the draw method to add a sunset of a sun.
- Use the slowMoveVertical(int) method.
- Compile by clicking the Compile button.
- Create an instance of class Picture named picture_1.
- Call the draw method.
- Open the
icon to see the results.
- Call the draw method again to see the sunset.
Exercise 15 - Challenge
- Open Editor to see the source code text.
- Add a new method named sunset.
- Use public void draw() as a pattern.
- Move the slowMoveVertical call to the sunset method.
- Compile by clicking the Compile button.
- Create an instance of class Picture named picture_1.
- Call the draw method.
- Open the
icon to see the results.
- Call the sunset method to see the sunset.
This is a simple example example of a student database that tracks students in a laboratory class and prints class lists.
Exercise 16 - Student
Database
|
Calling a method can produce a return value as a result. When the name field is "Jane", the Student class method getName produces the return value "Jane".
| Return values - Parameters pass information to a method when called. Methods pass information back as return values. |
Signature - Method signature
define:
|
Exercise 17 - Return Values of Methods
- Continue Exercise 16.
- Create two Student objects.
- Fill in the name and id fields with different String values (enclose String's in ").
- Inspect each object.
- Call method getStudentID for each object.
- Find the signature of the getStudentID method.
- Right-click Student class
- Open Editor.
- What are the parameters?
- What class is the return value?
- What field is returned?
- What is returned for one of the Student objects?
- Find the signature of the changeName method.
- Right-click Student class
- Open Editor.
- What are the parameters?
- What class is the return value?
- What field is returned?
- What is returned for one of the Student objects?
Any object can be a method parameter, as long as the object class matches the method signature.
Exercise 18 - LabClass
|
The enrolStudent method is passed a Student parameter.
Exercise 19 - Objects as Parameters
|
The printList method prints the fields of a LabClass object to a terminal window.
Exercise 20 - Print LabClass object
|
Exercise 21 - Exploring LabClass
- Create a LabClass object.
- The LabClass constructor signature indicates an int parameter which must be passed the maximum number of students.
- Create two Student objects with the following field values:
- Lisa Simpson, student ID: 122044, credits: 56
- Charlie Simpson, student ID: 12003P, credits: 6
- Call LabClass method enrolStudent to enter the two Student objects.
- Call the printList method for the LabClass object.
Exercise 22 - Exploring LabClass
- Continue Exercise 21.
- Set the instructor, room, and lab time fields of the LabClass object.
- Call the printList method for the LabClass object.
- Inspect the LabClass object.
Please send any comments to: jfdoyle@ius.edu
Copyright © 2001-2004, by
cgranda@ius.edu . All
rights reserved.