<< Chapter < Page Chapter >> Page >

And the answer is ...

The correct answer to the quiz is the program output shown below:

Original array contents 10 11 12 13 14 15 16 17Collection contents B0 B1 L2 B3 B4 L5New array contents B0 B1 L2 B3 B4 L5 null 17Modified array contents XX B1 L2 B3 B4 L5 null 17Collection contents XX B1 L2 B3 B4 L5

If that was your answer, you probably already understand most of the material covered in this module. In that case, you might consider skipping this moduleand moving on to some more productive activity. If that wasn't your answer, you should probably continue with your study of this module.

Similar to previous program

Except for the use of a different version of the toArray method, the overall structure of the program in Listing 1 is similar to the program in the previous module. Therefore, I will concentrate on those aspects of this programthat differentiate it from the program in the previous module.

A populated array

Unlike the program in the previous module, the code in Listing 2 creates and populates an eight-element array of type JComponent . This array will be re-populated by the toArray method later in the program. The array is populated with a set of initial element values at this point to make itobvious when it is re-populated (overwritten elements) by the toArray method later.

Listing 2 . A populated array.
JComponent[] array = new JComponent[8]; for(int cnt=0;cnt<8;cnt++){ array[cnt]= new JButton(); array[cnt].setToolTipText("" + (cnt+10)); }//end for loopSystem.out.println(); showArray(array,"Original array contents");

The JButton class, the JLabel class, and the setToolTipText method were discussed in detail in the previous module, so I won't repeat thatdiscussion here.

Display the array contents

After the array is populated by the code in Listing 2 , a reference to the array object is passed to the showArray method (also in Listing 2 ) to display the contents of the array.

With the exception of some minor changes implemented in this program to make the use of the showArray method more compact, this is the same showArray method used in the previous module. Therefore, I won't discuss that method further in this module. The output produced by the code in Listing 2 is as follows:

Original array contents 10 11 12 13 14 15 16 17

As you can see, each of the eight elements in the array was initialized with an easily-recognizable and unique value, ( which may be overwritten by the toArray method later).

A new LinkedList collection

The code in Listing 3 creates and populates a new LinkedList collection. The collection is populated by passing the LinkedList object's reference to a method named fillIt .

The code in Listing 2 also displays the contents of the LinkedList collection after it has been populated. The list is displayed by passing the LinkedList object's reference to a method named showCollection .

Listing 3 . A new LinkedList collection.
ref = new LinkedList(); Populator.fillIt(ref);showCollection(ref,"Collection contents");

Except for a couple of minor changes to the showCollection method, the code to create, populate, and display the collection is the same as the code inthe previous module.

Questions & Answers

what is phylogeny
Odigie Reply
evolutionary history and relationship of an organism or group of organisms
AI-Robot
ok
Deng
what is biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
what is biology
Victoria Reply
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask