Question

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Scanner;

public class UnitConversion {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("1. liters to gallons:");
        System.out.print("Please enter a value for liters:");
        double liters = in.nextDouble();
        System.out.printf(liters + " liters   =  %.2f gallons\n", liters*0.264172);

        System.out.println("\n2. gallons to liters:");
        System.out.print("Please enter a value for gallons:");
        double gallons = in.nextDouble();
        System.out.printf(gallons + " gallons  =  %.2f liters\n", gallons/0.264172);

        System.out.println("\n3. km to miles:");
        System.out.print("Please enter a value for kilometers:");
        double km = in.nextDouble();
        System.out.printf(km + " km  =  %.2f miles\n", km*0.621);

        System.out.println("\n4. miles to km:");
        System.out.print("Please enter a value for miles:");
        double miles = in.nextDouble();
        System.out.printf(miles + " miles   =  %.2f km\n", miles*1.609);
    }

}
Add a comment
Know the answer?
Add Answer to:
This is a basic java program where you convert units using only loops, control statements and...
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
  • 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...

  • **Please write a C++ program **I'd appreciate it if you could take a screenshot/picture and put...

    **Please write a C++ program **I'd appreciate it if you could take a screenshot/picture and put it up Thanks 2. 50 Peintsl Write a program that calculates the solution/s ofa quadratic equation. The program should prompt the user for values a, b, and c can prints the solution to the screen. Write a unit conversion program using the conversion factors of the table shown below. Your program should ask the user from which unit he/she want to convert (o al...

  • Need help creating a basic java string program using nested if/else, return loops or while loops...

    Need help creating a basic java string program using nested if/else, return loops or while loops or charAt methods while returning information using a console, Please leave notes as to compare my program and see where I went wrong or could've used a different method. secondsAfterMidnight Input: String that represents time of day Returns: integer number of seconds after midnight (return -1 if String is not valid time of day) General time of day format HH:MM:SS(AM/PM) These are examples where...

  • For this portion of the lab, you will reuse the program you wrote before That means...

    For this portion of the lab, you will reuse the program you wrote before That means you will open the lab you wrote in the previous assignment and change it. You should NOT start from scratch. Also, all the requirements/prohibitions from the previous lab MUST also be included /omitted from this lab. Redesign the solution in the following manner: 1. All the functions used are value-returning functions. 2. Put the functions in an external module and let the main program...

  • Please help me code the following in: JAVA Please create the code in as basic way...

    Please help me code the following in: JAVA Please create the code in as basic way as possible, so I can understand it better :) Full points will be awarded, thanks in advance! Program Description Write a program that demonstrates the skills we've learned throughout this quarter. This type of project offers only a few guidelines, allowing you to invest as much time and polish as you want, as long as you meet the program requirements described below. You can...

  • You previously wrote a program that calculates and displays: Miles driven Miles per gallon Kilometers driven...

    You previously wrote a program that calculates and displays: Miles driven Miles per gallon Kilometers driven Liters of fuel consumed Kilometers per liter Messages commenting on a vehicle's fuel efficiency And also validates input Now we'll make a somewhat major change. We'll remove the code for obtaining input from a user and replace it with hardcoded data stored in lists. Because the data is hardcoded, the program will no longer need the code for validating input. So, you may remove...

  • package _solution; /** This program demonstrates how numeric types and operators behave in Java Do Task...

    package _solution; /** This program demonstrates how numeric types and operators behave in Java Do Task #1 before adding Task#2 where indicated. */ public class NumericTypesOriginal { public static void main (String [] args) { //TASK #2 Create a Scanner object here //identifier declarations final int NUMBER = 2 ; // number of scores int score1 = 100; // first test score int score2 = 95; // second test score final int BOILING_IN_F = 212; // boiling temperature double fToC;...

  • In Java please Only use methods in the purpose. Thank you The purpose of this assignment is to help you learn Java iden...

    In Java please Only use methods in the purpose. Thank you The purpose of this assignment is to help you learn Java identifiers, assignments, input/output nested if and if/else statements, switch statements and non-nested loops. Purpose Question 2-String variables/Selection & loops. (8.5 points) Write a complete Java program which prompts the user for a sentence on one line where each word is separated by one space, reads the line into one String variable using nextline), converts the string into Ubbi...

  • Java Program Note: no break statements or switch staements High, Low, Sevens For this lab you...

    Java Program Note: no break statements or switch staements High, Low, Sevens For this lab you will write a Java program that plays the dice game High-Low. In this game a player places a bet on whether the sum of two dice will come up High (totaling 8 or higher), Low (totaling 6 or less) or Sevens (totaling exactly 7). If the player wins, they receive a payout based on the schedule given in the table below: Choice Payout ------...

  • Homework 3: Input Validation 1   Objectives control structures console-based user input using Scanner class writing complete...

    Homework 3: Input Validation 1   Objectives control structures console-based user input using Scanner class writing complete programs using two classes: client and supplier 2   User Interface Specification This is a console-based I/O program. Display should go to System.out (print or println) and the program will get user input using the Scanner class. The flow of execution should be as follows: When the program starts, display a one-line introduction to the user Display a menu with 5 options 1. validate zip...

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
ADVERTISEMENT