x.Hmritten these three classes forcalculating the Speed Of Sound. I would like t
ID: 3617557 • Letter: X
Question
x.Hmritten these three classes forcalculating the Speed Of Sound. I would like to know how to stop the menu from repeatedly showing up. In my DialogInput Class (getMenuItem method) I tried to replace "continue;" with "break;" but it won't let me compile. Any input would be great. Thank you!Hereare the class and two objects:
public class SpeedOfSound //class to calculate speed of sound(fps) in a medium
{
private double distance;
private String medium;
private double time;
public SpeedOfSound() //no arg constructor
{
distance = 0;
}
public void setDistance(double distance)
{
this.distance = distance;
}
public double getSpeed(String medium)
{
if (medium.startsWith("A")){
return distance/1100; //returns time sound travels in Air
} // Class method?
else if (medium.startsWith("W")){
return distance/4900; //returns time sound travels in Water
}
else {
return distance/16400; } //returns time sound travels in Steel
}
public String toString()
{
return "Distance =" + distance;
}
}
import javax.swing.*;
public class DialogInput
{
public String getMenuItem(String title, String...items)
{
String menu = "";
for(int i = 0; i<items.length; i++){
menu += items[i] + " ";
}
menu += "Enter Selection";
while(true)
{
String medium = JOptionPane.showInputDialog(menu);
if(medium==null||medium.equals("")){
continue;
}
for(int k=0; k<items.length;k++)
{
if(medium.equalsIgnoreCase(items[k]))
return medium;
}
}
}
public double getDouble(String prompt)
{
while(true)
{
try
{
String input = JOptionPane.showInputDialog(prompt);
if(input==null||input.equals("")){
return Double.MIN_VALUE;
}
return Double.parseDouble(input);
}
catch(NumberFormatException e)
{
}
}
}
}
import javax.swing.*;
import java.text.DecimalFormat;
public class SpeedOfSoundApp
{
public static void main(String [] args)
{
DecimalFormat d = new DecimalFormat("###,###,###,##0.00");
DecimalFormat t = new DecimalFormat("###,###,##0.0000000");
DialogInput input = new DialogInput();
String medium = input.getMenuItem("Medium Menu", "Air", "Water",
"Steel");
double distance = input.getDouble("Enter distance (ex. 5280): ");
SpeedOfSound sos = new SpeedOfSound();
sos.setDistance(distance);
double time = sos.getSpeed(medium);
JOptionPane.showMessageDialog(null,
"In " + medium + ", sound travels " +
&nb
Explanation / Answer
I figured it out.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.