Skip to main content
@Joren points out that "velocity" isn't actually wrong (comment edited Sep 19, 2016 at 19:32)
Source Link
200_success
  • 144.2k
  • 22
  • 188
  • 473
import java.util.Scanner;
import javax.swing.JOptionPane;

public class SpeedCalculatorVelocityCalculator {
    public static void main(String[] args) {
        …

You're not starting any decision making here… you're just gathering input.

Technically, you're dealing with speed, not velocity. Velocity comprises a speed and a direction.

import java.util.Scanner;

public class SpeedCalculatorVelocityCalculator {
    private static int askInt(Scanner scanner, String prompt) {
        System.out.print(prompt);
        return scanner.nextInt();
    }

    private static double askDouble(Scanner scanner, String prompt) {
        System.out.print(prompt);
        return scanner.nextDouble();
    }

    public static void main(String args[]) {
        System.out.println("What are you trying to calculate?");

        Scanner input = new Scanner(System.in);
        int menuChoice = askInt(input, 
            "Press:\n" +
            "3 for speedvelocity (m/s)\n" +
            "2 for distance (m)\n" +
            "1 for time (s):\n");
        System.out.println();

        double dist, speed, time;
        switch (menuChoice) {
        case 1: // Solving for time
            speedvelocity = askDouble(input, "Please enter the speedvelocity in m/s: ");
            dist = askDouble(input, "Please enter the distance in m: ");
            time = dist / speed;velocity;
            System.out.printf("Time equals %s seconds.\n", time);
            break;
        case 2: // Solving for distance
            speedvelocity = askDouble(input, "Please enter the speedvelocity in m/s: ");
            time = askDouble(input, "Please enter the time in s: ");
            dist = speedvelocity * time;
            System.out.printf("Distance equals %s meters.\n", dist);
            break;
        case 3: // Solving for speedvelocity
            time = askDouble(input, "Please enter the time in s: ");
            dist = askDouble(input, "Please enter the distance in m: ");
            speedvelocity = dist / time;
            System.out.printf("Speed equals %s m/s.\n", speedvelocity);
            break;
        default:
            System.out.println("Please enter something I, a computer, can understand.");
        }
    }
}
import java.util.Scanner;
import javax.swing.JOptionPane;

public class SpeedCalculator {
    public static void main(String[] args) {
        …

You're not starting any decision making here… you're just gathering input.

Technically, you're dealing with speed, not velocity. Velocity comprises a speed and a direction.

import java.util.Scanner;

public class SpeedCalculator {
    private static int askInt(Scanner scanner, String prompt) {
        System.out.print(prompt);
        return scanner.nextInt();
    }

    private static double askDouble(Scanner scanner, String prompt) {
        System.out.print(prompt);
        return scanner.nextDouble();
    }

    public static void main(String args[]) {
        System.out.println("What are you trying to calculate?");

        Scanner input = new Scanner(System.in);
        int menuChoice = askInt(input, 
            "Press:\n" +
            "3 for speed (m/s)\n" +
            "2 for distance (m)\n" +
            "1 for time (s):\n");
        System.out.println();

        double dist, speed, time;
        switch (menuChoice) {
        case 1: // Solving for time
            speed = askDouble(input, "Please enter the speed in m/s: ");
            dist = askDouble(input, "Please enter the distance in m: ");
            time = dist / speed;
            System.out.printf("Time equals %s seconds.\n", time);
            break;
        case 2: // Solving for distance
            speed = askDouble(input, "Please enter the speed in m/s: ");
            time = askDouble(input, "Please enter the time in s: ");
            dist = speed * time;
            System.out.printf("Distance equals %s meters.\n", dist);
            break;
        case 3: // Solving for speed
            time = askDouble(input, "Please enter the time in s: ");
            dist = askDouble(input, "Please enter the distance in m: ");
            speed = dist / time;
            System.out.printf("Speed equals %s m/s.\n", speed);
            break;
        default:
            System.out.println("Please enter something I, a computer, can understand.");
        }
    }
}
import java.util.Scanner;
import javax.swing.JOptionPane;

public class VelocityCalculator {
    public static void main(String[] args) {
        …

You're not starting any decision making here… you're just gathering input.

import java.util.Scanner;

public class VelocityCalculator {
    private static int askInt(Scanner scanner, String prompt) {
        System.out.print(prompt);
        return scanner.nextInt();
    }

    private static double askDouble(Scanner scanner, String prompt) {
        System.out.print(prompt);
        return scanner.nextDouble();
    }

    public static void main(String args[]) {
        System.out.println("What are you trying to calculate?");

        Scanner input = new Scanner(System.in);
        int menuChoice = askInt(input, 
            "Press:\n" +
            "3 for velocity (m/s)\n" +
            "2 for distance (m)\n" +
            "1 for time (s):\n");
        System.out.println();

        double dist, speed, time;
        switch (menuChoice) {
        case 1: // Solving for time
            velocity = askDouble(input, "Please enter the velocity in m/s: ");
            dist = askDouble(input, "Please enter the distance in m: ");
            time = dist / velocity;
            System.out.printf("Time equals %s seconds.\n", time);
            break;
        case 2: // Solving for distance
            velocity = askDouble(input, "Please enter the velocity in m/s: ");
            time = askDouble(input, "Please enter the time in s: ");
            dist = velocity * time;
            System.out.printf("Distance equals %s meters.\n", dist);
            break;
        case 3: // Solving for velocity
            time = askDouble(input, "Please enter the time in s: ");
            dist = askDouble(input, "Please enter the distance in m: ");
            velocity = dist / time;
            System.out.printf("Speed equals %s m/s.\n", velocity);
            break;
        default:
            System.out.println("Please enter something I, a computer, can understand.");
        }
    }
}
Source Link
200_success
  • 144.2k
  • 22
  • 188
  • 473

import java.util.Scanner; import javax.swing.JOptionPane; //imports
    //--------------------------BEGIN PROGRAM--------------------------------------------------------//
public class justForFun 
{public static void main(String args[]){
        …

It's weird that the entire program is text-based, except that the error message is shown using Swing.

It is standard practice to put each import on its own line. Not only would it be more readable, it wold also make it easier when viewing diffs between versions of the code.

The // imports and // ---BEGIN PROGRAM--- comments merely state the obvious, and just add clutter. I recommend dropping them.

The placement of the opening brace is weird.

You could come up with a more useful name for the class, even if you are just writing this code for fun.

import java.util.Scanner;
import javax.swing.JOptionPane;

public class SpeedCalculator {
    public static void main(String[] args) {
        …

        int number;
        double velocity;
        double Distance;
        double Time;
    //--------------------------Start Decision Making-----------------------------------------------//
        Scanner ekeys = new Scanner(System.in);

Variable names should start with lowercase, by convention.

You're not starting any decision making here… you're just gathering input.

Technically, you're dealing with speed, not velocity. Velocity comprises a speed and a direction.

Whenever possible, define the variable at the same time as the declaration. Not only would the code be more compact, it would also make it clearer what the variable is for.

          Scanner input = new Scanner(System.in);
          System.out.print(…);
          int menuChoice = input.nextInt();

Note that the default case only catches cases there the user enters an integer other than 1, 2, or 3. If it's not a number at all, then the program would still crash on .nextInt().

There is no need to instantiate a new Scanner every time you ask a question. Just reuse the same one.

A common task within your program is to print a question and expect a double to be entered. You should define a function to do that.

Here is a simple revision that addresses some of the issues mentioned above.

import java.util.Scanner;

public class SpeedCalculator {
    private static int askInt(Scanner scanner, String prompt) {
        System.out.print(prompt);
        return scanner.nextInt();
    }

    private static double askDouble(Scanner scanner, String prompt) {
        System.out.print(prompt);
        return scanner.nextDouble();
    }

    public static void main(String args[]) {
        System.out.println("What are you trying to calculate?");

        Scanner input = new Scanner(System.in);
        int menuChoice = askInt(input, 
            "Press:\n" +
            "3 for speed (m/s)\n" +
            "2 for distance (m)\n" +
            "1 for time (s):\n");
        System.out.println();

        double dist, speed, time;
        switch (menuChoice) {
        case 1: // Solving for time
            speed = askDouble(input, "Please enter the speed in m/s: ");
            dist = askDouble(input, "Please enter the distance in m: ");
            time = dist / speed;
            System.out.printf("Time equals %s seconds.\n", time);
            break;
        case 2: // Solving for distance
            speed = askDouble(input, "Please enter the speed in m/s: ");
            time = askDouble(input, "Please enter the time in s: ");
            dist = speed * time;
            System.out.printf("Distance equals %s meters.\n", dist);
            break;
        case 3: // Solving for speed
            time = askDouble(input, "Please enter the time in s: ");
            dist = askDouble(input, "Please enter the distance in m: ");
            speed = dist / time;
            System.out.printf("Speed equals %s m/s.\n", speed);
            break;
        default:
            System.out.println("Please enter something I, a computer, can understand.");
        }
    }
}