Clear, practical technology insights BSOD Code Lookup · Windows Error Code Lookup · Wi-Fi Troubleshooting · PC Troubleshooting Checklist

How to Create a Java Class in Netbeans to Compute the Area of a

Learn how to create a java class in netbeans to compute the area of a with clear steps, practical context, and useful troubleshooting guidance.

Table of Contents

This updated guide examines How to Create a Java Class in Netbeans to Compute the Area of a and organizes the essential facts, background, and practical takeaways in clear American English.

Part 1

Preparing Your Java Project

  1. How to Create a Java Class in Netbeans to Compute the Area of a — contextual image 1 Download and install the Java JDK and NetBeans bundle here: http://www.oracle.com/technetwork/articles/javase/jdk-netbeans-jsp-142931.html
  2. How to Create a Java Class in Netbeans to Compute the Area of a — contextual image 2 Open NetBeans on your computer and select new project.
  3. How to Create a Java Class in Netbeans to Compute the Area of a — contextual image 3 Pick your project settings. Under categories selectJavaand under projects selectJava Application. Then clickNext.
  4. How to Create a Java Class in Netbeans to Compute the Area of a — contextual image 4 DeselectCreate Main Classand then give your project a name. The one here is named "Heron". Then clickFinish.
    • You can choose any file destination for this project.
  5. How to Create a Java Class in Netbeans to Compute the Area of a — contextual image 5 Open the menu to create a new Java class.
    • By default theProjectstab 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, theSource PackagesandLibraries.
    • Click the+to open the Source Packages (or drop down icon).
    • Right click, selectNew>Java Class.
  6. How to Create a Java Class in Netbeans to Compute the Area of a — contextual image 6 Give your class a name. Remember that it must start with an uppercase letter.
    • ClickFinish. You are now ready to write the code.

Part 2

Programming Your Java Class

  1. How to Create a Java Class in Netbeans to Compute the Area of a — contextual image 7 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;
  2. How to Create a Java Class in Netbeans to Compute the Area of a — contextual image 8 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
  3. How to Create a Java Class in Netbeans to Compute the Area of a — contextual image 9 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.
  4. How to Create a Java Class in Netbeans to Compute the Area of a — contextual image 10 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, likegetArea. 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:Area=(s?(s?a)?(s?b)?(s?c)){displaystyle {text{Area}}={sqrt {(s*(s-a)*(s-b)*(s-c))}}}How to Create a Java Class in Netbeans to Compute the Area of a — contextual image 11wheres{displaystyle s}How to Create a Java Class in Netbeans to Compute the Area of a — contextual image 12is equal to half the perimeter, anda{displaystyle a}How to Create a Java Class in Netbeans to Compute the Area of a — contextual image 13,b{displaystyle b}How to Create a Java Class in Netbeans to Compute the Area of a — contextual image 14, andc{displaystyle c}How to Create a Java Class in Netbeans to Compute the Area of a — contextual image 15are the side lengths.
    • double area = Math.sqrt(halfPerim *(halfPerim - side1) * (halfPerim - side2) * (halfPerim - side3));
    • The last step is to create a return statement. Usereturn areaand then close your bracket.
  5. How to Create a Java Class in Netbeans to Compute the Area of a — contextual image 16 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!
  6. How to Create a Java Class in Netbeans to Compute the Area of a — contextual image 17 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 Classand name your new classHeronTester, because your files can't have the same name. This class will be saved in the same folder as your 'Heron" class.
    • ClickFinishand now you can write the tester code.
  7. How to Create a Java Class in Netbeans to Compute the Area of a — contextual image 18 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 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 abovepublic 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.
  8. How to Create a Java Class in Netbeans to Compute the Area of a — contextual image 19 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.
  9. How to Create a Java Class in Netbeans to Compute the Area of a — contextual image 20 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);
  10. How to Create a Java Class in Netbeans to Compute the Area of a — contextual image 21 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( ));
  11. How to Create a Java Class in Netbeans to Compute the Area of a — contextual image 22 Run the project!
    • Click the green arrow at the top. (Or right click anywhere in the code and hitRun). 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'.

FAQ

What is How to Create a Java Class in Netbeans to Compute the Area of a about?

It provides a structured overview of create, explains the main context, and highlights practical takeaways for readers.

Why does this topic matter?

Understanding the main concepts helps readers evaluate the issue, avoid common mistakes, and make better-informed decisions.

How should readers use this information?

Use the guidance as a practical starting point, confirm details that may have changed, and follow current product, safety, or security recommendations.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.