CP1 Module 7 Object Data
CP1 Module 7 Object Data
MODULE 7
COMPUTER PROGRAMMING 1
(JAVA)
Content Standard:
The learners demonstrate understanding of the:
1. modularity;
2. classes and objects;
3. reference variables;
4. string objects; and
5. type wrappers and packages.
Performance Standard:
The learners should be able to:
1. create a program that uses objects defined in the standard
software packages that come with Java.
Objectives:
After the lesson, the learners should be able to:
1. define the concept of modularity, objects, and classes; and
2. plan for a concept for your own object and class.
PRELIMINARY ACTIVITY
Direction: Draw objects you can find around you. State the attributes (color,
weight, height, etc.) and give what data type you can use for each
attribute.
OBJECT DATA: Modularity and Objects
A vital idea in programming (and perhaps in all of science and math) is how
large things are built of smaller things, and how those large things are used to build
even larger things.
The classic example is Lego bricks, where individual bricks are used to build
larger assemblies which in turn are used to build even larger assemblies, which are
used to build even larger constructions.
In geometry, Euclid's axioms are the basic bricks, which are used to build basic
theorems, which in turn are used to build more theorems, which are used to build even
more advanced theorems. Over the years, a very large structure has been built out of
the fundamental axioms (and the building continues to this day).
A primitive data type uses a small amount of memory to represent a single item
of data. All data of the same primitive type are the same size.
An object is a large chunk of memory that can potentially contain a great deal of
data along with methods (little programs) to process that data.
There are thousands of object classes that come standard with Java, and a
programmer can easily create additional classes.
PICTURE OF AN OBJECT
The picture illustrates a conceptual object. The little yellow bricks represent bytes
of memory. The methods of the object and the data of the object are constructed out of
bytes. This object contains the characters
"Elementary, my dear Watson!" and contains several
methods to manipulate that data.
CREATING AN OBJECT
The example program creates a String object. When the program runs, the
expression
All objects of the same class contain the same methods. All objects of the same
class contain the same types of data although the values of the data will be different
from object to object. For example, all String objects contain the same methods.
All String objects contain a string of characters, but the characters will be different
from object to object.
The program could now use the methods of this object to do some things with
the characters. However, this program does nothing further. After the program stops
running, the object no longer exists. The memory out of which it was made can now be
used for other purposes.
The declaration
String str;
After the program stops running, the String object no longer exists. Its memory
is reclaimed by the computer system for other uses.
OBJECT REFERENCE
Here is the assignment statement we are considering:
The expression
is evaluated. This constructs (creates) a new object. The value of this expression is a
reference to the new object.
A constructor for an object has the same name as the class and is used with
the new operator. Sometimes (as in this example) a constructor requires parameters.
A constructor returns a reference to the object it has created. A reference to an
object describes its location in memory. It enables the Java virtual computer to find the
object.
In the second step, the reference (the value) is stored in the reference variable:
Now whenever the program needs to refer to the object it uses the variable str.
A reference is like a cell phone number. Someone who has your number can send
you a message, and ask you to do something, no matter where you are. Think of your
boss calling you and sending a message, "Start working on that report!" This is like
using an object reference to ask the object to run a method.
ANALOGY
Objects and references to objects are very common in the real world. But
sometimes the distinction between objects and object references is hard to see in a
program. Here is an analogy that may help:
object you
This creates an object, fills it with data, and puts a reference to it in str.
The picture on the right shows the program after the assignment statement has
run. The reference is shown as an arrow that leads to the object, and this is a good way
to think of it.
The variable will continue to hold the reference to the object until some other
assignment statement changes it or the program ends.
RUNNING A METHOD
Here is the example program, now with additional statements:
The expression
str.length();
len = str.length();
is replaced with
len = 27;
DOT NOTATION
The various things an object contains—its variables and its methods—are called
the members of that object. The members of an object are accessed using dot
notation, which looks like this:
objectReference.memberName
objectReference.methodName( parameter )
ClassName variableName;
This declares a reference variable and declares the class of the object it will later
refer to. No object is created.
This declares a reference variable and declares the class of the object. But now, at
run time, a new object is constructed and a reference to that object is put in the
variable. Sometimes parameters are needed when the object is constructed.
This declares two reference variables. At run time, two objects are created and
their references are assigned to the variables. Again, you can do this for more
than two as long as you follow the pattern.
It seems a little odd that an object can create another object. This is not how real-
world objects usually behave. Bricks do not create other bricks. Cars do not create other
cars. But software objects often create new software objects.
1. By using a constructor.
2. By calling a method that constructs an object.
PACKAGES
Many packages come standard with Java. For example, the String class is part of
the package called java.lang. Several useful classes are part of that package. You can
use the classes in this package without doing anything special.
Other standard packages also come with Java. To use a class from one of these
packages your program must tell the compiler what package contains the class. One
way to do this is to use the package name with the class. Here is a program that
explicitly mentions where the String class is found:
This program works the same way as the previous version. The
package java.lang does not need to be explicitly mentioned when you need to
use String, although it does not hurt to do so.
import
Here is a program that creates a Scanner object. The program does nothing with this
object
import java.util.Scanner;
TYPE WRAPPERS
F o r e a c h p r i m i t i
A w r a p p e r c l a s
up the data with an object, like
wrapping up a gift.
W r a p p e r c l a s s e s
can be converted into primitive
data. The table shows primitive types
and their wrapper classes. Java is case
sensitive, so byte and Byte are
different types.
A s a n e x a m p l e , t
primitive data type int. The same value
could be held in an object that is of type Integer. The object will use many more than
32 bits.
The wrapper classes are defined in the package java.lang and so (like all classes
in that package) are automatically available to your programs.
ASSESSMENT
Directions: Write each of these programs as specified. None of these programs ask the
user to enter data. Data are "hard coded" with declaration statements or
assignment statements.
Write a program that creates a String object that contains the string "Hello World"
(or some other favorite string). Assign the reference to the object to the reference
variable str. Write the characters of the object to the monitor:
System.out.println( str );
Run the program to confirm that it works as you expect. Now add a second reference
variable to the program, but don't create an object for it to refer to. Add a statement
that uses the second variable as if it held a valid object reference. Run the program,
and see what happens:
System.out.println( secondVariable );
Play around with this program for a while to cement the notion in you head that an
object and its reference variable are different things, and that an object is created at
run time.
len = str.length();
}
}
Now change the String in various ways and observe the effect on the computed
string length. Add extra spaces on either side and in the middle of the string; add
more punctuation; insert a tab character (a real tab character, not just spaces). Note
that the length of the string "MMMMMM" is the same as "iiiiii". The length is the
number of characters, not the actual length of line it takes to print them.
String first = new String(" In a Hole in the ground there lived a Hobbit. ")
String second;
second = first.trim();
Write a program (or modify the previous one) that creates a String by calling
the trim() of an original String. Write both Strings to the monitor. Experiment
with spaces and tabs to determine exactly what is trimmed from the original.
FEEDBACK
________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
REFERENCES
1. http://programmedlessons.org/Java9/chap08/ch08_01.html
2. https://www.youtube.com/watch?v=qUXbJziVs_o
3. http://programmedlessons.org/Java9/chap09/ch09_01.html
4. https://www.youtube.com/watch?v=RA7wkTV6z4k