CSC/BIF 245 Objects and Data Abstraction Assignment #3 Recursion

$40.00

Category: You will Instantly receive a download link for .zip solution file upon Payment

Description

5/5 - (5 votes)

Question 1 (10 points)

Write a program that starts by displaying the following menu:

 

  1. Reverse number
  2. Count Digits
  3. Repeat
  4. Exit

– – – – – – – – – – – – –

Enter a choice:

 

Choice 1 invokes a recursive method that takes as a parameter an integer, n,  and returns another one with the digits reversed. For example, if n is 341, the method returns 143.

 

Choice 2 invokes a recursive method that takes as parameter an integer n and returns the number of digits in n. For example, if n is 67, the method returns 2.

 

Choice 3 invokes the recursive method repeat(String s, count n). The method creates and returns a new string s’ that is made by concatenating n copies of the parameter string s.  For example, calling this method with the parameters “Object” and 3 would return the string “ObjectObjectObject”. If  n is  zero, the method should return the empty string.

 

Choice 4 terminates the program.

 

Any other choice, will have the menu displayed again.

 

 

Question 2 (10 points)

 

The program starts by prompting the user for numbers and stores them in an array. Then the following menu is displayed:

 

  1. Even or odd
  2. Find max
  3. Adds up
  4. Sum
  5. Exit

— – – – – – –

Enter your choice:

 

Choice 1 prompts the user whether he/she wants to print even or odd numbers in the array and invokes a method that prints those numbers if found in the array.

 

Choice 2   invokes a recursive method that displays the max in the array.

 

Choice 3   invokes a recursive method that  returns true if we can split the array into two groups such that the sum of the numbers in one group is equal to that in the other group, false otherwise. For example, if the array is {3, 3}, the method returns true, if the array is {5, 10, 6, 3, 6}, the method returns true, if the array is {2, 4, 9, 10} the method returns false.

 

Choice 4 invokes a recursive method that accepts an array of integers and an int i  as parameters. The method returns true if  and only if we can choose numbers from the array  such that the sum of these numbers is equal to i but no two adjacent numbers in the array are chosen.

 

 

Question 3 (not graded – but a very good exercise!) We have seen in class the  recursive solution to the Towers of Hanoi problem. Write an iterative solution to the problem.

 

 

 

Notes

 

Since  you do not need to create an object of the class to invoke any of the methods in the above exericses,  you can declare all  methods static.  You can add as many parameters as needed to your methods. You can certainly find the solution to all questions online. If you look up the solutions, you will lose the chance of training and enhancing your problem-solving skills and you will not do well on the exam. Your choice!

 

 

 

 

Plagiarism and cheating policy

 

Cheating is a serious offense. If a student is found to have copied part or all of the assignment, he or she will receive a zero on the assignment.  During exams, if a student is caught cheating, he or she will receive a zero on the exam. This might result in the student getting an F on the course.  NO EXCUSES WILL BE ACCEPTED.  The same applies to the person providing others with material. The same applies to plagiarism (presenting someone else’s work as being one’s own work).

 

Deadline policy

 

Deadlines for submitting assignments will be announced in class or on the assignments. They are firm. Students  must respect them.  A student failing to meet the deadline will get 5% of the grade deducted per day late. A student cannot replace an assignment with another one.  If he or she fails to submit an assignment, a zero will be scored on the missed assignment. To illustrate, if the student submits assignments 1 and 3 only, he or she will get a 0 on  Assignment 2, Assignment 1 or 3  cannot compensate for the missed assignment.