Project Folder

Getting the project folder

For this first lab exercise you will be working with a NetBeans project that I have prepared ahead of time. To get the project folder, simply click the blue button above. This will download an archive containing the example project.

Go to your Downloads folder to locate the archive you just downloaded. Next you will need to extract the project folder from the archive. To do this on a Mac, just double-click the archive. To do this on Windows, right-click on the archive you downloaded and choose the option to extract files from the archive.

Once you have extracted the project folder from the archive, you should drag the folder from your Downloads directory to the NetBeansProjects folder in your home directory.

To open the project in NetBeans, start NetBeans and select Open Project from the File menu.

Once you have opened the project, locate the HeightInMeters.java file in the project and double-click it to open the source code file in the editor.

Fixing errors

The example project I have provided contains a short program designed to convert a person's height from feet and inches to meters. I have intentionally inserted a number of errors into the program. The purpose of today's lab exercise is to teach you about some of the tools that NetBeans offers to help you find and fix errors in your program.

The first thing you will notice when you open the source code file is that a number of statements and parts of statements are underlined in red. NetBeans has powerful built-in tools that can scan your source code and identify errors: whenever NetBeans finds an error in your code it will underline the problem item in red.

In this lab exercise we are going to look at a number of common error causes and learn how to fix them.

Import statements

The program that we are working with today makes use of the Scanner facility to read input from the user. The Scanner is a class provided by the Java Class Library. To use this class in a program, we have to import this class into our program via a Java import statement.

When you use a class in a program without importing it first, NetBeans will flag your first use of this class by underlining the class name in red. Locate the first mention of the Scanner class in the program and hover your mouse pointer over the red underline. This will pop up a hint giving further information on the error. In this case, you will see an "cannot find symbol" error message. This error is caused either by using a variable that has not been declared, or by using a class that has not been defined. In the case of a class, like the Scanner class, we define the class by importing the class definition.

NetBeans has a handy tool for fixing missing class definitions. To use this tool, pull down the Source menu and look for the Fix Imports command. This command will check for missing import statements and insert the correct import statements for you. (Note that this command also has a handy keyboard shortcut. Since you will find yourself using the Fix Imports command often, you may find it handy to memorize this keyboard shortcut.)

A type mismatch

All variables in a Java program have an associated type. For example, this program defines several variables designed to store the user's height information.

int feet, inches, totalInches;

Note that all three of these variables are declared as type int.

The next problem you will see underlined in red comes when we try to read values from the Scanner to put in these variables. If you hover the mouse over the red underline, you will see that NetBeans is complaining about "incomptible types". The issue here is that we have used a command designed to read double values in combination with an int variable. Since these are two different types, Java will flag this as an error. Go ahead and fix this error by replacing the nextDouble() command with the command needed to read an int instead.

A misspelled variable

The next error you will see underlined in red is a variable name that has been misspelled. When you use a variable in a Java program you have to declare it first, and then spell the name of that variable in exactly the same way each time you use it in the program. This includes even minor details such as using the correct case for each letter in the variable name. Go ahead and fix the misspelled variable.

A missing semicolon

The final error in this example program involves a missing semicolon at the end of a statement. Find the offending statement and fix it.

A logic error

The project contains one last error. NetBeans can not locate this error, because this error does not involve a violation of a language rule. This error is a logic error, which is a legal command that does the wrong thing. Take a look at the program logic and see if you can locate and fix this error.

If you are having trouble locating the error, try running the program and looking at the output it produces. Does the output look reasonable?

Finishing the lab exercise

Once you have fixed all of the errors in the program, please go to my personal meeting space on Zoom to show me your completed work. I will ask you to share your screen with me to show me your program working correctly.