Complete the class Vocab which has an arraylist of strings a

Complete the class Vocab which has an arraylist of strings as an instance variable and methods to act on the array list. Call the instance variable words

Provide a constructor that takes a ArrayList as a parameter and assigns it to the instance variable

Provide methods:

longest() gets the longest word or null if the array list is empty

averageLength() gets the average length of the words in the array list or 0 if the arraylist is empty

remove(String target) removes all occurrence of the given word

toString() returns a string representation of this Vocab object (already done for you)

Note: There is a second version of the remove method that will be helpf ul: to remove a specific object named target

words.remove(target);

Provide Javadoc

VocabTester.java

/**
* Models a list of vocabulary words
*/
public class Vocab
{


@Override
public String toString()
{
return words.toString();
}
}

Solution

Please follow the code and comments for description :

CODE :

VocabTester.java :

import java.util.ArrayList; // required imports

public class VocabTester { // calss to run the code

public static void main(String[] args) { // driver method
ArrayList theList = new ArrayList<>(); // creating a list to save the data
Vocab empty = new Vocab(theList);// creating a object and passing the list to the class
System.out.println(\"Initial List is : \"+empty); // print the initial status of the list
  
theList.add(\"polymorphism\"); // add data to the list
theList.add(\"interface\");
theList.add(\"encapsulation\");
theList.add(\"interface\");
theList.add(\"object-oriented\");
theList.add(\"java\");
theList.add(\"programming\");

Vocab vocab = new Vocab(theList); // create an object for the class to be used here
vocab.remove(\"interface\"); // calling the method to remove the string from the list
System.out.println(\"The Resultant List After Removing the Desired String is : \"+vocab); // print the result to console
System.out.println(\"The Longest String for the List Given is : \"+vocab.longest()); // calling the method to get the longest string of the list
System.out.println(\"The Average Length of the List Entered is : \"+vocab.averageLength()); // calling the method to return the average length of the list
}
}


Vocab.java :

import java.util.ArrayList;

public class Vocab { // class to run the code

ArrayList<String> words = new ArrayList(); // instnace variables of the data

Vocab(ArrayList theList) { // parameterised constructor
words = theList; // getting the data to the varaible
}

void remove(String name) { // method to remove the string entered
for ( int i = 0; i < words.size(); i++){ // iterating ove tthe loop to check for the string
String tempName = words.get(i); // getting the index value
if(tempName.equals(name)) { // checking for the string in the index
words.remove(i); // if correct removing the string data
}
}
}

String longest() { // method to return the largest or the longest string
int largestString = words.get(0).length(); // required iniitialisations
int index = 0;

if (words.isEmpty()) { // checking for the null characters in the list
return \"NULL\";
}
for (int i = 0; i < words.size(); i++) { // iterating over the loop to get the data

if (words.get(i).length() > largestString) { // checking for the length of the each string data
largestString = words.get(i).length();
index = i; // getting the index values
}
}
return words.get(index); // return the index values
}

double averageLength() { // method to return the avrage length of the list

if (words.isEmpty()) { // checking for the null characters in the list
return 0;
}

int length = 0, count = 0;
for (String str : words) { // getting the string data
count++; //increment the count
length = length + str.length(); // gwtting the length summed up
}
length = length + words.size() - 1; // saving the data to a variable
return (length / count); // return the average length
}

@Override
public String toString() { // method to return the string of the list
return words.toString();
}
}


OUTPUT :


Initial List is : []
The Resultant List After Removing the Desired String is : [polymorphism, encapsulation, object-oriented, java, programming]
The Longest String for the List Given is : object-oriented
The Average Length of the List Entered is : 11.0


Hope this is helpful.

Complete the class Vocab which has an arraylist of strings as an instance variable and methods to act on the array list. Call the instance variable words Provid
Complete the class Vocab which has an arraylist of strings as an instance variable and methods to act on the array list. Call the instance variable words Provid
Complete the class Vocab which has an arraylist of strings as an instance variable and methods to act on the array list. Call the instance variable words Provid

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site