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

in java Part One: Define a class named Money whose objects represent amounts of

ID: 3693010 • Letter: I

Question

in java

Part One: Define a class named Money whose objects represent amounts of U.S.

money. The class should have two instance variables of type int for the dollars and

cents in the amount of money. Include a constructor with two parameters of type

int for the dollars and cents, one with one constructor of type int for an amount of

dollars with zero cents, and a no-argument constructor. Include the methods add and

minus for addition and subtraction of amounts of money. These methods should be

static methods, should each have two parameters of type Money, and return a value

of type Money. Include a reasonable set of accessor and mutator methods as well as

the methods equals and toString. Write a test program for your class.

Part Two: Add a second version of the methods for addition and subtraction.

These methods should have the same names as the static version but should use a

calling object and a single argument. For example, this version of the add method

(for addition) has a calling object and one argument. So m1.add(m2) returns the

result of adding the Money objects m1 and m2. Note that your class should have all

these methods; for example, there should be two methods named add .

Alternate Part Two: Add a second version of the methods for addition and subtrac-

tion. These methods should have the same names as the static version but should

use a calling object and a single argument. The methods should be void methods.

The result should be given as the changed value of the calling object. For example,

this version of the add method (for addition) has a calling object and one argu-

ment. Therefore,

m1.add(m2);

changes the values of the instance variables of m1 so they represent the result of

adding m2 to the original version of m1. Note that your class should have all these

methods; for example, there should be two methods named add .

(If you want to do both Part Two and Alternate Part Two, they must be two

classes. You cannot include the methods from both Part Two and Alternate Part

Two in a single class. Do you know why?)

Explanation / Answer

public class currency { public currency() { char us_dollar_sym = 36; char pound_sym = 163; char yen_sym = 165; char euro_sym = 8364; String us_dollar = "Dollars"; String pound = "Pounds"; String yen = "Yen"; String euro = "Euros"; double rate = 0; // Interface System.out.println("Welcome to the Currency Converter Program "); System.out.println("Use the following codes to input your currency choices: 1 - US dollars 2 - Euros 3 - British Pounds 4 - Japanese Yen "); // System.out.println("Please choose the input currency:"); Scanner in = new Scanner(System.in); int choice = in.nextInt(); String inType = null; switch(choice) { case 1: inType = "US Dollars >> " + us_dollar_sym; break; case 2: inType = "Euros >> " + euro_sym; break; case 3: inType = "British Pounds >> " + pound_sym; break; case 4: inType = "Japanese Yen >> " + yen_sym; break; default: System.out.println("Please restart the program & enter a number from the list."); return; } System.out.println("Please choose the output currency"); int output = in.nextInt(); System.out.printf("Now enter the input in " + inType); double input = in.nextDouble(); if (choice == output) System.out.println("Same currency no need to convert"); if (choice == 1 && output == 2) { double dollar_euro_rate = 0.78391; rate = input * dollar_euro_rate; System.out.printf( "%s" + input + " at a conversion rate of " + dollar_euro_rate + " Dollars to %s = %.2f ", (char)us_dollar_sym, euro, rate); } else if (choice == 1 && output == 3){ double dollar_pound_rate = 0.621484; rate = input * dollar_pound_rate; System.out.printf( "%s" + input + " at a conversion rate of " + dollar_pound_rate + " Dollars to %s = %.2f ", (char)us_dollar_sym, pound, rate); } else if (choice == 1 && output == 4){ double dollar_yen_rate = 107.174; rate = input * dollar_yen_rate; System.out.printf( "%s" + input + " at a conversion rate of " + dollar_yen_rate + " Dollars to %s = %.2f ", (char)us_dollar_sym, yen, rate); } if (choice == 2 && output == 1) { double euro_dollar_rate = 1.27579; rate = input * euro_dollar_rate; System.out.printf( "%s" + input + " at a conversion rate of " + euro_dollar_rate + " Euros to %s = %.2f ", (char)euro_sym, us_dollar, rate); } else if (choice == 2 && output == 3) { double euro_pound_rate = 0.792648; rate = input * euro_pound_rate; System.out.printf( "%s" + input + " at a conversion rate of " + euro_pound_rate + " Euros to %s = %.2f ", (char)euro_sym, pound, rate); } else if (choice == 2 && output == 4) { double euro_yen_rate = 136.708; rate = input * euro_yen_rate; System.out.printf( "%s" + input + " at a conversion rate of " + euro_yen_rate + " Euros to %s = %.2f ", (char)euro_sym, yen, rate); } if (choice == 3 && output == 1) { double pound_dollar_rate = 1.60972; System.out.printf( "%s" + input + " at a conversion rate of " + pound_dollar_rate + " Pounds to %s = %.2f ", (char)pound_sym, us_dollar, rate); } else if (choice == 3 && output == 2) { double pound_euro_rate = 1.26161; System.out.printf( "%s" + input + " at a conversion rate of " + pound_euro_rate + " Pounds to %s = %.2f ", (char)pound_sym, euro, rate); } else if (choice == 3 && output == 4) { double pound_yen_rate = 172.511; System.out.printf( "%s" + input + " at a conversion rate of " + pound_yen_rate + " Pounds to %s = %.2f ", (char)pound_sym, yen, rate); } if (choice == 4 && output == 1) { double yen_dollar_rate = 0.00932574; System.out.printf( "%s" + input + " at a conversion rate of " + yen_dollar_rate + " Yen to %s = %.2f ", (char)yen_sym, us_dollar, rate); } else if (choice == 4 && output == 2) { double yen_euro_rate = 0.00730615; System.out.printf( "%s" + input + " at a conversion rate of " + yen_euro_rate + " Yen to %s = %.2f ", (char)yen_sym, euro, rate); } else if (choice == 4 && output == 3) { double yen_pound_rate = 0.00579135; System.out.printf( "%s" + input + " at a conversion rate of " + yen_pound_rate + " Yen to %s = %.2f ", (char)yen_sym, pound, rate); } System.out.println("Thank you for using the currency converter"); } }