Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Now let\'s extend the Echo class to a class called EchoLongest, which includes a

ID: 3567738 • Letter: N

Question

Now let's extend the Echo class to a class called EchoLongest, which includes a method called printLongest(). This method prints the longest line in the file. (Look at the file EchoTester.java below. It shows a call to printLongest() in main).

We've provided EchoLongest with a String attribute called longest, which will hold the longest line seen so far in the external file. Using the longest variable, implement processLine() in the EchoLongest class so that printLongest() behaves correctly when called from the EchoTester class. (Note: in case of ties, your code should return the first occurence of a longest line.)

import java.io.*;

public class EchoLongest extends Echo
{

// the current longest line
private String longest;

public EchoLongest (String datafile) throws IOException
{
super(datafile);
longest="";
}

// Sets the given line as the current longest if the line is
// longer than the value stored in the longest.
// Overrides the processLine method in Echo class
public void processLine(String line){
put your code here
}

public void printLongest(){
System.out.print(longest);
} //end method
} //end class

Explanation / Answer

Here you go :)

You should enter the file name when the EchoTester class is compiled

Full file name i.e., with extension <filename>.txt should be written.

And make sure that the txt file is in the same folder as the EchoTester class

//Echo class

import java.io.IOException;
import java.util.*;
import java.io.*;

public class Echo
{
   String fileName;
   Scanner scan;
  
   public Echo(String f)throws IOException
   {
       fileName=f;
       scan=new Scanner(new FileReader(fileName));
   }
  
   public void readLine()
   {
       while(scan.hasNext())
       {
           processLine(scan.nextLine());
       }
       scan.close();
   }
  
   public void processLine(String line)
   {
       System.out.println(line);
   }
}

//EchoLongest class


import java.io.*;
public class EchoLongest extends Echo
{
   private String longest;
   public EchoLongest(String datafile) throws IOException
   {
       super(datafile);
       longest="";
   }
  
   public void processLine(String line)
   {
       if(this.longest.length()<line.length())this.longest=line;
   }
   public void printLongest()
   {
       System.out.println(longest);
   }
}

//EchoTester class
import java.io.*;
import java.util.*;

public class EchoTester
{
   public static void main(String[] args) {
       try{
           String fileName;
           Scanner nameReader=new Scanner(System.in);
           fileName=nameReader.nextLine();
           EchoLongest e=new EchoLongest(fileName);
           e.readLine();
           e.printLongest();
       }catch(IOException e){e.printStackTrace();}
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote