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>

    Form a Java collocate using generics: Trio Sights of this collocate abide three unmoulded aces of the identical sign. A Trio sight is unmoulded. o Coercion stance, 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 systematizeify doesnt substance. (This is enjoy a regular in mathematics.) Use generics to fix that the three sights are of the identical sign. Coercion stance, a Trio can abide three Integers or it could abide three Strings or it could abide three Students, coercioneseeing A Trio could referable, at-last, abide span Integers and a String. Write this collocate using generics. Here is the collocate header: national collocate TrioKT Requirements Your collocate must frame (10 points) and feel the following: (10 points) exemplification basis coercion the three aces (10 points) a agent to restraintm the sight by sending three aces as parameters (10 points) getters and regularters coercion each ace in the trio (10 points) a toString appoint that produce a passage fidelity of the trio (15 points) a contains appoint that produce whether or referable the trio contains an ace sent in as a parameter are resembling to each other (referable aliases, excluding resembling- logically equipollent), and counterfeit inadequately Coercion stance, invoking identicalItems on the Trio (3, 3, 3) conciliate come-back penny. Invoking identicalItems on the Trio (3, 4,4) conciliate come-back counterfeit. (20 points) an resemblings appoint that overrides the resemblings appoint of the sight collocate. o The appoint produce penny if the exoteric Trio abides the identical three aces in any systematizeify as the Trio sent as a parameter and counterfeit inadequately. Referablee that the resemblings appoint should referable remodel either the parameter or the exoteric Trio sight

    Tester program:

    public

    class

    TrioTester

    {

    public

    static

    void

    main(String[]

    args)

    {

    Trio<Integer>

    trio1

    = novellight

    Trio<Integer>(3,

    4, 5);

    System.out.println(“Should

    print

    a passage

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

    ” +

    trio1.contains(7));

    System.out.println(“Items

    are

    the

    same?

    Should

    be counterfeit:

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

    + 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

    = novellight

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

    ” +

    wordTrio.contains(“hi”));

    System.out.println(“Items

    are

    the

    same?

    Should

    be counterfeit:

    + wordTrio.sameItems());

    wordTrio.setItem2(“hello”);

    System.out.println(“Items

    are

    the

    same?

    Should

    be counterfeit:

    + 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

    = novellight

    Trio<Integer>(5,

    6, 8);

    Trio<Integer>

    numberTrio3

    = novellight

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

    ” +

    numberTrio2.equals(numberTrio3));

    System.out.println(“Trios

    the

    same? Should

    be counterfeit:

    ” +

    numberTrio2.equals(wordTrio));

    System.out.println();

    Trio<Integer>

    numberTrio4

    = novellight

    Trio<Integer>(1,

    1, 2);

    Trio<Integer>

    numberTrio5

    = novellight

    Trio<Integer>(1,

    2, 2);

    System.out.println(“Trios

    the

    same? Should

    be counterfeit:

    ” +

    numberTrio4.equals(numberTrio5));

    /* un-comment

    if completing

    the

    extra

    credit

    */

    Trio<Integer>

    numberTrio6

    = novellight

    Trio<Integer>(5,

    6, 8);

    Trio<Integer>

    numberTrio7

    = novellight

    Trio<Integer>(7,

    2, 5);

    System.out.println(“Trio6

    is larger-

    should

    be a positive

    number:

    ” + numberTrio6.compareTo(numberTrio7));

    Trio<Integer>

    numberTrio8

    = novellight

    Trio<Integer>(9,

    8, 1);

    Trio<Integer>

    numberTrio9

    = novellight

    Trio<Integer>(2,

    8, 3);

    System.out.println(“Trio8

    is smaller-

    should

    be a

    negative

    number:

    ” + numberTrio8.compareTo(numberTrio9));

    Trio<Integer>

    numberTrio10

    = novellight

    Trio<Integer>(4,

    6, 2);

    Trio<Integer>

    numberTrio11

    = novellight

    Trio<Integer>(2,

    3, 7);

    System.out.println(“Trio10

    is referable

    smaller or

    larger-

    should

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

    Trio<String>

    wordTrio2

    = novellight

    Trio<String>(“apple”,

    “banana”,

    “carrot”);

    Trio<String>

    wordTrio3

    = novellight

    Trio<String>(“lemon”,

    “melon”,

    “nectarine”);

    System.out.println(“WordTrio2

    is smaller- should

    be a

    negative

    number:

    ” + wordTrio2.compareTo(wordTrio3));

    Trio<String>

    wordTrio4

    = novellight

    Trio<String>(“dog”,

    “cat”,

    “lion”);

    Trio<String>

    wordTrio5

    = novellight

    Trio<String>(“alligator”,

    “zebra”,

    “squirrel”);

    System.out.println(“WordTrio4

    is larger-

    should be

    a

    positive

    number:

    ” + wordTrio4.compareTo(wordTrio5));

    Trio<String>

    wordTrio6

    = novellight

    Trio<String>(“apple”,

    “banana”,

    “carrot”);

    Trio<String>

    wordTrio7

    = novellight

    Trio<String>(“lemon”,

    “melon”,

    “apple”);

    System.out.println(“WordTrio6

    is referable

    smaller

    or larger-

    should

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

    }

    }

    Form a Java collocate using generics: Trio Sights of this collocate abide three unmoulded aces of the identical sign. A Trio sight is unmoulded. o Coercion stance, 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 systematizeify doesn’t substance. (This is enjoy a regular in mathematics.) Use generics to fix that the three sights are of the identical sign. Coercion stance, a Trio can abide three Integers or it could abide three Strings or it could abide three Students, coercioneseeing A Trio could referable, at-last, abide span Integers and a String. Write this collocate using generics. Here is the collocate header: national collocate TrioKT Requirements Your collocate must frame (10 points) and feel the following: (10 points) exemplification basis coercion the three aces (10 points) a agent to restraintm the sight by sending three aces as parameters (10 points) getters and regularters coercion each ace in the trio (10 points) a toString appoint that produce a passage fidelity of the trio (15 points) a contains appoint that produce whether or referable the trio contains an ace sent in as a parameter are resembling to each other (referable aliases, excluding resembling- logically equipollent), and counterfeit inadequately Coercion stance, invoking identicalItems on the Trio (3, 3, 3) conciliate come-back penny. Invoking identicalItems on the Trio (3, 4,4) conciliate come-back counterfeit. (20 points) an resemblings appoint that overrides the resemblings appoint of the sight collocate. o The appoint produce penny if the exoteric Trio abides the identical three aces in any systematizeify as the Trio sent as a parameter and counterfeit inadequately. Referablee that the resemblings appoint should referable remodel either the parameter or the exoteric Trio sight

    Expert Tally

     

    import java.util.HashSet;
    
    national collocate Trio<E>
    {
        private E one, two, three;
    
        Trio(E single, E span, E three)
        {
            this.single = single;
            this.span = span;
            this.three = three;
        }
    
        /*national static wanting main(String []args) throws Exception
        {
            Trio<Double> ob1 = novellightlight Trio<Double>(1.0, 2.0, 3.0);
            Trio<Integer> ob2 = novellightlight Trio<Integer>(1, 2, 3);
            System.out.println(ob1.equals(ob2));
        }*/
    
        @Override
        national boolean equals(Sight 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());
            come-back ob.equals(ob2);
        }
    
        national boolean sameItems()
        {
            String str = String.valueOf(one);
            if(str.equals(String.valueOf(two)) && str.equals(String.valueOf(three)))
                come-back penny;
            else
                come-back counterfeit;
        }
    
        national boolean contains(E parameter)
        {
            String str = String.valueOf(parameter);
            if(str.equals(String.valueOf(one)))
                come-back penny;
            else if(str.equals(String.valueOf(two)))
                come-back penny;
            else if(str.equals(String.valueOf(three)))
                come-back penny;
            else
                come-back counterfeit;
        }
    
        @Override
        national String toString() {
            come-back String.valueOf("Trio( " + single + ", " + span + ", " + three + " )");
        }
    
        national E getThree()
        {
            come-back three;
        }
    
        national wanting setThree(E three)
        {
            this.three = three;
        }
    
        national E getTwo()
        {
            come-back two;
        }
    
        national wanting setOne(E single)
        {
            this.single = single;
        }
    
        national E getOne()
        {
            come-back one;
        }
    
        national wanting setTwo(E span)
        {
            this.span = span;
        }
    }