Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Implement the interface ScaleConverter, shown below, and test your implementatio

ID: 3821348 • Letter: I

Question

Implement the interface ScaleConverter, shown below, and test your implementations.

public interface ScaleConverter{
   public double convertTemperature(double tempIn);
   public double convertDistance(double distanceIn);
   public double convertWeight(double weightIn);      
}

Part A, 60 points

Implement the interface with two classes, EnglishToMetricConverter and MetricToEnglishConverter. The conversion formulas from English units to metric units are as follows:

temperature in Celsius = (temperature in Fahrenheit – 32) * 5/9
distance in KM = distance in miles * 1.609
weight in KG = weight in pounds/2.2

These formulas should be easy to reverse for metric to English conversion, but when you write the F to C conversion code recall the rules of operator precedence.

Part B, 40 points

Write JUnit tests to verify that your implementation of the EnglishToMetricConverter is correct. In a production application, of course, you would also have to test MetricToEnglishConverter, but you do not need to do so for this exam.

package fall2016cs2010final;

import static org.junit.Assert.*;

import org.junit.Test;

public class EnglishToMetricConverterTester {

   @Test
   public void testFreezingConversion() {
       EnglishToMetricConverter e = new EnglishToMetricConverter();
       final double freezingF = 32.0;
       final double freezingC = 0.0;
       assertEquals(e.convertTemperature(freezingF), freezingC, 0.0001);      
   }

   @Test
   public void testBoilingConversion() {
       EnglishToMetricConverter e = new EnglishToMetricConverter();
       final double boilingF = 212.0;
       final double boilingC = 100.0;
       assertEquals(e.convertTemperature(boilingF), boilingC, 0.0001);      
   }
  
   /* add another test to verify that your converter shows the value -40 C for the input -40 F*/

   /* add at least two tests to verify that miles to KM conversion is correct. Test several values, but one of them should
      test with the input value 1 mile. */
  
   /* add at least two tests to verify that pounds to KG conversion is correct. test several values, but one of them should    use the   input value 2.2 pounds. */
  

}

Explanation / Answer


import static org.junit.Assert.*;
import org.junit.Test;
interface ScaleConverter{
public double convertTemperature(double tempIn);
public double convertDistance(double distanceIn);
public double convertWeight(double weightIn);
}

class EnglishToMetricConverter implements ScaleConverter{

   @Override
   public double convertTemperature(double tempIn) {
      
       return (tempIn - 32.0) * 5.0/9.0;
   }

   @Override
   public double convertDistance(double distanceIn) {
      
       return distanceIn * 1.609;
   }

   @Override
   public double convertWeight(double weightIn) {
      
       return weightIn/2.2;
   }
  
}

class MetricToEnglishConverter implements ScaleConverter{

   @Override
   public double convertTemperature(double tempIn) {
      
       return ((tempIn*(9.0/5.0)) + 32.0);
   }

   @Override
   public double convertDistance(double distanceIn) {
      
       return distanceIn / 1.609;
   }

   @Override
   public double convertWeight(double weightIn) {
      
       return weightIn*2.2;
   }
  
}

class EnglishToMetricConverterTester {
@Test
public void testFreezingConversion() {
EnglishToMetricConverter e = new EnglishToMetricConverter();
final double freezingF = 32.0;
final double freezingC = 0.0;
assertEquals(e.convertTemperature(freezingF), freezingC, 0.0001);
}
@Test
public void testBoilingConversion() {
EnglishToMetricConverter e = new EnglishToMetricConverter();
final double boilingF = 212.0;
final double boilingC = 100.0;
assertEquals(e.convertTemperature(boilingF), boilingC, 0.0001);
}
  
@Test
public void testTempConversion() {
EnglishToMetricConverter e = new EnglishToMetricConverter();
final double boilingF = -40.0;
final double boilingC = -40.0;
assertEquals(e.convertTemperature(boilingF), boilingC, 0.0001);
}
  
@Test
public void testDistanceKM_MilesConversion() {
EnglishToMetricConverter e = new EnglishToMetricConverter();
final double distanceKM = 1.60;
final double distanceMiles = 1.0;
assertEquals(e.convertDistance(distanceMiles), distanceKM, 0.0001);
}
  
@Test
public void testDistanceKM_MilesConversion2() {
EnglishToMetricConverter e = new EnglishToMetricConverter();
final double distanceKM = 1.0;
final double distanceMiles = 0.62;
assertEquals(e.convertDistance(distanceMiles), distanceKM, 0.0001);
}
  
@Test
public void testDistancePonds_KGConversion() {
EnglishToMetricConverter e = new EnglishToMetricConverter();
final double wPonds = 1.0;
final double wKG = 0.45;
assertEquals(e.convertWeight(wPonds), wKG, 0.0001);
}
  
@Test
public void testDistancePonds_KGConversion2() {
EnglishToMetricConverter e = new EnglishToMetricConverter();
final double wPonds = 2.2;
final double wKG = 1.0;
assertEquals(e.convertWeight(wPonds), wKG, 0.0001);
}
}