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

implement the Calculable and Comparable interface in Money and Time classes whic

ID: 3730196 • Letter: I

Question

implement the Calculable and Comparable interface in Money and Time classes which you will create.  

Here is the Calculable Interface:

public interface Calculable<E> {

public E add(E o);

public E subtract(E o);

}

The Money Class:

Create a class to represent money.

Add any necessary data fields, constructors, getters/setters, and other methods.

This class should implement the Comparable and Calculable interfaces.

Implement the methods from these interfaces.

add should add two Money objects together and return the sum of the two as a new Money object

subtract should subtract two Money objects and return the difference of the two as a new Money object.  

NOTE: Money can be negative, but the negative sign should only be displayed on the dollars part, not the cents part.

You will need to account for the fact that cents can only go up to .99 before rolling over to the next dollar amount.

compareTo should be able to compare two money objects. It will be used later to sort a list of Money objects.

Also implement a Copy Constructor to make a deep copy of the object.

Implement toString to display money in the form $1.59 or $-1.59 with only two decimal places.

The Time Class:

Create a class to represent Time.

Add any necessary data fields, constructors, getters/setters, and other methods.

This class should implement the Comparable and Calculable interfaces.

Implement the methods from these interfaces.

add should add two Time objects together and return the sum of the two as a new Time object.

subtract should subtract two Time objects and return the difference of the two as a new Time object. NOTE: Time cannot be negative.

You will need to account for the fact that seconds can only go up to 59 before rolling over to the next minute, minutes can only go up to 59 before rolling over to the next hour, hours can only go up to 12 (or 24 if you implement 24 hour time) before rolling over to 1 o'clock again. (Or in the case of negative time, you should roll over in the opposite direction)

compareTo should be able to compare two Time objects. It will be used later to sort a list of Time objects.

For comparison purposes, an AM time should appear in a sorted list BEFORE a PM time.

Also implement a Copy Constructor (explained in Lab class) to make a deep copy of the object.

Implement toString to display Time in the form hours:minutes:seconds AM or PM, i.e. 03:04:45 AM.

The Main Class

Create two ArrayLists of five Money objects each (add the objects in an unsorted order).

Create two ArrayLists of five Time objects each (add the objects in an unsorted order).

Use Collections.sort() to sort each ArrayList. See the Java API on how to use Collections.sort().

Display each list before and after being sorted.

Take each pair of lists and add the corresponding elements together and display the results.

I.E. Take both Money lists and add together both elements at index 0, index 1, index 2, etc..

Do the same for the Time lists.

Take each pair of lists and subtract the corresponding elements, and display the results.

I.E. Take both Money lists and subtract both elements at index 0, index 1, index 2, etc...

Do the same for the Time lists.

My concern is that for the money addition and subtraction, make sure that the money can be negative. Consider the case when:

11-70-11.90 (-0.20)

-11.70-(-11.80) =(0.10)

-11.70+(-11.80) = (-23.50)

Explanation / Answer

Money.java : ------------>>>>>>>>>>

public class Money implements Calculable<Money>,Comparable<Money>{
private int dollor;
private int cents;

public Money(){
  setDollor(0);
  setCents(0);
}
public Money(int d,int c){
  setDollor(d);
  setCents(c);
}
public Money(Money m){
  setDollor(m.getDollor());
  setCents(m.getCents());
}
public void setCents(int c){
  cents = c % 100;
  dollor = dollor + c/100;
  if(cents < 0){
   cents = cents*(-1);
  }
}
public void setDollor(int d){
  dollor = d;
}
public int getDollor(){
  return dollor;
}
public int getCents(){
  return cents;
}

public String toString(){
  if(cents < 0){
   cents = cents * (-1);
  }
  return "$"+dollor+"."+cents;
}
public int compareTo(Money m){
  if(m.getCents() == getCents()){
   if(m.getCents() == getCents()){
    return 0;
   }
   if(m.getCents() > getCents()){
    return 1;
   }
   if(m.getCents() < getCents()){
    return -1;
   }
  }
  if(m.getDollor() > getDollor()){
   return 1;
  }

  return -1;
}

public Money add(Money o){
  int c = getCents() + o.getCents();
  int d = getDollor() + o.getDollor();
  Money m = new Money();
  m.setDollor(d);
  m.setCents(c);

  return m;
}

public Money subtract(Money o){
  int c = getCents() - o.getCents();
  int d = getDollor() - o.getDollor();
  Money m = new Money();
  m.setDollor(d);
  m.setCents(c);

  return m;
}
}

Time.java : ----------->>>>>>>>

public class Time implements Calculable<Time>,Comparable<Time>{
private int h;
private int m;
private int s;

public Time(){
  setS(0);
  setM(0);
  setH(0);
}
public Time(int h,int m,int s){
  setS(s);
  setM(m);
  setH(h);
}
public void setH(int h){
  this.h = h%24;
}
public void setS(int s){
  this.s = s%60;
  m = m + s/60;
  h = h + this.m/60;
  this.m = this.m%60;
}
public void setM(int m){
  this.m = m%60;
  h = h + m/60;
}
public int getS(){
  return s;
}
public int getM(){
  return m;
}
public int getH(){
  return h;
}
public String toString(){
  String st = "";
  if(h < 10){
   st = st + "0"+h;
  }else{
   st= st + h;
  }
  if(m < 10){
   st = st + ":0"+m;
  }else{
   st = st + ":"+m;
  }
  if(s < 10){
   st = st + ":0"+s;
  }else{
   st = st + ":"+s;
  }

  if(h > 11){
   st = st +" PM";
  }else{
   st = st + " AM";
  }
  return st;
}
public int compareTo(Time m){
  if(m.getH() == getH()){
   if(m.getM() == getM()){
    if(m.getS() == getS()){
     return 0;
    }
    if(m.getS() > getS()){
     return 1;
    }else{
     return -1;
    }
   }
   if(m.getM() >getM()){
    return 1;
   }else{
    return -1;
   }
  }
  if(m.getH() > getH()){
   return 1;
  }

  return -1;
}
public Time add(Time t){
  Time t1 = new Time();
  t1.setH(getH() + t.getH());
  t1.setM(getM() + t.getM());
  t1.setS(getS() + t.getS());

  return t1;
}
public Time subtract(Time t){
  Time t1 = new Time();
  t1.setH(getH() - t.getH());
  t1.setM(getM() - t.getM());
  t1.setS(getS() - t.getS());

  return t1;
}
}

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