Please help me with the task: Show me your bag

Hello, I’m working on this task:

Method Comparator.Compare compares 2 boxes by their volume and returns 1 if the first one is bigger than the second one, -1 if the second one is bigger than the first one, or 0 if the boxes are equal.
Modify the code in a way that class Box uses the Comparator.Compare method to be compared to another box. Depending on the result of the comparison, CompareTo should return “Bigger” if the current box is bigger than box, “Smaller” if the current box is smaller than box, and “Equal” if the boxes are equal.
In the Main, method, read Width, Height, and Length from the console for two boxes and output the result of box1.CompareTo(box2).
For example:

Here is my code:

namespace This
   class Box
       public int Width;
       public int Height;
       public int Length;

       public string CompareTo(Box box) 
           if (Comparator.Compare(box1, box2) == 1) // Compare 2 boxes here and return the result    
      = "Bigger";  
           else if (Comparator.Compare(box1, box2) == -1)
      = "Smaller";
           else if (Comparator.Compare(box1, box2) == 0)
      = "Equal";

   class Comparator
       public static int Compare(Box box1, Box box2) 
           var volume1 = box1.Width * box1.Height * box1.Length;
           var volume2 = box2.Width * box2.Height * box2.Length;

           if (volume1 > volume2)
               return 1;
           if (volume1 < volume2)
               return -1;

           return 0;

   class BoxComparator
       public static void Main()
           var box1 = new Box();
           int width1 = int.Parse(Console.ReadLine());// Create 2 boxes here and output the comparation result
           int height1 = int.Parse(Console.ReadLine());
           int length1 = int.Parse(Console.ReadLine());

           var box2 = new Box();
           int width2 = int.Parse(Console.ReadLine());
           int height2 = int.Parse(Console.ReadLine());
           int length2 = int.Parse(Console.ReadLine());


Привіт! Допоможіть, будь ласка, я не зрозуміла логіку завдання, і наробила щось weird у коді.
Заплуталася, чому у методі CompareTo стоїть комментар // Compare 2 boxes here and return the result, якщо ми вже порівняли у методі Compare…

Can anyone please help me to solve it?

  1. to use comparator you should rather use:
Comparator.Compare(this, box); 
  1. Instead of all the = "Bigger"; you should just return:
return "Bigger"; 
  1. since else if (Comparator.Compare(box1, box2) == 0) is the last option you could just use else
  2. when you read variables in Main, you don’t assign them to a box. One option to do it might be:
           box1.Width = width1;
           box1.Height = height1;
           box1.Length = length1;


var box1 = new Box
                Width = int.Parse(Console.ReadLine()),
                Height = int.Parse(Console.ReadLine()),
                Length = int.Parse(Console.ReadLine())
  1. It’s optional, but if smth can be called only once, don’t repeat the calls. Your solution could be improved by assigning Comparator.Compare(box1, box2) to a variable and then using it to compare with 1 or -1 :slight_smile:

Дуже дякую за такі детальні пояснення, все набагато зрозуміліше :slight_smile:

1 Like