Untitled
unknown
plain_text
2 years ago
12 kB
2
Indexable
Never
package labs; import java.util.Scanner; public class Lab01 { public static void main(String[] args) { problem01(); problem02(); problem03(); problem04(); problem05(); } public static void problem01() { //user chooses a word Scanner inKey = new Scanner(System.in); System.out.print("Choose a word "); String word1 = inKey.nextLine(); //the computer prints the length of the number System.out.println("There are " + word1.length() + " letters in " + word1) ; } public static void problem02() { //user writes two different words Scanner inKey = new Scanner(System.in); System.out.print("Choose two word "); //system see the words and puts them into 2 strings String word1 = inKey.next(); String word2 = inKey.next(); //system checks if the words or same and if not says which is bigger if (word1.length() == word2.length()) { System.out.println("Words are the same length"); } else if (word1.length() >= word2.length()) { System.out.println("The longer word is " + word1); } else{ System.out.println("The longer word is " + word2); } } public static void problem03() { //user enters a sentence with no puncutation Scanner inKey = new Scanner(System.in); System.out.print("Enter a short sentence (no punctuation) "); String word1 = inKey.nextLine(); //computer prints sentence without first letter System.out.println(word1.substring(1)); //computer prints sentence without last letter System.out.println(word1.substring(0,word1.length()-1)); //computer prints sentence without first and last letter System.out.println(word1.substring(1,word1.length()-1)); } public static void problem04() { //user puts a word with odd amount of letters Scanner inKey = new Scanner(System.in); System.out.print("Enter a word with an odd number of characters "); String word1 = inKey.nextLine(); //system checks if word is 1 letter only, if so prints the word out if (word1.length() == 1) { System.out.println(word1); } //if more than 1 letter and odd number system prints the 3 middle letters else { System.out.print("The middle three characters are \"" + word1.substring(word1.length()/2 - 1, word1.length()/2 + (2) )); System.out.println("\""); } } public static void problem05() { //system asks user to enter a word Scanner inKey = new Scanner(System.in); System.out.print("Enter a word "); String word1 = inKey.nextLine(); //system sees if the first to letters are the same as the last if (word1.substring(0,2).equals(word1.substring(word1.length()-2, word1.length()))) { System.out.println("The first and last two letters are the same."); } else { System.out.println("The first and last two letters are not the same."); } } } package labs; import java.util.Scanner; public class Lab02 { public static void main(String[] args) { problem01(); problem02(); problem03(); problem04(); problem05(); problem06(); } public static void problem01() { //user enters a word Scanner inKey = new Scanner(System.in); System.out.print("Enter a word "); String word1 = inKey.nextLine().toLowerCase(); int word = 0; //finds how many a's are in word String wordA = "a"; for (int i = 0; i < word1.length(); i++ ) { if (word1.substring(i, i+1).equals(wordA)) { word++; } } //prints answer if (word == 1) { System.out.println("There is " + word + " a's in the word " + word1); } else { System.out.println("There are " + word + " a's in the word " + word1); } } public static void problem02() { //ask user for work Scanner inKey = new Scanner(System.in); System.out.print("Enter a word "); String word1 = inKey.nextLine(); boolean wordDouble = false; //checks if there are two letters are together that are the same int word = 0; for (int i = 0; i < word1.length()-1; i++ ) { if(word1.substring(i,i+1).equals(word1.substring(i+1,i+2))) { wordDouble = true; } } //if there worddouble is true means that there are two a's and prints that it is otherwise it says there isn't if (wordDouble == true) { System.out.println("There is a double letter in the word " + word1); } else { System.out.println("There is not a double letter in the word " + word1); } } public static void problem03() { //ask user to print a sentence Scanner inKey = new Scanner(System.in); System.out.print("Enter a sentence "); String word1 = inKey.nextLine(); //makes a's into @'s, s' to $'s e to # otherwise doesn;t change them String wordModified = ""; for (int i = 0; i < word1.length(); i++ ) { if (word1.substring(i,i+1).equals("a")){ wordModified +="@"; } else if (word1.substring(i,i+1).equals("s")){ wordModified +="$"; } else if (word1.substring(i,i+1).equals("e")){ wordModified +="#"; } else { wordModified += word1.substring(i,i+1); } } //prints out new sentence System.out.println(wordModified); } public static void problem04() { //ask user for word Scanner inKey = new Scanner(System.in); System.out.print("Enter a word "); String word1 = inKey.nextLine(); //ask user for letter System.out.print("Enter a letter "); String letter = inKey.nextLine(); String letterIndex = ""; //sees how many letters are in the work int letterNum = 0; for (int i = 0; i < word1.length(); i++ ) { if (word1.substring(i,i+1).equals(letter)){ letterNum++; letterIndex += i + " "; } } //prints how many there are System.out.println("\"" +letter + "\" occurs at the following index(s): " + letterIndex ); } public static void problem05() { //ask user for name Scanner inKey = new Scanner(System.in); System.out.print("Enter your name "); //makes all letter lowercase String firstName = inKey.next().toLowerCase(); String lastName = inKey.next().toLowerCase(); String name = ""; //checks length of first and last name int firstNameNum = firstName.length(); int lastNameNum = lastName.length(); int c = 0; //prints out first letter of name caps System.out.println(firstName.substring(0,1).toUpperCase() + firstName.substring(1, firstNameNum) + " " + lastName.substring(0,1).toUpperCase() + lastName.substring(1, lastNameNum)); //prints last letter of name in caps System.out.println(firstName.substring(0,firstName.length()-1) + firstName.substring(firstNameNum-1, firstNameNum).toUpperCase() + " " + lastName.substring(0,lastName.length()-1) + lastName.substring(lastNameNum-1, lastNameNum).toUpperCase()); //prints every other letter caps for (int i = 0; i < firstName.length(); i++ ) { if (i%2==1) { name += firstName.substring(i,i+1).toUpperCase(); c++; } else { name += firstName.substring(i,i+1); c++; } } name+=" "; for (int i = 0; i < lastName.length(); i++ ) { if (c%2==1) { name += lastName.substring(i,i+1).toUpperCase(); c++; } else { name += lastName.substring(i,i+1); c++; } } System.out.println(name); } public static void problem06() { //ask user for sentence Scanner inKey = new Scanner(System.in); System.out.print("Enter a sentence "); //makes word lowercase String word1 = inKey.nextLine().toLowerCase(); //replaces and prints all vowels to uppercase System.out.println(word1.replaceAll("a", "A").replaceAll("e", "E").replaceAll("i", "I").replaceAll("o", "O").replaceAll("u", "U").replaceAll("y", "Y")); } } package labs; import java.util.Scanner; public class PerformanceTask { public static void main(String[] args) { //ask user for password Scanner inKey = new Scanner(System.in); String userPassword; String userPassword1; //boolean for all conditions boolean lowerCase = false; boolean upperCase = false; boolean number = false; boolean special = false; boolean same = false; boolean length = false; boolean notSpecial = true; //Keeps asking for password if any boolean is false while (!lowerCase || !upperCase || !number || !special || !same || !length || notSpecial) { //ask user for password System.out.print("Enter Password: "); userPassword = inKey.nextLine(); //ask user to reenter password System.out.print("Reenter Password: "); userPassword1 = inKey.nextLine(); //checks if passwords are the same if (userPassword.equals(userPassword1)) { same = true; } //if they aren't the same prints the following if (same == false) { System.out.println("Entries don’t match"); } //checks if password is between 8 and 20 if (userPassword.length() >= 8 && userPassword.length() <= 20) { length = true; } //if false and password less than 8 prints the following... if (length == false && userPassword.length() <8) { System.out.println("Too short: Must be 8-20 characters"); } //if false and password more than 20 prints the following... if (length == false && userPassword.length() > 20) { System.out.println("Too long: Must be 8-20 characters"); } //check each letter for the following for (int i=0; i<userPassword.length(); i++) { switch(userPassword.substring(i,i+1)) { //if password has lowercase letter case "a": case "b": case "c": case "d": case "e": case "f": case "g": case "h": case "i": case "j": case "k": case "l": case "m": case "n": case "o": case "p": case "q": case "r": case "s": case "t": case "u": case "v": case "w": case "x": case "y": case "z": lowerCase=true; break; //if password has uppercase letter case "A": case "B": case "C": case "D": case "E": case "F": case "G": case "H": case "I": case "J": case "K": case "L": case "M": case "N": case "O": case "P": case "Q": case "R": case "S": case "T": case "U": case "V": case "W": case "X": case "Y": case "Z": upperCase=true; break; //if password has special letter case "!": case "@": case "#": case "$": case "%": case "^": case "&": case "?": special = true; break; //if password has number letter case "1": case "2": case "3": case "4": case "5": case "6": case "7": case "8": case "9": case "0": number = true; break; //if password has any other characters the boolean "notSpecial" is false default: notSpecial = false; } } //if password doesn't have a lowercase letter prints the following if (lowerCase == false) { System.out.println ("Must include 1 lowercase letter"); } //if password doesn't have a uppercase letter prints the following if (upperCase == false) { System.out.println ("Must include 1 uppercase letter"); } //if password doesn't have a special character prints the following if (special == false) { System.out.println ("Must include 1 special character (!@#$%^&*?)."); } ////if password has a none character prints the following else if (notSpecial == false) { System.out.println ("Unknown Character. Must include 1 special character (!@#$%^&*?)."); } //if password doesn't have a number prints the following if (number == false) { System.out.println ("Must include 1 number"); } //if all boolean true password accepted if(length == true && number == true && upperCase == true && lowerCase == true && special == true && same == true && notSpecial == true) { System.out.println("New Password Accepted"); break; } //if all boolean not true reverts all boolean to false and tries again else { lowerCase = false; upperCase = false; number = false; special = false; same = false; length = false; notSpecial = true; } } } }