Write a code for the following: PART1: RECURSION OBJECTIVES -Students know how t
ID: 3780297 • Letter: W
Question
Write a code for the following:
PART1: RECURSION
OBJECTIVES
-Students know how to apply the Recursion Algorithm to solve some small Math problems
-Review how to create and call the static functions
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
PART1: RECURSION
OBJECTIVES
Explanation / Answer
Here I created two recursive function of Fibonacci and factorial program with switch case :-
#include <iostream.h>
int factorial(int);
void main(void) {
int number;
cout << "Please enter a positive integer: ";
cin >> number;
do {
cout << "*Menu*"<<endl;
count<<"1 for factorial, 2 for fibbonaci and 3 for exit"<<endl;
switch (number)
{
case '1': int number1;
cout<<"Enter the number for factorial : "<<endl;
cin>>number1;
if (number1 < 0)
cout << "That is not a positive integer. ";
else
cout << number1 << " factorial is: " << factorial(number1) << endl;
cout << "Please enter a positive integer: ";
cin >> number;
break;
case '2': int number1;
cout<<"Enter number for fibonacci series : "<<endl;
cin>>number1;
cout << fib(number1) << endl;
cout << "Please enter a positive integer: ";
cin >> number;
break;
case '3':
cout << "Please enter a positive integer: ";
cin >> number;
break;
}
}
while( number <=3);
}
int fib(int x) {
if (x == 1) {
return 1;
} else {
return fib(x-1)+fib(x-2);
}
}
int factorial(int number1) {
int temp;
if(number1 <= 1) return 1;
temp = number1 * factorial(number1 - 1);
return temp;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.