A Programming I 10 marks Rewrite the provided Box class so t

A: Programming I 10 marks Rewrite the provided Box class so that it implements the Comparable interface. Boxes should be compared as follows: a box is less than another box if its name is less than the other\'s name (alphabetically). If the two boxes have the same data then a box is less than the other if its name is less than the other\'s name (alphabetically. Two boxes are equal if they have the same name and data value.

Solution

program

                        interface Comparable
{
public class Box
{
   public int data1;
   public String name1;
public Box1(String n1, int d1)
{
     name1=n1;
     data1=d1;
   }
}public Box2(String n2, int d2)
{
     name2=n;
     data2=d;
   }
class Box implements Comparable
{
     if ( name1.compareTo(name2) > 0 )
         System.out.println(\"First string is greater than second.\");
      else if ( name1.compareTo(name2) < 0 )
         System.out.println(\"First string is smaller than second.\");
      else
         System.out.println(\"Both strings are equal.\");
}
if ( data1.compareTo(data2) > 0 )
         System.out.println(\"First string is greater than second.\");
      else if ( data1.compareTo(data2) < 0 )
         System.out.println(\"First string is smaller than second.\");
      else
         System.out.println(\"Both strings are equal.\");
}
class Comp
{
public static void main(String args[])
    {
Comparable obj=new Box();
obj.box1(\"abcd\",\"xyz\");
obj.box2(\"efgh\",\"pqr\");
     }
}

 A: Programming I 10 marks Rewrite the provided Box class so that it implements the Comparable interface. Boxes should be compared as follows: a box is less tha

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site