Question

CIT-111 Homework #5, Part A                                    Chapter 5, part 1

CIT-111 Homework #5, Part A                                    Chapter 5, part 1 and alternate part 2 (page 341)

A breakdown of the problem is as follows:

  • Class name:                        Money
  • Instance variables:           private, integer:                dollars, cents
  • Constructor methods:    Money () – sets dollars and cents both to zero

                                                    Money (int bucks) – sets dollars to bucks, cents to zero

                                                    Money (int buck, int pennies) – sets dollars to bucks, cents to pennies
  • Accessor methods:          getDollar () – returns value of dollars for the object
                                                    getCent () – returns value of cents for the object
  • Mutator methods:           setDollar (int bucks) – sets the value of dollars to bucks
                                                    setCent (int pennies) – sets the value of cents to pennies
                                                   
                                                    void add (Money pmt1) – public method adds the argument’s
                                                    dollar and cents to the instance variables
                                                   
                                                    Money add (Money pmt1, Money pmt2) – static method returns a
                                                    Money object with the sum of the dollars and cents of the 2 arguments

                                                    void minus (Money pmt1) – public method subtracts the argument’s
                                                    dollar and cents from the instance variables

                                                    Money minus (Money pmt1, Money pmt2) – static method returns a
                                                    Money object with the difference of the dollars and cents of the 2
                                                    arguments
  • Other methods:                 equals – determines if argument (object of class Money) is equal to
                                                    the value of dollars and cents for this object
                                                    toString – returns dollars and cents as a string that includes $ and
                                                    decimal point

EXTRA CREDIT: (some or all three)

  1. When setting dollars and cents, if the values are negative, use a value of zero. If cents is greater than 99, use a value of 99.
  2. When adding cents, if the result is greater than 99 adjust dollars and cents appropriately.
  3. When subtracting cents and getting a negative number, adjust dollars (if a positive number) and store cents result as a positive number. If subtracting dollars and getting a negative number, set dollars result to 0.

Use the driver program MoneyDemo (download from Blackboard) to test your Money class methods.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution:

CODE ( money.java )

public class Money {

   private int dollars, cents;
  
   public Money() {
       this.dollars = 0;
       this.cents = 0;
   }
  
   public Money(int bucks) {
       this.dollars = bucks;
       this.cents = 0;
   }

   public Money(int bucks, int pennies) {
       this.dollars = bucks;
       this.cents = pennies;
   }

   public int getDollars() {
       return dollars;
   }

   public void setDollars(int bucks) {
       if(bucks < 0) {
           this.dollars = 0;
       }
       else
           this.dollars = bucks;
   }

   public int getCents() {
       return cents;
   }

   public void setCents(int pennies) {
       if (pennies < 0) {
           this.cents = 0;
       }
       else
           this.cents = pennies;
   }
  
   public void add(Money pmt1) {
       this.dollars += pmt1.cents / 100 + pmt1.dollars;
       this.cents += pmt1.cents % 100;
   }
  
   static Money add( Money pmt1, Money pmt2 ) {
       int dollars, cents, total_cents;
       total_cents = pmt1.cents + pmt2.cents;
       dollars = pmt1.dollars + pmt2.dollars + total_cents / 100;
       cents = total_cents % 100;
       return new Money(dollars, cents);
   }
  
   public void minus(Money pmt1) {
       if (pmt1.cents > this.cents) {
           this.cents += 100 - pmt1.cents;
           this.dollars += pmt1.dollars - 1;
       }
       else {
           this.cents -= pmt1.cents;
           this.dollars += pmt1.dollars;
       }
   }
  
   static Money minus(Money pmt1, Money pmt2) {
       int dollars, cents;
       if (pmt1.dollars > pmt2.dollars) {
           if(pmt1.cents > pmt2.cents) {
               cents = pmt1.cents - pmt2.cents;
               dollars = pmt1.dollars - pmt2.dollars;
           }
           else {
               cents = pmt2.cents - pmt1.cents;
               dollars = pmt1.dollars - pmt2.dollars - 1;
           }
       }
       else {
           if(pmt2.cents > pmt1.cents) {
               cents = pmt2.cents - pmt1.cents;
               dollars = pmt2.dollars - pmt1.dollars;
           }
           else {
               cents = pmt1.cents - pmt2.cents;
               dollars = pmt2.dollars - pmt1.dollars - 1;
           }
       }
       if(dollars < 0) dollars = 0;
       return new Money(dollars, cents);
   }


   public boolean equals(Money other) {
       if (cents != other.cents)
           return false;
       if (dollars != other.dollars)
           return false;
       return true;
   }

   @Override
   public String toString() {
       return "Money [$" + dollars + "." + cents + "]";
   }
  
      
}

Sample tester code :

( MoneyTester.java )

import Money;

public class MoneyTester {

   public static void main(String[] args) {

       Money money1 = new Money(20, 2);
       Money money2 = new Money(32, 87);
       Money money3 = new Money(32, 87);
      
       System.out.println("Money 1 : " + money1);
       System.out.println("Money 2 : " + money2);
      
       System.out.println("Sum of money1 and money2 is " + Money.add(money1, money2));
       System.out.println("Differnce of money1 and money2 is : " + Money.minus(money1, money2));
      
       System.out.println("Money1 and Money2 are equal : " + money1.equals(money2));
       System.out.println("Money2 and Money3 are equal : " + money2.equals(money3));
   }

}

Sample Output ( Sample Run ) :

Add a comment
Know the answer?
Add Answer to:
CIT-111 Homework #5, Part A                                    Chapter 5, part 1
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • CIT-111 Homework #4, Part A                                      

    CIT-111 Homework #4, Part A                                                                    Chapter 4, problem #7 (pages 254-255) A breakdown of the problem is as follows: Class name:                        Temperature Instance variables:           temp (double) and tempScale (char; either ‘C’ or ‘F’) Constructor methods:    Temperature() – sets temp to 0 Celsius                                                 Temperature(double initialTemp) – sets temp                                                 Temperature(char tempType) – sets tempScale to ‘C’ or ‘F’)                                                 Temperature(double initialTemp, char tempType) --                                                                 sets temp and tempScale to ‘C’ or ‘F’) Accessor methods:          getTemp – returns value of...

  • implement a class called PiggyBank that will be used to represent a collection of coins. Functionality...

    implement a class called PiggyBank that will be used to represent a collection of coins. Functionality will be added to the class so that coins can be added to the bank and output operations can be performed. A driver program is provided to test the methods. class PiggyBank The PiggyBank class definition and symbolic constants should be placed at the top of the driver program source code file while the method implementation should be done after the closing curly brace...

  • Create an abstract class “Appointment” that represents an event on a calendar. The “Appointment” class will...

    Create an abstract class “Appointment” that represents an event on a calendar. The “Appointment” class will have four instance variables: • An instance variable called “year” which will be of type int • An instance variable called “month” which will be of type int • An instance variable called “day” which will be of type int • An instance variable called “description” which will be of type String The “Appointment” class must also implement the following methods: • A getter...

  • Create this c++ program. Create to classes and name their type Account and Money. Follow the...

    Create this c++ program. Create to classes and name their type Account and Money. Follow the instruction listed below. Please read and implement instructions very carefully Money Class Requirements - Negative amounts of Money shall be stored by making both the dollars and cents negative The Money class shall have 4 constructors * A default constructor that initializes your Money object to $0.00 A constructor that takes two integers, the first for the dollars and the second for the cents...

  • In this practical task, you need to implement a class called MyTime, which models a time...

    In this practical task, you need to implement a class called MyTime, which models a time instance. The class must contain three private instance variables: hour, with the domain of values between 0 to 23. minute, with the domain of values between 0 to 59. second, with the domain of values between 0 to 59. For the three variables you are required to perform input validation. The class must provide the following public methods to a user: MyTime() Constructor. Initializes...

  • This needs to be done in Java. Please use basic Java coding at an intro level....

    This needs to be done in Java. Please use basic Java coding at an intro level. THANK YOU FOR YOUR HELP! I WILL LEAVE A GOOD RATING! Design and implement MoneyTest and Money. Use the test-first approach, as we discussed, and document each method before implementing. Money - dollars : int - cents : int + Money (int, int) + getDollars() + getCents() + add (Money) : Money + subtract (Money) : Money + toString() : String + equals (Money)...

  • Write a Java class that implements the concept of Coins, assuming the following attributes (variables): number...

    Write a Java class that implements the concept of Coins, assuming the following attributes (variables): number of quarters, number of dimes, number of nickels, and number of pennies. Include two constructors (non-argument constructor that assigns 1 to each coin, and another constructor that takes necessary arguments to create an object), needed getter and setter methods, method toString (to print coins objects in a meaningful way), and method equals (to compare two coins objects based on number of coins). Also include...

  • Need the answers to these RECURSIVE practice problems using Linked Lists in JAVA . Thank you. Complete the Link cl...

    Need the answers to these RECURSIVE practice problems using Linked Lists in JAVA . Thank you. Complete the Link class by writing methods described below. Do not use loops, or create any more methods (other than those specified), class or instance variables. public class Link private Link next; /null if this is the last link private int value; public Link(Link n, int v) nextn valuev; Do not use loops, or create any more methods (other than those specified), class or...

  • Java Program Please help me with this. It should be pretty basic and easy but I...

    Java Program Please help me with this. It should be pretty basic and easy but I am struggling with it. Thank you Create a superclass called VacationInstance Variables destination - String budget - double Constructors - default and parameterized to set all instance variables Access and mutator methods budgetBalance method - returns the amount the vacation is under or over budget. Under budget is a positive number and over budget is a negative number. This method will be overwritten in...

  • Exercise 1 Adjacency Matrix In this part, you will implement the data model to represent a graph. Implement the followi...

    Exercise 1 Adjacency Matrix In this part, you will implement the data model to represent a graph. Implement the following classes Node.java: This class represents a vertex in the graph. It has only a single instance variable of type int which is set in the constructor. Implement hashCode() and equals(..) methods which are both based on the number instance variable Node - int number +Node(int number); +int getNumberO; +int hashCode() +boolean equals(Object o) +String toString0) Edge.java: This class represents a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT