Speed of sound

The problem

The following table shows the approximate speed of sound in air, water, and steel.

Medium Speed

  • Air 1,100 feet per second
  • Water 4,900 feet per second
  • Steel 16,400 feet per second

Write a program that displays a menu allowing the user to select air, water, or steel. After the user has made a selection, he or she should ask to enter the distance a sound wave will travel in the selected medium. The program will then display the amount of time it will take (round the answer to 4 decimal places). Input validation: check that the user has selected one of the available choices from the menu.Do not accept distances less than 0.

Breaking it down

Create variables

double distance = 0.0; // Distance
String medium; // To hold "air", "water", or "steel"

// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);

Create distance travelled method

/**
 * Method should return the distance traveled in seconds.
 *
 * @param medium
 * @param distance
 * @throws NullPointerException when medium is null
 *
 * @return distance traveled
 */
static double getDistanceTraveledInSeconds(String medium, double distance) {

    checkNotNull(medium);

    if (medium.equalsIgnoreCase("air")) {
        return distance / 1100.0;
    } else {
        if (medium.equalsIgnoreCase("water")) {
            return distance / 4900.0;
        } else {
            if (medium.equalsIgnoreCase("steel")) {
                return distance / 16400.0;
            }
        }
    }
    return 0.0;
}

Get input from user

// Get the user's medium of choice.
System.out.print("Enter one of the following: air, water, or steel: ");
medium = keyboard.nextLine();

// Get the distance.
System.out.print("Enter the distance the sound wave will travel: ");
distance = keyboard.nextDouble();

Calculate and display output

// calculate 
double distanceTravelled = getDistanceTraveledInSeconds(medium, distance);

// display output
System.out.println("It will take " + distanceTravelled + " seconds.");

Output

Enter one of the following: air, water, or steel: air
Enter the distance the sound wave will travel: 4500
It will take 4.090909090909091 seconds.

Unit tests

@Test(expected=NullPointerException.class)
public void get_distance_travelled_in_seconds_null_medium () {
    SpeedOfSound.getDistanceTraveledInSeconds(null, 333);
}

@Test
public void get_distance_travelled_in_seconds_air () {
    double val = SpeedOfSound.getDistanceTraveledInSeconds("air", 1100);
    assertEquals(1, val, 0);
}

@Test
public void get_distance_travelled_in_seconds_water () {
    double val = SpeedOfSound.getDistanceTraveledInSeconds("water", 4900);
    assertEquals(1, val, 0);
}

@Test
public void get_distance_travelled_in_seconds_steel () {
    double val = SpeedOfSound.getDistanceTraveledInSeconds("steel", 16400);
    assertEquals(1, val, 0);
}


@Test
public void get_distance_travelled_in_seconds_nothing () {
    double val = SpeedOfSound.getDistanceTraveledInSeconds("", 16400);
    assertEquals(0, val, 0);
}

Level Up

  • Validate the users inputs for air/water/steel
  • Create ENUM OR constants for air/water/steel, be sure to update the test!
  • Allow the user to enter have the ability to show the feet per second in time. adjust the getDistanceTraveledInSeconds -> getDistancedTraveled