<< 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

how to study physic and understand
Ewa Reply
what is conservative force with examples
Moses
what is work
Fredrick Reply
the transfer of energy by a force that causes an object to be displaced; the product of the component of the force in the direction of the displacement and the magnitude of the displacement
AI-Robot
why is it from light to gravity
Esther Reply
difference between model and theory
Esther
Is the ship moving at a constant velocity?
Kamogelo Reply
The full note of modern physics
aluet Reply
introduction to applications of nuclear physics
aluet Reply
the explanation is not in full details
Moses Reply
I need more explanation or all about kinematics
Moses
yes
zephaniah
I need more explanation or all about nuclear physics
aluet
Show that the equal masses particles emarge from collision at right angle by making explicit used of fact that momentum is a vector quantity
Muhammad Reply
yh
Isaac
A wave is described by the function D(x,t)=(1.6cm) sin[(1.2cm^-1(x+6.8cm/st] what are:a.Amplitude b. wavelength c. wave number d. frequency e. period f. velocity of speed.
Majok Reply
what is frontier of physics
Somto Reply
A body is projected upward at an angle 45° 18minutes with the horizontal with an initial speed of 40km per second. In hoe many seconds will the body reach the ground then how far from the point of projection will it strike. At what angle will the horizontal will strike
Gufraan Reply
Suppose hydrogen and oxygen are diffusing through air. A small amount of each is released simultaneously. How much time passes before the hydrogen is 1.00 s ahead of the oxygen? Such differences in arrival times are used as an analytical tool in gas chromatography.
Ezekiel Reply
please explain
Samuel
what's the definition of physics
Mobolaji Reply
what is physics
Nangun Reply
the science concerned with describing the interactions of energy, matter, space, and time; it is especially interested in what fundamental mechanisms underlie every phenomenon
AI-Robot
what is isotopes
Nangun Reply
nuclei having the same Z and different N s
AI-Robot
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