diff --git a/src/main/java/challenges/FundamentalsPractice.java b/src/main/java/challenges/FundamentalsPractice.java index 64e2aca..f1da865 100644 --- a/src/main/java/challenges/FundamentalsPractice.java +++ b/src/main/java/challenges/FundamentalsPractice.java @@ -25,7 +25,22 @@ public static int sum(int a, int b) { // The values in parentheses are set for y * Ex: Given [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15], you would RETURN: [3, 2, 1] */ public static int[] fizzbuzz(int[] arr) { - return null; + int[] array = {0, 0, 0}; + for (int num : arr) { + if ((num % 3 == 0) && (num % 5 == 0)) { + System.out.println("FizzBuzz"); + array[2] += 1; + } else if (num % 3 == 0) { + System.out.println("Fizz"); + array[0] += 1; + } else if (num % 5 == 0) { + System.out.println("Buzz"); + array[1] += 1; + } else { + System.out.println(num); + } + } + return array; } /** @@ -40,7 +55,14 @@ public static int[] fizzbuzz(int[] arr) { */ public static String reverseString(String input) { // Your code goes here - return null; + char[] charArray = input.toCharArray(); + for (int i = 0; i < Math.round(charArray.length / 2); i++) { + char temp = charArray[i]; + charArray[i] = charArray[charArray.length - i - 1]; + charArray[charArray.length - i - 1] = temp; + } + String str = new String(charArray); + return str; } /** @@ -49,14 +71,32 @@ public static String reverseString(String input) { */ public static int maxInArray(int[] arr) { // Your code goes here - return 0; + for (int i = 0; i < arr.length - 1; i++) { + for (int j = 0; j < arr.length - 1 - i; j++) { + if (arr[j] > arr[j+1]) { + int temp = arr[j]; + arr[j] = arr[j+1]; + arr[j+1] = temp; + + } + } + } + return arr[arr.length-1]; } public static boolean stringsAreSame(String a, String b) { + // use just .equals() if caps matter, otherwise, use the method below + if (a.equalsIgnoreCase(b)) { + System.out.println("strings are equal"); + return true; + } else { + System.out.println("strings are not equal"); + return false; + } + // If strings are equal, return true. Else, return false // Hint: use .equals() to compare strings not == // Do not care about capitals HI is equal to hi look up how to do. - return false; } /** @@ -66,8 +106,16 @@ public static boolean stringsAreSame(String a, String b) { * - strings can be accessed like this: variable.charAt(0) returns the first character */ public static int countVowels(String input) { + char[] charVowels = input.toCharArray(); // Your code goes here - return 0; + int vowels = 0; + for (int i = 0; i < charVowels.length; i++) { + if (input.charAt(i) == 'a' || input.charAt(i) == 'e' || input.charAt(i) == 'i' || input.charAt(i) == 'o' || input.charAt(i) == 'u') { + vowels++; + } + } + return vowels; + } /** @@ -77,7 +125,18 @@ public static int countVowels(String input) { */ public static boolean isPrime(int n) { // Your code goes here - return false; + boolean prime = true; + for (int i = 2; i <= Math.sqrt(n); i++) { + if (n % i == 0) { + prime = false; + return prime; + } + } + if (n <= 1 ) { + prime = false; + return prime; + } + return prime; } /** @@ -89,45 +148,71 @@ public static boolean isPrime(int n) { */ public static long factorial(int n) { // Your code goes here - return 0L; - } - + int value = 1; + for (int i = 2; i <= n; i++) { + value = (value) * (i); + } + return value; +} // Use for Manual Testing public static void main(String[] args) { - System.out.println("================ RUNNING CODE ================"); - // sum - int a = 3; - int b = 5; - System.out.println("sum(" + a + ", " + b + ") = " + sum(a, b)); // Expected: 8 - - // fizzbuzz - int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 30, 35, 36}; - System.out.println("fizzbuzz(arr) = " + Arrays.toString(fizzbuzz(arr))); // Expected: [5, 4, 3] - - // reverseString - String str = "hello"; - System.out.println("reverseString('" + str + "') = '" + reverseString(str) + "'"); // Expected: 'olleh' - - // maxInArray - int[] nums = {1, 5, 3, 9, 2}; - System.out.println("maxInArray([1,5,3,9,2]) = " + maxInArray(nums)); // Expected: 9 - - // countVowels - String word = "banana"; - System.out.println("countVowels('banana') = " + countVowels(word)); // Expected: 3 - - // isPrime - int primeTest = 7; - int notPrimeTest = 8; - System.out.println("isPrime(7) = " + isPrime(primeTest)); // Expected: true - System.out.println("isPrime(8) = " + isPrime(notPrimeTest)); // Expected: false - - // factorial - int factTest = 5; - System.out.println("factorial(5) = " + factorial(factTest)); // Expected: 120 - - System.out.println("=============== DONE RUNNING ==============="); + // System.out.println("================ RUNNING CODE ================"); + // // sum + // int a = 3; + // int b = 5; + // System.out.println("sum(" + a + ", " + b + ") = " + sum(a, b)); // Expected: 8 + + // // fizzbuzz + // int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 30, 35, 36}; + // System.out.println("fizzbuzz(arr) = " + Arrays.toString(fizzbuzz(arr))); // Expected: [5, 4, 3] + + // // reverseString + // String str = "hello"; + // System.out.println("reverseString('" + str + "') = '" + reverseString(str) + "'"); // Expected: 'olleh' + + // // maxInArray + // int[] nums = {1, 5, 3, 9, 2}; + // System.out.println("maxInArray([1,5,3,9,2]) = " + maxInArray(nums)); // Expected: 9 + + // // countVowels + // String word = "banana"; + // System.out.println("countVowels('banana') = " + countVowels(word)); // Expected: 3 + + // // isPrime + // int primeTest = 7; + // int notPrimeTest = 8; + // System.out.println("isPrime(7) = " + isPrime(primeTest)); // Expected: true + // System.out.println("isPrime(8) = " + isPrime(notPrimeTest)); // Expected: false + + // // factorial + // int factTest = 5; + // System.out.println("factorial(5) = " + factorial(factTest)); // Expected: 120 + + // System.out.println("=============== DONE RUNNING ==============="); + + // for static methods (the fundprac.sum print code below) different than calling classes + + + + + // System.out.println(FundamentalsPractice.sum(3,7)); + + // int[] someArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15}; + // int[] result = FundamentalsPractice.fizzbuzz(someArray); + // System.out.println(Arrays.toString(result)); + + + // System.out.println(FundamentalsPractice.reverseString("bestfriend")); + + + // int[] maxArray = {1, 3, 1082, 86, 23}; + // System.out.println(FundamentalsPractice.maxInArray(maxArray)); + + // System.out.println(FundamentalsPractice.stringsAreSame("hi", "hI")); + // System.out.println(FundamentalsPractice.countVowels("eee")); + // System.out.println(FundamentalsPractice.isPrime(7)); + // System.out.println(FundamentalsPractice.factorial(5)); } } diff --git a/src/main/java/challenges/PasswordChecker.java b/src/main/java/challenges/PasswordChecker.java index c42aafb..84ef4ea 100644 --- a/src/main/java/challenges/PasswordChecker.java +++ b/src/main/java/challenges/PasswordChecker.java @@ -26,15 +26,20 @@ public class PasswordChecker { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); + System.out.print("What do you want your password to be?: "); + String password = scanner.nextLine(); // TODO: Prompt the user to enter a password and read it from input // String password = scanner.nextLine(); + // Call the method to check password strength // String strength = checkPasswordStrength(password); + String strength = checkPasswordStrength(password); // TODO: Output the strength rating // System.out.println(strength); + System.out.println(strength); scanner.close(); } @@ -42,6 +47,7 @@ public static void main(String[] args) { /** * Checks the strength of the given password. * Returns "Weak", "Moderate", or "Strong". + * the things (@param and @return) below are tags for documentation * * @param password The password to check * @return The strength rating @@ -51,7 +57,53 @@ public static String checkPasswordStrength(String password) { // - Check length // - Check for uppercase, lowercase, digit, and symbol // - Return the appropriate rating + char[] charString = password.toCharArray(); + int strengthRate = 0; + // cond = condition + boolean condUpper = false; + boolean condLower = false; + boolean condDigit = false; + boolean condSymbol = false; + boolean condLength = false; - return ""; // Placeholder + for (int i = 0; i < charString.length; i++) { + if (Character.isUpperCase(charString[i]) == true) { + condUpper = true; + } else if (Character.isLowerCase(charString[i])) { + condLower = true; + } else if (Character.isDigit(charString[i])) { + condDigit = true; + } else if (Character.isLetterOrDigit(charString[i]) == false) { + condSymbol = true; + } + } + + if (charString.length >= 8) { + condLength = true; + } + + if (condUpper == true) { + strengthRate++; + } + if (condLower == true) { + strengthRate++; + } + if (condDigit == true) { + strengthRate++; + } + if (condSymbol == true) { + strengthRate++; + } + // System.out.println(strengthRate); + + if (strengthRate == 4 && condLength == true) { + return "Strong"; + } else if (strengthRate == 3 && condLength == true) { + return "Moderate"; + } else { + return "Weak"; + } } + + } \ No newline at end of file