Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Tester program: public class TrioTester { public static void main(String[] args)

ID: 3882814 • Letter: T

Question

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

Explanation / Answer

import java.util.HashSet; public class Trio { private E one, two, three; Trio(E one, E two, E three) { this.one = one; this.two = two; this.three = three; } /*public static void main(String []args) throws Exception { Trio ob1 = new Trio(1.0, 2.0, 3.0); Trio ob2 = new Trio(1, 2, 3); System.out.println(ob1.equals(ob2)); }*/ @Override public boolean equals(Object obj1) { HashSet ob = new HashSet(); ob.add(one); ob.add(two); ob.add(three); HashSet ob2 = new HashSet(); Trio obj = (Trio) obj1; ob2.add(obj.getOne()); ob2.add(obj.getTwo()); ob2.add(obj.getThree()); return ob.equals(ob2); } public boolean sameItems() { String str = String.valueOf(one); if(str.equals(String.valueOf(two)) && str.equals(String.valueOf(three))) return true; else return false; } public boolean contains(E parameter) { String str = String.valueOf(parameter); if(str.equals(String.valueOf(one))) return true; else if(str.equals(String.valueOf(two))) return true; else if(str.equals(String.valueOf(three))) return true; else return false; } @Override public String toString() { return String.valueOf("Trio( " + one + ", " + two + ", " + three + " )"); } public E getThree() { return three; } public void setThree(E three) { this.three = three; } public E getTwo() { return two; } public void setOne(E one) { this.one = one; } public E getOne() { return one; } public void setTwo(E two) { this.two = two; } }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote