In: Computer Science
A Java 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)
---------------------------------------------------------------------------------------------------------------------
Use the following file:
StaffTester.java
import java.util.ArrayList; /** * Tester */ public class StaffTester { public static void main(String[] args) { ArrayList list = new ArrayList<>(); list.add("Goodyear"); list.add("Sathyanantha"); list.add("Keomahavong"); list.add("Addison"); list.add("Smith"); ArrayList list2 = new ArrayList<>(); list2.add("^aeiouy$"); list2.add("^AEIOUY$"); Staff lister = new Staff(list); Staff lister2 = new Staff(list2); Staff empty = new Staff(new ArrayList()); //test an empty list lister.replaceVowelsWith("*"); System.out.println(lister); System.out.println("Expected: [G**d***r, S*th**n*nth*, K**m*h*v*ng, *dd*s*n, Sm*th]"); lister2.replaceVowelsWith("#"); System.out.println(lister2); System.out.println("Expected: [^######$, ^######$]"); empty.replaceVowelsWith("*"); System.out.println(empty); System.out.println("Expected: []"); //testing equals list.clear(); list.add("Goodyear"); list.add("Sathyanantha"); list.add("Keomahavong"); list.add("Addison"); list.add("Smith"); lister = new Staff(list); list2.clear(); list2.add("Goodyear"); list2.add("Sathyanantha"); list2.add("Keomahavong"); list2.add("Addison"); list2.add("Smith"); lister2 = new Staff(list2); System.out.println("equal?: " + lister.equals(lister2)); System.out.println("Expected: true"); list2.remove(list2.size() - 1); System.out.println("equal?: " + lister.equals(lister2)); System.out.println("Expected: false"); System.out.println("contains?: " + lister.sameContents(lister2)); System.out.println("Expected: false"); list2.add(1, "Smith"); System.out.println("contains?: " + lister.sameContents(lister2)); System.out.println("Expected: true"); list2.set(1, "smith"); System.out.println("equal?: " + lister.equals(lister2)); System.out.println("Expected: false"); System.out.println("equal?: " + empty.equals(empty)); System.out.println("Expected: true"); System.out.println(lister.mostVowels()); System.out.println("Expected: Keomahavong"); list.remove(2); System.out.println(lister.mostVowels()); System.out.println("Expected: Goodyear"); System.out.println(empty.mostVowels()); System.out.println("Expected: null"); } }
import java.util.ArrayList;
/**
* Tester
*/
class Staff
{
ArrayList list;
Staff(ArrayList list)
{
this.list=list;
}
public boolean equals(Staff other)
{
if(list.size()==other.list.size())
for(int i=0; i<list.size();i++)
{
if(!list.get(i).equals(other.list.get(i)))
return false;
}
else return false;
return true;
}
public boolean sameContents(Staff other)
{
boolean f=false;
if(list.size()!=other.list.size()) return false;
for(int i=0; i<list.size();i++)
{
f=false;
for(int j=0; j<list.size();j++)
{
if(list.get(i).equals(other.list.get(j)))
{
f=true;
break;
}
}
if(!f) return false;
}
return true;
}
public String toString()
{
String s="[";
for(int i=0; i<list.size();i++)
{
if(i!=list.size()-1)
s=s+list.get(i)+", ";
else
s=s+list.get(i);
}
s=s+"]";
return s;
}
public void replaceVowelsWith(String text)
{
String l;
for(int i=0; i<list.size();i++)
{
l=list.get(i).toString();
l=l.replaceAll("a",text);
l=l.replaceAll("e",text);
l=l.replaceAll("i",text);
l=l.replaceAll("o",text);
l=l.replaceAll("u",text);
l=l.replaceAll("y",text);
l=l.replaceAll("A",text);
l=l.replaceAll("E",text);
l=l.replaceAll("I",text);
l=l.replaceAll("O",text);
l=l.replaceAll("U",text);
l=l.replaceAll("Y",text);
list.set(i,l);
}
}
public String mostVowels()
{
if(list.size()==0) return "";
String l=""; int ind=0, count,h=0;
char[] Chararray;
for(int i=0; i<list.size();i++)
{
l=list.get(i).toString();
Chararray= l.toCharArray();
count = 0;
for (char ch : Chararray) {
switch (ch)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
count++;
break;
default:
}
}
if(count > h){
h=count; ind= i;
}
}
return list.get(ind).toString();
}
}
public class StaffTester
{
public static void main(String[] args)
{
ArrayList list = new ArrayList<>();
list.add("Goodyear");
list.add("Sathyanantha");
list.add("Keomahavong");
list.add("Addison");
list.add("Smith");
ArrayList list2 = new ArrayList<>();
list2.add("^aeiouy$");
list2.add("^AEIOUY$");
Staff lister = new Staff(list);
Staff lister2 = new Staff(list2);
Staff empty = new Staff(new ArrayList()); //test an empty
list
lister.replaceVowelsWith("*");
System.out.println(lister);
System.out.println("Expected: [G**d***r, S*th**n*nth*, K**m*h*v*ng,
*dd*s*n, Sm*th]");
lister2.replaceVowelsWith("#");
System.out.println(lister2);
System.out.println("Expected: [^######$, ^######$]");
empty.replaceVowelsWith("*");
System.out.println(empty);
System.out.println("Expected: []");
//testing equals
list.clear();
list.add("Goodyear");
list.add("Sathyanantha");
list.add("Keomahavong");
list.add("Addison");
list.add("Smith");
lister = new Staff(list);
list2.clear();
list2.add("Goodyear");
list2.add("Sathyanantha");
list2.add("Keomahavong");
list2.add("Addison");
list2.add("Smith");
lister2 = new Staff(list2);
System.out.println("equal?: " + lister.equals(lister2));
System.out.println("Expected: true");
list2.remove(list2.size() - 1);
System.out.println("equal?: " + lister.equals(lister2));
System.out.println("Expected: false");
System.out.println("contains?: " +
lister.sameContents(lister2));
System.out.println("Expected: false");
list2.add(1, "Smith");
System.out.println("contains?: " +
lister.sameContents(lister2));
System.out.println("Expected: true");
list2.set(1, "smith");
System.out.println("equal?: " + lister.equals(lister2));
System.out.println("Expected: false");
System.out.println("equal?: " + empty.equals(empty));
System.out.println("Expected: true");
System.out.println(lister.mostVowels());
System.out.println("Expected: Keomahavong");
list.remove(2);
System.out.println(lister.mostVowels());
System.out.println("Expected: Goodyear");
System.out.println(empty.mostVowels());
System.out.println("Expected: null");
}
}