Ready Tutorial 6. Ready Tutorial

Ready Tutorial 6 Ready Tutorial Introduction The Ready Tutorial is designed to gradually introduce the features of the Ready environment. By reading...
1 downloads 2 Views 544KB Size
Ready Tutorial

6

Ready Tutorial Introduction The Ready Tutorial is designed to gradually introduce the features of the Ready environment. By reading the Ready Tutorial and following along on the computer, you will learn the how to enter and run Java programs using the Ready to Program with Java Technology (hereafter called Ready) environment. The best way to learn how to use the environment is by actually using it, so we strongly recommend that you have access to a computer when you work your way through the Ready Tutorial. If you have not programmed using an IDE (Integrated Development Environment) you are advised to start with the Ready Tutorial. Once you are familiar enough with the environment to enter and run programs, you should read the Ready User's Guide. Likewise, if you want more detail about any aspect of the environment, consult the Ready User's Guide. If you are familiar with IDEs, you may wish to skip the Ready Tutorial and read the Ready User's Guide directly. This tutorial assumes that you are familiar with the basic windows paradigms such as clicking buttons, selecting menus, clicking and dragging and so on. If you are unfamiliar with these concepts, read the Microsoft Windows manual that accompanied your computer. You do not need to know the details of the Java language to use the Ready Tutorial . The Tutorial teaches some basic concepts about Java programs but is not intended to familiarize you with all elements of Java programming. When entering programs be very careful to enter the program exactly as shown in the text.

5

Creating a Program that Uses the HSA Console In this chapter, you will be introduced the Ready environment and some basics about Java programs. You will create an application that draws a blue rectangle using the Console class included with the Ready environment. The Console class is provided to help you begin writing Java programs quickly without getting bogged down in the details of Java input and output. You will also be introduced to the Replace command and finding and correcting program errors. Even if you are not planning to use the Console class in your coursework, you are advised to follow the tutorial in this section to get accustomed to the Ready environment.

Ready Tutorial

5.1

7

Terminology There are a few terms that are used in this tutorial that you should be familiar with before reading. Cursor The mouse cursor (this looks like an arrow). Caret The blinking vertical line in a window indicating where text will be entered. Click the Press and release the mouse button while the mouse cursor is over the item. Press the Press the mouse button while the mouse cursor is over the item but do not release it. Click and Drag Press the mouse button while the mouse cursor is over the initial location but do not release it. Move the mouse cursor to the final location. Release the mouse button.

5.2

Starting the Ready Environment If you are using Ready in a school environment, the instructions for starting Ready may differ from what is printed here. Check with the instructor as to how you are to start Ready. To begin programming using Ready, press the Start button to display the Start menu. Select the Programs menu item from the Start menu. This displays the Programs submenu. Select the Ready to Program menu item.

Figure 5.1 Starting the Ready Environment

Ready Tutorial

8

When the Ready environment is loaded, an empty Editor window appears in the upper-left corner. The Ready splash screen appears in the center of the screen. This tells you the version number of the software you are running. The Ready splash screen disappears when you click the mouse, press a key, or in five seconds if you do nothing.

Figure 5.2 The Ready Splash Screen

The initial blank Editor window is now available for you to enter in a new program. You can also create a new class or open an existing class.

5.3

The Editor Window

Figure 5.3 The Editor Window

Ready Tutorial

9

The editor window has four basic parts: the menu bar, the button bar, the text area, and a status bar. • The menu bar contains the menus from which most of the commands to Ready can be issued. • The button bar provides a set of shortcuts to various commonly used commands. Every command in the button bar can be found in the menus. • The text area is the area where the program text is displayed. Program text is entered there. • The status bar appears at bottom of the window . This is where messages are relayed from the system to you. If a message is too long for the status bar, then moving the mouse over the status bar will make a tooltip box appear showing the entire message.

5.4

Organization of Java Programs A Java programs consist of one or more classes. Each class has a name and is saved in a separate file with the same name as the class and the suffix ".java". A Java class named "BlueRect" must be saved in a file called "BlueRect.java". Thus when the chapter refers to "editing a class", this is equivalent to editing the file containing the class. Java classes contain subprograms called methods. A Java class with a main method (a method called main) is a Java application. A Java class that inherits from java.applet.Applet is a Java applet. In this chapter, we will give only an example of creating a Java application. Java classes can be grouped into packages. While this book does not discuss the creation of packages, it does use the packages that are part of the Java class libraries as well as a package included with the Ready called hsa which contains classes used to help you write programs.

Figure 5.4 Organization of a Java Program

Ready Tutorial

10

A Java program follows a certain format. First there are statement indicating what packages are used by the program. Then a statement indicating the name of the class. Inside the class, fields are declared followed by the methods in the class. This means that even the simplest Java program will be several lines long. Many of these lines are common to most Java programs. The Ready environment provides pre-built templates (called boilerplates) which contain these standard lines and other lines that are to be modified by the user. You can create programs more rapidly by editing the boilerplate.

5.5

Creating a New Application This section gives instructions for creating a Java program that uses the hsa Console class to draw a blue rectangle. When Ready starts, the editor window is in the upper-left corner of the screen. To load the template for a Java program using the Console class, select the New menu item from the File menu. This displays a sub-menu with a variety of templates. Select the HSA Console Application Boilerplate menu item.

Figure 5.5 The New Submenu

A dialog box asking for the name of the class appears. Enter BlueRect.

Figure 5.6 The Class Name Dialog Box

The appropriate boilerplate text then appears in the editor window. This is a Java application program. However, the program does not do anything. The text in the window should look as follows. // The "BlueRect" class. import java.awt.*; import hsa.Console; public class BlueRect { static Console c;

// The output console.

public static void main (String[] args) { c = new Console (); // Place your program here. 'c' is the output console. } // main method } // BlueRect class Figure 5.7 The BlueRect Boilerplate

Ready Tutorial

11

The file in the window is color coded. The editor colors each piece of the Java class to help make it easier to examine your program. Here is the default coloring scheme: Java Keywords Identifiers found in the Java Class Library Identifiers from your program Comments Quoted Strings Selected Text

Bold Face Black Blue Green Red White on Blue Background

To make the program draw a blue rectangle, replace the line // Place your program here. 'c' is the output console.

with c.setColor (Color.blue); c.fillRect (0, 0, 100, 100);

Select the comment line by moving the mouse to the beginning of the comment and pressing the mouse button and then dragging the mouse to the end of the line while holding the mouse down. The text is graphically selected as you do this. When the mouse cursor reaches the location where you want the selection to end, release the mouse button. Alternatively, you can click the mouse button once at the beginning of the comment, which moves the caret to the beginning of the comment and then click the mouse button a second time at the end of the comment while holding the Shift key down. To replace the selection simply start typing. The selection disappears and the text you are typing replaces it. The final program should look like this. // The "BlueRect" class. import java.awt.*; import hsa.Console; public class BlueRect { static Console c;

// The output console.

public static void main (String[] args) { c = new Console (); c.setColor (Color.blue); c.fillRect (0, 0, 100, 100); } // main method } // BlueRect class Figure 5.8 The BlueRect Class

The program sets the drawing color for the Console window to blue and then draws a filled rectangle with upper-left corner of (0, 0) and a width and height of 100. The "c." in front of the method names indicates the window into which the

Ready Tutorial

12

color is to be set and the rectangle is to be drawn. (You could theoretically have multiple Console windows.) Once the program has been created, it is a very good idea to save it. To save a Java class, click the Save button in the button bar (the shortcut is Ctrl+S) . If the class has not been saved before, this displays a file dialog box allowing you to specify the name of the file. The default name of the file is already filled in with BlueRect.java. It is not a good idea to change this name. Once you have saved the class, you may also wish to print a copy. You can print the contents of an editor window by selecting the Print button from the button bar (the shortcut is Ctrl+P). This displays a dialog box allowing you to select from a number of options. When you click the OK button, the contents of the editor window is printed.

5.5

Compiling the Program Now that you have finished editing the program and saved it, you can run it. To run a Java class: • press the Run button on the button bar, or • press Ctrl+R (a shortcut), or • press the F1 function key (a shortcut). When the Run command is given, the Ready environment attempts to execute the Java class in the active window. To do this, it first compiles the class into Java bytecode which can then be run on any platform that has a Java interpreter. One bytecode interpreter, the IBM Java Virtual Machine, is built into Ready. However, the bytecode files produced by Ready can be executed using any Java interpreter. When a program is compiled, the result is saved in a file with the same name as the class but with a ".class" suffix. If there were any errors in the program, compare the text in the window with Figure 5.8 and make any corrections before running the program a second time (See Section 5.9 for more details about errors in a program.) Once the program has successfully compiled, it is executed. A message to that effect appears in the status box of the editor window. In the upper-right corner of the screen, a Console window appears. It contains the output from the program.

Figure 5.9 The Output From the BlueRect Class

Ready Tutorial

5.6

13

Quitting Ready Before exiting Ready, make certain your program is saved. You can instantly tell whether there are any unsaved changes in a window by looking at the window's title bar. If the name of the file is preceded by a '*', the window has been modified since it was last saved. Saving the window causes the '*' to disappear. To exit the Ready environment, select Exit from the File menu (the shortcut is Alt+X). A dialog box appears confirming that you wish to exit. Click the Yes button. Ready now exits.

5.7

Opening a File We will now modify the BlueRect.java file to create a new program. We will introduce an error or two to examine how Ready handles program errors. First re-enter the Ready environment as you did in section 5.2. To re-open the BlueRect.java file select Open from the File menu (the shortcut is Ctrl+O ). This displays a standard Open File dialog box. Select the BlueRect.java file by double clicking on it. The file is opened in its own editor window. If the initial blank window is still empty, Ready replaces that window with the new window containing BlueRect.java.

Figure 5.10 The Open File Dialog Box

5.8

Using the Replace Dialog Box When creating programs, you may often wish to replace occurrences of one piece of text in the file with another piece of text. Rather than having to search through the file to find each occurrence and replace it, Ready has a command that can quickly find each occurrence of a piece of text and then replace it at the click of a button. For this example, you will replace the word "Blue" with "Red" using the Replace command in Ready. You will then save the resulting class in RedRect.java. Open up the Replace dialog box by pressing the Replace button in the button bar or selecting Replace from the Search menu (the shortcut is Ctrl+H). The Replace dialog box appears.

Ready Tutorial

14

Figure 5.11 The Replace Dialog Box

Enter "Blue" in the Find What text box and "Red" in the Replace With text box. "Blue" is called the Search string and "Red" is the Replace text. Next, click the Find Next button. The selection jumps to the word "blue" in the class on the line // The "BlueRect" class.

and highlights it. Click the Replace button in the dialog box. "Blue" is replaced with "Red" and the editor jumps to the next occurrence of "Blue", which is in the line public class BlueRect

Continue to replace all the occurrences of "Blue" with "Red". When there are no more occurrences, Ready notifies you that it could not find the search string. Now manually change "Color.blue" to "Color.red" in the line c.setColor (Color.blue);

by selecting the word "blue" and typing "red". If there is an occurrence of the Search string that you do not wish to replace, click the Find Next button rather than the Replace button. You can also make Ready find all occurrences of the search string regardless of the case. You do this by clicking the Match case check box. When this box is not selected, Ready disregards case when looking for a match to the search string. Once all the changes have been made, save the class in a new file by selecting Save As from the File menu. This displays a file dialog box allowing you to save the file with a new name and in a different location. If you try to save over BlueRect.java by clicking the Save button, Ready will warn you that you are trying to save the class RedRect in the file BlueRect.java and that this is not legal. Finally, introduce some errors into your program. In the line c.setColor (Color.blue);

change "c.setColor" to "c.setlor" by removing the "Co". Then go to the next line c.fillRect (0, 0, 100, 100);

and remove the ";" at the end of the line.

5.9

Errors in the Program During compilation errors may be detected. These are called syntax errors and are often caused by mistyping the name of variables, forgetting the semicolon at the end of a statement, or missing a curly brace. When a compilation is finished, if any errors are detected, the program is not executed. Instead, Ready displays a dialog box indicating the number of errors found and then jumps to the point in the file that the error was detected. The line itself is

Ready Tutorial

15

highlighted in light gray with the actual location of the error displayed in black. The error message is displayed in the status bar in bold.

Figure 5.12 Program with Errors

You can correct the error by editing the line. As soon as the line is modified, the error message and highlighting disappear. To go to the next error, if there is more than one, select Find Next Error menu item from the Search menu (the shortcut is the F5 function key). At any point, you can press the F1 function key to compile and run the program again. The compiler may not detect all the syntax errors in one pass. After fixing all the errors found by the compiler, you may find more errors when you compile the program a second time. For example, in the RedRect program, the compiler only found the missing semicolon the first time. Once that error is fixed, running a second time will display the error relating to the misspelled method name.

5.10

Running the RedRect Class

Once all the syntax errors are corrected, the program will begin executing. The Console window will appear in the upper-left corner with the programs output.

Ready Tutorial

6

16

Creating an Applet In this chapter, you will create and run a Java applet that draws a red circle in each corner of an applet window. A Java applet is a program that runs in a web page. Ready does not actually start a web browser. Instead, it contains a minibrowser that only runs Java applets. You will also be introduced to the Cut, Copy and Paste commands as well as the Undo command. This chapter assumes that you have already read Chapter 5.

6.1

Creating an Empty Applet Start the Ready environment. When Ready starts, the editor window is in the upper-left corner of the screen. To load the template for a Java applet, select the New menu item from the File menu. This displays the New sub-menu. Select the Applet Boilerplate menu item. A dialog box asking for the name of the class appears. Enter OvalsApplet. The appropriate boilerplate text then appears in the editor window. // The "OvalsApplet" class. import java.applet.*; import java.awt.*; public class OvalsApplet extends Applet { // Place instance variables here. public void init () { // Place the body of the initialization method here. } // init method

public void paint (Graphics g) { // Place the body of the drawing method here. } // paint method } // OvalsApplet class Figure 6.1 The OvalsApplet Boilerplate

This is a Java applet program containing two methods. However, the applet does not do anything. You will replace the contents of the paint method in the applet. The paint method is called by the system every time the applet is to be displayed, for example when it is first created and whenever another window is moved over top of the applet. We will make this program draw several circles at the corner of the applet's window. To do this, first replace the line // Place the body of the drawing method here.

with g.setColor (Color.red); g.fillOval (0, 0, 100, 100);

Ready Tutorial

17

We will now duplicate the fillOval line three more times. Rather than retyping the line several times, we will use the Copy and Paste command.

6.2

Copy and Paste To duplicate a section of the program one or more times, Ready has a paste buffer. This buffer can hold a section of code that can then be copied into the file as many times as desired. First, you need to copy the line g.fillOval (0, 0, 100, 100);

into the paste buffer. Do this by selecting the line. To select an entire line, move the mouse cursor to the beginning of the line, press the mouse button, drag the mouse button to the beginning of the next line, then release the mouse button. The entire line should be selected. To copy the selection into the paste buffer, select Copy from the Edit menu. You can also use the Copy button on the button bar. If you want to move the text instead of copying it, you can delete the selection at the same time as it is copied to the paste buffer by choosing the Cut command instead. Once the text is in the paste buffer, you can place it anywhere in the file using the Paste command. Move the cursor to the beginning of the line where you want the contents of the paste buffer inserted. Then select the Paste menu item from the Edit menu or click the Paste button in the button bar. The line of text in the paste buffer is placed into the file at the location of the caret. If text is selected when the Paste command is given, the contents of the paste buffer replaces the current selection. Note that pasting text into the file does not affect the contents of the paste buffer. You can paste the contents as many times as desired. Now, move the caret to the beginning of the line following g.fillOval (0, 0, 100, 100);

and paste the line four times. This should give you g.setColor g.fillOval g.fillOval g.fillOval g.fillOval g.fillOval

6.3

(Color.red); (0, 0, 100, 100); (0, 0, 100, 100); (0, 0, 100, 100); (0, 0, 100, 100); (0, 0, 100, 100);

Undo The class now contains five calls to fillOval and only four are needed (one for each corner). Ready allows you to "undo" the last change to the file. To undo the last change, select Undo from the Edit menu (the shortcut is Ctrl+Z). In this case, the last paste command is undone and a fillOval line disappears. If you decide that you did not mean to undo the last operation, you can select Undo again and the text is restored. You can keep skipping back and forth by pressing Ctrl+Z. The Undo command will operate on just about any change in the text. You can undo the results of a Replace All or Indent File command. However, you can only undo the last change made to the file. For example, if you have typed in

Ready Tutorial

18

some text, moved the caret to another location and then typed another piece of text, you can only undo the last piece typed in.

6.4

Running an Applet As the program stands, it will draw four ovals on top of each other. Now change the coordinates of the drawing. The fillOval method draws an oval inscribed in a square whose upper-left corner is specified by the first two coordinates and whose width and height are specified by the next two coordinates. (300, 400)

100

200 g.fillOval (300, 400, 200, 100) Figure 6.2 Oval Drawn by fillOval

Assume that you are drawing on a 500x400 pixel surface. To draw the ovals in the corners, change the first two coordinates in the fillOval methods to (400, 0), (0, 300) and (400, 300) respectively. Here is what the class now looks like. // The "OvalsApplet" class. import java.applet.*; import java.awt.*; public class OvalsApplet extends Applet { // Place instance variables here. public void init () { // Place the body of the initialization method here. } // init method

public void paint (Graphics g) { g.setColor (Color.red); g.fillOval (0, 0, 100, 100); g.fillOval (400, 0, 100, 100); g.fillOval (0, 300, 100, 100); g.fillOval (400, 300, 100, 100); } // paint method } // OvalsApplet class Figure 6.3 The OvalsApplet Class

Ready Tutorial

19

Once the program has been created, it is a very good idea to save it. To save a Java class, click the Save button in the button bar. If the class has not been saved before, this displays a file dialog box allowing you to specify the name of the file. The default name of the file is already filled in with OvalsApplet.java. It is not a good idea to change this name although you may save it in any folder you wish. Once you have saved the class, you may also wish to print a copy. You can print the contents of an editor window by selecting the Print button from the button bar. This displays a dialog box allowing you to select from a number of options. When you click the OK button, the contents of the editor window is printed. To run the applet, click the Run button on the button bar (the shortcut is Ctrl+R or the F1 function key). This compiles the applet. If there are any typing mistakes, they will appear now. Correct them and re-run the applet. Once the applet is successfully compiled, Ready will start to run the program. Unlike application programs, applets need an HTML file for a browser. The HTML file, among other things, specifies the height and width of the drawing area for the applet. In a web browser, this is the amount of space on the web page that is allocated for the applet's output. When you first run an applet, Ready creates an HTML file with the name of the class and an ".html" extension. In this case, Ready creates OvalsApplet.html. In subsequent runs, Ready reads the HTML file to get the applet's dimensions. For the first run, a dialog box appears asking you to supply the size of the applet's drawing area. There are default values, but in this case, enter 500 and 400 for the applet's width and height.

Figure 6.4 The Applet Size Dialog Box

The program is then run. The applet viewer window appears and the output from the applet appears in it.

Figure 6.5 The Output of OvalsApplet Program

Ready Tutorial

20

If you wish to change the size of the applet window in subsequent runs, you can select Run with Args from the Run menu. This displays a dialog box allowing you to specify the applet’s new width and height. Ready will change the HTML file to reflect the new width and height that you specify.

Figure 6.6 The Run with Args Dialog Box

6.5

Applets and Browsers If you wish to run the applet from an actual browser, open of the Internet browser you wish to use and select Open File from the appropriate menu. When opening the file, select the HTML file, not the ".class" file. The browser loads the HTML file and then loads the Java classes referenced in the HTML file. If you wish to place an applet on a web site, you must copy the ".class" files and the ".html" files to the appropriate location on the web site. The browser does not require the ".java" files. Note that you can edit the HTML file created by Ready by hand to add extra HTML tags. The applet viewer will ignore everything but the applet tag. Internet browsers, however, will display the HTML as well as load the applet.

Ready Tutorial

7

21

Creating a Program Consisting of Two Classes In this chapter, you will create a program consisting of two classes. One class will read two numbers from the user and call a method in the second class that prints out a list of numbers and squares starting at the first number up to the second number. You will also learn about the Insert Text and the Indent File commands. The program that you will create is divided into two classes only for the purpose of showing editor features. In actuality, you will not use multi-file programs until somewhat later in the course and with much larger programs. This chapter assumes that you have already read Chapter 5 and 6.

7.1

Creating an Empty Application Start the Ready environment. When Ready starts, the editor window is in the upper-left corner of the screen. To load the template for a Java application, select the New menu item from the File menu. This displays the New sub-menu. Select the Application Boilerplate menu item. A dialog box asking for the name of the class appears. Enter Squares. The appropriate boilerplate text then appears in the editor window. // The "Squares" class. public class Squares { public static void main (String[] args) { // Place your code here. } // main method } // Squares class Figure 7.1 The OvalsApplet Boilerplate

7.2

Inserting Text in a Class To read from the keyboard in Java requires a few lines of text. Rather than have you type the text in each time, Ready provides you with a way to insert preset blocks of code (called templates) into your program. Ready comes with templates for using the keyboard for input and for reading an integer from the keyboard. To start, move the caret to the beginning of the line // Place your code here.

in the text. Then select the Insert submenu from the Edit menu. From the Insert submenu, select Convert System.in to stdin from the submenu. Two lines of code will appear in your program.

Ready Tutorial

22

// The "Squares" class. public class Squares { public static void main (String[] args) { BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in)); // Place your code here. } // main method } // Squares class Figure 7.2 After Inserting "Convert System.in to stdin"

Note that its quite possible you will need to edit the inserted text. For example, if you are reading two integers using the Read Integer from stdin template in the same file, you will need to change the occurrences of the variable value to something else in the second case.

7.3

Indenting a Class While the text has been inserted in your file, it is not properly indented. In general, it is desirable to indent the contents of anything in curly braces so that you can quickly determine the end of a structure (for loop, if statement, method, and so on) without having to closely read the file. To automatically indent the file, simply click the Indent button in the button bar (the shortcut is Ctrl+I or the F2 function key). The class is indented properly. We recommend that you indent your file frequently to make it more presentable and to help detect missing curly braces. // The "Squares" class. public class Squares { public static void main (String[] args) { BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in)); // Place your code here. } // main method } // Squares class Figure 7.3 After Indent File Command

7.4

Adding More Inserted Text Reading a number from the keyboard is a fairly involved task in Java (which is why we supply the Console, TextConsole and Stdin classes). This is because Java forces you to handle the possibility of an I/O error when reading from the keyboard and the possibility of the user entering non-numeric input when converting a string to an integer. In other languages you can ignore these possibilities (with unpleasant results if they do occur). Ready supplies you with the text to read from the keyboard. If it is not already there, place the caret at the beginning of the line // Place your code here.

Ready Tutorial

23

in the text. Then select the Insert submenu from the Edit menu. From the Insert submenu, select Read Integer from stdin from the submenu. This adds a block of text. Because we want to read two integers, repeat the action a second time. Like the insertion in Section 7.2, the newly inserted text is not indented properly, so press the F2 function key to indent it. The program now looks like this. // The "Squares" class. public class Squares { public static void main (String [] args) { BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in)); int value; while (true) { try { String valueString = stdin.readLine (); try { value= Integer.parseInt (valueString); break; } catch (NumberFormatException e) { System.out.println ("Not a valid integer: " + valueString); } } catch (IOException e) { System.out.println ("I/O error while reading from standard input!"); System.exit (0); } } // while int value; while (true) { try { String valueString = stdin.readLine (); try { value = Integer.parseInt (valueString); break; } catch (NumberFormatException e) { System.out.println ("Not a valid integer: " + valueString); } } catch (IOException e) {

Ready Tutorial

24

System.out.println ("I/O error while reading from standard input!"); System.exit (0); } } // while // Place your code here. } // main method } // Square class Figure 7.4 After Inserting "Read Integer from stdin"

As mentioned earlier, you must edit the inserted templates. Replace the value in the lines int value;

and value = Integer.parseInt (valueString);

with lower for the first two occurrences and upper for the second two occurrences. You can do this by manually selecting and retyping or by using the Replace command. The program now looks like this. // The "Squares" class. public class Squares { public static void main (String [] args) { BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in)); int lower; while (true) { try { String valueString = stdin.readLine (); try { lower = Integer.parseInt (valueString); break; } catch (NumberFormatException e) { System.out.println ("Not a valid integer: " + valueString); } } catch (IOException e) { System.out.println ("I/O error while reading from standard input!"); System.exit (0); } } // while int upper; while (true) { try {

Ready Tutorial

25

String valueString = stdin.readLine (); try { upper = Integer.parseInt (valueString); break; } catch (NumberFormatException e) { System.out.println ("Not a valid integer: " + valueString); } } catch (IOException e) { System.out.println ("I/O error while reading from standard input!"); System.exit (0); } } // while // Place your code here. } // main method } // Square class Figure 7.5 After Substituting "lower" and "upper" for "value"

To complete the program, select the // Place your code here.

line, replace it with SquareTable.printTable (lower, upper);

and insert the following line just after the initial comment import java.io.*;

Here is the final program. // The "Squares" class. import java.io.*; public class Squares { public static void main (String [] args) { BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in)); int value; while (true) { try { String valueString = stdin.readLine (); try { value= Integer.parseInt (valueString); break; } catch (NumberFormatException e) { System.out.println ("Not a valid integer: " +

Ready Tutorial

26

valueString); } } catch (IOException e) { System.out.println ("I/O error while reading from standard input!"); System.exit (0); } } // while int upper; while (true) { try { String valueString = stdin.readLine (); try { upper = Integer.parseInt (valueString); break; } catch (NumberFormatException e) { System.out.println ("Not a valid integer: " + valueString); } } catch (IOException e) { System.out.println ("I/O error while reading from standard input!"); System.exit (0); } } // while SquareTable.printTable (lower, upper); } // main method } // Square class Figure 7.6 The Squares Class

Run this program. After correcting typographical errors, there should be one error left. The error message is "Error: "SquareTable" is either a misplaced package name or a non-existent entity". This means that you have not yet created the Square Table class.

7.5

Editing Multiple Files We will now create a second class. Adding another class will put two editor windows on the screen, one for each file. To create the second class, select the New submenu and select Empty File . This displays a second editor window with no contents. Enter the following code in the class: // The "SquareTable" class. public class SquareTable { public static void printSquares (int lower, int upper); {

Ready Tutorial

27

for (int i = lower ; i