Quizzes Assignments Puzzles Easy exercises Required knowledge |
Java Assignments Green = Easy, Blue = Normal, Red = Hard Select 01 02 03 04 05 06 07 08 09 10 By title |
Today, I am gonna teach you to write a simple useful Java program.
This program converts any amounts of dollars to euros, when 1 euro = 1.18 dollar.
If you run the program, a text appears in the standard output
that asks users to enter an amount in dollars.
When the user enters the amount and press the enter button, the program converts the dollars to euros.
Assignment: write the body of the method convert.
Java assignment 3: Converting dollars to euros
Level: Easy
public class Currency { public static double convert(double amount) { // write the body of this method. return amount; } public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Enter amount $: "); double amount = input.nextDouble(); System.out.println("Euro: "); System.out.print(convert(amount)); input.close(); } } /* Example: output of the amount $150 ---------------------------------- Enter an amount in dollars: 150 In euro: 127.11 */
Author: Sar Maroof
Answer explanation
The method must calculate the amount of euro and returns the result.
import java.util.Scanner; public class Currency { public static double convert(double amount) { double euro = amount / 1.18; return euro; } public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Enter amount $: "); double amount = input.nextDouble(); System.out.println("In Euro: "); System.out.print(convert(amount)); input.close(); } }
Suggested Articles
![]() ![]() ![]() |