Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1G. This program, unlike the previous 6 programs, will have several functions in

ID: 3791727 • Letter: 1

Question

1G.

This program, unlike the previous 6 programs, will have several functions in it: besides main, it willhave the following 7 functions:public static int triangle( int n )public static int multiply( int a, int b )public static void square( int size )public static void hollowSquare( int size )public static int factorial( int n )public static long fibonacci( int n )public static boolean prime( long n )These 7 functions should work as described below.Your main program should call each of these 7 functions. You decide the details of your main function.I don't much care what you decide to put in your main program, so it would be a good idea to use it toconvince yourself that your 7 functions are working properly.

MAKE SURE THAT INT N CAN BE INPUT BY USER AND INT A,B, AND INT SIZE TO BE INPUTED BY USER

1. public static int triangle( int n )return the triangle function of n. The triangle function of a number is the sum of all integers up to thatnumber. For instance, the triangle function of 5 is 1+2+3+4+5 == 15. No I/O in the function.

2. public static int multiply( int a, int b )

return the product of a and b. Don’t use the multiply operation (*), instead do repeated addition. Yourprogram should do the right thing for negative args.

3. public static void square( int size )

Output a square of *’s with the given size. For instance, square( 7 ) should output

*******

*******

*******

*******

*******

*******

*******

4. public static void hollowSquare( int size )

Output a hollow square of *’s with the given size. For instance, hollowSquare( 7 ) should output

******** (should be a hollow square)

* *

* *

* *

* *

* *

********

5. public static int factorial( int n )

Return n factorial. n factorial is the product of all positive integers up to n. For instance, factorial(5)should return 1*2*3*4*5 == 120. (Factorials of negative integers are not meaningful, you don’t needto worry about them.)

6. public static long fibonacci( int n )

Return the Fibonacci function of n. The Fibonacci function starts with Fibonacci(0) = 0, Fibonacci(1) = 1,and thereafter the Fibonacci function of a number is the sum of the Fibonacci function of the preceding2 numbers. So, Fibonacci(0) = 0 Fibonacci(1) = 1

Fibonacci(2) = 1

Fibonacci(3) = 2

Fibonacci(4) = 3

Fibonacci(5) = 5

Fibonacci(6) = 8, and so on

7. public static boolean prime( long n )

Return whether n is a prime number. A prime number, as you probably know, is a number >= 2 thathas no positive factors except one and itself. The 4 smallest prime numbers are 2, 3, 5, and 7. A decentalgorithm for determining whether n is prime is if n is less than 4, then it’s prime iff it’s greater than 1 otherwise, if n is even then it's not prime otherwise, for f taking on the values of 3, 5, 7, 9, 11, ... {  if f is greater than n/f, then we're done: n is prime  if n is divisible by f, then we're done: n is not prime }

Explanation / Answer

PROGRAM CODE:

package util;

import java.util.Scanner;

public class MathFunctions {

   //The triangle function of a number is the sum of all integers up to that number

   public static int triangle( int n )

   {

       int result = 0;

       for(int i=1; i<=n; i++)

           result += i;

       return result;

   }

  

   //returns the product of a and b

   public static int multiply( int a, int b )

   {

       if(a == 0 || b == 0)

           return 0;

       int result = Math.abs(b);

       for(int i=1; i<Math.abs(a); i++)

       {

           result += b;

       }

       if(a < 0 || b< 0)

           result *= -1;

       return result;

   }

   //Output a square of *’s with the given size

   public static void square( int size )

   {

       String line = "";

       for(int i=0; i<size; i++)

           line += "*";

       for(int i=0; i<size; i++)

           System.out.println(line);

   }

  

   //Output a hollow square of *’s with the given size

   public static void hollowSquare( int size )

   {

       String line = "";

       String hollowLine = "*";

       for(int i=0; i<=size; i++)

           line += "*";

      

       for(int i=0; i<size-2; i++)

           hollowLine += " ";

      

       hollowLine += "*";

      

       System.out.println(line);

      

       for(int i=0; i<size-2; i++)

           System.out.println(hollowLine);

       System.out.println(line);

  

   }

  

   //Returns n factorial

   public static int factorial( int n )

   {

       int result = 1;

       for(int i=1; i<=n; i++)

       {

           result *= i;

       }

       return result;

   }

  

   //Return the Fibonacci function of n

   public static long fibonacci( int n )

   {

       int n1=0,n2=1,result = 0;

       for(int i=2;i<n;++i)//loop starts from 2 because 0 and 1 are already printed

       {

          result=n1+n2;

          n1=n2;

          n2=result;

       }    

       return result;

   }

  

   //returns true if n is prime , false otherwise

   public static boolean prime( long n )

   {

       if(n%2 == 0)

           return false;

       else

       {

           int mid = (int)n/2;

           for(int i=3; i<=mid; i++)

           {

               if(n%i==0)

               {

                   return false;

               }

           }

       }

       return true;

   }

  

   public static void main(String[] args) {

       Scanner keyboard = new Scanner(System.in);

       int N, a, b, size;

       System.out.print("Enter the N value: ");

       N = keyboard.nextInt();

       System.out.print("Enter the value for a: ");

       a = keyboard.nextInt();

       System.out.print("Enter the value for b: ");

       b= keyboard.nextInt();

       System.out.print("Enter the size: ");

       size = keyboard.nextInt();

       System.out.println(" Triangle(n): " + triangle(N));

       System.out.println("Multiply(a,b): " + multiply(a, b));

       System.out.println("Square(size): " );

       square(size);

       System.out.println("Hollow Sqaure(size): ");

       hollowSquare(size);

       System.out.println("Factorial(n) : " + factorial(N));

       System.out.println("Fibonacci(n): " + fibonacci(N));

       System.out.println("Prime(n): " + prime(N));

   }

}

OUTPUT:

Enter the N value: 10

Enter the value for a: 12

Enter the value for b: 13

Enter the size: 6

Triangle(n): 55

Multiply(a,b): 156

Square(size):

******

******

******

******

******

******

Hollow Sqaure(size):

*******

* *

* *

* *

* *

*******

Factorial(n) : 3628800

Fibonacci(n): 34

Prime(n): false

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote