How to Create a Java Class in Netbeans to Compute the Area of a Triangle Using Heron's Formula
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
May be interested
- The unexplained mystery series in the terrestrial Bermuda triangle of demonsthe bermuda triangle and the egyptian pyramid are famous for their unanswered mysteries that are known to the world. song at mapimí silent zone, a mysterious land in mexico known as the silent area, also contains many mysteries that make scientists headaches.
- The formula calculates the area around the dead cone, the total area of the dead-end cone, the volume of the dead-end coneyou can easily see that we often catch a dead end in life like a bucket or lampshade ... with quantrimang learn about the dead end, the formula for calculating the area and volume of this shape.
- Formula to calculate trapezoidal area: normal, square, weightin the article below, tipsmake.com will work with you to learn some content related to the topic of trapezoid area calculation - a basic knowledge you can apply during the period of cultural study and in future algorithm programs.
- The formula for calculating rectangular box area, rectangular box volumeyou can easily deduce how to calculate the surrounding area, the total area and the rectangular box volume based on the formula for calculating the area of the rectangle and the perimeter of the rectangle.
- Formula to calculate area - circumference of a circlein this article, tipsmake.com will reintroduce effective circular area and circumference formulas for learning and work.
- Formula to calculate the area of the rectangle, the perimeter of the rectanglethe rectangle is one of the most common figures in reality when calculating the area, such as calculating the area of land, houses, objects ...
- Formula to calculate the area of rhombus, the circumference of the diamondthe rhombus is a quadrilateral with 4 equal sides. this is a parallelogram with two equal adjacent edges or parallelograms with two perpendicular diagonal lines.
- Download Java Runtime Environment 8-build-251java runtime environment (jre) is a software class that provides the services needed to execute java applications. it is an essential component of the java development kit (jdk) and contains all the tools needed to run various java-based software programs.
- Learn about Java Driver in MongoDBin the following article, we will introduce you some basic features of mongodv java driver as well as how to deploy and apply in practice.
- What is Java? Why choose Java?what is java? java is a high-level programming language, developed by sun microsystem and released in 1995. java runs on a variety of platforms, such as windows, mac and various versions of unix.