Question

In: Computer Science

JAVA PROGRAMMING Objectives JAVA Practice incremental development. Practice getting input from and printing output for the...

JAVA PROGRAMMING

Objectives JAVA

  1. Practice incremental development.
  2. Practice getting input from and printing output for the user.
  3. Write a program with conditional statements.
  4. Write a program with loops.

Specifications

Think of three countries you’d like to travel to and look up the conversion rate between dollars and the money used in those countries. (See http://www.ratesfx.com/ rates/rate-converter.html for conversion rates.) Write a program that will convert money from dollars into each of those three types of currency, or from one of the foreign currencies to US dollars. The program should display a menu that asks the user to choose a currency. The final selection on the menu should let the user quit the program.

When the user selects a currency, the program should ask whether the conversion is to or from dollars, request an amount, and display the result. The program should then display the menu again. This process should be repeated until the user chooses to exit the program.

Your job is to write a program with three classes:

  1. The MoneyChangerController: gets everything started, as usual
  2. The MoneyChangerView:
    1. Displays the menu
    2. gets a choice from the user and passes it on to the MoneyChanger
    3. gets the answer from the MoneyChanger and displays it for the user
    4. repeats until the user decides to quit
  3. The MoneyChanger: actually does the conversions

Keep it simple! Make sure you change your program a VERY small amount at each step, so that it always compiles.

Submit the following:

  • A copy of your source code (for all classes)
  • 3 screen shots for test runs of your program.

Solutions

Expert Solution

MoneyChanger.java

public class MoneyChanger {
private double amount;
  
public MoneyChanger()
{
this.amount = 0.0;
}
  
public MoneyChanger(double amount)
{
this.amount = amount;
}

public double getAmount() {
return amount;
}

public void setAmount(double amount) {
this.amount = amount;
}
  
public double dollarToYen()
{
return(this.amount * 108.24);
}
  
public double yenToDollar()
{
return(this.amount / 108.24);
}
  
public double dollarToEuro()
{
return(this.amount * 0.90);
}
  
public double euroToDollar()
{
return(this.amount / 0.90);
}
  
public double dollarToAustralianDollar()
{
return(this.amount * 1.45);
}
  
public double australianDollarToDollar()
{
return(this.amount / 1.45);
}
}

MoneyChangerView.java

import java.util.Scanner;

public class MoneyChangerView {
  
public MoneyChangerView()
{
Scanner sc = new Scanner(System.in);
String toFrom;
double amount;
int choice;
  
do
{
displayMenu();
choice = Integer.parseInt(sc.nextLine().trim());
switch(choice)
{
case 1:
{
System.out.print("\nConvert to Yen or from Yen? (enter to/from): ");
toFrom = sc.nextLine().trim();
while(!toFrom.equalsIgnoreCase("to") && !toFrom.equalsIgnoreCase("from"))
{
System.out.println("Invalid input for to/from clause!");
System.out.print("Convert to Yen or from Yen? (enter to/from): ");
toFrom = sc.nextLine().trim();
}
  
System.out.print("Enter the amount to convert: ");
amount = Double.parseDouble(sc.nextLine().trim());
MoneyChanger changer = new MoneyChanger(amount);
  
if(toFrom.equalsIgnoreCase("to"))
{
double dollarToYen = changer.dollarToYen();
System.out.println(amount + " Dollars = " + String.format("%.2f", dollarToYen) + " Yen\n");
}
else
{
double yenToDollar = changer.yenToDollar();
System.out.println(amount + " Yen = " + String.format("%.2f", yenToDollar) + " Dollars\n");
}
break;
}
  
case 2:
{
System.out.print("\nConvert to Euro or from Euro? (enter to/from): ");
toFrom = sc.nextLine().trim();
while(!toFrom.equalsIgnoreCase("to") && !toFrom.equalsIgnoreCase("from"))
{
System.out.println("Invalid input for to/from clause!");
System.out.print("Convert to Euro or from Euro? (enter to/from): ");
toFrom = sc.nextLine().trim();
}
  
System.out.print("Enter the amount to convert: ");
amount = Double.parseDouble(sc.nextLine().trim());
MoneyChanger changer = new MoneyChanger(amount);
  
if(toFrom.equalsIgnoreCase("to"))
{
double dollarToEuro = changer.dollarToEuro();
System.out.println(amount + " Dollars = " + String.format("%.2f", dollarToEuro) + " Euro\n");
}
else
{
double euroToDollar = changer.euroToDollar();
System.out.println(amount + " Euro = " + String.format("%.2f", euroToDollar) + " Dollars\n");
}
break;
}
  
case 3:
{
System.out.print("\nConvert to Australian Dollar or from Australian Dollar? (enter to/from): ");
toFrom = sc.nextLine().trim();
while(!toFrom.equalsIgnoreCase("to") && !toFrom.equalsIgnoreCase("from"))
{
System.out.println("Invalid input for to/from clause!");
System.out.print("\nConvert to Australian Dollar or from Australian Dollar? (enter to/from): ");
toFrom = sc.nextLine().trim();
}
  
System.out.print("Enter the amount to convert: ");
amount = Double.parseDouble(sc.nextLine().trim());
MoneyChanger changer = new MoneyChanger(amount);
  
if(toFrom.equalsIgnoreCase("to"))
{
double dollarToAustralianEuro = changer.dollarToAustralianDollar();
System.out.println(amount + " Dollars = "
+ String.format("%.2f", dollarToAustralianEuro) + " Australian Euro\n");
}
else
{
double australianDollarToDollar = changer.australianDollarToDollar();
System.out.println(amount + " Australian Euro = "
+ String.format("%.2f", australianDollarToDollar) + " Dollars\n");
}
break;
}
  
case 4:
{
System.out.println("\nThanks for using the Money Changer App!\nGoodbye.\n");
System.exit(0);
}
  
default:
System.out.println("\nInvalid selection!\n");
}
}while(choice != 4);
}
  
public final void displayMenu()
{
System.out.print("Money Changer Program. Please select the currency from below:\n"
+ "1. Yen\n"
+ "2. Euro\n"
+ "3. Australian Dollar\n"
+ "4. Exit\n"
+ "Enter your selection: ");
}
}

MoneyChangerController.java (Driver class)

public class MoneyChangerController {
  
public static void main(String[] args)
{
new MoneyChangerView();
}
}

****************************************************************** SCREENSHOT *********************************************************


Related Solutions

I/O Lab Java Purpose To practice the input and output concepts discussed in this module. Specifically,...
I/O Lab Java Purpose To practice the input and output concepts discussed in this module. Specifically, reading from and writing to local files, and formatting output. Instructions Read in file input.csv and generate a cleanly formatted table in output.txt. See the samples below. input.csv name,ID,salary,years experience foo,1,13890,12 bar,2,2342,3 baz,3,99999,24 output.txt Name | ID | Salary | Years experience -----+----+--------+----------------- Foo | 1 | 13890 | 12 Bar | 2 | 2342 | 3 Baz | 3 | 99999 | 24
Fundamentals of Programming USING JAVA Please copy here your source codes, including your input and output...
Fundamentals of Programming USING JAVA Please copy here your source codes, including your input and output screenshots. Please upload this document along with your source files (i.e., the .java files) on Blackboard by the due date. 1. (Display three messages) Write a program that displays Welcome to Java, Welcome to Computer Science, and Programming is fun. 2. (Convert feet into meters) Write a program that reads a number in feet, converts it to meters, and displays the result. One foot...
SHOW SCREEN SHOTS OF THE INPUT AND OUTPUT AS WELL AS THE RESULTS PROGRAMMING IN PERL...
SHOW SCREEN SHOTS OF THE INPUT AND OUTPUT AS WELL AS THE RESULTS PROGRAMMING IN PERL Task 2: Variables 2.1 Scalar Variables A scalar is a single unit of data. That data might be an integer number, floating point, a character, a string, a paragraph, or an entire web page. Simply saying it could be anything, but only a single thing. #!/usr/bin/perl $age = 35; # An integer assignment $name = "Gary Mann"; # A string $salary = 5445.50; #...
IN JAVA!!! In this project, you will use radix.txt as the input file, and output the...
IN JAVA!!! In this project, you will use radix.txt as the input file, and output the integers SORTED USING RADIX SORT. You may assume all your input consists of integers <=9999. Your main program will input the integers and put them into a QUEUE. It will then pass this queue to a method called radixSort which will sort the numbers in the queue, passing the sorted queue back to main. The main program will then call another method to print...
JAVA programming language: For refrence: nextInt ( ) Scans the next token of the input as...
JAVA programming language: For refrence: nextInt ( ) Scans the next token of the input as an int. How many times does the while loop execute, if the input is 38 20 4 0? Assume a Scanner object scnr has been instantiated, and that the tokens in the input are delimited by the space character. That means there are 4 tokens in the input. int num = 2000; while (num >= 20) { //Do something num = scnr.nextInt(); } select...
Objectives To learn to code, compile, and run a program using file input and an output...
Objectives To learn to code, compile, and run a program using file input and an output file. Assignment Plan and code a program utilizing one file for input and one file for output to solve the following problem: Write a program to determine the highest number, the lowest number, their total, and the average of each line of numbers in a file. A file contains 7 numbers per line. How many lines a file contains is unknown. Note Label all...
Find the minimum element from the lists of integers with Java. Input: [[5,2,31,9],[2,1,6,7,11],[55,0,7,8,9]] Output: 0. [The...
Find the minimum element from the lists of integers with Java. Input: [[5,2,31,9],[2,1,6,7,11],[55,0,7,8,9]] Output: 0. [The minimum among the lists of integers] 1. Implement a program of O(N^k) runtime complexity of the problem. 2. Implement a program of ~ O(N*logN) runtime complexity of the above same problem and explain how you have achieved. [Hint: Apply the log rule -> sort the list of integers, then compare the first element of each]
Java Programming I need an application that collects the user input numbers into an array and...
Java Programming I need an application that collects the user input numbers into an array and after that calls a method that sums all the elements of this array. and display the array elements and the total to the user. The user decides when to stop inputting the numbers. Thanks for your help!
Please write the program in JAVA and provide me with the output screenshots!! Assignment Objectives •...
Please write the program in JAVA and provide me with the output screenshots!! Assignment Objectives • Be able to write methods • Be able to call methods Introduction Methods are commonly used to break a problem down into small manageable pieces. A large task can be broken down into smaller tasks (methods) that contain the details of how to complete that small task. The larger problem is then solved by implementing the smaller tasks (calling the methods) in the correct...
Program in Java using Inheritence The purpose of this assignment is to practice OOP programming covering...
Program in Java using Inheritence The purpose of this assignment is to practice OOP programming covering Inheritance. Core Level Requirements (up to 6 marks) The scenario for this assignment is to design an online shopping system for a local supermarket (e.g., Europa Foods Supermarket or Wang Long Oriental Supermarket). The assignment is mostly concentrated on the product registration system. Design and draw a UML diagram, and write the code for the following classes: The first product category is a fresh...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT