Launch NetBeans by selecting it from the start menu.
When NetBeans starts up you should see an empty environment looking something like this.
![]() |
NetBeans uses a project structure to organize the various components of programs that you write. The first step in creating a new program is to create a project for your program. Select New Project... from the File menu to start this process.
The first thing you will see is a dialog box asking you for configuration options for the project.
![]() |
Select 'Java' from the list of project categories on the left and select the 'Java Application' project type from the list on the right. Click Next.
The next dialog will ask you to enter project settings having to do with the contents of the project.
![]() |
There are several setting you will want to change here.
Click Finish to dismiss the dialog. You should now have something that looks like this:
![]() |
You now have the minimal skeleton for a program. You could now start typing Java statements into the body of the main method in place of the comment that says
// TODO code application logic here
As a more convenient shortcut for this first exercise, you will copy and paste some code I have already written. Delete the contents of the Main.java file, click this link, select its contents and paste them into your Main.java source file.
The code I have provided is free of errors and will compile and run. To run the program, select 'Run Main Project' from the Run menu, or press F6, or click the green triangle in the toolbar. Your program will compile and run. When the program runs, it will use the output pane at the bottom of the NetBeans window for text input and output. This particular program will prompt you to enter a number of feet and inches. Type your height in the output pane. The program will respond by printing your height in meters.
A frequent experience you will have while learning to program is having to cope with errors. The NetBeans development environment is quite sophisticated, and includes numerous features to help you to quickly catch and fix errors.
Your primary resource for catching simple errors is the editor itself. As you type your program's source code, the editor will be monitoring what you type. When it discovers something that it thinks is an error, it will flag the offending code in red and give you information on what it thinks is wrong. Try this now: remove a semicolon somewhere or change the spelling of a variable. The editor should immediately flag the change you made as an error.
The types of errors that the editor is equipped to catch are mostly syntax errors, errors in the structure of the code. Another common type of error that programs can contain are runtime errors. A runtime error results when the source code of a program is all correct in that it follows all of the language rules for Java, yet the program enters an error state while running. To see what a runtime error looks like, change the line in the program that reads
input = new Scanner(System.in);
to read
input = null;
instead. That line is responsible for creating the Scanner object that reads the user's input from the output pane. If this object is not properly initialized, things will go wrong when we try to use the Scanner later in the program to read input. The program will still compile and run, but when the program hits the first line where we try to do something with input, Java will generate an exception. Java generates and reports exceptions whenever something goes wrong during the run of the program. When your program generates an exception, a message will appear in the output pane along with information on which line in your source code Java was running when the exception took place. If you click on that location information in the output pane, the editor will take you to the line where the exception was generated.
Go ahead and restore the line you modified back to its original form.
A final kind of error you will encounter is the logic error. If your program compiles and runs without throwing any exceptions yet produces results that are clearly wrong, you most likely have made a mistake in the logic of your program. Logic errors are somewhat harder to catch and fix, because they don't come with obvious clues pointing to the specific spot in your source code where you made the mistake.
One way to catch and fix a logic error is to think about the inputs and outputs to your program. Is there any clue in the output to help you figure out what you did incorrectly? Try reading through the structure of your computation to see if you can pinpoint the spot where you made a mistake that may have lead to the result you see.
If you can't locate the spot where you made the mistake, you may find it useful to use a tool called the debugger to illuminate what your program is doing.
To use the debugger, start by clicking the gray bar at the left of the source code editor window. This will insert a breakpoint in your code. The breakpoint will show up as a red square in the gray column where you clicked.
To activate the breakpoint, run your program in the debugger by selecting 'Debug Main Project' from the run menu. Your program will run until it hits the breakpoint you set. At this point NetBeans will look something like this:
![]() |
The green arrow in the source code window will point to the line where the program stopped. A pane in the lower right hand corner of the main window will display information about the current state of the variables in the program. With the program stopped at the breakpoint you can then run your program one statement at a time and watch the variable values get updated in the pane at the bottom. To step through one statement, press the F8 button.
Extend and modify the code for the example I have provided to solve this modified version of exercise 2.14 at the end of chapter 2 in the text:
Body Mass Index (BMI) is a measure of healthy weight. It can be calculated by taking your weight in kilograms and dividing by the square of your height in meters. Write a program that prompts the user to enter a weight in pounds and a height in feet and inches and displays the BMI. Note that one pound is 0.45359237 kilograms.
Write a program to solve exercise 2.5 at the end of chapter 2 in the text:
Write a program that reads the subtotal and the gratuity rate, and computes the gratuity and the total. For example, if the user enters 10 for the subtotal and 15% for the gratuity rate, the program displays $1.5 as the gratuity and $11.5 as the total.
Do exercise 2.11 from the end of chapter 2 (write only the b version using console input and output). To submit your work for grading, email me a copy of the .java file containing the source code for your program. This assignment is due by the start of class time on Friday.