How to get an Array List of Rectangle objects to Display the area of each Rectangle while using the...





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I have a Rectangle class that in part of the code finds the area of the rectangle class using:



public double calculateArea() {return this.width * this.height;}


I have to create a second class called RectangleComparator and implement the comparator interface to compare two Rectangle objects according to area. The following code is what I have come up with for that:



import java.util.Comparator;

public class RectangleComparator implements Comparator<Rectangle> {

public int compare(Rectangle r1, Rectangle r2) {

double areaDifference = r1.calculateArea() - r2.calculateArea();
if (areaDifference != 0) {return (int) areaDifference;}

if (areaDifference < 0) {return -1;}
else if (areaDifference == 0) {return 0;}
else {return 1}
}
}
}


My final class I had to create was a RectangleSortTest class where the method creates a List containing five Rectangle objects, not in order by area. The method should print the list, displaying each rectangle's area. Then sort the list, and print the list again showing the list has been sorted by area. The following is what I have come up with:



import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class RectangleSortTest {

public static void main(String args) {

List<Rectangle> list = new ArrayList<>();

list.add(new Rectangle (12, 15));
list.add(new Rectangle (5, 2));
list.add(new Rectangle (19, 10));
list.add(new Rectangle (9, 5));
list.add(new Rectangle (6, 10));

System.out.printf("Unsorted list elements:%n%s%n", list);

Collections.sort(list, new RectangleComparator());

System.out.printf("Sorted list elements: %n%s%n", list);
}
}


Everything seems to run correctly but when I run the program and displays the following results:



Unsorted list elements:
[Rectangle@7852e922, Rectangle@4e25154f, Rectangle@70dea4e, Rectangle@5c647e05, Rectangle@33909752]
Sorted list elements:
[Rectangle@4e25154f, Rectangle@5c647e05, Rectangle@33909752,Rectangle@7852e922, Rectangle@70dea4e]



If you look at it the objects seem to be sorting correctly but I cannot figure out why it will not display the area of each triangle instead of the results I got.



Any help would be appreciated.










