0

I have written a program in Java that calculates the area of a triangle using Heron's formula.

It's written using NetBeans with the sides of the triangle set in the source code:

public class TriangleArea {

    public static void main(String[] args) {
        double side1=6;
        double side2=8;
        double side3=10;

        double s = (side1 + side2 + side3) / 2;

        double area = Math.sqrt(s * (s - side1) * (s - side2 ) * (s - side3));

        System.out.println("area of the triangle is " + area);
    }
}

How can I turn this into a runnable program that asks the user for the triangle sides and can be run by double-clicking it?

Raniz
  • 10,048
  • 1
  • 28
  • 61
  • possible duplicate of [How can I convert my Java program to an .exe file?](http://stackoverflow.com/questions/147181/how-can-i-convert-my-java-program-to-an-exe-file) – fabian Aug 14 '15 at 08:51
  • P.s - once you convert it into an exe, you might want to recieve the parameters for the args[] for the side lengths :) – A. Abramov Aug 14 '15 at 08:52
  • Create `jar` file and used it any machine which have Java installed – Subodh Joshi Aug 14 '15 at 08:52
  • Also the user is not supposed to change the program after you compile it. You have to find other ways for the user to input the values, E.g. `System.in` or a GUI like swing. – fabian Aug 14 '15 at 08:52
  • for starter you probably need to design some ui, if you want interact with user, you can use for it ie swing. – user902383 Aug 14 '15 at 08:55

2 Answers2

0

To allow the user to change the parameters you can ask the user to input them using JOptionPane, you can also use this to display the message:

public class TriangleArea {

    public static void main(String[] args) {
        double side1 = getDouble("Length of the first side");
        double side2 = getDouble("Length of the second side");
        double side3 = getDouble("Length of the third side");

        double s = (side1 + side2 + side3) / 2;

        double area = Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));

        JOptionPane.showMessageDialog(null, "area of the triangle is " + area);
    }

    /**
     * Ask the user to input a double. Will continue asking until a valid double is input.
     * @param message
     * @return
     */
    public static double getDouble(String message) {
        while(true) {
            try {
                return Double.parseDouble(JOptionPane.showInputDialog(message));
            } catch (NumberFormatException e) {
                JOptionPane.showMessageDialog(null, "That is not a valid number, try again");
            }
        }
    }
}

To allow the user to run this by double-clicking it in Windows you can build a JAR file from it. I'm not familiar with Netbeans, but according to this answer on a question on how to do that you can enable it under:

Project Properties -> Build -> Packaging -> Build JAR after compiling

The JAR should show up somewher in your project when you build. Note that it will require your users to have a working installation of Java of the same version or later than you're using.

Community
  • 1
  • 1
Raniz
  • 10,048
  • 1
  • 28
  • 61
-1

Export as runnable jar.

Java programs cannot (should not) be compiled to machine code (EXE file), They can only be compiled to byte code (CLASS file) and executed using JVM.

The program cannot be modified after compilation. You should either the values from the user though stdin (or command line arguments or files)

The simplest implementation by getting the values through stdin

import java.util.Scanner;

public class TriangleArea {

    public static void main(String[] args) {
        double side1=6;
        double side2=8;
        double side3=10;

    Scanner input = new Scanner(System.in);
    System.out.println("Enter side1");
    side1 = input.nextDouble();
    System.out.println("Enter side2");
    side2 = input.nextDouble();
    System.out.println("Enter side3");
    side3 = input.nextDouble();

        double s = (side1 + side2 + side3) / 2;

        double area = Math.sqrt(s * (s - side1) * (s - side2 ) * (s - side3));

        System.out.println("area of the triangle is " + area);
    }
}
shanmuga
  • 3,667
  • 2
  • 16
  • 33
  • You mean the EULA forbids that? – dotvav Aug 14 '15 at 09:01
  • This is not an answer to the question since it doesn't show how to do it. – Raniz Aug 14 '15 at 09:01
  • @dotvav the main principle behind byte code is they can be executed on any machine with jvm (x86, x64, ARM, etc) so long as there is an JVM on the machine (machine independent) . When you compile to EXE it can only be executed on that particular machine. This defeats the purpose hence there are no official tool to do this. However there are third party tools which can do this, but use them at your own risk. – shanmuga Aug 14 '15 at 09:06