0% found this document useful (0 votes)
18 views

Chapter 1 Companion_ Objects and classes

M250 Java and object-oriented programming

Uploaded by

M T
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Chapter 1 Companion_ Objects and classes

M250 Java and object-oriented programming

Uploaded by

M T
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

4/10/22, 4:38 PM Chapter 1 Companion: Objects and classes: View as single page

Unless otherwise stated, copyright © 2022 The Open University, all rights reserved.
Printable page generated Sunday, 10 Apr 2022, 16:40

Chapter 1 Companion: Objects and classes

This chapter introduces some of the main ideas of object-oriented (OO) programming, including objects,
classes, methods and parameters.

‘Classes’ are like blueprints for objects – one class allows you to create many objects. For example, the
Circle class allows you to create as many circle objects as you like. So, a class is a category of object rather
than an object itself.

In this chapter you will be creating and interacting with objects on BlueJ’s Object Bench. This will allow you to
explore the BlueJ interface and start learning some of the vocabulary associated with object-oriented
programming.

You will see that we can communicate with objects by calling their ‘methods’, which is also the way that
objects communicate with each other.

Before you start reading this chapter you should:

read the M250 Module Guide, which gives you an overview of the module content

read the Chapter 1 Prequel, which explains why Java and object-oriented programming became
popular

read the M250 Software Guide, up to and including Section 3, and install the module software.

Aims
After studying this chapter, you should have learned the following important ideas about Java and object-
oriented programming:

Classes are used to create objects, which represent entities in a piece of software.

Objects contain data fields, and the values of the data fields tell us the object’s state.

All data in Java has a type. Some types are built into the language, while others are defined by classes.

To communicate with an object, you call one of its methods.

Some methods need to be sent extra data to do their work, called ‘parameters’.

Some methods return values that we can use.

A class is defined by a source code file, which is a text file that can be edited by a programmer.

Java code must be compiled to produce bytecode before it can be executed.

Resources
This companion uses the following projects:

https://learn2.open.ac.uk/mod/oucontent/view.php?id=1745973&printable=1 1/12
4/10/22, 4:38 PM Chapter 1 Companion: Objects and classes: View as single page

figures
house
lab-classes.

The figures project allows you to draw simple shapes, such as circles and squares, in a graphical
environment. The house project provides an example of a drawing created using these shapes. The lab-
classes project simulates a database to keep track of students in lab classes.

Note also that each project contains a README file, and you may find it useful to read this.

How to study this chapter

We will cover all sections of Barnes and Kölling (B&K) Chapter 1 and direct you as to which exercises
you need to complete.

Section and exercise numbers in readings refer to the numbers in the textbook.

Please note the following:

1. The screenshots in the book were taken using an earlier version of BlueJ, so you should not be
concerned if you notice minor differences compared to the version of BlueJ we are using now.

2. The preface of the book includes general information about the book that you may like to review
before starting, but we have covered the key points in the Module Guide.

3. When you come across exercises in the B&K text, please refer to the relevant module activity.

Creating objects and calling methods


Before your first reading, watch the following video notes (VN) from the book authors; opening each one in a
new tab will allow you to refer back to them easily when you get to the reading.

VN 1.1 Introduction to key concepts (4 minutes)

VN 1.2 Creating and using objects within BlueJ (9 minutes)

Reading – to end of Section 1.3

Work through B&K Chapter 1, pages 3–6.

https://learn2.open.ac.uk/mod/oucontent/view.php?id=1745973&printable=1 2/12
4/10/22, 4:38 PM Chapter 1 Companion: Objects and classes: View as single page

Note: You may need to compile first ( Tools | Compile ) before you can create a new Circle in Section
1.2.

Exercise 1.1 concerns the figures project, which contains Circle, Square, Triangle, Person and
Canvas classes.

Objects are created using classes. An object is also called an instance of a class. Objects model
aspects of a problem domain.

We are told in Section 1.2 that there are conventions for how classes are named – for example, all of
the class names above begin with a capital letter. For more on conventions for naming identifiers, see the
M250 Code Conventions and Design document, Section 3.

Section 1.3 tells us that objects have operations, which are implemented using methods. Operations
are the things an object can do, which include visible aspects of behaviour (such as changing the colour
of a graphical representation of an object) as well as less visible aspects of behaviour (such as adding up
numbers).

We talk about calling a method or invoking a method, which mean the same thing: asking an object to
run a method’s code.

Activity 1 (BlueJ)

Do Exercise 1.1 using the figures project in BlueJ. You may need to compile the project first.

Reveal discussion

Activity 2 (BlueJ)

Do Exercise 1.2 using the figures project in BlueJ.

Reveal discussion

Now would be a good time to watch the following video note (VN) from the book authors:

Video Note 1.3 Methods and parameters (10 minutes)

https://learn2.open.ac.uk/mod/oucontent/view.php?id=1745973&printable=1 3/12
4/10/22, 4:38 PM Chapter 1 Companion: Objects and classes: View as single page

