Untitled

mail@pastecode.io avatar
unknown
plain_text
3 days ago
5.4 kB
2
Indexable
Never
package assign02;
import java.util.Arrays;
import java.util.Scanner;

//CS 1410 Assignment 2: Problem solving practice by Hanh Nhi Tran (September 05, 2024)
public class ProblemSolvingPractice {
	
	public static long calculateProductIteratively(int a, int b) {	
		//Initialize the result variable to 0
		long result = 0; 
		// Iterate b times, adding a to the result each time
	    for (int i = 0; i < b; i++) { 
	        result = result + a; 
	    }
	    // Return the final result
	    return result; 
	}
	
	
	public static long calculateProductRecursively(int value1, int value2) {
		// Base case: if value2 is 1, return value1
	    if (value2 == 1) {
	        return value1;
	    } else {
	    	// Recursive case: add value1 to the result of the recursive call with value2 - 1
	        return value1 + calculateProductRecursively(value1, value2 - 1);
	    }
	}
	
	
	public static int countVowels(String str) {
	    // Initialize the vowel count to 0
	    int vowelCount = 0;
	    
	    // Iterate over each character in the string
	    for (char c : str.toCharArray()) {
	        // Check if the character is a vowel (both upper and lower case)
	        if ((c == 'a' || c == 'A') || (c == 'e' || c == 'E') || (c == 'i' || c == 'I') || (c == 'o' || c == 'O') || (c == 'u' || c == 'U')) {
	            // If it is, increment the vowel count
	            vowelCount++;
	        }
	    }
	    // Return the final vowel count
	    return vowelCount;
	}
	
	
	
	public static int countDoubleTokens(String str) {
		// Initialize a scanner to read the string
	    try (Scanner scanner = new Scanner(str)) {
	    	// Initialize the count to 0
			int count = 0;
			// Iterate over each token in the string
			while (scanner.hasNext()) {
				// Check if the token is a double
			    if (scanner.hasNextDouble()) {
			    	// If it is, increment the count and consume the token
			        count++;
			        scanner.next();
			    } else {
			    	// If not, consume the token and continue
			        scanner.next();
			    }
			}
			// Return the final count
			return count;
		}
	}
	
	
	public static boolean isPalindromeArray(int[] arr) {
		// Initialize two pointers, one at the start and one at the end of the array
	    int start = 0;
	    int end = arr.length - 1;
	    // Iterate until the pointers meet or cross
	    while (start < end) {
	    	// Check if the elements at the pointers are equal
	        if (arr[start] != arr[end]) {
	        	// If not, return false
	            return false;
	        }
	        // Move the pointers towards the center of the array
	        start++;
	        end--;
	    }
	    // If the loop completes without finding a mismatch, return true
	    return true;
	}
	
	
	public static int[] countSigns(double[] arr) {
	    int negativeCount = 0;
	    int zeroCount = 0;
	    int positiveCount = 0;
	    // Iterate over each element in the array
	    for (double num : arr) {
	    	// Check the sign of the element
	        if (num < 0) {
	        	// If negative, increment the negative count
	            negativeCount++;
	        } else if (num == 0) {
	        	// If zero, increment the zero count
	            zeroCount++;
	        } else {
	        	// If positive, increment the positive count
	            positiveCount++;
	        }
	    }
	    // Return the counts as an array
	    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