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

JAVA ONLY File Input and Output Chapter 1. It is a well-known phenomenon that mo

ID: 3690126 • Letter: J

Question

JAVA ONLY

File Input and Output Chapter

1. It is a well-known phenomenon that most people are easily able to read a text whose words have two characters flipped, provided the first and last letter of each word are not changed.

*For example, I dn'ot gvie a dman for a man taht can olny sepll a wrod one way. (Mark Twain)

-Write a program with a method scramble(String word) that reads the contents of a file with a paragraph in it and constructs a scrambled version of the paragraph, randomly flipping two characters other than the first and last for all words greater than length 3.

2. A spreadsheet file can be stored as a comma separated file. For instance if a file called data.csv had the following in it:

name, quiz1, quiz2, quiz3, hw1, hw2, hw3, midterm, final

bill,8,9,10,50,45,43,88,93

jill,9,9,10,50,48,40,90,91

gill,6,5,7,38,22,40,70,73

will,8,9,9,40,45,43,83,94

totals,10,10,10,50,50,50,100,100

It would display in a program like Microsoft excel appropriately.

a) create a file called data.csv with this data on your hard drive.

b) read the file into a program calculating each students final percentage.

c) have the program write a new file with an extra column containing the final percentage for each student, a column heading for the first row, and the last row can end with 100 for its percentage.

Explanation / Answer

/**
* @author srinu
*
*/
public class ScrambleString {

   // Constructs a scrambled version of a given word,
   // by randomly flipping two characters other than the first and last one.
   private static String scramble(String word) {

       int letterCount = word.length(); // get length of word
       // n I changed wordLength to letterCount for clarity.

       if (letterCount < 4) // if the word has 3 or less letters, then scramble
                               // not necessary
       {
           // abort function
           return word;
       }
       char charArr[] = new char[word.length()];
       for (int i = 1; i <= (word.length() - 1) / 2; i++) {

           charArr[word.length() - 1 - i] = word.charAt(i);
           charArr[i] = word.charAt(word.length() - 1 - i);

       }

       charArr[0] = word.charAt(0);
       charArr[word.length() - 1] = word.charAt(word.length() - 1);

       String scrambleStrong = new String(charArr);
       return scrambleStrong;

   }

   /**
   * @param args
   */
   public static void main(String[] args) {

       System.out.println(scramble("Srinivas"));
   }
}

OUTPUT:

Savinirs

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class ReadDataCSV {

   public static void main(String[] args) {
       Scanner scanner = null;
       FileWriter fileWriter = null;
       try {
           String fileName = "newdata.csv";
           fileWriter = new FileWriter(fileName);
           scanner = new Scanner(new File("data.csv"));
           String FILE_HEADER = scanner.nextLine() + ",percentage ";
           fileWriter.append(FILE_HEADER);
           while (scanner.hasNext()) {

               String line = scanner.nextLine();
               String lineArr[] = line.split(",");

               int sum = 0;
               int i;
               for (i = 1; i < lineArr.length; i++) {

                   sum += Integer.parseInt(lineArr[i]);
               }
               fileWriter.append(line + "," + (double) sum / (double) (i - 1)
                       + " ");

           }
           System.out.println("CSV file was created successfully !!!");

       } catch (Exception e) {
           // TODO: handle exception
       } finally {

           try {
               fileWriter.flush();
               fileWriter.close();
           } catch (IOException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
           }

       }

   }
}

data.csv

name, quiz1, quiz2, quiz3, hw1, hw2, hw3, midterm, final
bill,8,9,10,50,45,43,88,93
jill,9,9,10,50,48,40,90,91
gill,6,5,7,38,22,40,70,73
will,8,9,9,40,45,43,83,94
totals,10,10,10,50,50,50,100,100

OUTPUT:

newdata.csv

name, quiz1, quiz2, quiz3, hw1, hw2, hw3, midterm, final,percentage
bill,8,9,10,50,45,43,88,93,43.25
jill,9,9,10,50,48,40,90,91,43.375
gill,6,5,7,38,22,40,70,73,32.625
will,8,9,9,40,45,43,83,94,41.375
totals,10,10,10,50,50,50,100,100,47.5