rogram requires 2 java files, the testing program should use a switch

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

program requires 2 java files, the testing program should use a switch

9. The Speed of Sound
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
Design a class that stores in a distance field the distance, in feet, traveled by a sound wave.
The class should have the appropriate accessor and mutator methods for this field. In addi-
tion, the class should have the following methods:
• getSpeedInAir. This method should return the number of seconds it would take a
sound wave to travel, in air, the distance stored in the distance field. The formula
to calculate the amount of time it will take the sound wave to travel the specified
distance in air is:
Time = distance/1100
• getSpeedInWater. This method should return the number of seconds it would take a
sound wave to travel, in water, the distance stored in the distance field. The formula
to calculate the amount of time it will take the sound wave to travel the specified
distance in water is:
Time = distance/4900
• getSpeedInSteel. This method should return the number of seconds it would take a
sound wave to travel, in steel, the distance stored in the distance field. The formula
to calculate the amount of time it will take the sound wave to travel the specified
distance in air is:
Time = distance/16400
Write a program to demonstrate the class. The program should display a menu allowing the
user to select air, water, or steel. Once the user has made a selection, he or she should be
asked to enter the distance a sound wave will travel in the selected medium. The
will then display the amount of time it will take. Check that the user has selected one of
the available choices from the menu.
program
ling Roints
Transcribed Image Text:9. The Speed of Sound 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 Design a class that stores in a distance field the distance, in feet, traveled by a sound wave. The class should have the appropriate accessor and mutator methods for this field. In addi- tion, the class should have the following methods: • getSpeedInAir. This method should return the number of seconds it would take a sound wave to travel, in air, the distance stored in the distance field. The formula to calculate the amount of time it will take the sound wave to travel the specified distance in air is: Time = distance/1100 • getSpeedInWater. This method should return the number of seconds it would take a sound wave to travel, in water, the distance stored in the distance field. The formula to calculate the amount of time it will take the sound wave to travel the specified distance in water is: Time = distance/4900 • getSpeedInSteel. This method should return the number of seconds it would take a sound wave to travel, in steel, the distance stored in the distance field. The formula to calculate the amount of time it will take the sound wave to travel the specified distance in air is: Time = distance/16400 Write a program to demonstrate the class. The program should display a menu allowing the user to select air, water, or steel. Once the user has made a selection, he or she should be asked to enter the distance a sound wave will travel in the selected medium. The will then display the amount of time it will take. Check that the user has selected one of the available choices from the menu. program ling Roints
Expert Solution
switch

The switch statement allows us to execute a block of code among many alternatives.

switch (expression) {
  case value1:
    // code to be executed if
    // expression is equal to value1
    break;
  
  case value2:
    // code to be executed if
    // expression is equal to value2
    break;
  
  ...
  ...
  
  default:
    // default statements
  }

The expression is evaluated once and compared with the values of each case label.

  • If there is a match, the corresponding code after the matching case label is executed.

    For example, if the value of the expression is equal to value2, the code after case value2: is executed.
  • If there is no match, the code after default: is executed.

Note: We can do the same functionality using the Java if...else...if ladder. However, the syntax of the switch statement is cleaner and much easier to read and write.

 
steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Knowledge Booster
Function Arguments
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education