CS141 programming II java Assignment 2 Chapter 12 OOD Chapt

CS141 programming II (java)

Assignment 2

Chapter 12 – OOD

Chapter 13 - Recursion

Assignment 2 worth 5 points, Due on 26/10/2016. Will deduct 1 point for each day late. Copying each other work will result in giving 0 to both.

Q1: Briefly define the term Waterfall model? And list the phases of the development process in order they are done? (5 points)

Q2: Fill in the table below by drawing the correct arrow that represent the relationship between classes? And Indicate Type of relationship: if it is an “IS-A” or “HAS-A”, or “USES” relationship if applicable(5 points)

Relationship

Draw

Type of relationship

Inheritance

Interface Implementation

Aggregation

Dependency

Q3: Given Question 5 from the previous Assignment1.   ( 10 points )

Draw the correct UML diagram to represent the relationships between all classes.

Add an Interface called Measurable and indicate in the diagram that the Employee class implements Measurable interface.

For each class in your diagram do the CRC card for each to indicate responsibilities and collaborators.

Measurable

             

Q4: Write and run the java program that computes the Factorial of a given number using a Recursive method.   ( 20 points )

Submit your program files as .java file that can be run. And submit in the document sample run for example given the value 4 the factorial is 24

Q5. What is the advantage and disadvantage of using recursion? ( 5 points )

Q6. Briefly explain what is infinite recursion, and what causes it to happen? ( 5 points )

Relationship

Draw

Type of relationship

Inheritance

Interface Implementation

Aggregation

Dependency

Solution

Hi, I have solved last three questions: Q4, 5, 6:

Q4)

Please find my code. Please let me know in case of any issue

public class MyFactorial {

  

// recursive method to calculate factorial

   public static int recursiveFactorial(int n){

      

       // base case : use to stop recursion

       if(n <= 0)

           return 1; // factorial of 0 is 1

      

       // recursive call

       return n*recursiveFactorial(n-1);

   }

  

   public static void main(String[] args) {

      

       System.out.println(\"Factorial(6): \"+recursiveFactorial(6));

   }

}

/*

Sample Run:

Factorial(4): 24

*/

Q5)

Advantage:

Some programs if you write using iterative then they are more comples and big, but if you write same program using recursion, then they can reduced to very few lines and very easy to read/understand.

Disadvantage:

Recursion uses more memory as compare to iterative(they use system memory for stack: to maintain function calls)

Since they use internal system stack, so it is very hard to debug the program

If you miss BASE case in recursion (this is IF statement , where you put condition to stop your recursion), then your recursion go in INFINITE loop

Q6)

Infinite recursion happens when a recursive function fails to stop recursing. So, if you miss to provide condition to stop your recursion, then it ends in infinite loop

Foe example:

// recursive method to calculate factorial

   public static int recursiveFactorial(int n){   

       // recursive call

       return n*recursiveFactorial(n-1);

   }

So here i have removed IF statemnet(base case to stop recursion), now ii will ends in infinite loop.

CS141 programming II (java) Assignment 2 Chapter 12 – OOD Chapter 13 - Recursion Assignment 2 worth 5 points, Due on 26/10/2016. Will deduct 1 point for each da
CS141 programming II (java) Assignment 2 Chapter 12 – OOD Chapter 13 - Recursion Assignment 2 worth 5 points, Due on 26/10/2016. Will deduct 1 point for each da
CS141 programming II (java) Assignment 2 Chapter 12 – OOD Chapter 13 - Recursion Assignment 2 worth 5 points, Due on 26/10/2016. Will deduct 1 point for each da

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site