Untitled
unknown
plain_text
13 days ago
3.8 kB
5
Indexable
Never
package assign02; import java.util.Arrays; import java.util.Scanner; public class ProblemSolvingPractice { public static long calculateProductIteratively(int a, int b) { long result = 0; for (int i = 0; i < b; i++) { result = result + a; } return result; } public static long calculateProductRecursively(int value1, int value2) { if (value2 == 1) { return value1; } else { return value1 + calculateProductRecursively(value1, value2 - 1); } } public static int countVowels(String str) { int vowelCount = 0; str = str.toLowerCase(); for (char c : str.toCharArray()) { if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') { vowelCount++; } } return vowelCount; } public static int countDoubleTokens(String str) { try (Scanner scanner = new Scanner(str)) { int count = 0; while (scanner.hasNext()) { if (scanner.hasNextDouble()) { count++; scanner.next(); } else { scanner.next(); } } return count; } } public static boolean isPalindromeArray(int[] arr) { int start = 0; int end = arr.length - 1; while (start < end) { if (arr[start] != arr[end]) { return false; } start++; end--; } return true; } public static int[] countSigns(double[] arr) { int negativeCount = 0; int zeroCount = 0; int positiveCount = 0; for (double num : arr) { if (num < 0) { negativeCount++; } else if (num == 0) { zeroCount++; } else { positiveCount++; } } return new int[] {negativeCount, zeroCount, positiveCount}; } public static void main(String[] args) { System.out.println("Checking calculateProductIteratively(2, 3). Expecting a result of 6. The actual result is " + calculateProductIteratively(2, 3) + "."); System.out.println("Checking calculateProductIteratively(-20, 4). Expecting a result of -80. The actual result is " + calculateProductIteratively(-20, 4) + "."); System.out.println(); System.out.println("Checking calculateProductRecursively(2, 3). Expecting a result of 6. The actual result is " + calculateProductRecursively(2, 3) + "."); System.out.println("Checking calculateProductRecursively(-20, 4). Expecting a result of -80. The actual result is " + calculateProductRecursively(-20, 4) + "."); System.out.println(); System.out.println("Checking countVowels(\"Welcome to PROBLEM SOLVING!\"). Expecting a result of 8. The actual result is " + countVowels("Welcome to PROBLEM SOLVING!") + "."); System.out.println(); System.out.println("Checking countDoubleTokens(\"3.14 6 HI -100 9.8 pi x\"). Expecting a result of 4. The actual result is " + countDoubleTokens("3.14 6 HI -100 9.8 pi x") + "."); System.out.println(); System.out.println("Checking isPalindromeArray(new int[]{8, -4, 2, -4, 8}). Expecting a result of true. The actual result is " + isPalindromeArray(new int[]{8, -4, 2, -4, 8}) + "."); System.out.println("Checking isPalindromeArray(new int[]{8, -4, 4, 8}). Expecting a result of false. The actual result is " + isPalindromeArray(new int[]{8, -4, 4, 8}) + "."); System.out.println(); System.out.println("Checking countSigns(new double[]{8, -4, 2, -4, 8}). Expecting a result of [2, 0, 3]. The actual result is " + Arrays.toString(countSigns(new double[]{8, -4, 2, -4, 8})) + "."); System.out.println("Checking countSigns(new double[]{-800.5, 0, -3.14, -1, 0.0}). Expecting a result of [3, 2, 0]. The actual result is " + Arrays.toString(countSigns(new double[]{-800.5, 0, -3.14, -1, 0.0})) + "."); } }
Leave a Comment