<< Chapter < Page | Chapter >> Page > |
Revised: Wed May 11 18:39:23 CDT 2016
This page is included in the following Books:
This module is one in a collection of modules on Java Generics designed for teaching ITSE2317 - Java Programming (Intermediate) at Austin Community College in Austin, TX.
This and future modules in this series will be based on the Generics (Updated) section of The Java Tutorials from Oracle. (In the event that these links become broken, you should have no difficulty finding the Oracle material with an online search.)
You will find the learning resources for this module on the Oracle site at Wildcards .
This module contains review questions and answers keyed to the material in Wildcards .
Once you study that material, you should be able to answer the review questions in this module.
This is the final module in this series. This course does not cover the following topics:
However, once you complete your study of all the material on generics , (including the two topics listed above) , you should be able to answer the review questions at Questions and Exercises on the Oracle site.
The questions and the answers in this module are connected by hyperlinks to make it easy for you to navigate from the question to the answer and back again.
True or False: The code shown in Listing 1 will compile and run successfully producing the output shown in Figure 1 .
Listing 1 - Question 1.
/*File Q01.java
************************************************/import java.util.ArrayList;
import java.awt.Button;import java.awt.Label;
import java.awt.Component;public class Q01{
/*Given:
Button and Label are subclasses of the Componentclass.
The getClass method is defined in the Objectclass. When called on a reference to an object,
the method returns the name of the class fromwhich the object was instantiated.
*/public static void main(String[] args){//Create and populate a list designed to
// store references to objects of type Button// and display its contents.
ArrayList<Button>listA = new ArrayList<>();
listA.add(new Button("Button01"));listA.add(new Button("Button02"));
display(listA);System.out.println("");//blank line
//Create and populate a list designed to// store references to objects of type Label
// and display its contents.ArrayList<Label>listB = new ArrayList<>();
listB.add(new Label("Label01"));listB.add(new Label("Label02"));
display(listB);}//end main
//-------------------------------------------//static void display(ArrayList<Component>list){
for(Component n : list){System.out.println(n.getClass());
if(n instanceof Button){System.out.println(
((Button)n).getLabel());}else if(n instanceof Label) {
System.out.println(((Label)n).getText());}//end else
}//end for loop}//end display
}//end class Q01
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?