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

1. Suppose we want to extend the Echo class to a class called DoubleEcho, which

ID: 3627450 • Letter: 1

Question

1.

Suppose we want to extend the Echo class to a class called DoubleEcho, which echoes the text file to the console as Echo does, but does so in double-spaced format. Thus if the file sample.txt holds these lines:



the console should display:


[Note: To double-space the output, insert a blank line between consecutive lines of text.]

/JavaCS1/src/echo/scanner/Echo.java
/JavaCS1/src/echo/scanner/EchoDouble.java
/JavaCS1/src/echo/scanner/EchoTester.java
/JavaCS1/src/echo/sample.txt



import java.io.*;

public class EchoDouble extends Echo
{
  public EchoDouble (String datafile) throws IOException
  {
    super(datafile);
  }

  // Prints the given line and inserts a blank line
  // Overrides the processLine method in Echo class
  public void processLine(String line)
  {

put your code here

} //end main
} //end class

2. In this example we extend the Echo class to a class called EchoNoSpace. Here the external text file is written to the console as before, but now all spaces are removed first before displaying each line. Thus a line like

The best things in life are free

becomes

Thebestthingsinlifearefree

The code for doing this uses a StringTokenizer object. The link below gives the full class, and the answer block below provides the implementation for the processLine() method in that derived class.

The EchoNoSpace class has an addtional integer attribute, wordCount, which is initialized to 0. After the file has been processed, we would like this variable to hold the exact number of words (tokens) in the file. The EchoTester code at the link below reports this value as follows:

System.out.println("wordCount: " + e.getWordCount());

For this assignment, edit the processLine() code in the answer box so that the EchoTester's main() method reports the correct value for the number of words in the file.

/JavaCS1/src/echo/scanner/Echo.java
/JavaCS1/src/echo/scanner/EchoNoSpace.java
/JavaCS1/src/echo/scanner/EchoTester.java
/JavaCS1/src/echo/sample.txt

import java.io.*;
import java.util.StringTokenizer;

public class EchoNoSpace extends Echo
{

  // the number of the words counted
  private int wordCount;

  public EchoNoSpace (String datafile) throws IOException
  {
    super( datafile);
    wordCount=0;
  }

  // Prints the given line without any spaces.
  // Overrides the processLine method in Echo class
  public void processLine(String line){

put your code here


} //end main
} //end class


3. 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.)

/JavaCS1/src/echo/scanner/Echo.java
/JavaCS1/src/echo/scanner/EchoLongest.java
/JavaCS1/src/echo/scanner/EchoTester.java
/JavaCS1/src/echo/sample.txt

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

4.

Suppose an external file is made up entirely of integers. In the model we've been using in this unit, the file is actually read in, line by line, as a sequence of Strings. Using a StringTokenizer inside processLine() method can produce individual tokens that look like numbers, but are actually Strings that are composed of digits. To convert each token from String format to integer format, you need to use the parseInt() method from the Integer class. Thus

int birthYear = Integer.parseInt("1983");

correctly stores the integer 1983 into the birthYear integer variable.

For this assignment you should create a complete processLine method in the EchoSum class, so that it transforms each number in each line to integer format, and then sums the entries on the line and prints their sum on a separate line. For example, if the external text file looks like this:



your program should display this:




/JavaCS1/src/echo/scanner/Echo.java
/JavaCS1/src/echo/scanner/EchoSum.java
/JavaCS1/src/echo/scanner/EchoTester.java
/JavaCS1/src/echo/number.txt

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

public class EchoSum extends Echo
{

  public EchoSum (String datafile) throws IOException
  {
    super(datafile);
  }

  // Prints the sum of each line.
  public void processLine(String line){

put your code here

} //end method
} //end class

Explanation / Answer

import java.util.Scanner; import java.io.*; public class Echo{ String fileName; // external file name Scanner scan; // Scanner object for reading from external file public Echo(String f) throws IOException { fileName = f; scan = new Scanner(new FileReader(fileName)); } // reads lines, hands each to processLine public void readLines(){ while(scan.hasNext()){ processLine(scan.nextLine()); } scan.close(); } // does the real processing work public void processLine(String line){ System.out.println(line); } } theres the echo class, i also need help with these questions, any suggestions would be great!!