1. (20 points)Write a program named Lab1.java that reads the java file you creat
ID: 3690299 • Letter: 1
Question
1. (20 points)Write a program named Lab1.java that reads the java file you created for this lab (i.e. Lab1.java) and writes a copy of the file to another file with line numbers inserted. Name the output file as lab1.txt
Run your program with the following arguments list:
*Lab1.java *lab1.txt
2. (30 points)Design a class named Lab2.java that reads the contents of a text file (Lab2.java). then calculate and display the following information:
number of lines in the file
longest line in the file (the line number of the line containing the maximum number of characters),
shortest line int the file (again the line number)
and average number of characters per line.
Explanation / Answer
//* Write a program java that reads the file and writes a copy of the file to another file with line numbers inserted */
import java.io.*;
import java.util.Scanner;
public class Lab1
{
public static void main(String[] args) throws IOException
{
Scanner fiIn = new Scanner(new File("lab1.txt") );
FileWriter fiOut = new FileWriter("lab2.txt");
PrintWriter output = new PrintWriter(fiOut);
int number = 1;
while(fiIn.hasNext( ) )
{
String lineNum = fiIn.nextLine( );
output.println(number+" "+lineNum);
number++;
}
fiIn.close();
output.close();
}
}
// * Write a java program that reads the contents of a text file then calculate and display
1. number of lines in the file
2. longest line in the file
3. shortest line int the file
4. average number of characters per line. */
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.Scanner;
public class Lab2 {
static int previousLongLine = 0;
static int previousShortLine = 10000;
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
System.out.println("File to be read: ");
String fileName = console.next();
String line = null;
int key = 0, lineSize = 0,nbSentences = 0, lineNumber = 0;
int word=0;
Lab2 ln = new Lab2();
HashMap longLineMap = new HashMap();
HashMap shortLineMap = new HashMap();
try {
FileReader fileReader = new FileReader(fileName);
BufferedReader bufferedReader = new BufferedReader(fileReader);
while ((line = bufferedReader.readLine()) != null) {
String []parts = line.split(" ");
for( String w : parts)
{
nbSentences +=getNbSentences(word);
word++;
}
lineNumber++;
lineSize = line.length();
if (lineSize > previousLongLine) {
previousLongLine = lineSize;
longLineMap.clear();
longLineMap.put(lineNumber, line);
}
if (lineSize < previousShortLine) {
previousShortLine = lineSize;
shortLineMap.clear();
shortLineMap.put(lineNumber, line);
}
}
System.out.println("Number of Words: "+ words);
System.out.println("Number of Sentences: " + nbSentences);
System.out.println("Number of lines: " + lines);
System.out.println("Average Words In Sentence: " + (words/nbSentences));
bufferedReader.close();
} catch (FileNotFoundException ex) {
System.out.println("Unable to open file '" + fileName + "'");
} catch (IOException ex) {
System.out.println("Error reading file '" + fileName + "'");
}
ln.printLongLine(longLineMap);
ln.printShortLine(shortLineMap);
}
public static int getNbSentences(String word) {
int result = 0;
char[] chars = word.toCharArray();
for(Character c : chars) {
if(c == '.' || c == '!' || c == '?') {
result++;
}
}
return result;
}
public void printLongLine(HashMap longLineMap) {
Set keyofSet = longLineMap.keySet();
Iterator itr = keyofSet.iterator();
while (itr.hasNext()) {
Integer keys = (Integer) itr.next();
String value = (String) longLineMap.get(keys);
System.out.println("Line Number of Longest line: " + keys + " Longest line: " + value);
}
}
public void printShortLine(HashMap shortLineMap) {
Set keyofSet = shortLineMap.keySet();
Iterator itr = keyofSet.iterator();
while (itr.hasNext()) {
Integer keys = (Integer) itr.next();
String value = (String) shortLineMap.get(keys);
System.out.println("Line Number of Shortest line: " + keys + " Shortest line: " + value);
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.