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

Hello, I\'m taking a second semester Java programming and ineed help to set up a

ID: 3844162 • Letter: H

Question

Hello,

I'm taking a second semester Java programming and ineed help to set up a Money class. Program should be able to input dollars and cents as integers. Here is the specifications

Instance data: There are two integer values for the instance data representing the dollars and cents. These must be declared as private.

Instance methods: There are eight methods in the class. All methods are public accept the adjust method. DO NOT add any additional methods.

Constructors

1. Money (int dollarsIn, int centsIn) Set the dollars and cents to the values of the parameters. If either input is negative, set the dollars and cents to 0. Invoke the adjust method to make sure that the dollars and cents are valid.

2. Money (Money other) Set the dollars and cents to the values in the parameter.

Mutators

1. add(Money moneyIn) – add the dollars and cents of the parameter to the current object. Invoke the adjust method to make sure that the dollars and cents are valid.

2. subtract(Money moneyIn) – subtract the parameter from the current object. Invoke the adjust method to make sure that the dollars and cents are valid.

3. adjust() – make sure that the cents are from 0..99 borrowing from or adding to the dollars as needed. If both the cents and dollars are negative, set the values to 0.

Accessors

1. int compareTo (Money moneyIn) – return a value < 0 if the current object is less than the parameter, return 0 if the current object equals the parameter and a value > 0 if the current object is greater than the parameter

2. boolean equals(Money moneyIn): return true if the current matches the parameter. otherwise return false

3. String toString(): return the values of the object as a String formatted as $d.cc

===============================

here is the Testing code that checks the functionality of the code

Explanation / Answer

package assign2;

import java.lang.*;

public class Money
{
   private int dollar;
   private int cent;

   //constructor
   Money(int dollarsln,int centln)
   {
       if(dollarsln < 0)
       {
           dollarsln = 0;
       }
   if(centln < 0)
centln = 0;
  
       dollar = dollarsln;
       cent = centln;
   }

   Money(Money m1)
   {
       dollar = m1.dollar;
       cent = m1.cent;
   }

   //Mutator

   public void add(Money m1)
   {
       m1.adjust();
       m1.dollar = dollar + m1.dollar;
       m1.cent = cent + m1.cent;
       m1.adjust();
   }  

   public void subtract(Money m1)
   {
       m1.adjust();
       m1.dollar = dollar - m1.dollar;
       m1.cent = cent - m1.cent;
       m1.adjust();
   }  

   public void adjust()
   {
       if(dollar < 0)
           dollar = 0;
  
   if(cent < 0)
       cent = 0;
  
   }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote