In: Computer Science
Write a collection class named "Jumbler". Jumbler takes in an optional list of strings as a parameter to the constuctor with various strings. Jumbler stores random strings and we access the items based on the methods listed below.
Jumbler supports the following methods:
add() : Add a string to Jumbler
get() : return a random string from Jumbler
max() : return the largest string in the Jumbler based on the
length of the strings in the Jumbler.
iterator: returns an iterator to be able to iterate through all the
strings in the jumbler.
Program Code Screenshot

Sample Output

Program Code to Copy
import java.util.*;
class Jumbler{
    List<String> list;
    Jumbler(List<String> list){
        this.list = list;
    }
    public void add(String s){
        list.add(s);
    }
    public String get(){
        Random r = new Random();
        //Return a random string
        return list.get(r.nextInt(list.size()));
    }
    public String max(){
        String ans = list.get(0);
        //Loop through all strings. Update max
        for(String x : list){
            if(x.length()>ans.length()){
                ans = x;
            }
        }
        return ans;
    }
    public Iterator iterator(){
        return list.iterator();
    }}
class Main{
    public static void main(String[] args) {
        Jumbler jumbler = new Jumbler(new ArrayList(Arrays.asList(new String[]{"a","bc","def"})));
        System.out.println(jumbler.get());
        System.out.println(jumbler.get());
        System.out.println(jumbler.get());
        System.out.println("Maximum is "+jumbler.max());
        Iterator iterator = jumbler.iterator();
        System.out.println("Iterate using iterator");
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }
}