1) compareTo method must be negative not positive  2) the max value must be $24.09 but its showing "The Max is $4.80"

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

I have the following code for Money.java

public class Money {
    private int dollar;
    private int cent;
    final static int MIN_CENT_VALUE = 0;
    final static int MAX_CENT_VALUE = 99;

    public Money() {
        this.dollar = 0;
        this.cent = 0;
    }

    public Money(int dollar, int cent) {
        this.dollar = dollar;
        if (cent > MIN_CENT_VALUE && cent <= MAX_CENT_VALUE) {
            this.cent = cent;
        } else {
            cent = 0;
        }
    }

    public int getDollar() {
        return this.dollar;
    }

    public void setDollar(int dol) {
        this.dollar = dol;
    }

    public int getCent() {
        return this.cent;
    }

    public void setCent(int c) {
        if (c >= MIN_CENT_VALUE && c <= MAX_CENT_VALUE) {
            this.cent = c;
        }
    }

    public Money add(Money otherMoney) {
        Money m = new Money();
        m.dollar = this.dollar + otherMoney.dollar;
        m.cent = this.cent + otherMoney.cent;
        if (m.cent >= 100) {
            m.dollar++;
            m.cent -= 100;
        }

        return m;
    }

    public Money subtract(Money otherMoney) {
        Money m = new Money();
        m.dollar = this.dollar - otherMoney.dollar;
        m.cent = this.cent - otherMoney.cent;
        if (m.dollar < 0 || m.cent < 0) {
            return null;
        }
        return m;
    }

    public boolean equals(Money otherMoney) {
        return this == otherMoney;
    }

    public int compareTo(Money otherMoney) {
        Money  m =  new Money();
        if (this.dollar< otherMoney.dollar){
            m = otherMoney.subtract(this);
            return m.dollar*100+m.cent;
        }
        else if (this.dollar>otherMoney.dollar){
            m = this.subtract(otherMoney);
            return m.dollar*100+m.cent;
        }
        else{
            return 0;
        }
       
    }

    public String toString() {
        if (this.cent<10){
            return "$" + this.dollar + ".0" + this.cent;
        }
        return "$" + this.dollar + "." + this.cent;
    }

    public static Money max(Money[] m) {
        int max = m[0].dollar+m[0].cent;
        int index = 0;
        for (int i = 0; i < m.length; i++) {
            if (max < m[i].dollar+((m[i].cent)/100)) {
                max = m[i].dollar+((m[i].cent)/100);
                index = i;
            }
        }
        return m[index];
    }

    public static void selectionSort(Money arr[]) {
        int n = arr.length;

        for (int i = 0; i < n -1; i++) {
            int min = i;
            for (int j = i + 1; j < n; j++)
                if (arr[j].dollar+((arr[j].cent)/100.0) < arr[min].dollar+((arr[min].cent)/100.0))
                    min = j;

            // Swap the found minimum element with the first element
            Money temp = new Money();
            temp = arr[min];
            arr[min] = arr[i];
            arr[i] = temp;
        }
    }

    public static void printList(Money[] m, int i) // This method takes an array of object as parameter

    {
        for (Money mm : m){
            System.out.print(mm + "  ");
        }

        System.out.println();
    }
}

_________________________________________

for use with the following unchangleable driver program TestMoney

import java.util.*;
public class TestMoney
{
   public static void main(String[] args)
   {
      Random rnd = new Random(10);
      Money m1 = new Money(5,45);
      Money m2 = new Money(10,95);
      m1.setCent(101);
      Money sum = m1.add(m2);
      System.out.println(m1+" + "+m2+" is "+sum);
      Money diff = m1.subtract(m2);
      System.out.println(m1+" - "+m2+" is "+diff);
      diff = m2.subtract(m1);
      System.out.println(m2+" - "+m1+" is "+diff);
      System.out.println("equals method result: "+m1.equals(m2));
      System.out.println("compareTo method result: "+m1.compareTo(m2));

            
      Money[] list = new Money[15];
   // Generate 15 random Money objects
      for(int i=0;i<list.length;++i)
      {
         list[i] = new Money((int)(1+rnd.nextInt(30)),rnd.nextInt(100));
      }
   // print the list of Money objects, 10 per line
      System.out.println("Unsorted list");
      Money.printList(list,10);
   // Find the largest Money object
      System.out.println("The Max is "+Money.max(list));
  // Sort and print
      System.out.println("Sorted list");
      Money.selectionSort(list);
      Money.printList(list,10);
      }
}

______________________________________

it's having 2 issues still

1) compareTo method must be negative not positive 

2) the max value must be $24.09 but its showing "The Max is $4.80"

Output differs. See highlights below. Special character legend
Your output
Expected output
$5.45 + $10.95 is $16.40
$5.45 $10.95 is null
$5.45 is $5.50
$10.95
equals method result: false
compareTo method result: 550
Unsorted list
$4.80 $4.90 $17.56 $8.88 $22.14 $14.99 $22.08 $16.80 $17.53 $14
The Max is $4.80
Sorted list
$4.80 $4.90 $6.49 $8.88 $9.50 $14.38 $14.99 $15.70 $16.08 $16.8
▶
4
$5.45 + $10.95 is $16.40
$5.45 $10.95 is null
$10.95
$5.45 is $5.50
equals method result: false
compareTo method result: -550
Unsorted list
$4.80 $4.90 $17.56 $8.88 $22.14 $14.99 $22.08 $16.80 $17.53 $14
$24.09 $16.08 $6.49 $15.70 $9.50
The Max is $24.09
Sorted list
$4.80 $4.90 $6.49 $8.88 $9.50 $14.38 $14.99 $15.70 $16.08 $16.8
$17.53 $17.56 $22.08 $22.14 $24.09
Transcribed Image Text:Output differs. See highlights below. Special character legend Your output Expected output $5.45 + $10.95 is $16.40 $5.45 $10.95 is null $5.45 is $5.50 $10.95 equals method result: false compareTo method result: 550 Unsorted list $4.80 $4.90 $17.56 $8.88 $22.14 $14.99 $22.08 $16.80 $17.53 $14 The Max is $4.80 Sorted list $4.80 $4.90 $6.49 $8.88 $9.50 $14.38 $14.99 $15.70 $16.08 $16.8 ▶ 4 $5.45 + $10.95 is $16.40 $5.45 $10.95 is null $10.95 $5.45 is $5.50 equals method result: false compareTo method result: -550 Unsorted list $4.80 $4.90 $17.56 $8.88 $22.14 $14.99 $22.08 $16.80 $17.53 $14 $24.09 $16.08 $6.49 $15.70 $9.50 The Max is $24.09 Sorted list $4.80 $4.90 $6.49 $8.88 $9.50 $14.38 $14.99 $15.70 $16.08 $16.8 $17.53 $17.56 $22.08 $22.14 $24.09
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Follow-up Questions
Read through expert solutions to related follow-up questions below.
Follow-up Question

How can I correct spacing issue Thank you

>
ection 11.17 - C...
Books
ut
C Issue with output...
My library > COMP 110: Intro... >
11.17: Money
but
correct, but whitespace differs. See highlights below. Special character legend
$10.95 is $16.40
$10.95 is null
$5.45 is $5.50
ethod result: false
> method result: -550
date
learn.zybooks.com
+ $10.95 is $16.40
$10.95 is null
5 - $5.45 is $5.50
s method result: false
C Create following...
0/20
b Answered: 1) co...
B?
list
4.90 $17.56 $8.88 $22.14 $14.99 $22.08 $16.80 $17.53 $14.38 $24
is $24.09
ist
4.90 $6.49 $8.88 $9.50 $14.38 $14.99 $15.70 $16.08 $16.80 $17.5
View V
G screenshot mac
+
8 Rayan Jathan
ceTo method result: -550
ted list
$4.90 $17.56 $8.88 $22.14 $14.99 $22.08 $16.80 $17.53 $14.38
B $16.08 $6.49 $15.70 $9.50
ax is $24.09
1 list
$4.90 $6.49 $8.88 $9.50 $14.38 $14.99 $15.70 $16.08 $16.804
B $17.56 $22.08 $22.14 $24.09
0/20
MacBook Air
Transcribed Image Text:> ection 11.17 - C... Books ut C Issue with output... My library > COMP 110: Intro... > 11.17: Money but correct, but whitespace differs. See highlights below. Special character legend $10.95 is $16.40 $10.95 is null $5.45 is $5.50 ethod result: false > method result: -550 date learn.zybooks.com + $10.95 is $16.40 $10.95 is null 5 - $5.45 is $5.50 s method result: false C Create following... 0/20 b Answered: 1) co... B? list 4.90 $17.56 $8.88 $22.14 $14.99 $22.08 $16.80 $17.53 $14.38 $24 is $24.09 ist 4.90 $6.49 $8.88 $9.50 $14.38 $14.99 $15.70 $16.08 $16.80 $17.5 View V G screenshot mac + 8 Rayan Jathan ceTo method result: -550 ted list $4.90 $17.56 $8.88 $22.14 $14.99 $22.08 $16.80 $17.53 $14.38 B $16.08 $6.49 $15.70 $9.50 ax is $24.09 1 list $4.90 $6.49 $8.88 $9.50 $14.38 $14.99 $15.70 $16.08 $16.804 B $17.56 $22.08 $22.14 $24.09 0/20 MacBook Air
Solution
Bartleby Expert
SEE SOLUTION
Knowledge Booster
void method
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education