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

• Lab Report
• 2

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;

#### You've reached the end of your free preview.

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

• 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.

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

• 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.

Dana University of Pennsylvania ‘17, Course Hero Intern

• 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.

Jill Tulane University ‘16, Course Hero Intern