Question: Lab 4 - Immutable Objects Exercise 1 Create a file named A4.java. Place all your code in this file. The factorial (!) is defined as

Lab 4 - Immutable Objects Exercise 1 Create a file named A4.java. Place all your code in this file. The factorial (!) is defined as follows: n!= n* (n-1) * (n-2)* ... * 3 * 2 * 1 For example, 5! = 5 * 4 * 3 * 2 * 1 = 120 Write a function named factorial that takes as input an int and returns an int. Your function should verify that the input is non-negative If the input is not valid, return -1. If it is valid, your function should compute the value of the factorial using a for loop and return the value. Hint: The factorial of 0 is 1. Hint: A for loop will not execute if the condition is immediately false. Exercise 2 Create a method with the following header: public static int countTriple(String str) Define it as follows: 1 We'll say that a "triple" in a string is a char appearing three times in a row. Return the number of triples in the given string. The triples may overlap. countTriple("abcXXXabc") 1 countTriple("xxxabyyyycd") - 3 countTriple("a") > 0 Exercise 3 Create a method with the following header: public static boolean haveThree(int[] nums) Define it as follows: Given an array of ints, return true if the value 3 appears in the array exactly 3 times, and no 3's are next to each other. haveThree([3, 1, 3, 1, 3]) true haveThree([3, 1, 3, 3] false haveThree([3, 4, 3, 3, 4]) false
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
