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

Write a Java program for the following: REQUIREMENT: Write an application that a

ID: 3571686 • Letter: W

Question

Write a Java program for the following:

REQUIREMENT:

Write an application that allows users to select the following functions.

n!

Factorial of an integer n where n provided from the keyboard

an

a power n, where a and n are int numbers provided from the keyboard

Sum (n)

Sum(n) = 1 + 2 + 3 + .. + n where n is an int provided from the keyboard

Sum (m, n)

Sum(m, n) = m + (m+1), (m+2) + … + n where m and n are int numbers provided from the keyboard

Fn

Fibonacci sequence Fn = Fn – 1 + Fn-2; F0 = 0 and Fn1 = 1

GCD (n,m)

The greatest common divisor (GCD) of two integers m and n; m > n where m, n are provided from the keyboard

For each function, the application will ask users to enter an integer number n then print out the result in the following format:

For function 1:

Factorial of 5 is 120 For function 2:   

2 to the power 3 is 8 For function 3:

Sum from 1 to 10 is: 55

Sum from 5 to 7 is 18

The Fibonacci at 10 is 55

Greatest Common Divisor (GCD) of 120 and 90 is 30

Notes:

-The application should allow users to continue to work on other tasks until they want to exit.

-All the above functions have to be defined as static methods in a separate class and the code should be written in the recursion algorithm

PSEUDO-CODE

-Provide the pseudo-code or flowchart of main

WHAT YOU NEED TO KNOW TO DO THIS LAB

-How to write a class only includes static methods -How to create a static method:

To create a static method, you just need to add the keyword static to the heading of the method. For example:  

                public static int factorial (int n) { .. }

               

-How to access static members of other class in main()

To access a static method of class FA2016LAB7_StaticRecursionFunction_yourLastName, we do not need to create an object of the class FA2016LAB7_StaticRecursionFunction_yourLastName.

To access a static method, we have to use the class name to call. The syntax to call a static method is, for example: to calculate the result of the factorial of 5, the syntax in the main() is:

                      int factorialResult = FA2016LAB7_StaticRecursionFunction_yourLastName.factorial(5);

-Learn how to form a recursion algorithm with 4 steps: base case, reduce problem, general solution and recursion algorithm

-How to write the code based on the recursion algorithm

-Review how to handle the menu to redisplay after finishing one task

-Review the syntax of switch statement

HOW TO DO THE PART1:

-Create the project FA2016LAB7_PART1

-Add class FA2016LAB7_StaticRecursionFunction_yourLastName then write 6 static methods by writing the code based on the recursion algorithm for each above function

-Add the driver class named as FA2016LAB7_AccessStaticMemberDemo. In main() write the code based on the above pseudo-code

PART2: BINARY SEARCH TREE DATA STRUCTURE

OBJECTIVES

-Students know how to implement the Binary Search Tree structure: How to insert a node to a tree, How to fetch, delete or update a node on the Binary Search tree.

-Also, students know how to display the information of all nodes on the tree

REQUIREMENT

Create an application that allows users can work on the information of students with the following tasks:

1.insert one new student

2.search the information of one student

3.delete one student

4.update the address of a student

5.show all students in the structure

6.exit the program

The users can continue using the program until they want to exit

During the process, the application should use Binary Search Tree as the data structure

When the tree is empty, you need to display the message to tell: “There is no student on the Binary Search Tree data structure”

To search a student, if the student does not exist in the data structure, display the message “The student cannot be found”; else display the information of the student

If delete a student successfully, display the message: “Remove the student successfully”

If update the information of a student, display the message: “Update sucessfully”

The information of a student that is stored in the database includes Student Id, last name, first name, address, phone number. The information of one student is displayed in the following format:

Student name:

Smith, James

Student ID:         

1234567

Address:              

123 Abrams Road Dallas, TX 75243

Phone:                

4691234567

ANALYZE AND DESIGN

-Provide the UML of the data type class relating to the student information

-Provide the pseudo-code or the flowchart of main()

WHAT YOU SHOULD KNOW TO DO THE LAB

-Review the loop to manage the menu to re-display after each task to allow users to continue using the program until users want to exit

-The switch statement to handle all tasks after users select one

-Review how to write the constructors, method toString to display information of one object -Learn about the Data structure Binary Search tree: initialize, findNode, insert, fetch, delete, update, show all the nodes

-How to create an object of the data type class

-How to access data members of the data type class

HOW TO DO THE LAB

-Create the project named FA2016LAB7_PART2_YourLastname

-Add data type class name FA2016LAB7_Student_yourLastName then declare all data members and provide constructors, toString

-Add data structure class of BinarySearchTree_yourLastName (using the code from the book on page 394 - 396 for your reference). Note that the code from the book missing delete the root, you should add the code to delete the root in 3 cases

-Add driver class named as FA2016LAB7_StudentInformation_yourLastName then forllow the pseudocode to write the code

HOW TO GRADE LAB7

ITEMS

SCORES

PART1:

Factorial

1

an

1

sum of 1 to n

1

sum of m to n

1

nth term of Fibonacci sequence fn

1

GCD (m,n)

1

Handle the menu and the loop

1

Compile success and comment – access Static members correctly

3

PART2:

Data type class FA2016LAB7_Student_yourLastName

1

Class about Binary Search Tree

1

-Add code for deleting the root in 3 cases

2

-Add showAll method

1

class FA2016LAB7_StudentInformation_yourLastName

-Handle menu

1

-Insert task

2

-Fetch task

2

-Update task

2

-Delete task

2

-ShowAll task

1

Compile success – qualified the requirements

3

comments

2

Lab7 scores

30

HOW TO TURN IN THE LAB

Part 1:

Psuedo-code

FA2016LAB7_StaticRecursionFunction_yourLastName.java

FA2016LAB7_AccessStaticMemberDemo.java

FA2016LAB7_StaticRecursionFunction_yourLastName.class FA2016LAB7_AccessStaticMemberDemo.class Part 2:

UML and pseudo-code

FA2016LAB7_Student_yourLastName.java

FA2016LAB7_StudentInformation_yourLastName.java

BinarySearchTree_yourLastName.java

FA2016LAB7_Student_yourLastName.class

FA2016LAB7_StudentInformation_yourLastName.class

BinarySearchTree_yourLastName.class

n!

Factorial of an integer n where n provided from the keyboard

an

a power n, where a and n are int numbers provided from the keyboard

Sum (n)

Sum(n) = 1 + 2 + 3 + .. + n where n is an int provided from the keyboard

Sum (m, n)

Sum(m, n) = m + (m+1), (m+2) + … + n where m and n are int numbers provided from the keyboard

Fn

Fibonacci sequence Fn = Fn – 1 + Fn-2; F0 = 0 and Fn1 = 1

GCD (n,m)

The greatest common divisor (GCD) of two integers m and n; m > n where m, n are provided from the keyboard

Explanation / Answer

Here I have created the solution for the first question, The second part could not be addressed because of the length of first question which itself contains more than 6 methods to be developed. Please find the details of the first part below :

Here we have two java files, Main and CommonUtil.

Main contains the main method and Commonutil contains the rest of the methods. As asked , methods have been created as static and whenever it was asked to write the code with recursion, same has been provided. Please find the code below :

Main.java

import java.util.Scanner;
public class Main {

   public static void main(String[] args) {
       Scanner sc=new Scanner(System.in);
       int input=0;
       boolean flag=true;
       while(flag){
           System.out.println("1. Factorial");
           System.out.println("2. Exponent");
           System.out.println("3. Sum(n)");
           System.out.println("4 Sum(m,n)");
           System.out.println("5. Fibonacci series");
           System.out.println("6. GCD of two numbers");
           System.out.println();
       System.out.println("Please enter your choice by selecting 1 / 2/ 3/ 4/ 5/ 6");  
       input=sc.nextInt();
       if(input==1){
           System.out.println("You have selected Factorial");
           System.out.println("Please enter the number");
           int paramInput=sc.nextInt();
           int result=CommonUtil.factorial(paramInput);
           System.out.println("Factorial of "+paramInput +" is :"+result);
       }
       else if(input==2){
           System.out.println("You have selected Exponent");
           System.out.println("Please enter the numbers ");
           System.out.println("Enter first number");
           int input1=sc.nextInt();
           System.out.println("Enter second number");
           int input2=sc.nextInt();
           double result=CommonUtil.exponent(input1, input2);
           System.out.println(input1+ "to the power "+input2+" is : "+result);
          
          
       }
       else if(input==3){
           System.out.println("You have selected sum(n)");
           System.out.println("Please enter the number");
           int paramInput=sc.nextInt();
           int result=CommonUtil.getSum(paramInput);
           System.out.println("Sum upto "+paramInput+" is :"+result);
       }
       else if(input==4){
           System.out.println("You have selected sum(m,n)");
           System.out.println("Please enter the numbers ");
           System.out.println("Enter first number");
           int input1=sc.nextInt();
           System.out.println("Enter second number");
           int input2=sc.nextInt();
           if(input1<input2){
           int result=CommonUtil.getSum(input1, input2);
           System.out.println("Sum of "+input1+" and"+input2+" is :"+result);
           }else if(input1>input2){
               int result=CommonUtil.getSum(input2, input1);
               System.out.println("Sum of "+input1+" and"+input2+" is :"+result);
           }else if(input1==input2){
               System.out.println("Both Numbers cannont be same");
           }
          
       }
       else if(input==5){
           System.out.println("You have selected Factorial");
           System.out.println("Please enter the number");
           int paramInput=sc.nextInt();
           System.out.println("The Fibonacci series is :");
           CommonUtil.getFibonacci(paramInput);
          
       }
       else if(input==6){
           System.out.println("You have selected GCD of two numbers");
           System.out.println("Please enter the numbers ");
           System.out.println("Enter first number");
           int input1=sc.nextInt();
           System.out.println("Enter second number");
           int input2=sc.nextInt();
           int result=CommonUtil.getGcd(input1, input2);
           System.out.println("GCD of "+input1+" and "+input2+" is :"+result);
       }
      
       System.out.println("Press Any Key to Continue, To exit type EXIT");
       String choice=sc.nextLine();
       choice=sc.nextLine();
       if(choice.equalsIgnoreCase("EXIT")){
           flag=false;
       }else if(!choice.equalsIgnoreCase("EXIT")){
           continue;
       }
      
       }
      
   }
  
}

CommonUtil.java

public class CommonUtil {

   public static int factorial(int num){
       int fact=1;
       if(num==0){
           fact=1;
       }
       else{
           fact=num*factorial(num-1);
       }
       return fact;
   }
  
   public static double exponent(int m,int n){
       double result=1;
       double a=Double.valueOf(m);
       double b=Double.valueOf(n);
       result=Math.pow(a,b);
       return result;
   }
  
   public static int getSum(int n){
       int sum=0;
       for(int i=1;i<=n;i++){
           sum=sum+i;
       }
       return sum;
   }
  
   public static int getSum(int m,int n){
       int sum=0;
       for(int i=m;i<=n;i++){
           sum=sum+i;
       }
       return sum;
   }
  
   public static void getFibonacci(int limit){
       int n1=0,n2=1,n3,i,count=limit;
       System.out.print(n1+" "+n2);//printing 0 and 1
      
       for(i=2;i<=count;++i)//loop starts from 2 because 0 and 1 are already printed
       {
       n3=n1+n2;
       System.out.print(" "+n3);
       n1=n2;
       n2=n3;
       }
       System.out.println();
   }
  
   public static int getGcd(int n1,int n2){
       int r=1;
       while(n2 != 0)
{
r = n1 % n2;
n1 = n2;
n2 = r;
}

return n1;
   }
}

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