The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Apr 2017 |
| Last Sign in: | 103 Weeks Ago, 3 Days Ago |
| Questions Answered: | 4870 |
| Tutorials Posted: | 4863 |
MBA IT, Mater in Science and Technology
Devry
Jul-1996 - Jul-2000
Professor
Devry University
Mar-2010 - Oct-2016
Here is the first class and it seems to be fine. I'm trying to pass the method of getSpeedInAir()/getSpeedInWater()/getSpeedInSteel to the second class. It is saying that my object is undefined and I'm not sure what to define it with to pass the method over to the second class.
Secondly, I would like to use a string instead of an Int as my medium selection. I'd like the user to use "air", "water" and "steel" instead of 1, 2 ,3.
Â
I'm using JAVA and i've attached both JAVA files, if you can't get to them let me know! :) Thanks for all of your help!
public class SpeedOfSound {// class nameprivate double distance;// instance variables -- only accessible to thisclass.public SpeedOfSound(double dis)//constructor{distance = dis;}public void setDistance(double dis) // setter methods (Mutator methods)areused to set the values of the instance variables above.{distance = dis;}public double getDistance() // getter methods (Accessor methods) are usedto return the value of the instance variables above.{return distance;}public double getSpeedInAir(){return distance/1100;}public double getSpeedInWater(){return distance/4900;}public double getSpeedInSteel(){return distance/16400;}}
import javax.swing.JOptionPane;public class SpeedOfSoundChoice {public static void main(String[] args){int choice;double distance;String input;input = JOptionPane.showInputDialog("1:Air, \n 2:Water \n 3:Steel \nEnter your medium:");choice = Integer.parseInt(input);SpeedOfSound speed = new SpeedOfSound();switch(choice){case 1:input = JOptionPane.showInputDialog("Enter in your distance for yourselected medium:");distance = Double.parseDouble(input);input = JOptionPane.showInputDialog("Your speed in air is:" +speed.getSpeedInAir());}}}