Untitled

 avatar
unknown
plain_text
3 years ago
3.0 kB
7
Indexable
package labs;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;

public class Performance_Task {
	//the set of code, new public void to make code easier
	public static void speechName(String speechName) throws FileNotFoundException{
		//so i can get the files of speech
		Scanner inFile = new Scanner(new File("src/labs/" + speechName));
		String speech = "";
		//all the letters in alphabet
		String[] allLetters =  {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
		//array for count
		int[] count = new int[26];
		//array for percent
		double[] percent = new double[26];
		//array for how many letters to include for each letter
		int[] toInclude = new int[26];
		//int to calculate the total words to use
		int toIncludeTotal = 0;
		//add speech inFile
		while (inFile.hasNext()) {
			speech += inFile.nextLine();

		}
		//make speech lowercase
		speech = speech.toLowerCase();
		//letters in the speech
		double letters =0.0;
		//start printing the header
		System.out.println("-------------------------------------------\n" + " Letter    Count    Percent    #To Include\n"
				+ "-------------------------------------------");
		
		
		//find how many of each letter there is
		for (int i = 0; i < speech.length(); i++) {
			if (speech.charAt(i) >= 97 && speech.charAt(i) <= 122) {
				letters++;
				count[speech.charAt(i) - 97]++;
			}
				
			
		}
		//find the percentage of how many time each letter is in the speech
		for (int i = 0; i < count.length; i++) {
			percent[i] = (count[i]/letters);
		}
		
		//how manu letters to include in the bundle
		for (int i = 0; i<count.length; i++) {
			toInclude[i] = (int) Math.round((180 * percent[i]));
			if(toInclude[i]<1) {
				toInclude[i] = 1;
			}
			toIncludeTotal += toInclude[i];
	
		}
		
		
		//after crunching all data
		//then print
		for (int i = 0; i < count.length; i++) {
			System.out.print("   "+ allLetters[i]+ "    " );
			System.out.printf("   %4d     %5.2f%%        %4d      %n", count[i], percent[i]*100, toInclude[i]);
			
		}
		System.out.println("-------------------------------------------\n" + " " + "total:    "+ letters + "                   " + toIncludeTotal);
		System.out.println("-------------------------------------------\n");
	}
		
	//another public static for the printing, will print everything in the before public static for each speech
	public static void main(String[] args) throws Exception {
		//name of speech 1
		System.out.println("Data for Speech_1.txt \"The Gettysburg Address\" by Abraham Lincoln");
		speechName("Speech_1.txt");
		//name of speech 2
		System.out.println("Data for Speech_2.txt \"Freedom or Death\" by Emmeline Pankhurst");
		speechName("Speech_2.txt");
		//name of speech 3
		System.out.println("Data for Speech_3.txt \"I Have A Dream\" by Dr. Martin Luther King");
		speechName("Speech_3.txt");
		
	}
	
}
Editor is loading...