Question
Java, I cannot fix the syntax error in this source code please help! // Interface class public interface Filter{ // declare the accept() method public
Java, I cannot fix the syntax error in this source code please help!
// Interface class
public interface Filter{
// declare the accept() method
public boolean accept(String x);
}
////////////////////////////////////////////////////////////////////////////////
import java.util.Random;
import java.util.ArrayList;
import java.awt.Rectangle;
// the class FilterMain is used to test the Filter interface
public class FilterMain{
// the main() function is used to determine and display the at most
// 3 characters in accepted all filtered array strings
public static void main(String[] args){
// declare and intialize the variable
int count = 15;
String alphabet = "abcdefghijklmnopqrstuvwxyz";
int maxLen = 8;
// declare the random object
Random rObj = new Random();
// create an object "a" for String class in array list
String[] a = new String[count];
// the for loop executes until the "i" value less than count to
// generate the random array strings
// loop executes until i less than count
for(int i=0; i // declare and initialize the variable int length = rObj.nextInt(maxLen); String s = ""; // loop executes until less than length for(int j=0; j // generate the random character s = s + alphabet.charAt(rObj.nextInt(26)) // assign s value to array element a{i} = s; } // display the accepted all charAts in the array list System.out.println("Accept all filtered array elements"); System.out.println(); // display all the accepted character in the array list by using for loop. // loop compares the string "s" from "a" for(String s:a){ // display the accepted all charAt in array list System.out.println("" + s + ""); } // create an object for Filter interface to define the accept() method // create an object "filt_obj" for Filter interface Filter filt_obj = new Filter(){ // method accept() definition public boolean accept(String x){ // return the array elementwith at most three character return x.length() <= 3; } }; // call the filter() method using the String to filter out the at most // three characters in the accepted array list System.out.println(); System.out.println("------------------"); System.out.println(); System.out.println("Accept all filtered strings at most 3 characters"); System.out.println(); // declare the String object and call the filter() method String[] b = filter(a, filt_obj); // display the rectangle area in the array by using for loop // loop compares the string "s" from "b" for(String s:b){ // display the rectangle area in the array System.out.println("" + s + ""); } } // the method filter() that accepts "a" and "f" are the input parameters to filter // the array elements at most 3 characters in the accepted array list by using Filter interface public static String[] filter(String[] a, Filter f){ //create an object "b" for ArrayList class ArrayList b = new ArrayList(); // the for loop processes by compareing the two strings to filter out // the array element with at most 3 characters. // loop compares the string "toCheck" from "a" for(String toCheck:a){ //verify the accepted array element at most 3 characters if(f.accept(toCheck)) // assign the filtered characters into the array list b.add(toCheck); } // return the filtered at most 3 characters in the accepted array list. return b.toArray(new String[b.size()]; } }
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