Home

 

Using the Java scrapbook

The scrapbook feature can be used to quickly run and test Java code without having to create an executable testing class. Snippets of Java code can be entered in a scrapbook page and evaluated by simply selecting the code and running it.

A scrapbook page can be added to any project and package. The extension of a scrapbook page is .jpage, to distinguish it from normal Java source file.

To create and run a scrapbook page, do these steps:

Right-click a package (itso.rad75.bank.client) in the Package Explorer and select New Æ Other Æ Java Æ Java Run/Debug Æ Scrapbook Page.

Enter a file name (TestScrapBook) and click Finish. Note that we already imported such a scrapbook.

The scrapbook page opens in the Java editor and you can enter the snippet Java code.

Example | -7 contains two little snippets. The first one is related to the ITSO Bank application and based on the BankClient class main method, and the second one is a simple code snippet to produce a multiplication table.

Example 8-7 Java scrapbook examples

// ITSO Bank Snippet
itso.rad75.bank.ifc.Bank oITSOBank = 
		itso.rad75.bank.impl.ITSOBank.getBank();
System.out.println("\nITSO Bank is listing all customers status");
System.out.println(oITSOBank.getCustomers() + "\n");
for (itso.rad75.bank.model.Customer
		customer:oITSOBank.getCustomers().values()) 
{
	System.out.println("Customer: 	"+ customer);	 
	System.out.println(oITSOBank.getAccountsForCustomer(customer.getSsn()));
}
// Multiplication Table Snippet
String line;
int result;
for (int i = 1; i <= 10; i++) {
	line ="row " + i + ": ";
	
	// begin inner for-loop
	for (int j = 1; j <= 10; j++) {
		result = i*j;
		line += result + " ";
	} // end inner for-loop
	System.out.println(line);
}

Important: All classes that are not from the java.lang package must be fully qualified, or you have to set import statements:

Right-click anywhere in the scrapbook page editor and select Set Imports.

For the example, add the following types and packages:
itso.rad75.bank.model.*
itso.rad75.bank.ifc.Bank
itso.rad75.bank.impl.ITSOBank

You can execute, display, or inspect a snippet:

Select the code of the // Multiplication Table Snippet, right-click, and select Execute, or press Ctrl+U, or click in the toolbar. All output is displayed in the Console view.
Select the // ITSO Bank Snippet, right-click, and select Display, or click in the toolbar. Again, all output is displayed in the Console view.
Select the // ITSO Bank Snippet, right-click, and select Inspect, or press Ctrl+Shift+I, or click in the toolbar. Again, all output is displayed in the Console view. But in addition, an expression box opens, which allows you to inspect the current variables. Pressing Ctrl+Shift+I again opens the Expression view, which you can find in the Debug perspective, as described in Chapter | 4, Debugging local and remote applications.

Note: You cannot execute, display, or inspect a snippet in the scrapbook page, unless you have selected the code.

Click in the Console view to end the scrapbook evaluation.
ibm.com/redbooks