
Patrick B. answered 09/14/19
Math and computer tutor/teacher
//* MetersToEnglish.java
class MetersToEnglish
{
public static final double CENTIMETERS_PER_INCH = 2.54;
public static final int INCHES_PER_FOOT = 12;
public static final int FEET_PER_YARD = 3;
public static final int YARDS_PER_MILE = 1760;
public static final int ROUND_TO_INCH = 16;
private InputOutputIO myIO;
MetersToEnglish()
{
myIO = new InputOutputIO();
}
public void Go()
{
long feet,yards,miles;
int inchFrac16;
double meters = myIO.promptForAmount(" Please input the # of meters :>");
feet=yards=miles=inchFrac16=0;
if (meters != 0)
{
double cm = meters*100;
double inches = cm / CENTIMETERS_PER_INCH;
System.out.println(" cenimeters = " + cm + " : inches = "+ inches);
long intInches = (long) (Math.floor(inches));
double fraction_of_inch = inches - intInches;
System.out.println(" intInches = " + intInches + " : fraction_of_inch = "+ fraction_of_inch);
for (int iLoop=0; iLoop<2; iLoop++)
{
feet = intInches/INCHES_PER_FOOT;
intInches = intInches % INCHES_PER_FOOT;
System.out.println(" feet = " + feet + " : inches = " + intInches);
yards = feet / FEET_PER_YARD;
feet = feet % FEET_PER_YARD;
System.out.println(" yards = " + yards + " : feet = " + feet);
miles = yards / YARDS_PER_MILE;
yards = yards % YARDS_PER_MILE;
System.out.println(" miles = " + miles + " : yards = " + yards);
inchFrac16 = InchFrac16(fraction_of_inch);
System.out.println( inchFrac16);
System.out.println(" rounds to full inch " + InchFrac16(0.9876));
if (inchFrac16 != ROUND_TO_INCH)
{
break; // does not have to round to next integer, so we're done
}
else
{
intInches++;
fraction_of_inch = 0;
}
} //for loop
System.out.println(" miles : "+ miles);
System.out.println(" yards : "+ yards);
System.out.println(" feet : " + feet );
System.out.println(" inches : " + intInches + " & " + inchFrac16 + "\\16");
}
}
public int InchFrac16( double fracInch )
{
double [] fractionsOfInch = { 0.0625, 0.125, 0.1875, 0.25,0.3125,0.375,0.4375, 0.5, 0.5625, 0.625, 0.6875, 0.75,0.8125,0.875,0.9375 };
int iLoop=-1;
int iReturn = 0;
if (fracInch != 0)
{
boolean found_flag = false;
for (iLoop=0; iLoop<15; iLoop++)
{
if (fractionsOfInch[iLoop]>fracInch)
{
found_flag = true;
break;
}
}
iReturn = iLoop+1;
}
return (iReturn);
}
public static void main (String args[])
{
MetersToEnglish myConverter = new MetersToEnglish();
myConverter.Go();
}
}
//* InputOutputIO.java
import java.io.*;
public class InputOutputIO
{
private Console console;
private boolean input_error_flag;
InputOutputIO()
{
console = System.console();
input_error_flag = false;
}
public String promptForString( String promptMsg)
{
String inbuff;
input_error_flag = false;
System.out.println("**************************************");
System.out.print(promptMsg);
try
{
inbuff = console.readLine();
}
catch (Exception ex)
{
inbuff = null;
input_error_flag = true;
System.out.println(" Input exception error has occured ");
}
return(inbuff);
}
public long promptForInteger( String promptMsg)
{
input_error_flag = false;
String inbuff = this.promptForString(promptMsg);
if (inbuff != null)
{
return(Long.parseLong(inbuff));
}
else
{
return(0);
}
}
public double promptForAmount( String promptMsg)
{
input_error_flag = false;
String inbuff = this.promptForString(promptMsg);
if (inbuff != null)
{
return(Double.parseDouble(inbuff));
}
else
{
return(0);
}
}
public static void main( String args[])
{
InputOutputIO myIO = new InputOutputIO();
String nameStr = myIO.promptForString(" What is your name ??? :>");
long longIntNum = myIO.promptForInteger(" Please input an integer :> ");
double dblFlAmt = myIO.promptForAmount(" Please input an amount :> ");
System.out.println(" Hello " + nameStr);
System.out.println(" long int # = " + longIntNum);
System.out.println(" amount = "+ dblFlAmt );
}
}
Rylen B.
that must take a long time to do:()09/18/20