Untitled
unknown
plain_text
2 years ago
6.2 kB
3
Indexable
Never
package labs; import java.util.Scanner; public class Lab01 { // #1 public static String scanString(String instructions) { //Scanner inKey, where user can enter answer Scanner inKey = new Scanner(System.in); //prints instructions System.out.print(instructions); //this is where String input = inKey.nextLine(); //answer printing return input; } // #2 public static boolean isVowel(char letter) { //all vowels String vowels = "aeiouAEIOU"; //seeing if the letter is a vowel char[] vowelsArray = vowels.toCharArray(); for (int i = 0; i < vowels.length(); i++) { if (letter == vowelsArray[i]) { return true; } } return false; } // #3 public static int numVowels(String str) { int count = 0; String isVowels = "aeiouAEIOU"; //finding how many vowels are in the parameter char[] vowelsArray = isVowels.toCharArray(); for (int i = 0; i < str.length(); i++) { if (isVowel(str.charAt(i))) { count++; } } return count; } // #4 public static int compareVowels(String str1, String str2) { int count = 0; int count1 = 0; String isVowels = "aeiouAEIOU"; char[] vowelsArray = isVowels.toCharArray(); //how many vowels in parameter 1 for (int i = 0; i < str1.length(); i++) { if (isVowel(str1.charAt(i))) { count++; } } //how many vowels in parameter 2 for (int i = 0; i < str2.length(); i++) { if (isVowel(str2.charAt(i))) { count1++; } } //which one has the greater amount of vowels if (count > count1) { return 1; } else if (count1 > count) { return 2; } return 0; } // #5 public static String rot13(String str) { //changing all str to lowercase str.toLowerCase(); //making the char for the cipher char[] cipher = str.toCharArray(); //assigning a value of 13 to each letter for (int i = 0; i < str.length(); i++) { if (cipher[i] >= 94 && cipher[i] <= 122) { cipher[i] += 13; //is cipher is less than z, subtract it by 26 to make it a letter again if (cipher[i] > 122) { cipher[i] -= 26; } } } return String.valueOf(cipher); } // DO NOT MESS WITH THIS // DO NOT MESS WITH THIS // DO NOT MESS WITH THIS // DO NOT MESS WITH THIS public static void main(String[] args) { // for #2 char c = scanString("Enter a letter: ").charAt(0); if (isVowel(c)) { System.out.println(c + " is a vowel."); } else { System.out.println(c + " is not a vowel."); } // for #3 int count = numVowels(scanString("Enter a word: ")); System.out.println("That has " + count + " vowels."); // for #4 String word1 = scanString("Enter a word: "); String word2 = scanString("Enter another word: "); int results = compareVowels(word1, word2); if (results == 1) { System.out.println(word1 + " has more vowles than " + word2 + "."); } else if (results == 2) { System.out.println(word2 + " has more vowles than " + word1 + "."); } else { System.out.println(word1 + " and " + word2 + " have the same number of vowels."); } // for #5 String cypher = rot13(scanString("Enter a message: ").toLowerCase()); System.out.println("Coded Message:"); System.out.println(cypher); } } 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 //print:LII::LLI:L:LI//’[;/;[[[[[[[[[[[[[[[=-==-=----------------------------------------------------------============--=-- -: 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 void for printing 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"); } }