Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program as instructed in your textbook on page 224, #20. Display a menu

ID: 3796912 • Letter: W

Question


Write a program as instructed in your textbook on page 224, #20. Display a menu along with meaning u instructions. Ask the user for the distance in feet (Accept floating point values for feet; don't restrict the distance to integers only.) Your output should be stated precisely and in a complete sentence Give your distance with one place after the decimal, and your answer (in seconds) with 4 places after the decimal. For example: Through water, sound waves travel 1000.0 feet in .2041 seconds. Be sure to validate the input as instructed in the problem. Correct answer examples: Turn in output for all media at 5.7 feet and at 32000 feet. (You can run your program 6 times to gather this output or you can use a loop, looking ahead to chapter 5.)

Explanation / Answer

import java.text.DecimalFormat;
import java.util.Scanner;

public class SoundWaves {

   public static void main(String[] args) {
      
       Scanner input = new Scanner(System.in);

       System.out.println("Enter the distance in feet: ");
      
       float totalDistance = (float)input.nextFloat();
      
       // Air
       float airSeconds = (float) ((0.9091/1000)*totalDistance);
      
       // Water
       float waterSeconds = (float) ((0.2041/1000)*totalDistance);
      
       // Steel
       float steelSeconds = (float) ((0.0610/1000)*totalDistance);
      
       DecimalFormat df = new DecimalFormat();
       df.setMaximumFractionDigits(1);
      
       DecimalFormat df1 = new DecimalFormat();
       df1.setMaximumFractionDigits(4);
      
       System.out.println("Through water, sound waves travel "+df.format(totalDistance)+" feet in "+df1.format(waterSeconds)+" seconds.");
       System.out.println("Through air, sound waves travel "+df.format(totalDistance)+" feet in "+df1.format(airSeconds)+" seconds.");
       System.out.println("Through steel, sound waves travel "+df.format(totalDistance)+" feet in "+df1.format(steelSeconds)+" seconds.");

   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote