• 0

[Java] working with recursive methods


Question

I know this will be a pretty easy one to solve but it eludes me for the time being and since im really up fulltime on workload right now i havent been able to sit down much and look it over carefully. For one assignment I was asked to create a program that given a string of N number of characters (meaning it could be any length the user desires) that it be placed centered on screen with some symbol character (such as ***) covering either side of it where empty spaces would be. Should the text be too big to fit in one line, then the program should sub divide it and print it in as many lines necessary. Now for this problem specifically imma be working with the assumption that the max number of chars that can the user can input per line are 80 (thats the min default for the linux console screen) ... This all would be real easy were I to use for's or while's but the purpose of this assignment is to create the program using the recursive technique..meaning creating a class with different methods inside that call each other, and one of them has to keep calling itself under an if condition to keep itself in aloop until that condition is met. Maybe I've jumbled myself a lot right now for u guys hehee...but hopefully someone will see through this mess and help me out ;)

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

The lack of paragraphs confuses and infuriates us! .. But without the futurama reference, what exactly do you want to do?

Maybe an example, eg. show the input, and what you want the output to look like.

Link to comment
Share on other sites

  • 0

    //There is a variable called MAXIMUM_LINE somewhere in the code that will determine how many chars per line
    private static int MAXIMUM_LINE = 20;
     

    //
    // str -> the string
    // count -> the counter. adds one each iteration to check if we have reached the end of the string
    // len -> stores the length of str
    //
    public static void RecursiveMeth(String str, int count, int len)
    {
        //  If we have reached the end of the line, then switch to another
        if(count == MAXIMUM_LINE)
        {
            text.setText(text.getText() + "\n");
            
            //  Add MAXIMUM_LINE to itself to it will match count next time around
            MAXIMUM_LINE += MAXIMUM_LINE;            
        }
        
        //  If we have reached the end of the string then step out
        if (count == len)
        {
            return;
        }
        
        //text.setText(text.getText() + str.charAt(count));        
        text.setText(text.getText() + "*");
        RecursiveMeth(str, count + 1, len);
        
        return;
    }

You can do it without writing character by charecter in the textbox, which would probably be bad for BIG strings. But that's up to you to figure out how :)

Hope it helps!

Link to comment
Share on other sites

  • 0

thnx a bunch sig...that does gimme some reference to work on! :) though im a bit of a beginner hehe, and im not sure im allowed to use yet all of the classes you are calling but we shall have to see...mainly I have to do it by means of a

System.out.print( type command

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.