bute: max an integer variable initialize to 10, static and final Method signatures: - public void add(int e) -> this method adds e in an array., e is not added in the array if e already exists in the array. Attribute count in increment every successful add of e. - public void display( ) -> display the contents of the array (NOTE: the array is an attribute in the class later) - public int count( ) -> returns the value of count (NOTE: count is an attribute in the class later) - public boolean isElementOf(int e) -> returns true if e is in the array false if not - public int getElement(int index) -> returns the value in array at index - public Set union(Set s) -> returns the union Example:
Write a java interface code class according to the instructions below:
1. Write an interface name InterfaceSet with the following components:
Attribute: max an integer variable initialize to 10, static and final
Method signatures:
- public void add(int e) -> this method adds e in an array., e is not added in the array if e already exists in the array. Attribute count in increment every successful add of e.
- public void display( ) -> display the contents of the array (NOTE: the array is an attribute in the class later)
- public int count( ) -> returns the value of count (NOTE: count is an attribute in the class later)
- public boolean isElementOf(int e) -> returns true if e is in the array false if not
- public int getElement(int index) -> returns the value in array at index
- public Set union(Set s) -> returns the union
Example:
current Object set[] ={1,2,3};
Set s[] = {1,3,10,5};
returns: Set union[]={1,2,3,10,5};
NOTE: current object added first, then parameter Set s.
2. Write a class Set that implements interface InterfaceSet. Class Set has the following attributes: set - an array of integer and an integer variable count. Default constructor must set the size of the array with the max variable from interface. A constructor that accepts an integer variable size and set the array size of attribute set array to size.
NOTE: remove the public modifier in defining interface and class
Output results that should be seen:
Test | Result |
---|---|
Set s1 = new Set(); s1.add(1); s1.add(2); s1.add(3); s1.add(2); s1.display(); |
1 2 3 |
Set s1 = new Set(); s1.add(10); s1.add(12); s1.add(13); s1.add(14); s1.add(15); s1.add(17); s1.add(16); s1.add(18); s1.add(20); s1.add(21); s1.add(22); System.out.println("Count: "+s1.count()); s1.display(); |
Count: 10 10 12 13 14 15 17 16 18 20 21 |
Set s1 = new Set(); s1.add(1); s1.add(2); s1.add(3); s1.add(6); System.out.println(s1.isElementOf(2)); System.out.println(s1.isElementOf(10)); |
true false |
Set s1 = new Set(); Set s2 = new Set(); Set union = null; s1.add(1); s1.add(2); s1.add(3); s1.add(4); s2.add(10); s2.add(2); s2.add(20); union = s1.union(s2); union.display(); |
1 2 3 4 10 20 |
Set s1 = new Set(); s1.add(10); s1.add(12); s1.add(13); s1.add(14); s1.add(15); ]s1.add(17); s1.add(16); s1.add(18); s1.add(20); s1.add(21); s1.add(22); Set s2 = new Set(); s2.add(41); s2.add(42); s2.add(43); s2.add(44); s2.add(45); s2.add(46); s2.add(47); s2.add(48); s2.add(49); s2.add(50); Set union = s1.union(s2); union.display(); |
10 12 13 14 15 17 16 18 20 21 41 42 43 44 45 46 47 48 49 50 |
Trending now
This is a popular solution!
Step by step
Solved in 5 steps with 2 images