In this homework your are going to develop a simple metri/impreial system conert
ID: 638542 • Letter: I
Question
In this homework your are going to develop a simple metri/impreial system conerter to conver from centimeters to inches, from inches to centimeters, and from ounces to grams and from grams to ounces. You will have to implement some methods first and then put alltogether in a program that ask the user to input a quantity and it automatically recognizes what was entered and convert it to the other system (metric to imperial, or imperial to metric). All methods you have to implement are defined below.
Create a Eclipse project named [your pitt id]_hw2
Create a package edu.pitt.is17.[your pitt id].hw2
Create a class named UnitConverter and implement the followin static methods in it:
double in2Cm(double inches) : return the amount of centimeters corrsponding to the value of inches. Factor of conversion is 2.54 cm per inch.
double cm2In(double centimeters) : return the amount of inches corresponding to the value of centimeters. Factor of conversion is 2.54 cm per inch.
double oz2Gr(double ounces) : return the amount of grams corresponding to the value of ounces. Factor of conversion is 28.35 grams per ounce.
double gr2Oz(double grams) : return the amount of ounces corresponding to the value of grams. Factor of conversion is 28.35 grams per ounce.
String getUnit(String input) : return a 2-character string containing the unit from an input String. Assume that the input string will contain a number followed by a unit that can be one of the following: in, cm, oz, gr. For example, if input is "19.342124 cm" this method will return "cm". To make it better, you can trim the input to be able to deal with spaces at the end. For example, if input is "19.342124 cm " (note the spaces at the end) you can do input = input.trim(); and then input will have the value "19.342124 cm". TIP: you can use substring to get the last 2 characters of the string.
double getValue(String input) : this method take the input and return the number contained before the 2-character unit string. TIP: you can use substring to get the portion of input up to the last 2 characters and then convert this to a double number using Double.parseDouble() method.
Implement a main method (the definitive program) that performs the following actions:
Asks the user to input a quantity. Explicitly state that the number has to be followed by
a unit in any of the 4 units supported: in, cm, oz, gr.
Process the input using the methods getUnit and getValue explained before to get the
unit and the number.
Decide which conversion to perform given the unit extracted from the input. For
example if the unit is "cm", you will perform centimeters to inches conversion calling the method cm2In. TIP: when comparing the unit to "cm", "in", "oz" and "gr", remember that these are string and comparing string should be done using the method equals. You can also use equalsIgnoreCase method, which will make your program better.
d. Output the result of the conversion including original value and units. For example, if input is "19.342124 cm" , the system should output :
19.342124 cm = 7.61501 in Also consider as VERY important:
Implement methods and class with EXACLTY the names defined here. Remember that case matters.
Use constants when appropriate
When defining variables, constants, methods, respect name conventions
Add comments to your methods explaining succintly what they do.
Explanation / Answer
/*java program that converts the cm to inches, iches to centimeters
* and grams to ounces and ounces and grams.
* */
//package name .
package edu.pitt.is17.your_pitt_id.hw2;
//UnitConverter.java
public class UnitConverter
{
//Converstion constant measurements
private static double CM_TO_INCHES=2.54;
private static double INCHES_PER_CM=0.393;
private static double GRAMES_PER_OUNCES=28.3495;
private static double OUNCES_PER_GRAM=0.035274;
/*The method in2Cm that accepts the inches and converst the
* inches to centimetes*/
public static double in2Cm(double inches)
{
double cm;
cm=inches*CM_TO_INCHES;
return cm;
}
/*The method cm2In that accepts the centimeters and converts the
* centimeters to inches*/
public static double cm2In(double centimeters)
{
double in;
in=centimeters*INCHES_PER_CM;
return in;
}
/*The method ox2Gr that accepts the ounces and converts the
* ounces to grams*/
public static double oz2Gr(double ounces)
{
double grams;
grams=ounces*GRAMES_PER_OUNCES;
return grams;
}
/*The method gr2Oz that accepts the grams and converts the
* grams to ounces*/
public static double gr2Oz(double grams)
{
double oz;
oz=grams*OUNCES_PER_GRAM;
return oz;
}
/*The method getUnit accepst the input and and then trim
* the string and gets the last two characters of the unit.
* */
public static String getUnit(String input)
{
input=input.trim();
String unit;
unit=input.substring(input.length()-2, input.length());
return unit;
}
/*The method getValue accepts the input and and then trim
* the string and gets value using substring method upto except last two characters.
* and converts the value to double using Double.parseDouble method.
* */
public static double getValue(String input)
{
input=input.trim();
String value;
value=input.substring(0, input.length()-2);
return Double.parseDouble(value);
}
//main method that implements the methods
public static void main(String[] args)
{
String input="19.342124 cm ";
String unit=getUnit(input);
double value=getValue(input);
//checking if the unit is cm then converts the cm to inches
//by calling the static method cm2In method
if(unit.equalsIgnoreCase("cm"))
{
double centimeters=UnitConverter.cm2In(value);
System.out.println(input+" = "+centimeters+" inches");
}
input="19.342124 in ";
unit=getUnit(input);
value=getValue(input);
//checking if the unit is in then converts the in to cm
//by calling the static method in2Cm method
if(unit.equalsIgnoreCase("in"))
{
double centimeters=UnitConverter.in2Cm(value);
System.out.println(input+" = "+centimeters+" cm");
}
input="19.342124 oz ";
unit=getUnit(input);
value=getValue(input);
//checking if the unit is oz then converts the ounces to grams
//by calling the static method oz2Gr method
if(unit.equalsIgnoreCase("oz"))
{
double centimeters=UnitConverter.oz2Gr(value);
System.out.println(input+" = "+centimeters+" grams");
}
input="19.342124 gr ";
unit=getUnit(input);
value=getValue(input);
//checking if the unit is grams then converts the grams to ounces
//by calling the static method gr2Oz method
if(unit.equalsIgnoreCase("gr"))
{
double centimeters=UnitConverter.gr2Oz(value);
System.out.println(input+" = "+centimeters+" ounces");
}
}
}
Note :
//If dont need package then delete the package
//run directly ,if get any errors.
Sample output:
19.342124 cm = 7.601454732 inches
19.342124 in = 49.12899495999999 cm
19.342124 oz = 548.3395443379999 grams
19.342124 gr = 0.6822740819759999 ounces
Hope this helps you
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.