TheSpeedOfSound.java - import javax.swing.JOptionPane import [email protected] Lemuel M [email protected] Book Title From Control Structures through Objects by

TheSpeedOfSound.java - import javax.swing.JOptionPane...

This preview shows page 1 - 2 out of 2 pages.

import javax.swing.JOptionPane; import java.text.*; /** * @author Lemuel M. Uhuru * @Date June 15, 2013 * Book Title: From Control Structures through Objects by Tony Gaddis * Page: 190 Problem 12: The Speed of Sound * @Description Write a program that asks the user to enter * "air", "water", or "steel", and the distance that a sound * wave will travel in the medium. The program should then display * the amount of time it will take. */ public class TheSpeedOfSound { public static void main(String[] args) { //Declare constants that approximate the //speed of sound in Air, Steel, and Water int AIR = 1100; int WATER = 4900; int STEEL = 16400; //Declare variable to hold medium type and distance value. String medium, distance; //Prompt user for medium medium = JOptionPane.showInputDialog("Enter the medium (water/steel/air) ").toLowerCase(); //Prompt user for distance distance = JOptionPane.showInputDialog("Enter distance in feet"); //Convert distance to data-type double double D = Double.parseDouble(distance); // Calculate time for sound to travel through medium double timeA = D / AIR;
Image of page 1
Image of page 2

You've reached the end of your free preview.

Want to read both pages?

Unformatted text preview: double timeW = D / WATER; double timeS = D / STEEL; // Decimal formating for Time variables NumberFormat df = DecimalFormat.getInstance(); df.setMaximumFractionDigits(2); //Design if-else control structure to display the amount //of time it will take sound to travel through the medium given //the users input. // *Don't understand why the '==' or equal to operator cannot be used // as a substitute for the equals method in the following conditions. if (medium.equals("water")) JOptionPane.showMessageDialog(null, "Medium: " + medium + "\nDistance: " + D + " ft" + "\nTime: " + df.format(timeW) + " seconds"); else if (medium.equals("air")) JOptionPane.showMessageDialog(null, "Medium: " + medium + "\nDistance: " + D + " ft" + "\nTime: " + df.format(timeA) + " seconds"); else if (medium.equals("steel")) JOptionPane.showMessageDialog(null, "Medium: " + medium + "\nDistance: " + D + " ft" + "\nTime: " + df.format(timeS) + " seconds"); else JOptionPane.showMessageDialog(null, "Invalid input"); } }...
View Full Document

  • Spring '08
  • Garcia,D
  • Java Programming, Java Code, International System of Units, Control flow, Equals sign, @author, Lemuel M. Uhuru

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors