Question
Create an Arraylist of WordPair objects. import java.util.*; public class WordPairTest { public static void main(String[] args) { // Create an ArrayList of WordPair objects
Create an Arraylist of WordPair objects.
import java.util.*;
public class WordPairTest { public static void main(String[] args) { // Create an ArrayList of WordPair objects called pairs
pairs.add(new WordPair("hi","there")); pairs.add(new WordPair("hi","bye")); System.out.println(pairs); } }
class WordPair { private String word1; private String word2;
public WordPair(String w1, String w2) { word1 = w1; word2 = w2; } public String getFirst() { return word1; } public String getSecond() { return word2; } public String toString() { return "(" + word1 + ", " + word2 + ")"; } }
In this FRQ, you are given an array of words and you will create pairs of them by taking the first word and pairing it with all the other words, then taking the second word and pairing it with all but the first one, and so on. For example, if the word array is [Hi, there, Tyler, Sam], this figure shows how the word pairs are formed.
In the class WordPairsList below, you will write the constructor which takes the array of words and pairs them up as shown in the figure. You will need nested loops to pair each element with the rest of the elements in the list. Here is the pseudocode.
-
Initialize the allPairs list to an empty ArrayList of WordPair objects.
-
Loop through the words array for the first word in the word pair (for loop from index i = 0 to length-1)
-
Loop through the rest of the word array starting from index i+1 for the second word in the word pair (for loop from index j = i+1 to length)
-
Add the new WordPair formed from the ith word and the jth word to the allPairs ArrayList.
-
-
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started