Java assignment 03: Converting dollar to euro

Java Assignments   Green = Easy,  Blue = Normal,  Red = Hard
 01   02   03   04   05   06   07   08   09   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();
  }
}

If you found this difficult, you might consider reading my book Java quizmaster for beginners. It is easy to understand, organized to learn Java in 17 days and it guides you to master Java code by solving 105 quizzes and 117 assignments. This book explains each chapter properly before starting with exercises and assignments. It is available on Amazon. See inside the book here!

Please, leave your questions, feedback and suggestions in the comments below!
SarMaroof.com offers a practical method to learn and improve your Java skills. It avoids unnecessary long boring theoretical explanations, but it uses many exercises and quizzes.




Sar Maroof is graduated from HBO Amsterdam "higher professional education". He is a professional computer programming teacher, gives master classes and publishes technical articles. His experience with developing software started from 2001 and he has worked as a software developer for several big as well as small companies and later as a freelancer. He is the author of Java quizmaster and Build a Java application in 7 days.

Books by Sar Maroof

           

 Subscribe to Sar Maroof's RSS feeds!
 Mail this page to a friend!
 Bookmark this page!

Leave a Reply

Your email address will not be published. Required fields are marked *