share|improve this question





























    0















    I have a Rectangle class that in part of the code finds the area of the rectangle class using:



    public double calculateArea() {return this.width * this.height;}


    I have to create a second class called RectangleComparator and implement the comparator interface to compare two Rectangle objects according to area. The following code is what I have come up with for that:



    import java.util.Comparator;

    public class RectangleComparator implements Comparator<Rectangle> {

    public int compare(Rectangle r1, Rectangle r2) {

    double areaDifference = r1.calculateArea() - r2.calculateArea();
    if (areaDifference != 0) {return (int) areaDifference;}

    if (areaDifference < 0) {return -1;}
    else if (areaDifference == 0) {return 0;}
    else {return 1}
    }
    }
    }


    My final class I had to create was a RectangleSortTest class where the method creates a List containing five Rectangle objects, not in order by area. The method should print the list, displaying each rectangle's area. Then sort the list, and print the list again showing the list has been sorted by area. The following is what I have come up with:



    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;

    public class RectangleSortTest {

    public static void main(String args) {

    List<Rectangle> list = new ArrayList<>();

    list.add(new Rectangle (12, 15));
    list.add(new Rectangle (5, 2));
    list.add(new Rectangle (19, 10));
    list.add(new Rectangle (9, 5));
    list.add(new Rectangle (6, 10));

    System.out.printf("Unsorted list elements:%n%s%n", list);

    Collections.sort(list, new RectangleComparator());

    System.out.printf("Sorted list elements: %n%s%n", list);
    }
    }


    Everything seems to run correctly but when I run the program and displays the following results:



    Unsorted list elements:
    [Rectangle@7852e922, Rectangle@4e25154f, Rectangle@70dea4e, Rectangle@5c647e05, Rectangle@33909752]
    Sorted list elements:
    [Rectangle@4e25154f, Rectangle@5c647e05, Rectangle@33909752,Rectangle@7852e922, Rectangle@70dea4e]



    If you look at it the objects seem to be sorting correctly but I cannot figure out why it will not display the area of each triangle instead of the results I got.



    Any help would be appreciated.










    share|improve this question

























      0












      0








      0








      I have a Rectangle class that in part of the code finds the area of the rectangle class using:



      public double calculateArea() {return this.width * this.height;}


      I have to create a second class called RectangleComparator and implement the comparator interface to compare two Rectangle objects according to area. The following code is what I have come up with for that:



      import java.util.Comparator;

      public class RectangleComparator implements Comparator<Rectangle> {

      public int compare(Rectangle r1, Rectangle r2) {

      double areaDifference = r1.calculateArea() - r2.calculateArea();
      if (areaDifference != 0) {return (int) areaDifference;}

      if (areaDifference < 0) {return -1;}
      else if (areaDifference == 0) {return 0;}
      else {return 1}
      }
      }
      }


      My final class I had to create was a RectangleSortTest class where the method creates a List containing five Rectangle objects, not in order by area. The method should print the list, displaying each rectangle's area. Then sort the list, and print the list again showing the list has been sorted by area. The following is what I have come up with:



      import java.util.ArrayList;
      import java.util.Collections;
      import java.util.List;

      public class RectangleSortTest {

      public static void main(String args) {

      List<Rectangle> list = new ArrayList<>();

      list.add(new Rectangle (12, 15));
      list.add(new Rectangle (5, 2));
      list.add(new Rectangle (19, 10));
      list.add(new Rectangle (9, 5));
      list.add(new Rectangle (6, 10));

      System.out.printf("Unsorted list elements:%n%s%n", list);

      Collections.sort(list, new RectangleComparator());

      System.out.printf("Sorted list elements: %n%s%n", list);
      }
      }


      Everything seems to run correctly but when I run the program and displays the following results:



      Unsorted list elements:
      [Rectangle@7852e922, Rectangle@4e25154f, Rectangle@70dea4e, Rectangle@5c647e05, Rectangle@33909752]
      Sorted list elements:
      [Rectangle@4e25154f, Rectangle@5c647e05, Rectangle@33909752,Rectangle@7852e922, Rectangle@70dea4e]



      If you look at it the objects seem to be sorting correctly but I cannot figure out why it will not display the area of each triangle instead of the results I got.



      Any help would be appreciated.










      share|improve this question














      I have a Rectangle class that in part of the code finds the area of the rectangle class using:



      public double calculateArea() {return this.width * this.height;}


      I have to create a second class called RectangleComparator and implement the comparator interface to compare two Rectangle objects according to area. The following code is what I have come up with for that:



      import java.util.Comparator;

      public class RectangleComparator implements Comparator<Rectangle> {

      public int compare(Rectangle r1, Rectangle r2) {

      double areaDifference = r1.calculateArea() - r2.calculateArea();
      if (areaDifference != 0) {return (int) areaDifference;}

      if (areaDifference < 0) {return -1;}
      else if (areaDifference == 0) {return 0;}
      else {return 1}
      }
      }
      }


      My final class I had to create was a RectangleSortTest class where the method creates a List containing five Rectangle objects, not in order by area. The method should print the list, displaying each rectangle's area. Then sort the list, and print the list again showing the list has been sorted by area. The following is what I have come up with:



      import java.util.ArrayList;
      import java.util.Collections;
      import java.util.List;

      public class RectangleSortTest {

      public static void main(String args) {

      List<Rectangle> list = new ArrayList<>();

      list.add(new Rectangle (12, 15));
      list.add(new Rectangle (5, 2));
      list.add(new Rectangle (19, 10));
      list.add(new Rectangle (9, 5));
      list.add(new Rectangle (6, 10));

      System.out.printf("Unsorted list elements:%n%s%n", list);

      Collections.sort(list, new RectangleComparator());

      System.out.printf("Sorted list elements: %n%s%n", list);
      }
      }


      Everything seems to run correctly but when I run the program and displays the following results:



      Unsorted list elements:
      [Rectangle@7852e922, Rectangle@4e25154f, Rectangle@70dea4e, Rectangle@5c647e05, Rectangle@33909752]
      Sorted list elements:
      [Rectangle@4e25154f, Rectangle@5c647e05, Rectangle@33909752,Rectangle@7852e922, Rectangle@70dea4e]



      If you look at it the objects seem to be sorting correctly but I cannot figure out why it will not display the area of each triangle instead of the results I got.



      Any help would be appreciated.







      java list arraylist collections comparator






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 23 '18 at 18:49









      Andrew DeWittAndrew DeWitt

      153




      153
























          1 Answer
          1






          active

          oldest

          votes


















          1














          Your implmentation of toString doesn't output the area - It's using the default implementation. You'll need to override it to generate a usable String based on your Rectangle. (Note - I say "your" Rectangle as it's clear you're not using java.awt.Rectangle, which outputs the x,y,w,h properties in its toString implementation)



          Side note - your compare implementation is overly complicated. Why not just return (int) areaDifference?






          share|improve this answer
























          • I agree with you about the compare implementation. Thanks for the advice. I'm still confused on where to override the toString method. Previous programs I wrote for the Rectangle class, in my test class all I had to do was System.out.println ("Area: " + r1.calculateArea()); but with this ArrayList it will not let me do that.

            – Andrew DeWitt
            Nov 23 '18 at 19:22













          • Literally just define a public method named toString on your Rectangle class, and make it return something useful. It's the method that Java will call to convert any object to a String, and by default it is implemented by Object class to print the object's address (as you're seeing). See this question for some further pointers.

            – Krease
            Nov 23 '18 at 19:26











          • Sorry for the late response. Thanks for the advise. At had to put an @Override and define the toString like you said. Was all I was missing. Thanks again for the help

            – Andrew DeWitt
            Nov 26 '18 at 0:12












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53451670%2fhow-to-get-an-array-list-of-rectangle-objects-to-display-the-area-of-each-rectan%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Your implmentation of toString doesn't output the area - It's using the default implementation. You'll need to override it to generate a usable String based on your Rectangle. (Note - I say "your" Rectangle as it's clear you're not using java.awt.Rectangle, which outputs the x,y,w,h properties in its toString implementation)



          Side note - your compare implementation is overly complicated. Why not just return (int) areaDifference?






          share|improve this answer
























          • I agree with you about the compare implementation. Thanks for the advice. I'm still confused on where to override the toString method. Previous programs I wrote for the Rectangle class, in my test class all I had to do was System.out.println ("Area: " + r1.calculateArea()); but with this ArrayList it will not let me do that.

            – Andrew DeWitt
            Nov 23 '18 at 19:22













          • Literally just define a public method named toString on your Rectangle class, and make it return something useful. It's the method that Java will call to convert any object to a String, and by default it is implemented by Object class to print the object's address (as you're seeing). See this question for some further pointers.

            – Krease
            Nov 23 '18 at 19:26











          • Sorry for the late response. Thanks for the advise. At had to put an @Override and define the toString like you said. Was all I was missing. Thanks again for the help

            – Andrew DeWitt
            Nov 26 '18 at 0:12
















          1














          Your implmentation of toString doesn't output the area - It's using the default implementation. You'll need to override it to generate a usable String based on your Rectangle. (Note - I say "your" Rectangle as it's clear you're not using java.awt.Rectangle, which outputs the x,y,w,h properties in its toString implementation)



          Side note - your compare implementation is overly complicated. Why not just return (int) areaDifference?






          share|improve this answer
























          • I agree with you about the compare implementation. Thanks for the advice. I'm still confused on where to override the toString method. Previous programs I wrote for the Rectangle class, in my test class all I had to do was System.out.println ("Area: " + r1.calculateArea()); but with this ArrayList it will not let me do that.

            – Andrew DeWitt
            Nov 23 '18 at 19:22













          • Literally just define a public method named toString on your Rectangle class, and make it return something useful. It's the method that Java will call to convert any object to a String, and by default it is implemented by Object class to print the object's address (as you're seeing). See this question for some further pointers.

            – Krease
            Nov 23 '18 at 19:26











          • Sorry for the late response. Thanks for the advise. At had to put an @Override and define the toString like you said. Was all I was missing. Thanks again for the help

            – Andrew DeWitt
            Nov 26 '18 at 0:12














          1












          1








          1







          Your implmentation of toString doesn't output the area - It's using the default implementation. You'll need to override it to generate a usable String based on your Rectangle. (Note - I say "your" Rectangle as it's clear you're not using java.awt.Rectangle, which outputs the x,y,w,h properties in its toString implementation)



          Side note - your compare implementation is overly complicated. Why not just return (int) areaDifference?






          share|improve this answer













          Your implmentation of toString doesn't output the area - It's using the default implementation. You'll need to override it to generate a usable String based on your Rectangle. (Note - I say "your" Rectangle as it's clear you're not using java.awt.Rectangle, which outputs the x,y,w,h properties in its toString implementation)



          Side note - your compare implementation is overly complicated. Why not just return (int) areaDifference?







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 '18 at 18:51









          KreaseKrease

          11.9k84263




          11.9k84263













          • I agree with you about the compare implementation. Thanks for the advice. I'm still confused on where to override the toString method. Previous programs I wrote for the Rectangle class, in my test class all I had to do was System.out.println ("Area: " + r1.calculateArea()); but with this ArrayList it will not let me do that.

            – Andrew DeWitt
            Nov 23 '18 at 19:22













          • Literally just define a public method named toString on your Rectangle class, and make it return something useful. It's the method that Java will call to convert any object to a String, and by default it is implemented by Object class to print the object's address (as you're seeing). See this question for some further pointers.

            – Krease
            Nov 23 '18 at 19:26











          • Sorry for the late response. Thanks for the advise. At had to put an @Override and define the toString like you said. Was all I was missing. Thanks again for the help

            – Andrew DeWitt
            Nov 26 '18 at 0:12



















          • I agree with you about the compare implementation. Thanks for the advice. I'm still confused on where to override the toString method. Previous programs I wrote for the Rectangle class, in my test class all I had to do was System.out.println ("Area: " + r1.calculateArea()); but with this ArrayList it will not let me do that.

            – Andrew DeWitt
            Nov 23 '18 at 19:22













          • Literally just define a public method named toString on your Rectangle class, and make it return something useful. It's the method that Java will call to convert any object to a String, and by default it is implemented by Object class to print the object's address (as you're seeing). See this question for some further pointers.

            – Krease
            Nov 23 '18 at 19:26











          • Sorry for the late response. Thanks for the advise. At had to put an @Override and define the toString like you said. Was all I was missing. Thanks again for the help

            – Andrew DeWitt
            Nov 26 '18 at 0:12

















          I agree with you about the compare implementation. Thanks for the advice. I'm still confused on where to override the toString method. Previous programs I wrote for the Rectangle class, in my test class all I had to do was System.out.println ("Area: " + r1.calculateArea()); but with this ArrayList it will not let me do that.

          – Andrew DeWitt
          Nov 23 '18 at 19:22







          I agree with you about the compare implementation. Thanks for the advice. I'm still confused on where to override the toString method. Previous programs I wrote for the Rectangle class, in my test class all I had to do was System.out.println ("Area: " + r1.calculateArea()); but with this ArrayList it will not let me do that.

          – Andrew DeWitt
          Nov 23 '18 at 19:22















          Literally just define a public method named toString on your Rectangle class, and make it return something useful. It's the method that Java will call to convert any object to a String, and by default it is implemented by Object class to print the object's address (as you're seeing). See this question for some further pointers.

          – Krease
          Nov 23 '18 at 19:26





          Literally just define a public method named toString on your Rectangle class, and make it return something useful. It's the method that Java will call to convert any object to a String, and by default it is implemented by Object class to print the object's address (as you're seeing). See this question for some further pointers.

          – Krease
          Nov 23 '18 at 19:26













          Sorry for the late response. Thanks for the advise. At had to put an @Override and define the toString like you said. Was all I was missing. Thanks again for the help

          – Andrew DeWitt
          Nov 26 '18 at 0:12





          Sorry for the late response. Thanks for the advise. At had to put an @Override and define the toString like you said. Was all I was missing. Thanks again for the help

          – Andrew DeWitt
          Nov 26 '18 at 0:12




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53451670%2fhow-to-get-an-array-list-of-rectangle-objects-to-display-the-area-of-each-rectan%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown