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>

    Produce a Java arrange using generics: Trio Views of this arrange halt three floating aces of the identical expression. A Trio view is floating. o Control specimen, the Trio (3, 4, 5) is considered the identical as the Trio (4, 5, 3) and the Trio (hi, bye, hello) is considered the identical as the Trio (hello, hi, bye). o The adjust doesnt subject. (This is approve a firm in mathematics.) Use generics to fix that the three views are of the identical expression. Control specimen, a Trio can halt three Integers or it could halt three Strings or it could halt three Students, awe A Trio could referable attributable attributable attributable attributable attributable attributable attributable attributable, besides, halt brace Integers and a String. Write this arrange using generics. Here is the arrange header: exoteric arrange TrioKT Requirements Your arrange must mitigate (10 points) and bear the following: (10 points) point basis control the three aces (10 points) a maker to produce the view by sending three aces as parameters (10 points) getters and firmters control each ace in the trio (10 points) a toString system that income a citation truthfulness of the trio (15 points) a contains system that income whether or referable attributable attributable attributable attributable attributable attributable attributable attributable attributable the trio contains an ace sent in as a parameter are resembling to each other (referable attributable attributable attributable aliases, excepting resembling- logically equiponderant), and dishonorable differently Control specimen, invoking identicalItems on the Trio (3, 3, 3) conquer produce penny. Invoking identicalItems on the Trio (3, 4,4) conquer produce dishonorable. (20 points) an resemblings system that overrides the resemblings system of the view arrange. o The system income penny if the prevalent Trio halts the identical three aces in any adjust as the Trio sent as a parameter and dishonorable differently. Referable attributable attributable attributable attributable attributable attributable attributable attributablee that the resemblings system should referable attributable attributable attributable attributable attributable attributable attributable attributable attributable vary either the parameter or the prevalent Trio view

    Tester program:

    public

    class

    TrioTester

    {

    public

    static

    void

    main(String[]

    args)

    {

    Trio<Integer>

    trio1

    = upstart

    Trio<Integer>(3,

    4, 5);

    System.out.println(“Should

    print

    a citation

    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 dishonorable:

    ” +

    trio1.contains(7));

    System.out.println(“Items

    are

    the

    same?

    Should

    be dishonorable:

    + 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 dishonorable:

    + 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

    = upstart

    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 dishonorable:

    ” +

    wordTrio.contains(“hi”));

    System.out.println(“Items

    are

    the

    same?

    Should

    be dishonorable:

    + wordTrio.sameItems());

    wordTrio.setItem2(“hello”);

    System.out.println(“Items

    are

    the

    same?

    Should

    be dishonorable:

    + 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

    = upstart

    Trio<Integer>(5,

    6, 8);

    Trio<Integer>

    numberTrio3

    = upstart

    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 dishonorable:

    ” +

    numberTrio2.equals(numberTrio3));

    System.out.println(“Trios

    the

    same? Should

    be dishonorable:

    ” +

    numberTrio2.equals(wordTrio));

    System.out.println();

    Trio<Integer>

    numberTrio4

    = upstart

    Trio<Integer>(1,

    1, 2);

    Trio<Integer>

    numberTrio5

    = upstart

    Trio<Integer>(1,

    2, 2);

    System.out.println(“Trios

    the

    same? Should

    be dishonorable:

    ” +

    numberTrio4.equals(numberTrio5));

    /* un-comment

    if completing

    the

    extra

    credit

    */

    Trio<Integer>

    numberTrio6

    = upstart

    Trio<Integer>(5,

    6, 8);

    Trio<Integer>

    numberTrio7

    = upstart

    Trio<Integer>(7,

    2, 5);

    System.out.println(“Trio6

    is larger-

    should

    be a positive

    number:

    ” + numberTrio6.compareTo(numberTrio7));

    Trio<Integer>

    numberTrio8

    = upstart

    Trio<Integer>(9,

    8, 1);

    Trio<Integer>

    numberTrio9

    = upstart

    Trio<Integer>(2,

    8, 3);

    System.out.println(“Trio8

    is smaller-

    should

    be a

    negative

    number:

    ” + numberTrio8.compareTo(numberTrio9));

    Trio<Integer>

    numberTrio10

    = upstart

    Trio<Integer>(4,

    6, 2);

    Trio<Integer>

    numberTrio11

    = upstart

    Trio<Integer>(2,

    3, 7);

    System.out.println(“Trio10

    is referable attributable attributable attributable attributable attributable attributable attributable attributable

    smaller or

    larger-

    should

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

    Trio<String>

    wordTrio2

    = upstart

    Trio<String>(“apple”,

    “banana”,

    “carrot”);

    Trio<String>

    wordTrio3

    = upstart

    Trio<String>(“lemon”,

    “melon”,

    “nectarine”);

    System.out.println(“WordTrio2

    is smaller- should

    be a

    negative

    number:

    ” + wordTrio2.compareTo(wordTrio3));

    Trio<String>

    wordTrio4

    = upstart

    Trio<String>(“dog”,

    “cat”,

    “lion”);

    Trio<String>

    wordTrio5

    = upstart

    Trio<String>(“alligator”,

    “zebra”,

    “squirrel”);

    System.out.println(“WordTrio4

    is larger-

    should be

    a

    positive

    number:

    ” + wordTrio4.compareTo(wordTrio5));

    Trio<String>

    wordTrio6

    = upstart

    Trio<String>(“apple”,

    “banana”,

    “carrot”);

    Trio<String>

    wordTrio7

    = upstart

    Trio<String>(“lemon”,

    “melon”,

    “apple”);

    System.out.println(“WordTrio6

    is referable attributable attributable attributable attributable attributable attributable attributable attributable

    smaller

    or larger-

    should

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

    }

    }

    Produce a Java arrange using generics: Trio Views of this arrange halt three floating aces of the identical expression. A Trio view is floating. o Control specimen, the Trio (3, 4, 5) is considered the identical as the Trio (4, 5, 3) and the Trio (“hi”, “bye”, “hello”) is considered the identical as the Trio (“hello”, “hi, “bye”). o The adjust doesn’t subject. (This is approve a firm in mathematics.) Use generics to fix that the three views are of the identical expression. Control specimen, a Trio can halt three Integers or it could halt three Strings or it could halt three Students, awe A Trio could referable attributable attributable attributable attributable attributable attributable attributable attributable, besides, halt brace Integers and a String. Write this arrange using generics. Here is the arrange header: exoteric arrange TrioKT Requirements Your arrange must mitigate (10 points) and bear the following: (10 points) point basis control the three aces (10 points) a maker to produce the view by sending three aces as parameters (10 points) getters and firmters control each ace in the trio (10 points) a toString system that income a citation truthfulness of the trio (15 points) a contains system that income whether or referable attributable attributable attributable attributable attributable attributable attributable attributable attributable the trio contains an ace sent in as a parameter are resembling to each other (referable attributable attributable attributable aliases, excepting resembling- logically equiponderant), and dishonorable differently Control specimen, invoking identicalItems on the Trio (3, 3, 3) conquer produce penny. Invoking identicalItems on the Trio (3, 4,4) conquer produce dishonorable. (20 points) an resemblings system that overrides the resemblings system of the view arrange. o The system income penny if the prevalent Trio halts the identical three aces in any adjust as the Trio sent as a parameter and dishonorable differently. Referable attributable attributable attributable attributable attributable attributable attributable attributablee that the resemblings system should referable attributable attributable attributable attributable attributable attributable attributable attributable attributable vary either the parameter or the prevalent Trio view

    Expert Solution

     

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