Saturday, September 5, 2020

#156 An object of this class represents an amount

 An object of this class represents an amount - Computer Science

ChemistryExplain daily providing Q&A content “#156 An object of this class represents an amount" in Computer science, Ba computer science, Berkeley computer science, Computer science associate degree jobs
ChemistryExplain “#156 An object of this class represents an amount in Computer science, Ba computer science, Berkeley computer science, Computer science associate degree jobs
Get the Free Online Chemistry Q&A Questions And Answers with explain. To crack any examinations and Interview tests these Chemistry Questions And Answers are very useful. Here we have uploaded the Free Online Chemistry Questions. Here we are also given the all chemistry topic.
 ChemistryExplain team has covered all Topics related to inorganic, organic, physical chemistry, and others So, Prepare these Chemistry Questions and Answers with Explanation Pdf.
For More Chegg Questions

Free Chegg Question

Class Money
An object of this class represents an amount of money in a particular currency. Amounts can be added and subtracted. The amount is stored as a quantity of the minor unit of the currency e.g. 1 Rand will be stored as 100 cents.
Constructors
public Money(String amount, Currency currency)
// Create a Money object that represents the given amount of the given currency.
// The String is assumed to have the following format: <currency symbol> <quantity of
//units> <quantity of minor units> e.g in the case of USD, $50.30, $0.34
Methods
public money add (money other)
// Add the other amount of money to this amount and return the result. The objects must be of the
// same currency
public String toString()
// Obtain a string representation of the monetary value represented by this object e.g. "€45.10" for
// a Money object that represents 45 euros and 10 cents.
Class Currency
An object of this class represents a Currency such as US Dollars or British Pound Stirling.
A currency has an ISO 4217 currency code and a symbol denoting the currency's major unit. Many
currencies have a minor (or fractional) unit such as the cent in the case of US dollars.
A currency object provides facilities for creating strings and for interpreting strings that represent amounts of the currency.
It is assumed that the currency symbol always appears in front of an amount; that negative amounts are represented by a minus sign, that precedes the currency symbol, "-£34.50" for example; that the decimal point is always represented using a full stop; that no attempt is made to group the digits of large quantities, so for example, one million Rand is assumed to be represented as "R1000000" (as opposed to "R1,000,000").
Constructors
public Currency(String symbol, String code, int minorPer Major)
// Create a Currency object that represents the currency with the given unit symbol (e.g. "E" for
// Sterling), ISO 4217 code, and number of minor units per major units (e.g. 100 in the case of
// pennies per British Pound).
Write a program called "SumCosts.java" that asks the user to enter a series of Rand amounts, sums them, and prints the result.
Sample I/O:
Enter an amount or '[D] one' to print the sum and quit: Done
Total: RO.00
Sample I/O:
Enter an amount or '[D] one' to print the sum and quit: R0.99
Enter an amount or '[D] one' to print the sum and quit: R15
Enter an amount or '[D] one' to print the sum and quit: R17.95
Enter an amount or '[D] one' to print the sum and quit: D Total: R33.94
You must use the classes and methods described.
 For More Chemistry Notes and Helpful Content Subscribe Our YouTube Chanel - Chemistry Explain  

Free Chegg Answer

import java.util.Scanner;
class Currency{
   String symbol;
   String code;
   int minorPerMajor;
   public Currency(String symbol, String code, int minorPerMajor) {
       this.symbol = symbol;
       this.code = code;
       this.minorPerMajor = minorPerMajor;
   }

}
class Money{

   String amount;
   Currency currency;
public Money(String amount, Currency currency){
this.amount=amount;
this.currency=currency;
}
public Money add(Money other){
   //System.out.println(other.amount);
   double d1=0, d2=0;
   if(other.amount.contains("-")) {
       d2=-(Double.parseDouble(other.amount.split("-")[1]));
   }
   else {
       d2=Double.parseDouble(other.amount);
   }
   if(amount.contains("-")) {
       d1=-(Double.parseDouble(amount.split("-")[1]));
   }
   else {
       d1=Double.parseDouble(amount);
   }
   double amt=d1+d2;
   //System.out.println("d1="+d1+" d2="+d2);
Money m=new Money(String.valueOf(amt), currency);
//System.out.println(m.toString());
return m;
}
   @Override
   public String toString() {
       return currency.symbol+amount;
   }

}
public class Main
{
   public static void main(String[] args) {
   @SuppressWarnings("resource")
       Scanner sc=new Scanner(System.in);
   String input="NA";
   Currency cr=new Currency("R","710",100);

   Money m=new Money("0.00", cr);
   while(true){
       System.out.println("Enter an amount or '[D]one' to print the sum and quit:");
   input=sc.next();
   if(input.equals("D") || input.equals("Done")) {
       break;
   }
   String arr[]=input.split(cr.symbol);
   Money other=null;
   if(arr[0].equals("-")) {
       other=new Money("-"+arr[1],cr);
   }
   else {
       other=new Money(arr[1],cr);
   }
   m=m.add(other);
   }
   System.out.println("Total: "+m.toString());
   }
}
ChemistryExplain “#155 Your task is to develop a Ticket class in Computer science, Ba computer science, Berkeley computer science, Computer science associate degree jobs
ChemistryExplain “#155 Your task is to develop a Ticket class in Computer science, Ba computer science, Berkeley computer science, Computer science associate degree jobs

Labels: , ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home