Question

In: Computer Science

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

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 }

Solutions

Expert Solution

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("\nTriangle(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


Related Solutions

Write a C++ program which consists of several functions besides the main() function. The main() function,...
Write a C++ program which consists of several functions besides the main() function. The main() function, which shall ask for input from the user (ProcessCommand() does this) to compute the following: SumProductDifference and Power. There should be a well designed user interface. A void function called SumProductDifference(int, int, int&, int&, int&), that computes the sum, product, and difference of it two input arguments, and passes the sum, product, and difference by-reference. A value-returning function called Power(int a, int b) that...
This program must have 9 functions: •main() Controls the flow of the program (calls the other...
This program must have 9 functions: •main() Controls the flow of the program (calls the other modules) •userInput() Asks the user to enter two numbers •add() Accepts two numbers, returns the sum •subtract() Accepts two numbers, returns the difference of the first number minus the second number •multiply() Accepts two numbers, returns the product •divide() Accepts two numbers, returns the quotient of the first number divided by the second number •modulo() Accepts two numbers, returns the modulo of the first...
Java It would be nice if you use many Functions. rite a method, besides main(), which...
Java It would be nice if you use many Functions. rite a method, besides main(), which will calculate the lowest common multiple (LCM) of two numbers. For example, the multiples of 4 are 4, 8, 12, 16, 20, 24 …, and the multiples of 6 are 6, 12, 18, 24 …. The LCM is 12. Do NOT use any code from the Internet! Here are some more examples: 3:5 LCM is 15 4:8 LCM is 8 5:7 LCM is 35...
Write an IPO diagram and Python program that has two functions, main and determine_grade. main –...
Write an IPO diagram and Python program that has two functions, main and determine_grade. main – Should accept input of five numeric grades from the user USING A LOOP.   It should then calculate the average numeric grade.    The numeric average should be passed to the determine_grade function. determine_grade – should display the letter grade to the user based on the numeric average:        Greater than 90: A 80-89:                 B 70-79:                 C 60-69:              D Below 60:           F Modularity:...
python code Write a simple calculator: This program must have 9 functions: •main() Controls the flow...
python code Write a simple calculator: This program must have 9 functions: •main() Controls the flow of the program (calls the other modules) •userInput() Asks the user to enter two numbers •add() Accepts two numbers, returns the sum •subtract() Accepts two numbers, returns the difference of the first number minus the second number •multiply() Accepts two numbers, returns the product •divide() Accepts two numbers, returns the quotient of the first number divided by the second number •modulo() Accepts two numbers,...
Split the main function given into multiple functions. You have been given a very simple program...
Split the main function given into multiple functions. You have been given a very simple program that performs basic operations (addition, subtraction, editing) on two randomly generated integer vectors. All functionality has been included in main, causing code segments to be repeated as well as diminishing the readability. Rewrite the program by grouping calculations and related operations into functions. In particular, your program should include the following functions. InitializeVectors: This is a void function that initializes the two vectors by...
Unlike carbohydrates, nucleus acids, and lipids, proteins have diverse functions in a cell. Proteins are polymers...
Unlike carbohydrates, nucleus acids, and lipids, proteins have diverse functions in a cell. Proteins are polymers of the same subunits, amino acids. Discuss how the structure of amino acids allows proteins to perform so many functions. At least 250 words Please cite two sources
In C++ Prototype your functions above "main" and define them below "main"; Write a program that...
In C++ Prototype your functions above "main" and define them below "main"; Write a program that uses two identical arrays of at least 20 integers. It should call a function that uses the bubble sort algorithm to sort one of the arrays in ascending order. The function should keep count of the number of exchanges it makes. The program then should call a function that uses the selection sort algorithm to sort the other arrays. It should also keep count...
In C++ prototype functions above "main" and define them below "main"; Write a program that uses...
In C++ prototype functions above "main" and define them below "main"; Write a program that uses two identical arrays of at least 20 integers. It should call a function that uses the bubble sort algorithm to sort one of the arrays in ascending order. The function should keep count of the number of exchanges it makes. The program then should call a function that uses the selection sort algorithm to sort the other arrays. It should also keep count of...
Intro C++ Programming Chapter 6 Functions You have been tasked to write a new program for...
Intro C++ Programming Chapter 6 Functions You have been tasked to write a new program for the Research Center's shipping department. The shipping charges for the center are as follows: Weight of Package (in kilograms)                Rate per mile Shipped 2 kg or less                                                      $0.05 Over 2 kg but no more than 6 kg    $0.09 Over 6 kg but not more than 10 kg    $0.12 Over 10 kg    $0.20 Write a function in a program that asks for...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT