Access Millions of academic & study documents

Java Recursion Example

Content type
User Generated
Subject
Programming
Type
Lab
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
Showing Page:
2/6
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
Showing Page:
3/6

Sign up to view the full document!

lock_open Sign Up
End of Preview - Want to read all 6 pages?
Access Now
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.
Studypool
4.7
Indeed
4.5
Sitejabber
4.4