0% found this document useful (0 votes)
24 views2 pages

Sheet 3 - Nested Classes

The document discusses nested classes and provides examples. It asks questions about the Java API documentation for the Box class and its nested classes. It also provides code examples for an inner class and iterating over elements using iterators and lambda expressions.

Uploaded by

rahmasalah862
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views2 pages

Sheet 3 - Nested Classes

The document discusses nested classes and provides examples. It asks questions about the Java API documentation for the Box class and its nested classes. It also provides code examples for an inner class and iterating over elements using iterators and lambda expressions.

Uploaded by

rahmasalah862
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Menoufia University Advanced Programming Languages

Faculty of Electronic Engineering


Computer Science and Engineering
‫جام عة الم نوف ية‬

Nested Classes
Questions
1. The following program Problem.java doesn't compile. What do you need to do to make it compile?
Why?

public class Problem {


String s;
static class Inner {
void testMethod() {
s = "Set from Inner";
}
}
}

2. Use the Java API documentation for the Box class (in the javax.swing package)
to help you answer the following questions.
a. What static nested class does Box define?
b. What inner class does Box define?
c. What is the superclass of Box's inner class?
d. Which of Box's nested classes can you use from any class?
e. How do you create an instance of Box's Filler class?

3. Get the following file Class1.java. Compile and run Class1. What is the output?

public class Class1 {


protected InnerClass1 ic;

public Class1() {
ic = new InnerClass1();
}

public void displayStrings() {


System.out.println(ic.getString() + ".");
System.out.println(ic.getAnotherString() + ".");
}

1
static public void main(String[] args) {
Class1 c1 = new Class1();
c1.displayStrings();
}

protected class InnerClass1 {


public String getString() {
return "InnerClass1: getString invoked";
}

public String getAnotherString() {


return "InnerClass1: getAnotherString invoked";
}
}
}

4. The following exercises involve modifying the class DataStructure.java, which


the section Inner Class Example discusses.
a. Define a method named print(DataStructureIterator iterator). Invoke this
method with an instance of the class EvenIterator so that it performs the
same function as the method printEven.
b. Invoke the method print(DataStructureIterator iterator) so that it prints
elements that have an odd index value. Use an anonymous class as the
method's argument instead of an instance of the
interface DataStructureIterator.
c. Define a method named print(java.util.function.Function<Integer,
Boolean> iterator) that performs the same function.
as print(DataStructureIterator iterator). Invoke this method with a lambda
expression to print elements that have an even index value. Invoke this
method again with a lambda expression to print elements that have an odd
index value.
d. Define two methods so that the following two statements print elements
that have an even index value and elements that have an odd index value:

DataStructure ds = new DataStructure()


// ...
ds.print(DataStructure::isEvenIndex);
ds.print(DataStructure::isOddIndex);

You might also like