Question

Consider the following Java interface. public interface Converter<A, B> { B convert(A xs); } (a) Write...

Consider the following Java interface.
public interface Converter<A, B> {
B convert(A xs);
}

(a) Write a default method convertAll in the interface Converter that takes
ArrayList<A> xs as input and returns a new ArrayList<B>. If the ArrayList
xs is [x1, : : :, xn], the method returns [convert(x1), : : :, convert(xn)].
In your method you may assume that xs is never null. If one of the calls to
convert throws an exception, your method should not catch it.


(b) Write a class StringConverter that implements the above interface suitably.
In particular, the class is supposed to have a method
public Integer convert(String word)
that behaves as follows:
If word is the null reference, an IllegalArgumentException is thrown.
Otherwise the method returns the length of word.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Converter.java

import java.util.ArrayList;

public interface Converter<A, B> {
   B convert(A xs);
  
   default ArrayList<B> convertAll(ArrayList<A> xs) {
       ArrayList<B> converted = new ArrayList<>();
       for(A item: xs) {
           converted.add(convert(item));
       }
       return converted;
   }
}

Code image:

import java.util.ArrayList; public interface Converter<A, B> { B convert(A xs); default ArrayList<B> convertAll(ArrayList<A>

StringConverter.java

public class StringConverter implements Converter<String, Integer> {

   @Override
   public Integer convert(String xs) {
       if (xs == null)
           throw new IllegalArgumentException("INput can't be null");
       return xs.length();
   }
  
}

Code image:

public class StringConverter implements Converter<String, Integer> { @Override public Integer convert(String xs) { if (xs ==

StringConverterMain.java (added this to show how this is working)

import java.util.ArrayList;

public class StringConverterMain {

   public static void main(String[] args) {
       ArrayList<String> input = new ArrayList<>();
       input.add("First");
       input.add("Second");
       input.add("Third");
       input.add("Fourth");
       input.add("Fifth");
      
       StringConverter stringConverter = new StringConverter();
       ArrayList<Integer> converted = stringConverter.convertAll(input);
      
       for(int i = 0; i < converted.size(); i++) {
           System.out.println("Converted value of " + input.get(i) + " is " + converted.get(i));
       }

   }

}

Code image:

import java.util.ArrayList; public class String ConverterMain { public static void main(String[] args) { ArrayList<String> in

Sample execution:

Converted value of First is 5
Converted value of Second is 6
Converted value of Third is 5
Converted value of Fourth is 6
Converted value of Fifth is 5

Please give a thumbs up if this helped you in any way.

Add a comment
Know the answer?
Add Answer to:
Consider the following Java interface. public interface Converter<A, B> { B convert(A xs); } (a) Write...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements...

    Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements DoubleEndedList { private Node front; // first node in list private Node rear; // last node in list private int size; // number of elements in list ////////////////////////////////////////////////// // YOU MUST IMPLEMENT THE LOCATE METHOD BELOW // ////////////////////////////////////////////////// /** * Returns the position of the node containing the given value, where * the front node is at position zero and the rear node is...

  • create a new Java application called "CheckString" (without the quotation marks) according to the following guidelines....

    create a new Java application called "CheckString" (without the quotation marks) according to the following guidelines. ** Each method below, including main, should handle (catch) any Exceptions that are thrown. ** ** If an Exception is thrown and caught, print the Exception's message to the command line. ** Write a complete Java method called checkWord that takes a String parameter called word, returns nothing, and is declared to throw an Exception of type Exception. In the method, check if the...

  • q2: Write a public class named MythMouseListener that implements the Mouselistener interface. This class will have...

    q2: Write a public class named MythMouseListener that implements the Mouselistener interface. This class will have a public constructor that takes a JTextArea and a JLabel as parameters and stores these in instance variables. Override the mouseEntered method to . display the text from the JTextArea on the JLabel in all upper case letters. Then, override the mouseReleased method to display the text from the JTextArea on the JLabel in all lower case letters. The other three methods from the...

  • Suppose we have the following Java Interface: public interface Measurable {    double getMeasure(); // An...

    Suppose we have the following Java Interface: public interface Measurable {    double getMeasure(); // An abstract method    static double average(Measurable[] objects) { // A static method    double sum = 0;    for (Measurable obj : objects) {    sum = sum + obj.getMeasure();    }    if (objects.length > 0) { return sum / objects.length; }    else { return 0; }    } } Write a class, called Person, that has two instance variables, name (as...

  • I've been assigned to create a new Java application called "CheckString" (without the quotation marks) according...

    I've been assigned to create a new Java application called "CheckString" (without the quotation marks) according to the following guidelines. ** Each method below, including main, should handle (catch) any Exceptions that are thrown. ** ** If an Exception is thrown and caught, print the Exception's message to the command line. ** Write a complete Java method called checkWord that takes a String parameter called word, returns nothing, and is declared to throw an Exception of type Exception. In the...

  • Assume that you have a String "name" attribute in a Java class definition.  Write a public method...

    Assume that you have a String "name" attribute in a Java class definition.  Write a public method to return this "name" attribute. public String getName() { return name;         } 1.) Write a statement that throws an IllegalArgumentException with the error message “Argument cannot be negative”.​ 2.) The method getValueFromFile is public and returns an int. It accepts no arguments. The method is capable of throwing an IOException and a FileNotFoundException. Write the header for this method.

  • Write a program to pack boxes with blobs. Write two classes and a driver program. A...

    Write a program to pack boxes with blobs. Write two classes and a driver program. A Blob class implements the following interface. (Defines the following methods.) public interface BlobInterface { /** getWeight accessor method. @return the weight of the blob as a double */ public double getWeight(); /** toString method @return a String showing the weight of the Blob */ public String toString(); } A Blob must be between 1 and 4 pounds, with fractional weights allowed. A default constructor...

  • 6 (10 points Remember the recursive Searching algorithm Binary Search. Write a recursive method to search...

    6 (10 points Remember the recursive Searching algorithm Binary Search. Write a recursive method to search for a target character in the array and return the index location if found or -1 if it is not found. 7 a5 points 17 points cach) Write the code to create a GUI based class Temperature Converter which inherits from JFrame and implements the ActionListerner interface. public static int binary Search(char target, char( theValues, int firstIndex, int lastindex) Example: Clicked "F to C"...

  • please write code in java, assignment is due soon Thanks. public interface Named The Named interface...

    please write code in java, assignment is due soon Thanks. public interface Named The Named interface is a simple interface which applies to anything which has a name. The only thing required is the following method: String name() which returns the name of the instance. public interface MessageSink, It includes: void deliver(Named sender, String message) delivers the specified message from the given sender. public interface Sharable, which is Named, It includes no method. public abstract class GeneralCapability It incudes: The...

  • Please write in dr java Here is the driver: import java.util.*; public class SquareDriver { public...

    Please write in dr java Here is the driver: import java.util.*; public class SquareDriver { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); String input =""; System.out.println("Welcome to the easy square program"); while(true) { System.out.println("Enter the length of the side of a square or enter QUIT to quit"); try { input = keyboard.nextLine(); if(input.equalsIgnoreCase("quit")) break; int length = Integer.parseInt(input); Square s = new Square(); s.setLength(length); s.draw(); System.out.println("The area is "+s.getArea()); System.out.println("The perimeter is "+s.getPerimeter()); } catch(DimensionException e)...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT