Lab Exercises and homeworks
- Exercise 1: Build and run Java applications that use inner classes (20 minutes)
- Homework Exercise (for people who are taking Sang Shin’s “Java Programming online course”)
Exercise 1: Build and run Java applications that use inner class
(1.1) Build and run a simple application that uses an inner class
0. Start the NetBeans IDE if you have not done so.
1. Create a NetBeans project
- Select File from top-level menu and select New Project.
- Observe that the New Project dialog box appears.
- Select General under Categories section and Java Application under Projects section.
- Click Next.
- Under Name and Location pane, for the Project Name field, enter InnerClassExample. (Figure-1.10 below)
- Click Finish.
Figure-1.10: Create a new NetBeans project
- Observe that the InnerClassExample project node is created under Projects pane of the NetBeans IDE and IDE generated Main.java is displayed in the editor window of the IDE.
2. Modify the IDE generated Main.java as shown in Code-1.11 below.
package innerclassexample;
public class Main { public static void main(String[] args) { // Create an object instance of a class that contains // Create an object instance of an inner class. // Display data from both outer class and inner class. // Invoke a method from an inner class } |
Code-1.11: Main.java
3. Write OuterClass.java as shown in Code-1.12 below.
package innerclassdemo;
/** /** Creates a new instance of OuterClass */ // Define a variable in the outer class // Define an inner class } |
Code-1.12: OuterClass.java
3. Build and run the program
- Right click InnerClassExample project node and select Run Project.
- Observe the result in the Output window of the NetBeans IDE. (Figure-1.12 below)
Access data from outer class = 5 Access data2 from inner class = 10 data from OuterClass = 5 data2 from InnerClass = 10 |
Figure-1.13: Result
Solution: This exercise is provided as a ready-to-open-and-run NetBeans project as part of hands-on lab zip file. You can find it as <LAB_UNZIPPED_DIRECTORY>/javainnerclass/samples/InnerClassExample. You can just open it and run it.
Summary
In this exercise, you have build and run Java applications that use an inner class.
Homework exercise (for people who are taking Sang Shin’s “Java Programming online course”)
- Modify OuterClass.java to create another inner class within the InnerClass itself.
- Modify InnerClassExample.java as shown in Code-1.15 below.
package innerclassexample;
public class InnerClassDemo { public static void main(String[] args) { // Create an object instance of a class that contains // Create an object instance of an inner class. // Create an object instance of an inner class. // Display data from both outer class and inner class. // Invoke a method from an inner class } |
Code-1.15: Modified InnerClassDemo.java
- The result should look something similar to Figure-1.16 below.
Access data from outer class = 5 Access data2 from inner class = 10 Access data3 from innerinner class = 15 data from OuterClass = 5 data2 from InnerClass = 10 data from OuterClass = 5 data2 from InnerClass = 10 data3 from InnerInnerClass = 15 |
- Zip file of the the MyOwnInnerClassExample NetBeans project. (Someone else should be able to open and run it as a NetBeans project.) You can use your favorite zip utility or you can use “jar” utility that comes with JDK as following.
- cd <parent directory that contains MyOwnjavainnerclass directory> (assuming you named your project as MyOwnInnerClassExample)
- jar cvf MyOwnInnerClassExample.zip MyOwnInnerClassExample (MyOwnInnerClassExample should contain nbproject directory)
- Captured output screen – name it as JavaIntro-javainnerclass.gif orJavaIntro-javainnerclass.jpg (or JavaIntro-javainnerclass.<whatver graphics format>)
- Any screen capture that shows that your program is working is good enough. No cosmetic polishment is required.
- If you decide to use different IDE other than NetBeans, the zip file should contain all the files that are needed for rebuilding the project.