Filter.java: 3 interface Filter { 4 int filt(InputStream in); 5 } FilterFW.java:
ID: 3536280 • Letter: F
Question
Filter.java:
3 interface Filter {
4 int filt(InputStream in);
5 }
FilterFW.java:
3 public class FilterFW { // primitive unix filter framework
4 public static int main(String [] a, Filter f) {
5 int status=0;
6 for (int i=1; i<a.length; i++)
7 try {
8 status = f.filt(new FileInputStream(a[i]));
9 } catch (FileNotFoundException fnfe) {}
10 return status;
11 }
12 }
Diplopia.java:
4 public class Diplopia implements Filter {
5 public int filt(InputStream in) {
6 Scanner input = new Scanner(in);
7 while (input.hasNext()) {
8 String line = input.nextLine();
9 System.out .println(line+line);
10 }
11 return 0;
12 }
13
14 public static void main(String [] a) {
15 System.exit (FilterFW.main(a,new Diplopia()));
16 }
17 }
Explanation / Answer
a) run the following commands in commandline
javac Diplopia.java
java Diplopia filenam1 filename2 filename3
where filename1 ,filename2 and filename3 are name of the file you have to provide . The number of files can be zero or one or more than one.
b). public static void main(String [] a) {
System.exit (FilterFW.main(a,new Diplopia()));
}
the above method is the entry point for the program. or we can say line System.exit (FilterFW.main(a,new Diplopia())); creates the entry point.
c)f is reference of type Filter , and it is pointing to object which is created in line ' System.exit (FilterFW.main(a,new Diplopia())); 'using statement new Diplopia(), here through reference f we can execute only those methods of objects of type Diplopia which is declared in interface Filter.
d). Scanner constructor requires argument of type InputStream, and we are proving an object of type FileInputStream, since FileInputStream is inherited from InputStream, we can pass the objects, now object can only those methods which is declared or defined in InputStream, so in brief
the argument to Scanner constructor is an object of type FileInputStream and it is created in line ' status = f.filt(new FileInputStream(a[i])); ',The object is create using statement new FileInputStream(a[i]) where a[] is array of string.
e). we can use reference of an interface to point an object of class which implents that reference, since class Diplopia implements Fiter reference so we can use the reference of interface Filter to point to object of class Diplopia . the only difference will be that, through reference we can only call those methods of class Diplopia which are declared in interface Filter.
f).public static void exit(int status) method is used to Terminates the currently running Java Virtual Machine. The argument serves as a status code; by convention, a nonzero status code indicates abnormal termination. Now since FilterFW.main(String [] a, Filter f) method is the source point or entry point of the actuall task of the program and it returns the status of task completion ,whther the task happenes correctly or not, that's why we pass Filter.FW to System.exit in Diplopia.java.
g).Filter.java is creating an interface in which method 'int filt(InputStream in)' is declared . This interface is implemented by class Diplopia, Generally interface is used to provide multiple inheritance , as well as abstraction in java
h). The method 'public static int main(String [] a, Filter f) ' is just an ordinarry methods as others we generally define in program according to our requirenment, it has nothing to do with the method 'public static void main(string argv[]) ' which is generally called first during program execution. since we can define any return type for a method other than method 'public static void main(string argv[]) ',so defining a return type int for method 'public static int main(String [] a, Filter f) ' is possible.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.