Question: Create a class that accepts a number value and a string. Use the class to convert a given temperature from either Celsius to Fahrenheit or

Create a class that accepts a number value and a string. Use the class to convert a given temperature from either Celsius to Fahrenheit or Fahrenheit to Celsius when provided the temperature and scale. Requirements: The project must have two classes: A Test Driver and the Object class with the methods. An object must be invoked. A method must be invoked. The distance converter program demonstrates how an input can be passed to an object class and the method invoked for each object. DistanceConverter.java uses the "case/switch" statement for the scale options. ifDistance.java uses the "if/then" statement for the scale options. Either method is acceptable. In your own program, it would be best to invoke two objects as well. Remember you are testing the method for two conditions: Temps in F and temps in C. Please attempt the assignment. I will help if you get stuck. When submitting this assignment either submit the .java file or export the project from Eclipse. The .java file is preferred. ---------------------------------------------------------------------------------------------------------------------------- Ifdistance.Java

class myDistance { void convert(double distance, String scale) { double newDistance = 0; if (scale == "M") { System.out.println("Converting to KM"); newDistance = distance * 1.60934; System.out.println(distance +" in M is "+ newDistance+" in KM"); } if (scale =="K") { System.out.println("Converting to M"); newDistance = distance * 0.621371; System.out.println(distance +" in KM is "+ newDistance+" in M"); } } } class DistanceTest { public static void main (String[] args) { myDistance d1 = new myDistance(); d1.convert(10,"M"); myDistance d2 = new myDistance(); d2.convert(10,"K"); } }

----------------------------------------------------------------------------------------------------------------------------

DistanceConverter.Java class myDistance { void convert(double distance, String scale) { double newDistance = 0; switch (scale) { case "M" : //Miles to KMs; newDistance = distance * 1.60934; System.out.println(distance +" in M is "+ newDistance+" in KM"); break; case "K" : newDistance = distance * 0.621371; System.out.println(distance +" in KM is "+ newDistance+" in M"); break; } } } class DistanceTest { public static void main (String[] args) { myDistance d1 = new myDistance(); d1.convert(10,"M"); myDistance d2 = new myDistance(); d2.convert(10,"K"); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Answer Heres the TemperatureConverter class that accepts a temperature value and a scale either C fo... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!