Write the class Staff. It contains methods that manipulate an ArrayList of Strin
ID: 3856070 • Letter: W
Question
Write the class Staff. It contains methods that manipulate an ArrayList of Strings representing the names of staff members. The constructor takes an ArrayList of String names as a parameter. In addition to the constructor, you need to implement the following methods
The methods
1. public boolean equals(Staff other) - Determines if the other Staff contains all the same elements in the same order as this Staff
2. public boolean sameContents(Staff other) - Determines if the other Staff and this Staff contain exactly the same elements but in any order
3. public void replaceVowelsWith(String text) Replaces each vowel in every ArrayList element with the replacement value text. Assume the vowels are aeiouyAEIOUY
4. public String mostVowels() Gets the staff member whose name has the most vowels. If more than one has that number of vowels, return the first. Return null if the list is empty. Assume the vowels are aeiouAEIOU
5. public String toString() gets a string represent ion using ArrayList's toString method
(set different vowel strings for both of aeiouyAEIOUY and aeiouAEIOU)
(This is all the imformation I have here,please read the question carefully)
---------------------------------------------------------------------------------------------------------------------
StaffTester.java
Explanation / Answer
Here is the code for Staff.java:
import java.util.*;
class Staff
{
ArrayList al;
Staff(ArrayList al)
{
al = new ArrayList<>();
for(int i = 0; i < al.size(); i++)
this.al.add(al.get(i));
}
public void add(String staff)
{
al.add(staff);
}
// 1. public boolean equals(Staff other) - Determines if the other Staff contains all
// the same elements in the same order as this Staff
public boolean equals(Staff other)
{
for(int i = 0; i < al.size(); i++)
if(al.get(i) != other.al.get(i))
return false;
return true;
}
// 2. public boolean sameContents(Staff other) - Determines if the other Staff and this
// Staff contain exactly the same elements but in any order
public boolean sameContents(Staff other)
{
for(int i = 0; i < al.size(); i++)
if(!al.contains(other.al.get(i)))
return false;
return true;
}
// 3. public void replaceVowelsWith(String text) Replaces each vowel in every ArrayList
// element with the replacement value text. Assume the vowels are aeiouyAEIOUY
public void replaceVowelsWith(String text)
{
for(int i = 0; i < al.size(); i++)
{
String temp = (String)al.get(i);
String tempNew = "";
for(int j = 0; j < temp.length(); j++)
{
char ch = temp.charAt(j);
switch(ch)
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U': tempNew += text; break;
default : tempNew += ch;
}
}
al.set(i, tempNew);
}
}
// 4. public String mostVowels() Gets the staff member whose name has the most vowels.
// If more than one has that number of vowels, return the first. Return null if the
// list is empty. Assume the vowels are aeiouAEIOU
public String mostVowels()
{
int index = 0;
int mostVowelsCount = 0;
for(int i = 0; i < al.size(); i++)
{
int vowelsCount = 0;
String temp = (String)al.get(i);
String tempNew = "";
for(int j = 0; j < temp.length(); j++)
{
char ch = temp.charAt(j);
switch(ch)
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U': vowelsCount++; break;
}
}
if(vowelsCount > mostVowelsCount)
{
mostVowelsCount = vowelsCount;
index = i;
}
}
return (String)al.get(index);
}
// 5. public String toString() gets a string represent ion using ArrayList's toString
// method (set different vowel strings for both of aeiouyAEIOUY and aeiouAEIOU)
public String toString()
{
String output = "";
for(int i = 0; i < al.size(); i++)
output += al.get(i) + " ";
return output;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.