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

CSC java chapter 6 Write a complete program that reads an input file and reports

ID: 3813871 • Letter: C

Question

CSC java chapter 6

Write a complete program that reads an input file and reports various statistics

about the file's text. In particular, your program should report the number of lines

in the file, the longest line, the number of tokens on each line, and the length of

the longest token on each line. You may assume that the input file has at least

one line of input and that each line has at least one token.

A shell has been provided with all the needed methods. Make sure that your

program generates the following output exactly.

You must create a three different input files using the stories in the sample

1.- data.txt

the jaws that bite, the claws that catch,

Beware the JubJub bird and shun

the frumious bandersnatch.

2.- data1.txt

consectetur adipisicing elit,

sed do eiusmod tempor incididunt

3.- data2.txt

Imprimis: I am a man who, from his youth upwards,

has been filled with a profound conviction

that the easiest way of life is the best.

Hence, though I belong to a profession proverbially energetic and nervous,

output.--------------------------------------------------------------------------------------------

Your program should output the result on the screen and also in an output file.

Your program must include data validation. The input file name that the user

enters must be valid.

Sample output:

Enter the input file name: sadsd

Enter the input file name: a.txt

Enter the input file name: data.txt

Enter the output file name: out.txt

Beware the Jabberwock, my son, Line 1 has 5 tokens (longest = 11)

the jaws that bite, the claws that catch, Line 2 has 8 tokens (longest = 6)

Beware the JubJub bird and shun Line 3 has 6 tokens (longest = 6)

the frumious bandersnatch. Line 4 has 3 tokens (longest = 13)

Longest line: the jaws that bite, the claws that catch,

******************************

Do you have another file:yes/no? yes

Enter the input file name: data1.txt

Enter the output file name: out1.txt

Lorem ipsum dolor sit amet, Line 1 has 5 tokens (longest = 5)

consectetur adipisicing elit, Line 2 has 3 tokens (longest = 11)

sed do eiusmod tempor incididunt Line 3 has 5 tokens (longest = 10)

Longest line: sed do eiusmod tempor incididunt

******************************

Do you have another file:yes/no? Yes

Enter the input file name: data2.txt

Enter the output file name: out2.txt

Imprimis: I am a man who, from his youth upwards, Line 1 has 10 tokens (longest = 9)

has been filled with a profound conviction Line 2 has 7 tokens (longest = 10)

that the easiest way of life is the best. Line 3 has 9 tokens (longest = 7)

Hence, though I belong to a profession proverbially energetic and nervous, Line 4 has 11

tokens (longest = 12)

Longest line: Hence, though I belong to a profession proverbially energetic and nervous,

******************************

Do you have another file:yes/no? no

Good Bye

Shell of the program:

import java.util.*;

import java.io.*;

public class Chapter6LabShell

{

public static void main(String[] args)throws FileNotFoundException

{

     Scanner kb = new Scanner(System.in);

   boolean repeat = true;

     while (repeat)

     {

       //call the method getFile

       //call the method inputStats

       

       

       System.out.print("Do you have another file:yes/no? ");

       String answer = kb.next();

       kb.nextLine();

       if(answer.equalsIgnoreCase("no"))

          repeat = false;

      }   

}

/*This method asks the user for an output file name

cretaes an object of PritStream and returns it*/

public static PrintStream outputFile( ) throws FileNotFoundException

{

      return null;

}  

    

/*this method asks the user to enter an input file name

as long the user entering a valid file name, the user must be prompted for a valid file nale

then a Acnner object will be cretaed abd returned*/  

public static Scanner getFile(Scanner kb)throws FileNotFoundException

{

     

      return null;

    

}

/*this method gets the input file name as its paramter

call the method outputFile to get the name of the output file

as long as there are lines in the input file

{

    read one line

    print the line in the output file

    update the line count

    call the method longestLine

    call the method countTokens

    call the method longest

    output the result on the screen

}

   output the longest line

    */

    public static void inputStats(Scanner input)throws FileNotFoundException {

    

   }

    

   /*gets two String and return the longest string*/

    public static String longestLine(String longestLine, String line)

    {

         return "";

    }

    /*this method gets ons string of words seperated by spaces

    and returns the number of the words(token) in the given String*/

    public static int countTokens(String line)

    {

         return 0;

    }

     /*this methods gets a String and returns the length of the longest word in the String*/   

     public static int longest(String line)

     {

         return 0;

   }

}

Explanation / Answer

private static category BinOpNode extends ExpNode quantity.
ExpNode right; // The expression for its right quantity.
BinOpNode(char op, ExpNode left, ExpNode right) the required information.
assert op == '+' || op == '-' || op == '*' || op == '/';
assert left != null && right != null;
this.op = op;
this.left = left;
this.right = right;
}
double value() the worth is obtained by evaluating the left and right
// operands and mixing the values with the operator.
double x = left.value();
double y = right.value();
switch (op) come x + y;
case '-':
come back x - y;
case '*':
come back x * y;
case '/':
come back x / y;
default:   
come back Double.NaN; // dangerous operator! mustn't be potential.
}
}
void printStackCommands() to guage the expression on a stack machine, first do
// no matter is important to guage the left quantity, leaving
// the solution on the stack. Then do constant issue for the
// second quantity. Then apply the operator (which suggests that sound
// the operands, applying the operator, and pushing the result).
left.printStackCommands();
right.printStackCommands();
System.out.println(" Operator " + op);
}
}
It's also attention-grabbing to appear at the new parsing subroutines. rather than computing a worth, every subprogram builds AN expression tree. as an example, the subprogram like the rule for <expression> becomes

static ExpNode expressionTree() throws ParseError browse AN expression from the present line of input and
// come back AN expression tree representing the expression.
TextIO.skipBlanks();
mathematician negative; // True if there's a number one sign.
negative = false;
if (TextIO.peek() == '-')
ExpNode exp; // The expression tree for the expression.
exp = termTree(); // begin with a tree for 1st term.
if (negative) single minus operator to the term we've
// simply scan.
exp = new UnaryMinusNode(exp);
}
TextIO.skipBlanks();
whereas ( TextIO.peek() == '+' || TextIO.peek() == '-' ) {
// scan successive term and mix it with the
// previous terms into an even bigger expression tree.
char op = TextIO.getAnyChar();
ExpNode nextTerm = termTree();
// produce a tree that applies the binary operator
// to the previous tree and also the term we tend to simply scan.
exp = new BinOpNode(op, exp, nextTerm);
TextIO.skipBlanks();
}
come back exp;
} // finish expressionTree()