Design a class that stores in a distance field, in feet, traveled by a sound wave. The class should have the appropriate accessor and mutator methods for this field. In addition, 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 steel is:
Time= distance/16400
this is the code i wrote for the program
/**
Lab 4
* This is Lab Assignment 4 located on page 258 problem number 9.
* The following program will ask the user to enter "air", "water", or "steel",
* And the distance that a sound wave will in that perticular medium.
* After that the program will display the amount of time it will take.
* This program is designed by "Divy Tolia"
*/
public class DTSpeed
{// Begin class
private double distance; //the distance the sound traveled
//Constructor
public DTSpeed (double dist)
{
distance=dist;
}
/**
set method to appropriate name
*/
public void setDistance(double dist)
{
distance = dist;
}
/**
get method and return value of distance
*/
public double getDistance()
{
return distance;
}
/**
get time methods
*/
public double gettimeinAir()
{
return distance/1100;
}
public double gettimeinWater()
{
return distance/4900;
}
public double gettimeinSteel()
{
return distance/16400;
}
} // End class
Here is the program with GUI:
import java.util.Scanner; //needed for the scannner class
import javax.swing.JOptionPane;// For GUI
import java.text.DecimalFormat; //needed to format the Output
import javax.swing.JFrame;//needed for the GUI
import javax.swing.JDialog;//Needed for the GUI
import java.awt.*;//GUI
import java.awt.event.*;//GUI
import javax.swing.*;//GUI
/**
*This is a SpeedDemo Class
*It displays the speed of sound in different mediums
*/
public class DTSpeedOfSound
{
public static void main(String[] args)
{
JFrame frame = new JFrame ("frame");
String input; //To hold keyboard input
String inputString; // For reader's input
int choice; // To hold the keyboard input
double dist = 0.0; // To Define the variable dist
DecimalFormat formatter = new DecimalFormat("##0.00"); //To round the speed two digits after the decimal
Scanner keyboard = new Scanner(System.in); //Create a scanner object to read input
String[] choices = {"1.Air","2.Water","3.Steel","4.Quit"};
//Display key for choices
JDialog.setDefaultLookAndFeelDecorated(true);
Object[] selectionValues = {"Air", "Water", "Steel","Quit"};
String initialSelection = "Air";
String selection = JOptionPane.showInputDialog (null, "Your Choice Of Medium is ?",
"Medium Choices", JOptionPane.QUESTION_MESSAGE, null, selectionValues, initialSelection);
System.out.println(selection);
// User Choice inputs and Dialog boxes
inputString= JOptionPane.showInputDialog("What is the distance covered in feet? ");
dist = Double.parseDouble (inputString) ;
DTSpeed s = new DTSpeed (dist);
//Determine which choice did the user make
switch (choice)
{
case 1 :JOptionPane.showMessageDialog(null,"The time in Air:\n"+ formatter.format(s.gettimeinAir())+"s" +
" for a distance of "+ dist +"ft");
break;
case 2 :JOptionPane.showMessageDialog(null,"The time in Water: \n"+ formatter.format(s.gettimeinWater())+"s" +
" for a distance of "+ dist +"ft");
break;
case 3 :JOptionPane.showMessageDialog(null,"The time in Steel: \n"+ formatter.format(s.gettimeinSteel())+"s" +
" for a distance of "+ dist +"ft");
break;
case 4 :JOptionPane.showMessageDialog(null, "Have a Good Day.\n"+"Bye");
break;
}
}// End the main method
}// End the class
Now how to i convert the object into a string for JOptionPane to work?
Please help me

New Topic/Question
Reply



MultiQuote







|