How to Create a Java Class in Netbeans to Compute the Area of a Triangle Using Heron's Formula
This guide will walk you through the steps need to compute the area of a triangle using the Heron's Formula in NetBeans. You may use any IDE you like, but this tutorial will be using NetBeans and Oracle's Java JDK 8u101. Download and...
Part 1 of 2:
Preparing Your Java Project
- Download and install the Java JDK and NetBeans bundle here:http://www.oracle.com/technetwork/articles/javase/jdk-netbeans-jsp-142931.html
- Open NetBeans on your computer and select new project.
- Pick your project settings. Under categories select Java and under projects select Java Application. Then click Next.
- Deselect Create Main Class and then give your project a name. The one here is named "Heron". Then click Finish.
- You can choose any file destination for this project.
- Open the menu to create a new Java class.
- By default the Projects tab will be open. With the projects tab open, select the "+" (or drop down icon) to the right of your "Heron" project within the Projects tab.
- You will then see two more items, the Source Packages and Libraries.
- Click the + to open the Source Packages (or drop down icon).
- Right click , select New > Java Class.
- Give your class a name. Remember that it must start with an uppercase letter.
- Click Finish. You are now ready to write the code.
Part 2 of 2:
Programming Your Java Class
- Set up 3 instance variables for this class, one for each of the three sides.
- It is good to make them private and you will also want to give them a type of double so that you can get decimals for more accuracy.
- Give your instance variables each their own unique name. Make it something literal, for example, mine was side1, side2, side3.
- Insert the instance variables under the bracket after Heron.
private double side1;
private double side2;
private double side3;
- Create the parameters.
- Under the instance variables, create a comment section listing the parameters.
- To make a long comment, type /** and then hit ↵ Enter. This will create a multi-line comment.
- Type the parameters in this space.
- Create a triangle, given the lengths of the side:
@param side1 length of a side
@param side2 length of another side
@param side3 length of the other side
- Create our constructor.
- Create a public constructor so that when you create an object in your tester class, it will have the 3 parameters you established earlier.
- Your constructors parameters should all be set to type double.
- It should read:
public Heron(double side1, double side2, double side3) {
- Now create 'this' statements to link to the parameters. Type these after the bracket and then close the bracket.
- Create a mutator method to return the area of the triangle.
- Create a mutator method that uses Heron's formula.
- Set the method to public and give it a type double.
- Give the method a name of your choosing, like
getArea
. Make sure to use camel case meaning the first word is lowercase and the second is uppercase. - It should read:
public double getArea( ) {
- You need a variable that calculates half of the perimeter. To do this, give it a type double and a name, like "halfPerim". Set it equal to a formula that would return half of the perimeter of a triangle. In this case,
halfPerim = (side1 + side2 + side3) / 2
- Now you need to create the formula that actually gives us the area and uses Heron's formula. You will also give this a type double and set area equal to the formula.
- Heron's formula: where is equal to half the perimeter, and , , and are the side lengths.
double area = Math.sqrt(halfPerim *(halfPerim - side1) * (halfPerim - side2) * (halfPerim - side3));
- The last step is to create a return statement. Use
return area
and then close your bracket.
- Create a string to return each side of the triangle.
- Create a public string to return the lengths of each side of the triangle:
public String toString( ) {
- Make sure the S in string is capitalized and then after the brackets insert your return statement:
return side1 + ", " + side2 + ", " + side3
- Then close your bracket and your class is now complete!
- Create a public string to return the lengths of each side of the triangle:
- Create a test class. It will be your Main Class that will run the file.
- Create a tester class to show that your Heron class is properly functioning.
- To create another Java Class, you can repeat the same steps in Part 1 of this tutorial; the only difference is that when in the "New Java Application" window, you will check Create a Main Class and name your new class HeronTester, because your files can't have the same name. This class will be saved in the same folder as your 'Heron" class.
- Click Finish and now you can write the tester code.
- Import swing classes to be able to allow the user to input data in your tester.
- Before you start the code, you should import the swing class in order to be able to input data and easily change it.
- The swing class is:
import javax.swing.JOptionPane
- This needs to be placed at the very top above
public class HeronTest
- If you are unsure where to put this line of code, you can put it on the very first line above all of your other code.
- Add the code to make it your "main" class, if needed. By default, when you created a new java class and checked the box to create the main class, NetBeans will automatically generate the code necessary to consider it the "main" class. If you don't see the code, you can follow these instructions.
- To create a main method, after the bracket type:
public static void main(String args[ ]) {
- This makes it the first method that will be read and in this case, it is the only method to be read.
- To create a main method, after the bracket type:
- Create the statements that allow the user to input the data.
- Insert statements to get the lengths of the sides from the user and store them in side1, side2, and side3. This is where we will use the swing class to create boxes to input information.
- Type the following lines of code below to create input boxes when you run the program.
String input = JOptionPane.showInputDialog("Enter length of side 1 ");
double side1 = Double.parseDouble(input);
vString input2 = JOptionPane.showInputDialog("Enter length of side 2 ");
double side2 = Double.parseDouble(input2);
vString input3 = JOptionPane.showInputDialog("Enter length of side 3 ");
double side3 = Double.parseDouble(input3);
- Write the code to create a new Heron object, and print the results in the run console.
- To do this, Type the name of the object with a capital, the name of this version of the object (can be any name you want) to make it look like the line below:
Heron heron = new Heron(side1, side2, side3);
- To get the line to print we need to use a System.out.println statement:
System.out.println("A triangle with sides of " + heron.toString() + "...n...has area of " + heron.getArea( ));
- To do this, Type the name of the object with a capital, the name of this version of the object (can be any name you want) to make it look like the line below:
- Run the project!
- Click the green arrow at the top. (Or right click anywhere in the code and hit Run). A box should pop up saying 'Enter length of side 1'. Enter a number and repeat the process for sides 2 and 3.
- You should then get an output stating 'A triangle with sides of x, x, x… ...has area of x'.
3.5 ★ | 2 Vote
You should read it
- Interface in C ++ (Abstract class)
- Instructions for creating double exposure images in Photoshop
- Storage class in C / C ++
- How to create a double exposure effect using Snapseed
- Contructor and Destructor in C ++
- Class selector in CSS
- Pseudo-Class in CSS
- Class (Class) in C #
- How to Crash a Class in College
- Storage class in C programming
- How To Make The Best Class Assignments?
- Calculate inheritance in C #
Maybe you are interested
How to create a 2024 collage on Instagram
How to Create a Budget Spreadsheet in Google Sheets
How to create names with special characters, add special characters to game names, game character names
How to create a notification channel on Instagram
10 Christmas-themed YouTube backgrounds to help create a festive atmosphere
How to create name tags in Word