Write a program to hold some items by JAVA

User Generated

gxhrr8903

Computer Science

Description

Write a program to hold some items by Java

the detail is in the following files 

please include algorithm by comments 

Unformatted Attachment Preview

import java.util.ArrayList; public class TrioTester { public static void main(String[] args) { Trio numberTrio = new Trio(3, 4, 5); /* * un-comment the line of code below and it should cause a compiler error because trio1 should only accept Strings */ //numberTrio.setItem2("apple"); System.out.println("***TESTING TOSTRING METHOD***"); System.out.println("Should print a text representation of the Trio that contains 3, 4, 5:"); System.out.println(numberTrio); System.out.println("\n***TESTING GETTERS AND SETTERS METHOD***"); System.out.println("Item 1 should be 3: " + numberTrio.getItem1()); System.out.println("Item 2 should be 4: " + numberTrio.getItem2()); System.out.println("Item 3 should be 5: " + numberTrio.getItem3()); numberTrio.setItem1(6); numberTrio.setItem2(7); numberTrio.setItem3(8); System.out.println("Item 1 should be 6: " + numberTrio.getItem1()); System.out.println("Item 2 should be 7: " + numberTrio.getItem2()); System.out.println("Item 3 should be 8: " + numberTrio.getItem3()); System.out.println("\n***TESTING HASDUPLICATES METHOD***"); System.out.println("Trio contains duplicates? false: " + numberTrio.hasDuplicates()); numberTrio.setItem2(6); System.out.println("Trio contains duplicates? true: " + numberTrio.hasDuplicates()); numberTrio.setItem3(6); System.out.println("Trio contains duplicates? true: " + numberTrio.hasDuplicates()); System.out.println("\n***TESTING COUNT METHOD***"); numberTrio.setItem1(0); numberTrio.setItem2(2); numberTrio.setItem3(3); System.out.println("Trio contains how many 1s? 0: " numberTrio.count(1)); numberTrio.setItem1(1); System.out.println("Trio contains how many 1s? 1: " numberTrio.count(1)); numberTrio.setItem2(1); System.out.println("Trio contains how many 1s? 2: " numberTrio.count(1)); numberTrio.setItem3(1); System.out.println("Trio contains how many 1s? 3: " numberTrio.count(1)); + + + + System.out.println("\n***TESTING RESET METHOD***"); numberTrio.reset(43); System.out.println("Should print a text representation of the Trio that contains 43, 43, 43:"); System.out.println(numberTrio); System.out.println("Trio contains how many 43s? 3: " + numberTrio.count(43)); System.out.println("Trio contains duplicates? true: " + numberTrio.hasDuplicates()); Trio wordTrio = new Trio("hello"); wordTrio.setItem2("goodbye"); wordTrio.setItem3("nice knowing you"); /* * un-comment the line of code below and it should cause a compiler error because wordTrio should only accept Strings */ //wordTrio.setItem2(3); System.out.println("\n***TESTING COUNT METHOD***"); String testString1 = new String("hello"); String testString2 = new String("HELLO"); System.out.println("Trio contains how many hello? 1: " + wordTrio.count(testString1)); System.out.println("Trio contains how many HELLO? 0: " + wordTrio.count(testString2)); System.out.println("\n***TESTING EQUALS METHOD***"); // no duplicates, same items in same order Trio wordTrio2 = new Trio(new String("a"), new String("b"), new String("c")); Trio wordTrio3 = new Trio(new String("a"), new String("b"), new String("c")); System.out.println("Trios the same? true: " + wordTrio2.equals(wordTrio3)); // no duplicates, same items in different order Trio numberTrio2 = new Trio(5, 6, 8); Trio numberTrio3 = new Trio(8, 5, 6); System.out.println("Trios the same? true: " + numberTrio2.equals(numberTrio3)); // different items numberTrio2.setItem2(5); System.out.println("Trios the same? false: " + numberTrio2.equals(numberTrio3)); System.out.println("Trios the same? false: " + numberTrio2.equals(wordTrio)); System.out.println(); // same items, but different items are duplicated Trio numberTrio4 = new Trio(1, 1, 2); Trio numberTrio5 = new Trio(1, 2, 2); System.out.println("Trios the same? false: " + numberTrio4.equals(numberTrio5)); // same items, but different items are duplicated Trio numberTrio14 = new Trio(1, 2, 1); Trio numberTrio15 = new Trio(1, 1, 2); System.out.println("Trios the same? true : " + numberTrio14.equals(numberTrio15)); } } A Trio is a class representing a data structure that holds a group of three items.A Trio object has the following characteristics: • Trios hold three items of the same type. o For example, a Trio could 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. A Trio can contain duplicates. • A Trio's items are unordered. • The order doesn't matter. (This is like a set in mathematics. A Trio is different from a set, however, because a Trio can contain duplicates.) 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"). Here is the class header: public class Trio • Your class must compile, use generics, and have the following: (10 points) instance data variables to store the three items contained in the Trio (10 points) two constructors: o one constructor takes in the three items as parameters o a second constructor takes in a single item and the Trio will then consist of three of these items o for full credit, reduce duplicated code in the constructors and follow best practices for overloading constructors. (5 points) getters and setters for each item in the trio (5 points) a toString method that returns a text representation of the trio (10 points) a method called reset that takes in one item as a parameter and modifies the Trio to hold three of that item (15 points) a method called count that returns a count of how many times an item is in the Trio (15 points) a method called hasDuplicates that returns true if the at least two items within the Trio are the same as each other. . Note this does not determine duplicates based on whether any of the items are aliases, but whether any of the items are equal-logically equivalent to each other. • (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 (logically equivalent) three items in any order as the Trio sent as a parameter and false otherwise. The equals method should not alter either the current Trio object or the Trio object passed in as a parameter. o Be sure to test your method with different cases, particularly cases where the Trios have duplicate items! (10 points) Style: Your class should follow Java coding conventions and best practices. Follow naming conventions for variables, classes, and methods. Reduce duplicated code. .
Purchase answer to see full attachment
Explanation & Answer:
Worksheet
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

Explanation & Answer

At...


Anonymous
I was having a hard time with this subject, and this was a great help.

Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4

Similar Content

Related Tags