Speed with GUI

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 1257 Views - Last Post: 27 February 2012 - 10:31 AM Rate Topic: -----

#1 CodeMasterNinja  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 90
  • Joined: 22-September 11

Speed with GUI

Posted 26 February 2012 - 04:10 PM

Hello, I am trying to write a program that 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 program will then display the amount of time it will take. Check that the user had selected on of the the available choices from the menu.

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

Is This A Good Question/Topic? 0
  • +

Replies To: Speed with GUI

#2 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1152
  • View blog
  • Posts: 2,530
  • Joined: 05-May 05

Re: Speed with GUI

Posted 26 February 2012 - 04:36 PM

Use a cast.

String selection = (String)JOptionPane.showInputDialog(null,
                "Your Choice Of Medium is ?", "Medium Choices",
                JOptionPane.QUESTION_MESSAGE, null, selectionValues,
                initialSelection);


Was This Post Helpful? 0
  • +
  • -

#3 CodeMasterNinja  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 90
  • Joined: 22-September 11

Re: Speed with GUI

Posted 26 February 2012 - 04:52 PM

View Postblackcompe, on 26 February 2012 - 04:36 PM, said:

Use a cast.

String selection = (String)JOptionPane.showInputDialog(null,
                "Your Choice Of Medium is ?", "Medium Choices",
                JOptionPane.QUESTION_MESSAGE, null, selectionValues,
                initialSelection);


Well i did that now i get this error:

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 = (String)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



i am getting this error though
E:\Foundations of Java\Chapter 4 Labs\DTSpeedOfSound.java:56: error: variable choice might not have been initialized
switch (choice)
^
1 error

Tool completed with exit code 1

What should i do?
Was This Post Helpful? 0
  • +
  • -

#4 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1152
  • View blog
  • Posts: 2,530
  • Joined: 05-May 05

Re: Speed with GUI

Posted 26 February 2012 - 04:54 PM

int choice; // To hold the keyboard input



You never set it equal to anything. Either set it's value or initialize it.
Was This Post Helpful? 0
  • +
  • -

#5 CodeMasterNinja  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 90
  • Joined: 22-September 11

Re: Speed with GUI

Posted 26 February 2012 - 05:00 PM

View Postblackcompe, on 26 February 2012 - 04:54 PM, said:

int choice; // To hold the keyboard input



You never set it equal to anything. Either set it's value or initialize it.


what do mean can you show an example?
i am not getting it
Was This Post Helpful? 0
  • +
  • -

#6 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1152
  • View blog
  • Posts: 2,530
  • Joined: 05-May 05

Re: Speed with GUI

Posted 26 February 2012 - 05:37 PM

Quote

what do mean can you show an example?


Initializing a variable means giving it a value when you declare it. E.g.

int choice = 0;



That will remove the compile-time errors, although it won't solve your problem. choice is supposed to be an integer value: 1, 2, 3, or 4, based on the user-selected choice during the prompt. You have:

Object[] selectionValues = { "Air", "Water", "Steel", "Quit" };
String selection = (String)JOptionPane.showInputDialog(null,
                "Your Choice Of Medium is ?", "Medium Choices",
                JOptionPane.QUESTION_MESSAGE, null, selectionValues,
                initialSelection);



So selection will return "Air", "Water", etc. You need to map those values to integers.

if(selection.equals("Air"))choice = 1;
else if(selection.equals("Water"))choice = 2;



Note that you could have switched on selection itself.

switch(selection) {
      case "Air": JOptionPane.showMessageDialog(null, "The time in Air:\n"
                    + formatter.format(s.gettimeinAir()) + "s"
                    + " for a distance of " + dist + "ft");
                  break;
}


This post has been edited by blackcompe: 26 February 2012 - 05:38 PM

Was This Post Helpful? 0
  • +
  • -

#7 CodeMasterNinja  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 90
  • Joined: 22-September 11

Re: Speed with GUI

Posted 26 February 2012 - 05:43 PM

View Postblackcompe, on 26 February 2012 - 05:37 PM, said:

Quote

what do mean can you show an example?


Initializing a variable means giving it a value when you declare it. E.g.

int choice = 0;



That will remove the compile-time errors, although it won't solve your problem. choice is supposed to be an integer value: 1, 2, 3, or 4, based on the user-selected choice during the prompt. You have:

Object[] selectionValues = { "Air", "Water", "Steel", "Quit" };
String selection = (String)JOptionPane.showInputDialog(null,
                "Your Choice Of Medium is ?", "Medium Choices",
                JOptionPane.QUESTION_MESSAGE, null, selectionValues,
                initialSelection);



So selection will return "Air", "Water", etc. You need to map those values to integers.

if(selection.equals("Air"))choice = 1;
else if(selection.equals("Water"))choice = 2;



Note that you could have switched on selection itself.

switch(selection) {
      case "Air": JOptionPane.showMessageDialog(null, "The time in Air:\n"
                    + formatter.format(s.gettimeinAir()) + "s"
                    + " for a distance of " + dist + "ft");
                  break;
}



Thanks a lot but now if i choose quit it still asks me for the distance what should i do for that?
here's the new code

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
{//Begin class
   public static void main(String[] args)
   {//Begin main method

JFrame frame = new JFrame ("frame");
String input;                                              //To hold keyboard input
String inputString;      	                              // For reader's input
                                             // 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 = (String)JOptionPane.showInputDialog(null,
	                "Your Choice Of Medium is ?", "Medium Choices",
	                JOptionPane.QUESTION_MESSAGE, null, selectionValues,
                	initialSelection);


// 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 (selection)

{
	case "Air" :JOptionPane.showMessageDialog(null,"The time in Air:\n"+ formatter.format(s.gettimeinAir())+ " s" +
	                                         " for a distance of "+ dist + " ft");
    break;

    case "Water" :JOptionPane.showMessageDialog(null,"The time in Water: \n"+ formatter.format(s.gettimeinWater())+ " s" +
                  " for a distance of "+ dist + " ft");
    break;

    case "Steel" :JOptionPane.showMessageDialog(null,"The time in Steel: \n"+ formatter.format(s.gettimeinSteel())+ " s" +
                  " for a distance of "+ dist + " ft");
    break;

    case "Quit" :JOptionPane.showMessageDialog(null, "Have a Good Day.\n"+ " Bye");

    break;


}

   }// End the main method

}// End the class


Was This Post Helpful? 0
  • +
  • -

#8 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1152
  • View blog
  • Posts: 2,530
  • Joined: 05-May 05

Re: Speed with GUI

Posted 26 February 2012 - 05:47 PM

     inputString = JOptionPane
                .showInputDialog("What is the distance covered in feet? ");
        dist = Double.parseDouble(inputString);



You've displayed the next dialog before hitting the switch statement. That's why it asks.
Was This Post Helpful? 0
  • +
  • -

#9 CodeMasterNinja  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 90
  • Joined: 22-September 11

Re: Speed with GUI

Posted 26 February 2012 - 05:51 PM

View Postblackcompe, on 26 February 2012 - 05:47 PM, said:

     inputString = JOptionPane
                .showInputDialog("What is the distance covered in feet? ");
        dist = Double.parseDouble(inputString);



You've displayed the next dialog before hitting the switch statement. That's why it asks.

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
{//Begin class
   public static void main(String[] args)
   {//Begin main method

JFrame frame = new JFrame ("frame");
String input;                                              //To hold keyboard input
String inputString;      	                              // For reader's input
                                             // 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 = (String)JOptionPane.showInputDialog(null,
	                "Your Choice Of Medium is ?", "Medium Choices",
	                JOptionPane.QUESTION_MESSAGE, null, selectionValues,
                	initialSelection);

DTSpeed s = new DTSpeed (dist);
//Determine which choice did the user make

switch (selection)

{
	case "Air" :JOptionPane.showMessageDialog(null,"The time in Air:\n"+ formatter.format(s.gettimeinAir())+ " s" +
	                                         " for a distance of "+ dist + " ft");
    break;

    case "Water" :JOptionPane.showMessageDialog(null,"The time in Water: \n"+ formatter.format(s.gettimeinWater())+ " s" +
                  " for a distance of "+ dist + " ft");
    break;

    case "Steel" :JOptionPane.showMessageDialog(null,"The time in Steel: \n"+ formatter.format(s.gettimeinSteel())+ " s" +
                  " for a distance of "+ dist + " ft");
    break;

    case "Quit" :JOptionPane.showMessageDialog(null, "Have a Good Day.\n"+ " Bye");

    break;


}

// User Choice inputs and Dialog boxes


inputString=
			JOptionPane.showInputDialog("What is the distance covered in feet? ");
dist = Double.parseDouble (inputString) ;


   }// End the main method

}// End the class


well i did this however its still acting all weird. can you tell or atleast show an example its easier for me to understand and solve my problem

This post has been edited by CodeMasterNinja: 26 February 2012 - 05:55 PM

Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10466
  • View blog
  • Posts: 38,788
  • Joined: 27-December 08

Re: Speed with GUI

Posted 26 February 2012 - 08:27 PM

What specific problems or errors are you encountering?
Was This Post Helpful? 1
  • +
  • -

#11 CodeMasterNinja  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 90
  • Joined: 22-September 11

Re: Speed with GUI

Posted 26 February 2012 - 08:40 PM

Well when the user selects any of the options it will display a message dialog saying the time in air, water or steel is :

0.0s in 0.0ft

and then it will ask the user to input the distance covered.
Was This Post Helpful? 0
  • +
  • -

#12 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Speed with GUI

Posted 26 February 2012 - 11:12 PM

double dist = 0.0;  // To Define the variable dist
/*
 * More code inbetween
 */
DTSpeed s = new DTSpeed (dist);
//Determine which choice did the user make

switch (selection)



See the problem? You set distance to 0.0 and then never change it. So how long are you in the air at 0 distance? 0. How many times can something go into 0? 0 times. So your math in the Speed class returns 0.

Change the distance so that it isn't 0 every time. You might want to move this -
inputString=    JOptionPane.showInputDialog("What is the distance covered in feet? ");
dist = Double.parseDouble (inputString) ;


To be before the DTSPeed object creation.

This post has been edited by Fuzzyness: 26 February 2012 - 11:14 PM

Was This Post Helpful? 0
  • +
  • -

#13 CodeMasterNinja  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 90
  • Joined: 22-September 11

Re: Speed with GUI

Posted 27 February 2012 - 06:24 AM

View PostFuzzyness, on 26 February 2012 - 11:12 PM, said:

double dist = 0.0;  // To Define the variable dist
/*
 * More code inbetween
 */
DTSpeed s = new DTSpeed (dist);
//Determine which choice did the user make

switch (selection)



See the problem? You set distance to 0.0 and then never change it. So how long are you in the air at 0 distance? 0. How many times can something go into 0? 0 times. So your math in the Speed class returns 0.

Change the distance so that it isn't 0 every time. You might want to move this -
inputString=    JOptionPane.showInputDialog("What is the distance covered in feet? ");
dist = Double.parseDouble (inputString) ;


To be before the DTSPeed object creation.


Well if i do that then when i hit quit it still asks me for the distance instead of saying "Good Bye"
What should i do about that
Was This Post Helpful? 0
  • +
  • -

#14 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Speed with GUI

Posted 27 February 2012 - 06:45 AM

Then add it inside of the case. You would have that piece in each case.

Ex. -
case "Air" :
	{
		inputString=    JOptionPane.showInputDialog("What is the distance covered in feet? ");
		dist = Double.parseDouble (inputString) ;
		JOptionPane.showMessageDialog(null,"The time in Air:\n"+ formatter.format(s.gettimeinAir())+ " s" + " for a distance of "+ dist + " ft");
		break;
	}



Edit - Or you could make an if else statement after you read in the selection.

Ex. -
String selection = (String)JOptionPane.showInputDialog(null, "Your Choice Of Medium is ?", "Medium Choices", JOptionPane.QUESTION_MESSAGE, null, selectionValues, initialSelection);
if(selection.ignoreCase().equals("quit"))
    break;
else
{
	inputString = JOptionPane.showInputDialog("What is the distance covered in feet? ");
	dist = Double.parseDouble (inputString);
	DTSpeed s = new DTSpeed (dist);
	switch(selection)
	{
		case "Air" : ...
	}
}

This post has been edited by Fuzzyness: 27 February 2012 - 06:49 AM

Was This Post Helpful? 0
  • +
  • -

#15 CodeMasterNinja  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 90
  • Joined: 22-September 11

Re: Speed with GUI

Posted 27 February 2012 - 08:46 AM

View PostFuzzyness, on 27 February 2012 - 06:45 AM, said:

Then add it inside of the case. You would have that piece in each case.

Ex. -
case "Air" :
	{
		inputString=    JOptionPane.showInputDialog("What is the distance covered in feet? ");
		dist = Double.parseDouble (inputString) ;
		JOptionPane.showMessageDialog(null,"The time in Air:\n"+ formatter.format(s.gettimeinAir())+ " s" + " for a distance of "+ dist + " ft");
		break;
	}



Edit - Or you could make an if else statement after you read in the selection.

Ex. -
String selection = (String)JOptionPane.showInputDialog(null, "Your Choice Of Medium is ?", "Medium Choices", JOptionPane.QUESTION_MESSAGE, null, selectionValues, initialSelection);
if(selection.ignoreCase().equals("quit"))
    break;
else
{
	inputString = JOptionPane.showInputDialog("What is the distance covered in feet? ");
	dist = Double.parseDouble (inputString);
	DTSpeed s = new DTSpeed (dist);
	switch(selection)
	{
		case "Air" : ...
	}
}


Okay so this is my code now
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
{//Begin class
   public static void main(String[] args)
   {//Begin main method

JFrame frame = new JFrame ("frame");
String input;                                              //To hold keyboard input
String inputString;      	                              // For reader's 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 = (String)JOptionPane.showInputDialog(null,
	                "Your Choice Of Medium is ?", "Medium Choices",
	                JOptionPane.QUESTION_MESSAGE, null, selectionValues,
                	initialSelection);

//Create a new object

DTSpeed s = new DTSpeed (dist);

//Determine which choice did the user make

switch(selection)
{
	case "Air" :
				{
				 inputString=    JOptionPane.showInputDialog("What is the distance covered in feet? ");
				 		dist = Double.parseDouble (inputString) ;
				 JOptionPane.showMessageDialog(null,"The time in Air is :\n"+ formatter.format(s.gettimeinAir())+ " s" +
	                                         " for a distance of "+ dist + " ft","Time in Medium",JOptionPane.INFORMATION_MESSAGE);
    				break;
			     }

    case "Water" :
    			{
    			inputString=    JOptionPane.showInputDialog("What is the distance covered in feet? ");
					dist = Double.parseDouble (inputString) ;
    			JOptionPane.showMessageDialog(null,"The time in Water is : \n"+ formatter.format(s.gettimeinWater())+ " s" +
                  " for a distance of "+ dist + " ft","Time in Medium",JOptionPane.INFORMATION_MESSAGE);
    			break;
    			}
    case "Steel" :
    			{
				inputString=    JOptionPane.showInputDialog("What is the distance covered in feet? ");
				dist = Double.parseDouble (inputString) ;
    			JOptionPane.showMessageDialog(null,"The time in Steel is : \n"+ formatter.format(s.gettimeinSteel())+ " s" +
                  " for a distance of "+ dist + " ft","Time in Medium",JOptionPane.INFORMATION_MESSAGE);
    			break;
    			}
    case "Quit" :
    			{
    			JOptionPane.showMessageDialog(null,"Good Bye\n"+"Have a Good Day.","Bye",JOptionPane.WARNING_MESSAGE);
				break;
    			}

}

  }//End the main method
}// End the class



However i am still getting this error:
E:\Foundations of Java\Chapter 4 Labs\DTSpeedOfSound.java:46: incompatible types
found : java.lang.String
required: int
switch(selection)
^
1 error
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2