Programming Languages Chapter 11 The Focus How Create Entire Class This Point Students Have

subject Type Homework Help
subject Pages 2
subject Words 1093
subject Authors Barbara Ericson, Mark J. Guzdial

Unlock document.

This document is partially blurred.
Unlock all pages and 1 million more documents.
Get Access
page-pf1
Chapter 11
The focus in chapter 11 is on how to create an entire class. Up to this point students have only
been adding methods to existing classes. We think that this allows the students to focus on
learning basic concepts without the added overhead of how to create a class. Many introductory
In section 11.1 we talk about how to identify the objects and classes in a program. This is a very
useful exercise for students and it is helpful for them to try to do this in many contexts. You
could ask them to identify objects and classes in many different situations such the objects
involved in a visit to a movie theater, the objects involved in getting money from an ATM
machine, and the objects involved in putting on a play. Often it is fun to tell students the
In section 11.2 we show how to define a class including defining fields. An important thing to
point out is that each object “knows” what class created it since it keeps a reference to the Class
object that defines that class. By creating a simple class with just some fields we can show the
power of inherited methods and introduce overriding inherited methods. We next show creating
a constructor and explain that if your class didn’t provide a constructor the compiler created one
In section 11.3 we show that you can overload constructors as long as the parameter list is
different. Again you can use a debugger to check which method is called.
In section 11.4 we show creating and initializing an array. Even though we introduce the
concept of an array early we don’t really have students working directly with arrays until this
chapter. We find the average of an array of grades to show processing an array. We continue to
show how to use a debugger to trace execution.
In section 11.5 we introduce getter and setter methods and the purpose of making the data private
but allowing public getters and setters. It is important to point out that objects should protect
their data. One way to underscore this is to ask a student what his or her name is and then tell
You can also talk about bank accounts and that the amount in your account should be private but
you should have a way to add and remove money from the account. Students don’t usually
understand why we make fields private until we show that other classes can reach in and change
the data if it is public. They still don’t usually understand that one object of a class can reach in
page-pf2
and change the private data of another object of the same class. You can explain that this has to
do with the assumption that only one programmer will write a class and so it is assumed that the
programmer won’t do stupid things, but that you shouldn’t trust another programmer not to do
stupid and/or dangerous things, so all fields should be private.
In section 11.6 we show how to create a main method. We introduce it here because we can
explain all of the main method declaration since we created static methods back in chapter 10.
Your students may get tired of using the interactions pane before this and you can introduce the
In section 11.7 we introduce Javadoc comments. By this point students should be able to create
comments in their code and you should be able to grade them on comments. In DrJava it is easy
to generate the HTML from the Javadoc comments for just the current file by clicking on
“Tools” and then on “Preview Javadoc for Current Document”. One way to underscore the
importance of comments is to have students modify another student’s class.
In section 11.8 we show how to create another class and introduce UML class diagrams. Using a
UML class diagram can help a student understand the classes in a program and the relationship
between objects of the classes.
In section 11.9 we show how to reuse a class via inheritance by creating a confused turtle that
turns left when told to turn right and turns right when asked to turn left. This is a nice way to
introduce the need for a way to specify that you want to use a parent’s method (using super). It
At this point you might want to have the students create some additional simple classes. They
could create an address class and each student could have an address field. They could create a
bank account class with subclasses of checking account and savings account. Students can
create a comic panel class that takes a picture and a caption and shows the caption on the picture

Trusted by Thousands of
Students

Here are what students say about us.

Copyright ©2022 All rights reserved. | CoursePaper is not sponsored or endorsed by any college or university.