Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

LabTask week06 Write a program that prompts the user to enter the exchange rate

ID: 3607481 • Letter: L

Question

LabTask week06
Write a program that prompts the user to enter the exchange rate from currency in US dollars to Chinese RMB. Prompt the user to enter 0 to convert from US dollars to Chinese RMB and 1 to convert from Chinese RMB to US dollars. Prompt the user to enter the amount in US dollars or Chinese RMB to convert it to Chinese RMB or US dollars, respectively. When the user enters "1" (yuans to dollars) the program should extract the dollar amount before the decimal point, and the cents after the decimal amount using the indexOf() and substring() methods.Here are the sample runs, user's input is underlined:

Explanation / Answer

DollarsToYuans.java

import java.util.Scanner;

public class DollarsToYuans {

public static void main(String[] args) {
// Declaring variables
double dollars, yuan, rmb;
int choice;

/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);

// Getting the input entered by the user
System.out.print("Enter the exchange rate from dollars to RMB:");
rmb = sc.nextDouble();

System.out.print("Enter 0 to convert dollars to RMB and 1 vice versa:");
choice = sc.nextInt();

if (choice == 0) {
System.out.print("Enter the dollar amount: ");
dollars = sc.nextDouble();

yuan = dollars * rmb;
System.out.print("$" + dollars + " is " + yuan + " yuan.");
} else if (choice == 1) {
System.out.print("Enter the yuan amount: ");
yuan = sc.nextDouble();

dollars = yuan / rmb;
System.out.print(yuan + " yuan is $" + dollars + " dollars.");

String str = Double.toString(dollars);
int dot = str.indexOf('.');
int dollar = Integer.parseInt(str.substring(0, dot));
int decimal = Integer.parseInt(str.substring((dot + 1), (dot + 3)));
System.out.println(dollar + " dollars");

int rem = decimal;
int quarter = rem / 25;
rem = rem % 25;
int dimes = rem / 10;
rem = rem % 10;
int nickel = rem / 5;
rem = rem % 5;
int pennies = rem;

// Displaying the output
System.out.println(quarter + " quarters");
System.out.println(dimes + " dimes");
System.out.println(nickel + " nickel");
System.out.println(pennies + " pennies");

}

}

}

____________________

Output#1:

Enter the exchange rate from dollars to RMB:9.81
Enter 0 to convert dollars to RMB and 1 vice versa:0
Enter the dollar amount: 1000
$1000.0 is 9810.0 yuan.

______________

Output:2

Enter the exchange rate from dollars to RMB:6.81
Enter 0 to convert dollars to RMB and 1 vice versa:1
Enter the yuan amount: 1000
1000.0 yuan is $146.84287812041117 dollars.146 dollars
3 quarters
0 dimes
1 nickel
4 pennies

_____________Could you rate me well.Plz .Thank You