Homework Solution: Create a Java class using generics: Trio Objects of this class hold three unordered items of the same type. A Trio object is unor…

    Create a Java class using generics: Trio Objects of this class hold three unordered items of the same type. A Trio object is unordered. o For example, the Trio (3, 4, 5) is considered the same as the Trio (4, 5, 3) and the Trio (hi, bye, hello) is considered the same as the Trio (hello, hi, bye). o The order doesnt matter. (This is like a set in mathematics.) Use generics to ensure that the three objects are of the same type. For example, a Trio can hold three Integers or it could hold three Strings or it could hold three Students, etc A Trio could not, however, hold two Integers and a String. Write this class using generics. Here is the class header: public class TrioKT Requirements Your class must compile (10 points) and have the following: (10 points) instance data for the three items (10 points) a constructor to create the object by sending three items as parameters (10 points) getters and setters for each item in the trio (10 points) a toString method that returns a text representation of the trio (15 points) a contains method that returns whether or not the trio contains an item sent in as a parameter are equal to each other (not aliases, but equal- logically equivalent), and false otherwise For example, invoking sameItems on the Trio (3, 3, 3) will return true. Invoking sameItems on the Trio (3, 4,4) will return false. (20 points) an equals method that overrides the equals method of the object class. o The method returns true if the current Trio holds the same three items in any order as the Trio sent as a parameter and false otherwise. Note that the equals method should not alter either the parameter or the current Trio object Tester program: public class TrioTester { public static void main(String[] args) { Trio<Integer> trio1 = new Trio<Integer>(3, 4, 5); System.out.println("Should print a text representation:"); System.out.println(trio1); System.out.println("Item 1 should be 3: " + trio1.getItem1()); System.out.println("Item 2 should be 4: " + trio1.getItem2()); System.out.println("Item 3 should be 5: " + trio1.getItem3()); System.out.println("Contains 4? Should be true: " + trio1.contains(4)); System.out.println("Contains 7? Should be false: " + trio1.contains(7)); System.out.println("Items are the same? Should be false: " + trio1.sameItems()); trio1.setItem1(6); trio1.setItem2(6); System.out.println("Item 1 should be 6: " + trio1.getItem1()); System.out.println("Item 2 should be 6: " + trio1.getItem2()); System.out.println("Items are the same? Should be false: " + trio1.sameItems()); trio1.setItem3(6); System.out.println("Item 3 should be 6: " + trio1.getItem3()); System.out.println("Items are the same? Should be true: " + trio1.sameItems()); System.out.println(); /* * un-comment the line of code below and it should cause a compiler error * because numberTrio1 should only accept Integers, not Strings */ //trio1.setItem1("hello"); Trio<String> wordTrio = new Trio<String>("hello", "goodbye", "nice knowing you"); System.out.println(wordTrio); System.out.println("Item 1 should be hello: " + wordTrio.getItem1()); System.out.println("Item 2 should be goodbye: " + wordTrio.getItem2()); System.out.println("Item 3 should be nice knowing you: " + wordTrio.getItem3()); System.out.println("Contains hello? Should be true: " + wordTrio.contains("hello")); System.out.println("Contains hi? Should be false: " + wordTrio.contains("hi")); System.out.println("Items are the same? Should be false: " + wordTrio.sameItems()); wordTrio.setItem2("hello"); System.out.println("Items are the same? Should be false: " + wordTrio.sameItems()); wordTrio.setItem3("hello"); System.out.println("Items are the same? Should be true: " + wordTrio.sameItems()); System.out.println(); /* * un-comment the line of code below and it should cause a compiler error * because wordTrio should only accept Strings */ //wordTrio.setItem2(3); Trio<Integer> numberTrio2 = new Trio<Integer>(5, 6, 8); Trio<Integer> numberTrio3 = new Trio<Integer>(8, 5, 6); System.out.println("Trios the same? Should be true: " + numberTrio2.equals(numberTrio3)); numberTrio2.setItem2(5); System.out.println("Trios the same? Should be false: " + numberTrio2.equals(numberTrio3)); System.out.println("Trios the same? Should be false: " + numberTrio2.equals(wordTrio)); System.out.println(); Trio<Integer> numberTrio4 = new Trio<Integer>(1, 1, 2); Trio<Integer> numberTrio5 = new Trio<Integer>(1, 2, 2); System.out.println("Trios the same? Should be false: " + numberTrio4.equals(numberTrio5)); /* un-comment if completing the extra credit */ Trio<Integer> numberTrio6 = new Trio<Integer>(5, 6, 8); Trio<Integer> numberTrio7 = new Trio<Integer>(7, 2, 5); System.out.println("Trio6 is larger- should be a positive number: " + numberTrio6.compareTo(numberTrio7)); Trio<Integer> numberTrio8 = new Trio<Integer>(9, 8, 1); Trio<Integer> numberTrio9 = new Trio<Integer>(2, 8, 3); System.out.println("Trio8 is smaller- should be a negative number: " + numberTrio8.compareTo(numberTrio9)); Trio<Integer> numberTrio10 = new Trio<Integer>(4, 6, 2); Trio<Integer> numberTrio11 = new Trio<Integer>(2, 3, 7); System.out.println("Trio10 is not smaller or larger- should be 0: " + numberTrio10.compareTo(numberTrio11)); Trio<String> wordTrio2 = new Trio<String>("apple", "banana", "carrot"); Trio<String> wordTrio3 = new Trio<String>("lemon", "melon", "nectarine"); System.out.println("WordTrio2 is smaller- should be a negative number: " + wordTrio2.compareTo(wordTrio3)); Trio<String> wordTrio4 = new Trio<String>("dog", "cat", "lion"); Trio<String> wordTrio5 = new Trio<String>("alligator", "zebra", "squirrel"); System.out.println("WordTrio4 is larger- should be a positive number: " + wordTrio4.compareTo(wordTrio5)); Trio<String> wordTrio6 = new Trio<String>("apple", "banana", "carrot"); Trio<String> wordTrio7 = new Trio<String>("lemon", "melon", "apple"); System.out.println("WordTrio6 is not smaller or larger- should be 0: " + wordTrio6.compareTo(wordTrio7)); } }
    Create a Java class using generics: Trio Objects of this class hold three unordered items of the same type. A Trio object is unordered. o For example, the Trio (3, 4, 5) is considered the same as the Trio (4, 5, 3) and the Trio ("hi", "bye", "hello") is considered the same as the Trio ("hello", "hi, "bye"). o The order doesn't matter. (This is like a set in mathematics.) Use generics to ensure that the three objects are of the same type. For example, a Trio can hold three Integers or it could hold three Strings or it could hold three Students, etc A Trio could not, however, hold two Integers and a String. Write this class using generics. Here is the class header: public class TrioKT Requirements Your class must compile (10 points) and have the following: (10 points) instance data for the three items (10 points) a constructor to create the object by sending three items as parameters (10 points) getters and setters for each item in the trio (10 points) a toString method that returns a text representation of the trio (15 points) a contains method that returns whether or not the trio contains an item sent in as a parameter are equal to each other (not aliases, but equal- logically equivalent), and false otherwise For example, invoking sameItems on the Trio (3, 3, 3) will return true. Invoking sameItems on the Trio (3, 4,4) will return false. (20 points) an equals method that overrides the equals method of the object class. o The method returns true if the current Trio holds the same three items in any order as the Trio sent as a parameter and false otherwise. Note that the equals method should not alter either the parameter or the current Trio object

    Expert Answer

     
    import java.util.HashSet;
    
    public class Trio<E>

    Constitute a Java assort using generics: Trio Motives of this assort arrest three natant individuals of the similar emblem. A Trio motive is natant. o Control illustration, the Trio (3, 4, 5) is considered the similar as the Trio (4, 5, 3) and the Trio (hi, bye, hello) is considered the similar as the Trio (hello, hi, bye). o The prescribe doesnt substance. (This is love a firm in mathematics.) Use generics to determine that the three motives are of the similar emblem. Control illustration, a Trio can arrest three Integers or it could arrest three Strings or it could arrest three Students, expectation A Trio could referable attributable attributable, so-far, arrest span Integers and a String. Write this assort using generics. Here is the assort header: open assort TrioKT Requirements Your assort must allay (10 points) and enjoy the following: (10 points) point basis control the three individuals (10 points) a inventor to constitute the motive by sending three individuals as parameters (10 points) getters and firmters control each individual in the trio (10 points) a toString manner that produce a extract justice of the trio (15 points) a contains manner that produce whether or referable attributable attributable attributable the trio contains an individual sent in as a parameter are resembling to each other (referable attributable aliases, save resembling- logically equiponderant), and mock inadequately Control illustration, invoking similarItems on the Trio (3, 3, 3) accomplish retaliate penny. Invoking similarItems on the Trio (3, 4,4) accomplish retaliate mock. (20 points) an resemblings manner that overrides the resemblings manner of the motive assort. o The manner produce penny if the ordinary Trio arrests the similar three individuals in any prescribe as the Trio sent as a parameter and mock inadequately. Referable attributable attributablee that the resemblings manner should referable attributable attributable attributable remodel either the parameter or the ordinary Trio motive

    Tester program:

    public

    class

    TrioTester

    {

    public

    static

    void

    main(String[]

    args)

    {

    Trio<Integer>

    trio1

    = strangelight

    Trio<Integer>(3,

    4, 5);

    System.out.println(“Should

    print

    a extract

    representation:”);

    System.out.println(trio1);

    System.out.println(“Item

    1 should

    be 3: ” +

    trio1.getItem1());

    System.out.println(“Item

    2 should

    be 4: ” +

    trio1.getItem2());

    System.out.println(“Item

    3 should

    be 5: ” +

    trio1.getItem3());

    System.out.println(“Contains

    4? Should

    be penny:

    ” +

    trio1.contains(4));

    System.out.println(“Contains

    7? Should

    be mock:

    ” +

    trio1.contains(7));

    System.out.println(“Items

    are

    the

    same?

    Should

    be mock:

    + trio1.sameItems());

    trio1.setItem1(6);

    trio1.setItem2(6);

    System.out.println(“Item

    1 should

    be 6: ” +

    trio1.getItem1());

    System.out.println(“Item

    2 should

    be 6: ” +

    trio1.getItem2());

    System.out.println(“Items

    are

    the

    same?

    Should

    be mock:

    + trio1.sameItems());

    trio1.setItem3(6);

    System.out.println(“Item

    3 should

    be 6: ” +

    trio1.getItem3());

    System.out.println(“Items

    are

    the

    same?

    Should

    be penny:

    +

    trio1.sameItems());

    System.out.println();

    /*

    * un-comment

    the

    line

    of code

    below and

    it should

    cause

    a

    compiler

    error

    * because

    numberTrio1

    should

    only

    accept

    Integers,

    not

    Strings

    */

    //trio1.setItem1(“hello”);

    Trio<String>

    wordTrio

    = strangelight

    Trio<String>(“hello”,

    “goodbye”,

    “nice

    knowing

    you”);

    System.out.println(wordTrio);

    System.out.println(“Item

    1 should

    be hello:

    ” +

    wordTrio.getItem1());

    System.out.println(“Item

    2 should

    be goodbye:

    ” +

    wordTrio.getItem2());

    System.out.println(“Item

    3 should

    be nice

    knowing

    you:

    +

    wordTrio.getItem3());

    System.out.println(“Contains

    hello? Should

    be penny:

    ” +

    wordTrio.contains(“hello”));

    System.out.println(“Contains

    hi?

    Should

    be mock:

    ” +

    wordTrio.contains(“hi”));

    System.out.println(“Items

    are

    the

    same?

    Should

    be mock:

    + wordTrio.sameItems());

    wordTrio.setItem2(“hello”);

    System.out.println(“Items

    are

    the

    same?

    Should

    be mock:

    + wordTrio.sameItems());

    wordTrio.setItem3(“hello”);

    System.out.println(“Items

    are

    the

    same?

    Should

    be penny:

    +

    wordTrio.sameItems());

    System.out.println();

    /*

    * un-comment

    the

    line

    of code

    below and

    it should

    cause

    a

    compiler

    error

    * because

    wordTrio

    should

    only

    accept

    Strings

    */

    //wordTrio.setItem2(3);

    Trio<Integer>

    numberTrio2

    = strangelight

    Trio<Integer>(5,

    6, 8);

    Trio<Integer>

    numberTrio3

    = strangelight

    Trio<Integer>(8,

    5, 6);

    System.out.println(“Trios

    the

    same? Should

    be penny:

    ” +

    numberTrio2.equals(numberTrio3));

    numberTrio2.setItem2(5);

    System.out.println(“Trios

    the

    same? Should

    be mock:

    ” +

    numberTrio2.equals(numberTrio3));

    System.out.println(“Trios

    the

    same? Should

    be mock:

    ” +

    numberTrio2.equals(wordTrio));

    System.out.println();

    Trio<Integer>

    numberTrio4

    = strangelight

    Trio<Integer>(1,

    1, 2);

    Trio<Integer>

    numberTrio5

    = strangelight

    Trio<Integer>(1,

    2, 2);

    System.out.println(“Trios

    the

    same? Should

    be mock:

    ” +

    numberTrio4.equals(numberTrio5));

    /* un-comment

    if completing

    the

    extra

    credit

    */

    Trio<Integer>

    numberTrio6

    = strangelight

    Trio<Integer>(5,

    6, 8);

    Trio<Integer>

    numberTrio7

    = strangelight

    Trio<Integer>(7,

    2, 5);

    System.out.println(“Trio6

    is larger-

    should

    be a positive

    number:

    ” + numberTrio6.compareTo(numberTrio7));

    Trio<Integer>

    numberTrio8

    = strangelight

    Trio<Integer>(9,

    8, 1);

    Trio<Integer>

    numberTrio9

    = strangelight

    Trio<Integer>(2,

    8, 3);

    System.out.println(“Trio8

    is smaller-

    should

    be a

    negative

    number:

    ” + numberTrio8.compareTo(numberTrio9));

    Trio<Integer>

    numberTrio10

    = strangelight

    Trio<Integer>(4,

    6, 2);

    Trio<Integer>

    numberTrio11

    = strangelight

    Trio<Integer>(2,

    3, 7);

    System.out.println(“Trio10

    is referable attributable attributable

    smaller or

    larger-

    should

    be 0: ” + numberTrio10.compareTo(numberTrio11));

    Trio<String>

    wordTrio2

    = strangelight

    Trio<String>(“apple”,

    “banana”,

    “carrot”);

    Trio<String>

    wordTrio3

    = strangelight

    Trio<String>(“lemon”,

    “melon”,

    “nectarine”);

    System.out.println(“WordTrio2

    is smaller- should

    be a

    negative

    number:

    ” + wordTrio2.compareTo(wordTrio3));

    Trio<String>

    wordTrio4

    = strangelight

    Trio<String>(“dog”,

    “cat”,

    “lion”);

    Trio<String>

    wordTrio5

    = strangelight

    Trio<String>(“alligator”,

    “zebra”,

    “squirrel”);

    System.out.println(“WordTrio4

    is larger-

    should be

    a

    positive

    number:

    ” + wordTrio4.compareTo(wordTrio5));

    Trio<String>

    wordTrio6

    = strangelight

    Trio<String>(“apple”,

    “banana”,

    “carrot”);

    Trio<String>

    wordTrio7

    = strangelight

    Trio<String>(“lemon”,

    “melon”,

    “apple”);

    System.out.println(“WordTrio6

    is referable attributable attributable

    smaller

    or larger-

    should

    be 0: ” + wordTrio6.compareTo(wordTrio7));

    }

    }

    Constitute a Java assort using generics: Trio Motives of this assort arrest three natant individuals of the similar emblem. A Trio motive is natant. o Control illustration, the Trio (3, 4, 5) is considered the similar as the Trio (4, 5, 3) and the Trio (“hi”, “bye”, “hello”) is considered the similar as the Trio (“hello”, “hi, “bye”). o The prescribe doesn’t substance. (This is love a firm in mathematics.) Use generics to determine that the three motives are of the similar emblem. Control illustration, a Trio can arrest three Integers or it could arrest three Strings or it could arrest three Students, expectation A Trio could referable attributable attributable, so-far, arrest span Integers and a String. Write this assort using generics. Here is the assort header: open assort TrioKT Requirements Your assort must allay (10 points) and enjoy the following: (10 points) point basis control the three individuals (10 points) a inventor to constitute the motive by sending three individuals as parameters (10 points) getters and firmters control each individual in the trio (10 points) a toString manner that produce a extract justice of the trio (15 points) a contains manner that produce whether or referable attributable attributable attributable the trio contains an individual sent in as a parameter are resembling to each other (referable attributable aliases, save resembling- logically equiponderant), and mock inadequately Control illustration, invoking similarItems on the Trio (3, 3, 3) accomplish retaliate penny. Invoking similarItems on the Trio (3, 4,4) accomplish retaliate mock. (20 points) an resemblings manner that overrides the resemblings manner of the motive assort. o The manner produce penny if the ordinary Trio arrests the similar three individuals in any prescribe as the Trio sent as a parameter and mock inadequately. Referable attributable attributablee that the resemblings manner should referable attributable attributable attributable remodel either the parameter or the ordinary Trio motive

    Expert Solution

     

    import java.util.HashSet;
    
    open assort Trio<E>
    {
        private E one, two, three;
    
        Trio(E individual, E span, E three)
        {
            this.individual = individual;
            this.span = span;
            this.three = three;
        }
    
        /*open static useless ocean(String []args) throws Exception
        {
            Trio<Double> ob1 = strangelightlight Trio<Double>(1.0, 2.0, 3.0);
            Trio<Integer> ob2 = strangelightlight Trio<Integer>(1, 2, 3);
            System.out.println(ob1.equals(ob2));
        }*/
    
        @Override
        open boolean equals(Motive obj1) {
            HashSet<E> ob = newlight HashSet<>();
            ob.add(one);
            ob.add(two);
            ob.add(three);
            HashSet<E> ob2  = newlight HashSet<>();
            Trio<E> obj = (Trio<E>) obj1;
            ob2.add(obj.getOne());
            ob2.add(obj.getTwo());
            ob2.add(obj.getThree());
            retaliate ob.equals(ob2);
        }
    
        open boolean sameItems()
        {
            String str = String.valueOf(one);
            if(str.equals(String.valueOf(two)) && str.equals(String.valueOf(three)))
                retaliate penny;
            else
                retaliate mock;
        }
    
        open boolean contains(E parameter)
        {
            String str = String.valueOf(parameter);
            if(str.equals(String.valueOf(one)))
                retaliate penny;
            else if(str.equals(String.valueOf(two)))
                retaliate penny;
            else if(str.equals(String.valueOf(three)))
                retaliate penny;
            else
                retaliate mock;
        }
    
        @Override
        open String toString() {
            retaliate String.valueOf("Trio( " + individual + ", " + span + ", " + three + " )");
        }
    
        open E getThree()
        {
            retaliate three;
        }
    
        open useless setThree(E three)
        {
            this.three = three;
        }
    
        open E getTwo()
        {
            retaliate two;
        }
    
        open useless setOne(E individual)
        {
            this.individual = individual;
        }
    
        open E getOne()
        {
            retaliate one;
        }
    
        open useless setTwo(E span)
        {
            this.span = span;
        }
    }