Access over 20 million homework & study documents

search

Java Recursion Example

Subject

Programming

Type

Lab

Rating

Showing Page:
1/6
import java.ul.*;
public class Recursion6
{
/**
* Start of class. Must enter a commandline argument and it Must be an integer
*/
public stac void main(String [] arguments)
{
if(arguments.length == 0)
{
System.out.println("ERROR: Enter at least one commandline argument.");
}
else
{
/**
* gets the commandline input and converts to an integer
* plugs into methods for later use
*/
try
{
int x;
x = Integer.parseInt(arguments[0]);
Recursion6.reverse(x);

Sign up to view the full document!

lock_open Sign Up
Recursion6.forward(0, x);
Integer result = Recursion6.sum(x);
System.out.println(x + "! = " + result);
Recursion6.twoPower(0, x);
System.out.println("\nMethod4 - SUM is " + method4(x));
}
//catch statement for string inputed into commandline
catch (NumberFormatExcepon e)
{
System.out.println("you must enter an integer");
}
}
}
//method for prinng astericks.
//keeps prinng unl the amount entered into the commandline
public stac String astericks(String xk)
{
//convert x to a integer
int k = Integer.parseInt(xk);
//K is always used for recursion sequence.
//X is for the amount entered
if(k == 0)

Sign up to view the full document!

lock_open Sign Up
return xk;
if(k > 0) {
k = k - 1;
astericks("*" + "*");
}
return xk;
}
//method for prinng in reverse order
//keeps prinng unl entered reaches zero
public stac void reverse(Integer x)
{
if(x == 0)
{
System.out.println(x);
}
else
{
System.out.print(x + ", ");
Recursion6.reverse(x-1);
}
}

Sign up to view the full document!

lock_open Sign Up

Unformatted Attachment Preview

import java.util.*;public class Recursion6{ /** * Start of class. Must enter a commandline argument and it Must be an integer */ public static void main(String [] arguments) { if(arguments.length == 0) { System.out.println("ERROR: Enter at least one commandline argument."); } else { /** * gets the commandline input and converts to an integer * plugs into methods for later use */ try { int x; x = Integer.parseInt(arguments[0]); Recursion6.reverse(x); Recursion6.forward(0, x); Integer result = Recursion6.sum(x);System.out.println(x + "! = " + result); Recursion6.twoPower(0, x); System.out.println("\nMethod4 - SUM is " + method4(x)); } //catch statement for string inputed into commandline catch (NumberFormatException e) { System.out.println("you must enter an integer"); } } } //method for printing astericks. //keeps printing until the amount entered into the commandline public static String astericks(String xk) { //convert x to a integer int k = Integer.parseInt(xk); //K is always used for recursion sequence. //X is for the amount entered if(k == 0)return xk;if(k > 0) {k = k - 1;astericks("*" + "*");}return xk; } //method for printing in reverse order //keeps print ...
Purchase document to see full attachment
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

Anonymous
Really helpful material, saved me a great deal of time.

Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4