If you would like to experiment further with this project (don't spend too long on this, though), make sure that
you are interacting with the object that is being displayed, and bear in mind that a drawing of an object might
lie on top of a drawing of another object.

Parameters and data types


Reading – Sections 1.4 to 1.5

Work through B&K Chapter 1, pages 6–8.

Section 1.4 introduces the idea of a parameter, which is a value we can provide to a method to affect
what it does. A method’s header includes its parameters (if any) and their types. A method’s signature
tells us the name of the method and the types of parameter that it needs to do its work (if any).

Section 1.5 says a bit more about types, such as int, for integers, and String for strings.

Activity 3 (BlueJ)

Do Exercises 1.3 to 1.6 using the figures project. Remember that you need to create an object on the
Object Bench before you can call its methods, and you need to call the makeVisible method to see the
effects in the Picture Demo window of calling other methods.

Reveal discussion

Additional discussion of the Canvas class

Notice that the figures project includes a class called Canvas.

This class implements a ‘canvas’ drawing area containing representations of shapes that you have created.
The way this project is designed is somewhat unusual in that you don’t have to create an object of the Canvas
class – instead this object is created for you when you create a shape object.

We can’t yet explain how Canvas works in detail, but it is somewhat counter-intuitive in that when you remove
an object from the Object Bench it doesn’t disappear from the canvas. This is because the canvas keeps a list
of objects that have been added to it, and this list is not affected by interactions with the Object Bench.

There are a few ways of clearing the canvas. One is to individually hide shapes, one by one, by right-clicking
on them and calling their makeInvisible method. However, if you’ve already removed an object from the
Object Bench, this doesn’t help. In this case your quickest solution for now is to use Rebuild Package , on
the Tools menu. You will have to recreate any shape objects you want, after rebuilding the project. Later you
will learn better ways of handling this.

https://learn2.open.ac.uk/mod/oucontent/view.php?id=1745973&printable=1 4/12
4/10/22, 4:38 PM Chapter 1 Companion: Objects and classes: View as single page

Objects and their state


Reading – Sections 1.6 to 1.8

Work through B&K Chapter 1, pages 8–11.

Section 1.6 mentions that the shape objects we are drawing have attributes such as colour and position,
whose values can be changed.

Notice that you can create as many objects as you like and interact with them individually. We call objects
‘instances’ of classes.

The state of an object is the set of values of its attributes, such as (for our shape objects) colour and
position. Attribute values are stored in fields. Objects of different classes typically have different
attributes.

Each object can have a different state, although all objects of the same class will have the same
attributes. This reflects that each object can be interacted with individually.

Section 1.7 introduces you to the inspector feature in BlueJ.

Activity 4 (BlueJ)

Continue to use the figures project and do Exercises 1.7 and 1.8.

Reveal discussion

You may like to try Exercise 1.9, but don't spend too long on it. At a high level, if you are describing
what you are doing to solve the problem, you might say things like ‘create a red square and move it
near the centre of the canvas’ and ‘create a triangle and move it above the square to make a roof’
and so on. If you were being more specific you would say exactly how you are positioning the objects
and give more information about their state.

Java code in the Code Pad


Reading – Section 1.9

Work through B&K Chapter 1, pages 11–12.

https://learn2.open.ac.uk/mod/oucontent/view.php?id=1745973&printable=1 5/12
4/10/22, 4:38 PM Chapter 1 Companion: Objects and classes: View as single page

In this section you begin to see how to work with objects without using BlueJ’s menus.

You are introduced to the ‘terminal window’ and the ‘Code Pad’.

The terminal window is briefly discussed in Section 4.16 of the M250 Software Guide.

The Code Pad is a useful part of BlueJ that allows you to execute code snippets and interact with objects
you have created using code. It is discussed in more detail in Section 4.15 of the M250 Software Guide.

Activity 5 (BlueJ)

Do Exercises 1.10 to 1.12 using the figures project in BlueJ.

Reveal discussion

The house project and source code


Reading – Sections 1.10 and 1.11

Work through B&K Chapter 1, pages 12–14.

These sections discuss another example project, called house, which is similar to the figures project,
but which has added another class called Picture.

Section 1.11 takes us into the source code for the Picture class, which defines the fields and methods
for objects of that class. You may like to discuss how the class Picture works in more detail on the
M250 forums.

You saw in the Prequel to Chapter 1 that a Java Virtual Machine (JVM) runs bytecode (machine code for
a JVM) and that the process of compilation involves converting source code into bytecode.

Because the JVM requires bytecode, whenever you edit a class you first have to compile it before it can
be loaded and run by the JVM.

In the house project, as with the figures project, you may need to rearrange the BlueJ windows to see
what is going on. The graphical display might not maximise by itself, or it may be hidden behind another
window.

Activity 6 (BlueJ)

https://learn2.open.ac.uk/mod/oucontent/view.php?id=1745973&printable=1 6/12
4/10/22, 4:38 PM Chapter 1 Companion: Objects and classes: View as single page

Do Exercises 1.13 to 1.17, after opening the house project in BlueJ. Remember that you need to
compile the project before you can create objects.

If you like, you can close the figures project. It is easy to reopen later by clicking on Project | Open
Recent .

Reveal discussion

You are free to skip Exercises 1.18 to 1.20. You may like to come back to them later if you want
some more practice.

The lab-classes project


Reading – Sections 1.12 to 1.15

Work through B&K Chapter 1, pages 15–19.

The final sections of this chapter provide a third example of a Java project, called lab-classes. The
project represents a simple database application, in which an object of class LabClass stores
information about objects of class Student. It is not a persistent database – it lasts only as long as you
have a LabClass object. Once you remove this object, any information it stores is lost.

As with the figures project, you may need to rearrange the windows to see what is going on. The
graphical display might not maximise by itself, or it may be hidden behind another window.

In Section 1.13 we see that some methods return a value when called.

Activity 7 (BlueJ)

Using the lab-classes project, do Exercises 1.21 to 1.30.

Reveal discussion

Further practice
The following activities are based on Exercises 1.31 onwards.

Activity 8 (online)
https://learn2.open.ac.uk/mod/oucontent/view.php?id=1745973&printable=1 7/12
4/10/22, 4:38 PM Chapter 1 Companion: Objects and classes: View as single page

This question is based on Exercise 1.31.

Match the following values to their types, based on the provided options. (Hint: See B&K Appendix
B.1.) When you're done, press the Check button to check your answer.

0 Choose...

"hello world" Choose...

-10 Choose...

42 Choose...

"£1" Choose...

true Choose...

"3.14" Choose...

3.14 Choose...

Check
Not complete

Activity 9 (self-assessment)

Do Exercise 1.32. What would you have to do to add a field called name to a Circle object?

Save and reveal discussion Reset

Activity 10 (online)

https://learn2.open.ac.uk/mod/oucontent/view.php?id=1745973&printable=1 8/12
4/10/22, 4:38 PM Chapter 1 Companion: Objects and classes: View as single page

Exercise 1.33 in B&K asks you for the header for a method named send that has one parameter of
type String, and does not return a value.

Complete the method header below by dragging and dropping four of the code fragments into the
correct positions.

public ( )

int paramName String send void

Check
Not complete

Reveal discussion

Activity 11 (online)

https://learn2.open.ac.uk/mod/oucontent/view.php?id=1745973&printable=1 9/12
4/10/22, 4:38 PM Chapter 1 Companion: Objects and classes: View as single page

In this question (based on Exercise 1.34) you have to type code into the provided editor window. We
call this the CodeRunner environment.

After you press Check the environment will try to compile your code. If your code compiles then tests
will be run to check whether your code meets the question requirements.

You are given a class called Maths that has a method whose header has not been completed yet.
Complete the header for the method, which should be named average and have two parameters, both
of type int, called a and b . The method needs to return an int value representing the (integer)
average of its parameters.

The first word of the method header (public) is given already. The body of the method is already
complete, so you should not alter it.

Answer:

Reset answer

1 class Maths
2 ▼ {
3 ▼ /**
4 * Return the (integer) average of the parameters
5 * named a and b.
6 */
7 public
8 ▼ {
9 return (a + b) / 2; //leave this code alone
10 }
11 }

Check
Not complete

Activity 12 (self-assessment)

Do Exercises 1.35 and 1.36, answering the exercises using the box below

https://learn2.open.ac.uk/mod/oucontent/view.php?id=1745973&printable=1 10/12
4/10/22, 4:38 PM Chapter 1 Companion: Objects and classes: View as single page

Save and reveal discussion Reset

Glossary quiz
A quiz on the glossary definitions of concepts from this chapter can be found on the study planner, or by
following this link: Chapter 1 glossary quiz. Testing your knowledge of these important terms will help you to
understand the material ahead, and will allow you to focus on the new material rather than having to reread
previous chapters or look up glossary definitions; to this end, each Chapter Companion will have a glossary
quiz.

Learning outcomes
After studying this chapter, you should be able to:

open a project in BlueJ and compile its code

use BlueJ’s Object Bench and Code Pad features to create and interact with objects, by calling their
methods and providing parameter values when necessary
use BlueJ's inspector to view the state of objects
explain the relationship between objects, classes and the problem domain
give examples of how objects in the real world have attributes and operations
edit source code files to make small changes to objects' attributes and operations, as directed
understand that when a method is called, its code is executed using received parameter values, if any,
and the current state of the object
explain the idea of a data type, and how it relates to valid values of parameters and returned values
understand that classes are named using a word beginning with a capital letter, by convention, while
objects are named using a word beginning with a lower-case letter
write a header for a method, given a name, parameter types and return type.

Activity links
Activity 1 (BlueJ)
Activity 2 (BlueJ)
Activity 3 (BlueJ)
Activity 4 (BlueJ)
Activity 5 (BlueJ)
Activity 6 (BlueJ)
Activity 7 (BlueJ)
Activity 8 (online)
Activity 9 (self-assessment)

Activity 10 (online)

https://learn2.open.ac.uk/mod/oucontent/view.php?id=1745973&printable=1 11/12
4/10/22, 4:38 PM Chapter 1 Companion: Objects and classes: View as single page

Activity 11 (online)
Activity 12 (self-assessment)

https://learn2.open.ac.uk/mod/oucontent/view.php?id=1745973&printable=1 12/12

You might also like