Question

In: Computer Science

A Java question. Write the class Staff. It contains methods that manipulate an ArrayList of Strings...

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");
        
    }
    
}

Solutions

Expert Solution

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");
  
  
}
  
}


Related Solutions

Java 1.Write a method removeEvenLength that takes an ArrayList of Strings as a parameter and that...
Java 1.Write a method removeEvenLength that takes an ArrayList of Strings as a parameter and that removes all of the strings of even length from the list. 2. Given the following Vehicle interface and client program in the Car class: public interface Vehicle{ public void move(); } public class Car implements Vehicle{ public static void main(String args[]) Vehicle v = new Vehicle(); // vehicle declaration } The above declaration is valid? True or False? 3. Java permits a class to...
Given two ArrayLists of Strings (ArrayList<String>), write a Java method to return the higher count of...
Given two ArrayLists of Strings (ArrayList<String>), write a Java method to return the higher count of the characters in each ArrayList.  For example, if list1 has strings (“cat, “dog”, “boat”, “elephant”) and list 2 has strings (“bat”, “mat”, “port”, “stigma”), you will return the value 18.  The list 1 has 18 characters in total for all its strings combined and list2 has 16 characters for all of its strings combined.  The higher value is 18. If the character count is the same, you...
Question 3 A java source module contains the following class with the static methods main and...
Question 3 A java source module contains the following class with the static methods main and procedure1, and the instance method procedure2 (assume given the bodies of procedure1 and procedure2): public class TestQuestion3             {                         static int result, num1 = 10;                         public static void Main( String [ ] args )                         {                                     int [ ] list1 =   { 2, 4, 6, 8, 10}, list2;                                     .    .    .                         }                         static void procedure1( void )                         {                                     .   .   .                         } void procedure2( void )...
Write a java class for a Priority Queue. Use an arraylist, and include enque, deque, and...
Write a java class for a Priority Queue. Use an arraylist, and include enque, deque, and a method to get all the values of the queue. (This is not writing a file implementing the java class PriorityQueue, but rather you are writing a program that is a priority queue).
Write a simple java class that contains the following three methods: 1. isosceles -- accepts 3...
Write a simple java class that contains the following three methods: 1. isosceles -- accepts 3 integers which represent the sides of a triangle. Returns true if the triangle is isosceles and false otherwise. 2. perimeter - accepts 3 integers that represent the sides of a triangle and returns the perimeter of the triangle. 3. area -- accepts 3 integers, which represent the sides of a triangle and calculates and returns the area of the triangle. Hint: use Heron's formula....
Write methods contains and remove for the BinarySearchTree class. Use methods find and delete to do...
Write methods contains and remove for the BinarySearchTree class. Use methods find and delete to do the work
6.6 Parsing strings (Java) (1) Prompt the user for a string that contains two strings separated...
6.6 Parsing strings (Java) (1) Prompt the user for a string that contains two strings separated by a comma. (1 pt) Examples of strings that can be accepted: Jill, Allen Jill , Allen Jill,Allen Ex: Enter input string: Jill, Allen (2) Report an error if the input string does not contain a comma. Continue to prompt until a valid string is entered. Note: If the input contains a comma, then assume that the input also contains two strings. (2 pts)...
String Manipulator Write a program to manipulate strings. (Visual Studio C++) In this program take a...
String Manipulator Write a program to manipulate strings. (Visual Studio C++) In this program take a whole paragraph with punctuations (up to 500 letters) either input from user, initialize or read from file and provide following functionalities within a class: a)   Declare class Paragraph_Analysis b)   Member Function: SearchWord (to search for a particular word) c)   Member Function: SearchLetter (to search for a particular letter) d)   Member Function: WordCount (to count total words) e)   Member Function: LetterCount (ONLY to count all...
write program that develop a Java class Dictionary to support the following public methods of an...
write program that develop a Java class Dictionary to support the following public methods of an abstract data type: public class Dictionary { // insert a (key, value) pair into the Dictionary, key value must be unique, the value is associated with the key; only the last value inserted for a key will be kept public void insert(String key, String value); // return the value associated with the key value public String lookup(String key); // delete the (key, value) pair...
in java please: Create an ArrayListReview class with one data field of ArrayList and one with...
in java please: Create an ArrayListReview class with one data field of ArrayList and one with LinkedList with the generic type passed to the class. (2 point) Create a constructor that populate an array list and the LinkedList filled with the generic type through inserting new elements into the specified location index-i in the list. (2 points)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT