Question

Write a program that contains functions YardtoKilo( ) to convert Yard to Kilometers and kilotoYard() Kilometers...

Write a program that contains functions YardtoKilo( ) to convert Yard to Kilometers and kilotoYard() Kilometers to Yard
1 Yard = 0.9114 Kilometer
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Actually, a small correction needed 1 Yard=0.0009144 Kms and 1 Yard= 0.9144 metres

1. Accepting a user input whether YardtoKilo( ) function called or kilotoYard() called with a choice of 1 and 2 respectively otherwise error message is displayed.

2. Accepting a double value from the user for conversion as ip1 and ip2

3. calling the respective function and returning the result.

4. Displaying back to the console

We use this  BigDecimal.valueOf(a).toPlainString() for precision of resultant values.

Program:

import java.util.*;
import java.math.BigDecimal;
public class Main
{
public static double YardtoKilo(double yards)
{
       return (yards*0.0009114);
   }
   public static double KilotoYard(double kms)
{
       return (kms/0.9114);
   }
   public static void main(String[] args)
   {
      
       Scanner src=new Scanner(System.in);
       System.out.println("Please enter your choice");
       System.out.println("1.YardtoKilo\t2.KilotoYard");
       int choice=src.nextInt();
       if(choice==1)
       {
       System.out.println("Please enter the Yards");
       double ip1=src.nextDouble();
       double a=YardtoKilo(ip1);
System.out.println(BigDecimal.valueOf(a).toPlainString()+"kms");
       }
       else if(choice==2)
       {
       System.out.println("Please enter the Kilometres");
       double ip2=src.nextDouble();
       double a=KilotoYard(ip2);
System.out.println(BigDecimal.valueOf(a).toPlainString()+"yards");
       }
       else
       {
       System.out.println("Wrong choice");
       }
   }
}

Output:

Please enter your choice 1. YardtoKilo 2.KilotoYard Please enter the Yards 0.0009114kms ... Program finished with exit code o

Please enter your choice 1.YardtoKilo 2.KilotoYard Please enter the Kilometres 1.097213078779899yards ... Program finished wi

Hope this helps!!!

Add a comment
Know the answer?
Add Answer to:
Write a program that contains functions YardtoKilo( ) to convert Yard to Kilometers and kilotoYard() Kilometers...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that will convert miles to kilometers and kilometers to miles. The user will...

    Write a program that will convert miles to kilometers and kilometers to miles. The user will indicate both a number (representing a distance) and a choice of whether that number is in miles to be converted to kilometers or kilometers to be converted to miles Each conversion is done with a value returning function You may use the following conversions. 1 kilometer = .621 miles 1 mile = 1.61 kilometers Sample Run: Please input Convert miles to kilometers Convert kilometers...

  • k2m.cpp Write a program which converts kilometers to miles and miles to kilometers. Input value (m...

    k2m.cpp Write a program which converts kilometers to miles and miles to kilometers. Input value (m or km)is in argv[1] Program is accessed by "k2m" or "m2k", using Unix' ability to assign SYMBOLIC LINKS to files, so they have multiple names. The program knows to do kilometers -> or miles -> km by examining its name in argv[0]. (It does a string comparison on argv[0]). Miles to kilometers: m2k Kilometers to miles: k2m If you're developing on a Windows machine...

  • Write a C++ program that will convert distance measurements from miles to kilometers. Specifically, the program...

    Write a C++ program that will convert distance measurements from miles to kilometers. Specifically, the program needs to convert 0, 10, 20, 30, 40 , 50, 60, 70, 80, and 90 miles to kilograms displaying both the number of miles and the number of kilograms on the same line. please comment on what is pre and post condition too

  • MATLAB - create MATLAB functions to convert kilometers to miles and vice versa according to the...

    MATLAB - create MATLAB functions to convert kilometers to miles and vice versa according to the following conditions. (a) The functions created will have input of miles or kilometers and the output with converted kilometers or miles. (b) The output of the function should be a conversion table. No input for the function is needed. (1 mile = 1 609.344 meter) - function with the following: (1) output a third-order polynomial function with the coefficients as the input variables; (2)...

  • POWERSHELL: Write Multiple Functions in a script call ConversionFunctions.ps1 The first function ...

    POWERSHELL: Write Multiple Functions in a script call ConversionFunctions.ps1 The first function script ConvertToFeet($meters) is given as an example. Please complete ConvertToFahrenheit, ConvertTocelsius, ConvertToMiles, ConvertToKilometers ConversionFunctions.ps1 Function Script:ConvertToFeet($meters) { # Convert meters into to Feet # 1 meter = (1 * 3.28) feet "$meters meters equals $($meters * 3.28) feet“ } #end ConvertToFeet Function Script:ConvertToFahrenheit($celsius) { # Convert celsius into Fahrenheit # 1 celsius = ((1.8 * 1) + 32) fahrenheit "write your answer here" } #end ConvertToFahrenheit Function Script:ConvertTocelsius($fahrenheit)...

  • java programming Problem 2) Convert Kilometers to Miles Write a JavaFX program using JavaFX Menu for...

    java programming Problem 2) Convert Kilometers to Miles Write a JavaFX program using JavaFX Menu for to solve the following problem about converting between Kilometers to Miles. Sample Run is shown below You are asked to implement only the menu item under Formulas which reads as"KM to Mile". Choosing this menu item will display the following input box: Input Enter Kilometers Value OK Cancel The result will be displayed in a TextArea as shown below: JavaF× Menu Example " File...

  • This is a basic java program where you convert units using only loops, control statements and...

    This is a basic java program where you convert units using only loops, control statements and format modifiers: Create a Java program named UnitConversion.java that converts the following: 1. Liter to gallon 2. Gallon to liter 3. Kilometers to miles 4. Miles to kilometers The conversion formulas: 1 liter = 0.264172 gallon 1 gallon = 3.78541 liters 1 kilometer = 0.621 miles I mile = 1.609 kilometers

  • You want to write a converter program which will let user convert distance and weight from SI to ...

    Use functions to complete this C + + program You want to write a converter program which will let user convert distance and weight from SI to the USCS system and vice versa The formulae are as follows: 1. Distance in miles- distance in kilometers x 1.60 2. Weight in kilograms weight in pounds x 2.20 The program would let user input the number that they want to convert and then choose the units to and from which they want...

  • Write a C++ program that shows the following menu options and lets the user to convert...

    Write a C++ program that shows the following menu options and lets the user to convert from Metric to Imperial system: Converter Toolkit --------------------     1. Temperature Converter     2. Distance Converter     3. Weight Converter     4. Quit If the user enters 1, the program should ask for the temperature in Celsius and convert it to Fahrenheit If the user enters 2, the program should ask for the distance in Kilometer and convert it to Mile If the user...

  • Using Atmel Studios Write an AVR program that convert a distance reading in Miles (variable Mi)...

    Using Atmel Studios Write an AVR program that convert a distance reading in Miles (variable Mi) to Kilometer (variable Km). Include .INCLUDE <m328pdef.inc>

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT