The Artima Developer Community
Sponsored Link

Java Answers Forum
Using Recursion

5 replies on 1 page. Most recent reply: Oct 3, 2004 5:40 PM by Mele2004m

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
Mele2004m

Posts: 10
Nickname: nenny
Registered: Sep, 2004

Using Recursion Posted: Sep 28, 2004 6:34 PM
Reply to this message Reply
Advertisement
Problem 1
Make a program that follows the following requirements:

Ask the user for their full name. Example: “Dave Shin”
Display the following using a loop.
Dave Shin
ave Shin
ve Shin
e Shin
Shin
Shin
hin
in
n
Hints
You will have to use the following code/ideas
JOptionPane messageboxes
String Functions
Problem 2
Make a program that follows the following requirements:

Ask the user for their full name. Example: “Dave Shin”
Display the following using recursion.
Dave Shin
ave Shin
ve Shin
e Shin
Shin
Shin
hin
in
n
Hints
You will have to use the following code/ideas
JOptionPane messageboxes
String Functions


Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: Using Recursion Posted: Sep 28, 2004 9:00 PM
Reply to this message Reply
I know it is classroom assignemnt. Here is your solutions. There are two versions of each problem. Version II of each solution is better in the sense that they avoid creating new string objects.

package test;
 
import javax.swing.JOptionPane;
 
public class Recursion {
  public static void main(String[] args) {
    String name = getName();
    //printRecursively (name);
    //printRecursively (name, 0);
    //printInLoop_1(name);
    //printInLoop_2(name);
  }
 
  // Version I
  public static void printRecursively (String name ) {
    if ( name == null || name.length() == 0 ) {
      return;
    }
    System.out.println (name);
 
    // Call the method recursively
    printRecursively (name.substring(1));
  }
 
  // Version II
  public static void printRecursively (String name, int start ) {
    int len = 0 ;
 
    if ( name == null ) {
      return;
    }
    else {
      len =  name.length();
      if ( start >= len ) {
        return;
      }
    }
 
    for ( int i = start; i < len; i++ ) {
      System.out.print(name.charAt(i));
    }
 
    System.out.println ();
 
    // Call the method recursively
    printRecursively (name, ++start);
  }
 
  // Version I - Loop
   public static void printInLoop_1(String name){
     if ( name == null ) {
       return;
     }
 
     int len = name.length();
 
     for ( int i = 0 ; i < len; i++ ) {
       System.out.println(name.substring(i));
     }
   }
 
  // Version II - Loop
  public static void printInLoop_2(String name){
    if ( name == null ) {
      return;
    }
 
    int len = name.length();
 
    for ( int i = 0 ; i < len; i++ ) {
      for ( int j = i; j < len; j++ ) {
        System.out.print(name.charAt(j));
      }
      System.out.println();
    }
  }
 
 
  public static String getName() {
    String name = null;
    name = JOptionPane.showInputDialog("Please type your name.");
    return name;
  }
}

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Using Recursion Posted: Oct 2, 2004 1:20 AM
Reply to this message Reply
I think solution I is better

Doesn't the System.out.print(...) Method create Strings, even if you give it a single char?

In this case there would be created lots of String objects.

In my opinion calling 10 times System.out.print(char) is much slower than calling one time System.out.print(String) with a 10 characters long String.

Mele2004m

Posts: 10
Nickname: nenny
Registered: Sep, 2004

Re: Using Recursion Posted: Oct 3, 2004 5:00 PM
Reply to this message Reply
Kishori ur code is not work it's something wrong whent I was cope and paste to my Jcreator and run their's an error appear so here is the error
-*Class recursion is public,should be declared in a file named Recursion.java
-*Cannot resolve symbol method getName()

Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: Using Recursion Posted: Oct 3, 2004 5:11 PM
Reply to this message Reply
Please make sure you have copied and pasted this code in a file named Recursion.java . Make sure file name is correctly spelled. That's what your error says. It seems you have pasted this code in an existing file which is not named Recursion.java. If you wish to place this code in another java file then take out the modifier "public" from the class definition and then it will work.

Mele2004m

Posts: 10
Nickname: nenny
Registered: Sep, 2004

Re: Using Recursion Posted: Oct 3, 2004 5:40 PM
Reply to this message Reply
I already take public but theirs an error appear here is an error --- cannot resolve symble method getName()

Flat View: This topic has 5 replies on 1 page
Topic: About javax.comm package Previous Topic   Next Topic Topic: Servlet  Does have Comstructor ?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use