Question: Can someone help me check my code? Basically i have 2 classes, time and money class that implements the Comparable and Calculable interface I will

Can someone help me check my code? Basically i have 2 classes, time and money class that implements the Comparable and Calculable interface

I will post the calculable interface below.. both money class and time class needs to have addition and subtraction of time and money method. I just could not get the right output for time.

(for my time class, I am using 24 hour time instead of 12)

The Calculable Interface:

public interface Calculable {

public E add(E o);

public E subtract(E o);

}

here is my code:

import java.text.DecimalFormat;

public class Money implements Calculable, Comparable{

private int dollar;

private int cent;

public Money() {

this.cent=0;

this.dollar=0;

}

public Money(int dollar, int cent) {

this.dollar= dollar+cent/ 100;

this.cent = cent % 100;

if(cent<0) {

cent=(-1)*cent;

}

}

// public Money(int dollar) {

// this.dollar = dollar;

// cent = 0;

// }

//

public int getDollar() {

return dollar;

}

public void setDollar(int dollar) {

this.dollar = dollar;

}

public int getCent() {

return cent;

}

public void setCent(int cent) {

this.cent = cent;

}

@Override

public int compareTo(Money o) {

if(this.dollar == o.dollar && this.cent == o.cent) {

System.out.println("They are equal.");

return 0;}

else if(this.dollar < o.dollar || (this.dollar == o.dollar && this.cent < o.cent)) {

System.out.println(o.dollar + "." + o.cent + " is bigger");

return -1;}

else if(this.dollar>o.dollar || (this.dollar == o.dollar && this.cent > o.cent)) {

System.out.println(this.dollar + "." + this.cent + " is bigger");

return 1;}

return cent;

}

@Override

public Money add(Money o) {

int newDol;

int newCen;

newDol = dollar + o.dollar;

newCen = cent+o.cent;

if(newCen > 99) {

++newDol;

newCen-=100;

}

Money newMoney = new Money(newDol,newCen);

return newMoney;

}

@Override

public Money subtract(Money o) {

int newDol = dollar - o.dollar;

int newCen = cent - o.cent;

if(newCen<0 && newdol>0) {

newDol--;

newCen+=100;

}

if(newCen<0&&newDol<0) {

newCen= newCen*(-1);

}

if(newCen < 0 && newDol == 0) {

newCen= newCen*(-1);

System.out.println("$-" + newDol + "." + newCen);

}

Money newMoney = new Money(newDol,newCen);

return newMoney;

}

public Money(Money m) {

this(m.dollar,m.cent);

}

@Override

public String toString() {

DecimalFormat money = new DecimalFormat("00");

return "$"+dollar+"."+money.format(cent);

}}

public class Time implements Calculable

private int hour;

private int minute;

private int second;

public Time() {

hour = 0;

minute = 0;

second = 0;

}

public Time(int hour, int minute, int second) {

this.hour=hour;

this.minute = minute;

this.second = second;

}

public int getHour() {

return hour;

}

public void setHour(int hour) {

this.hour=hour%24;

}

public int getMinute() {

return minute;

}

public void setMinute(int minute) {

this.minute = minute % 60;

hour = hour + minute/60;

}

public int getSecond() {

return second;

}

public void setSecond(int second) {

this.second = second%60;

minute = minute + second/60;

hour = hour + minute / 60;

}

public String toString() {

String in = "";

if(hour<10) {

in = in + "0" + hour;

}else

{in = in + hour;

}

if(minute < 10){

in = in + ":0" + minute;

}else {

in = in + ":" + minute;

}

if(second < 10) {

in = in+":0" + second;

}else {

in = in+":"+second;

}

if(hour>11) {

in = in + "PM";

}else {

in = in + "AM";

}

return in;

}

@Override

public Time add(Time o) {

// TODO Auto-generated method stub

Time newTime = new Time();

newTime.hour= o.hour+hour;

newTime.minute= o.minute+minute;

newTime.second= o.second+second;

if(newTime.second>=60) {

newTime.minute += newTime.second/60;

newTime.second = newTime.second%60;

}

if(newTime.minute>=60) {

newTime.hour += newTime.minute/60;

newTime.minute = newTime.minute%60;

}

if(newTime.hour>24) {

newTime.hour=0;

}

return newTime;

}

@Override

public Time subtract(Time o) {

// TODO Auto-generated method stub

Time newTime = new Time();

if(second>o.second) {

--o.minute;

second+=60;

}

newTime.second = second - o.second;

if(minute>o.minute) {

--o.hour;

minute+=60;

}

newTime.minute= minute - o.minute;

newTime.hour = hour - o.hour;

return newTime;

}

@Override

public int compareTo(Time o) {

if(o.getHour() == getHour()){

if(o.getMinute() == getMinute()){

if(o.getSecond() == getSecond()){

return 0;

}

if(o.getSecond() > getSecond()){

return 1;

}else{

return -1;

}

}

if(o.getMinute() >getMinute()){

return 1;

}else{

return -1;

}

}

if(o.getHour() > getHour()){

return 1;

}

return -1;

}}

import java.util.ArrayList;

import java.util.Collections;

public class Main {

public static void main(String[] args) {

// TODO Auto-generated method stub

ArrayList money = new ArrayList<>();

ArrayList mon = new ArrayList<>();

ArrayList

ArrayList

Money money1 = new Money(11,22);

Money money2 = new Money(-11,10);

Money money3 = new Money(120,80);

Money money4 = new Money(100,20);

Money money5 = new Money(80,10);

Money money6 = new Money(23,14);

Money money7 = new Money(90,87);

Money money8 = new Money(29,18);

Money money9 = new Money(78,79);

Money money10 = new Money(22,18);

money.add(money1);

money.add(money2);

money.add(money3);

money.add(money4);

money.add(money5);

Collections.sort(money);

System.out.println(money);

mon.add(money6);

mon.add(money7);

mon.add(money8);

mon.add(money9);

mon.add(money10);

Collections.sort(mon);

System.out.println(mon);

Time time1 = new Time(3,20,30);

Time time2 = new Time(16,45,30);

Time time3 = new Time(22,20,20);

Time time4 = new Time(14,25,31);

Time time5 = new Time(8,21,33);

Time time6 = new Time(7,2,3);

Time time7 = new Time(13,20,30);

Time time8 = new Time(15,22,32);

Time time9 = new Time(2,20,12);

Time time10 = new Time(21,22,9);

time.add(time1);

time.add(time2);

time.add(time3);

time.add(time4);

time.add(time5);

Collections.sort(time);

System.out.println(time);

tim.add(time6);

tim.add(time7);

tim.add(time8);

tim.add(time9);

tim.add(time10);

Collections.sort(tim);

System.out.println(tim);

for(int i = 0; i

System.out.println(money.get(i).add(mon.get(i)));

System.out.println(money.get(i).subtract(mon.get(i)));

}

for(int j = 0; j < time.size();j++) {

System.out.println(time.get(j).add(tim.get(j)));

System.out.println(time.get(j).subtract(tim.get(j)));

}

}

}

Here is my output:

11.22 is bigger

120.80 is bigger

120.80 is bigger

100.20 is bigger

120.80 is bigger

100.20 is bigger

80.10 is bigger

[$-11.10, $11.22, $80.10, $100.20, $120.80]

90.87 is bigger

90.87 is bigger

90.87 is bigger

29.18 is bigger

78.79 is bigger

90.87 is bigger

78.79 is bigger

29.18 is bigger

23.14 is bigger

[$22.18, $23.14, $29.18, $78.79, $90.87]

[22:20:20PM, 16:45:30PM, 14:25:31PM, 08:21:33AM, 03:20:30AM]

[21:22:09PM, 15:22:32PM, 13:20:30PM, 07:02:03AM, 02:20:12AM]

$11.28

$-33.08

$34.36

$-12.08

$109.28

$50.92

$178.99

$21.41

$211.67

$29.93

00:42:29AM

01:0-1:71AM

00:08:02AM

02:83:0-2AM

00:46:01AM

02:66:61AM

15:23:36PM

02:80:90AM

05:40:42AM

02:61:78AM

I wonder why does it print out the comparasion even as I did not call the compareTo method??how do I fix this??

and also please helo me on the time subtraction and addition

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!