Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Pls complete all 3 exercises as on java file Exercise 1 Create a file named A4.java. Place all your code in this file. The factorial

image text in transcribedPls complete all 3 exercises as on java file

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 have Three(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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional SQL Server 2000 Database Design

Authors: Louis Davidson

1st Edition

1861004761, 978-1861004765

More Books

Students also viewed these Databases questions

Question

How do the side effects compare?

Answered: 1 week ago

Question

Does the firm leverage its core competencies?

Answered: 1 week ago

Question

What is cost plus pricing ?

Answered: 1 week ago

Question

1. What are the types of wastes that reach water bodies ?

Answered: 1 week ago

Question

Which type of soil has more ability to absorb water?

Answered: 1 week ago