Question

In: Computer Science

Write a collection class named "Jumbler". Jumbler takes in an optional list of strings as a...

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.

Solutions

Expert Solution

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

Related Solutions

LANGUAGE PYTHON 3.7 Write a collection class named "Jumbler". Jumbler takes in an optional list of...
LANGUAGE PYTHON 3.7 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....
Write a function that takes a list of strings and outputs them, one per line, in a rectangular frame.
python:Read the list of strings as separate lines from a text file whose file name is provided at run-time (Dr. Hanna's input file E29.txtPreview the document).Write a function that takes a list of strings and outputs them, one per line, in a rectangular frame. Forexample the list[ "Hello", "World", "in", "a", "frame" ]gets output as********** Hello ** World ** in ** a ** frame **********
Write a class named GroceryList that represents a list of items to buy from the market,...
Write a class named GroceryList that represents a list of items to buy from the market, and another class named GroceryItemOrder that represents a request to purchase a particular item in a given quantity (example: four boxes of cookies). It also has client class called GroceryClient which creates objects of GroceryList and GroceryItemOrder. (For this assignment, you will have to submit 3 .java files: one for each class and 3 .class files associated with these .java files. So in total...
Design a function in python that takes a list of strings as an argument and determines...
Design a function in python that takes a list of strings as an argument and determines whether the strings in the list are getting decreasingly shorter from the front to the back of the list
Create a class named Ship with a field that stores a collection of Shippable things and...
Create a class named Ship with a field that stores a collection of Shippable things and another that stores a maximum weight capacity. Include a constructor with a parameter for the max weight, and that gives the Ship an empty collection of Shippables. (Javascript)
Create a class named Ship with a field that stores a collection of Shippable things and...
Create a class named Ship with a field that stores a collection of Shippable things and another that stores a maximum weight capacity. Include a constructor with a parameter for the max weight, and that gives the Ship an empty collection of Shippables. (Javascript)
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...
please solve by utilizing python Define a function named checkOut which takes two JSON strings, each...
please solve by utilizing python Define a function named checkOut which takes two JSON strings, each representing a dictionary. The first has the following structure, { "customer" : name , "cart" : [ item1, item2, ... ] } The second has the following structure, { item1 : price1, item2 : price2, ... } The function must return a JSON string representing the following dictionary: { "customer" : name, "amount_due" : x } where x is the sum of the prices...
Write a function (in Javascript) called longestMorseCodeWords which takes in an array of strings. The output...
Write a function (in Javascript) called longestMorseCodeWords which takes in an array of strings. The output of longestMorseCodeWords should be an array of the strings that were passed in, but ordered by the length of their Morse Code equivalent in descending order. If the length of Morse Code is equal, order the words alphabetically.For convenience, the full table for the 26 letters of the English alphabet is given below: [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."] let words = ["gin", "zen", "gig", "msg"] longestMorseCodeWords(words) The Morse...
Write a program in C++ that will make changes in the list of strings by modifying...
Write a program in C++ that will make changes in the list of strings by modifying its last element. Your program should have two functions: 1. To change the last element in the list in place. That means, without taking the last element from the list and inserting a new element with the new value. 2. To compare, you need also to write a second function that will change the last element in the list by removing it first, and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT