views
Create a Java project. Click the drop down arrow beside the first icon at the top left of the screen called “new.” Select “Java Project." Give your new project a name and click finish. This example project will be called "Tip Calculator." You can see that a new folder with your project name appears in the left hand column.
Highlight the folder you have just created. Now click the “new” drop down menu once again. This time, pick "Class." Give your new class a name. For this class, we can simply use the default settings. Click finish and Eclipse will open a new Java class in the program window.
Eclipse automatically creates a class in the program window. This class uses a method called calculate tip to calculate the tip on a restaurant check. Each line in the example class shown is explained using line comments. Line comments start with “//” and are ignored by the java compiler.
Create the main method. Go back to the drop down menu called “new” and, once again, pick “Class.” This time, check the box that says “public static void main(String[]args).” This indicates to Eclipse that you want to create a main method. Give your main class a name and click finish. Eclipse now opens a new tab with your main class.
Write the main method. The main method should call the method calculateTip from the Calculator class. This main class will take input from the user for the amount of the bill and the % that they would like to tip. It then passes this information to the Calculator class and gets the calculation. Then the program displays the total.
Click the "run" button (which looks like a "play" icon) that is located just above your program window. This action will automatically save and compile your code. Your program will run in the console window just below the program window.
Check to see if your program works correctly. Eclipse includes an excellent debug feature. The image shows example code with some mistakes. To go through the program line by line, add a break point by double clicking the left margin of the line where we expect to encounter problems. You can see the break point at line 9.
Click the "debug" icon at the top of the window, right beside the "run" button. It looks like a bug. Debugging mode will open some new windows that show you exactly what is happening when your program runs. The program will stop at the break point that you have set up. From there, you can navigate using the “step over” arrow, just above your main display or by hitting F6. The “step over” command takes you line by line through your program.Write a Java Program With Two Classes Using Eclipse Step 9 Version 2.jpg
Debug as you step through your program. As you "step" through your program, you can "mouse" over your variables in your program window to see what values they contain during each step. Alternatively, you can open the variable window located at the top right of Eclipse. With this window open, you can see what a list of your variables, and what numbers they are holding.
Comments
0 comment