HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaModerate

Velocity, Distance and Time Calculator

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
velocitydistancetimeandcalculator

Problem

This is a velocity, distance, and time calculator that I wrote not too long ago. How can I improve upon this code?

```
import java.util.Scanner; import javax.swing.JOptionPane; //imports
//--------------------------BEGIN PROGRAM--------------------------------------------------------//
public class justForFun
{public static void main(String args[]){
int number;
double velocity;
double Distance;
double Time;
//--------------------------Start Decision Making-----------------------------------------------//
Scanner ekeys = new Scanner(System.in);
System.out.print("What are you trying to calculate?\npress:\n3 "
+ "for Velocity(m/s):\n2 for Distance(m):\n1 for Time(sec):\n");
number = ekeys.nextInt();
//--------------------------Begin SWITCH--------------------------------------------------------//
switch(number){
//--------------------------Solving For TIME----------------------------------------------------//
case 1:
//makes a new scanner for(users input) called 'ekey' to hold Users VELOCITY input.
Scanner ekey = new Scanner(System.in);
System.out.print("\n\nPlease enter a velcoity in m/s:\n");
velocity = ekey.nextDouble();
//end of 'ekey' for scanner, change scanner variable to 'ekeyp' to hold Users DISTANCE Input.

Scanner ekeyp = new Scanner(System.in);
System.out.print("Please enter the distance in Meters:\n");
Distance = ekeyp.nextDouble();
//end of 'ekeyp' scanner. next solving for TIME.
//calculates for time, and prints the time in seconds.
Time = Distance/velocity;
System.out.print("Time equals: " + Time + " Seconds");

Solution

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 VelocityCalculator {
    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.

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 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.");
        }
    }
}

Code Snippets

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

public class VelocityCalculator {
    public static void main(String[] args) {
        …
int number;
        double velocity;
        double Distance;
        double Time;
    //--------------------------Start Decision Making-----------------------------------------------//
        Scanner ekeys = new Scanner(System.in);
Scanner input = new Scanner(System.in);
          System.out.print(…);
          int menuChoice = input.nextInt();
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.");
        }
    }
}

Context

StackExchange Code Review Q#141719, answer score: 11

Revisions (0)

No revisions yet.