Make a list inheritance for java. The InvalidStringException class should inherit the…
- User
Make a list inheritance for java. The InvalidStringException class should inherit the inbuilt Exception class. It should implement InvalidStringException(String message): calls the constructor of the parent class.
My ArrayToList class should implement interface Mylist and have a clss variable of type ArrayList. It should implement following methods: ArrayToList(), void convert(Strong[] a), void replace(int idx): replaces string index idx of arrayToList, with empty string, and ArrayList compact(): remove all empty string. Constraints: 1 <= n <= 100, 1 <= length(a[i]) <= 5 template:
import java.util.;
interface MyList{ void convert(String[] a); void replace(int idx); ArrayList compact(); }
/ Write the implementation of the InvalidStringException and the ArrayToList classes Model strings for cut and paste: I have added the string: {string} at the index: {index} I have replaced the string: {string} with a null string */ class InvalidStringException { } class ArrayToList{ }
public class Solution{
public static void main(String[] args){
Scanner sc = new Scanner(System.in); Random rand = new Random(0);
int n = Integer.parseInt(sc.nextLine()); String[] a = new String[n];
for(int i = 0; i < n; i++) a[i] = sc.nextLine();
MyList obj = new ArrayToList();
obj.convert(a); int x = rand.nextInt(n); for(int i = 0; i < x; i++) obj.replace(rand.nextInt(n));
ArrayList s = obj.compact();
for (int i = 0; i < s.size(); i++){ if(s.get(i).charAt(0) >= 97 && s.get(i).charAt(0) <= 122){ try{ throw new InvalidStringException(“This is an invalid string”); } catch(InvalidStringException e){System.out.println(e.getMessage());} } }
} }
- Assistant