# Missed school, need help getting started

• 03-09-2012, 07:52 PM
jcarosella10
Missed school, need help getting started
Hey guys I've missed class the last two weeks and missed the lesson on this, so i am lost on how to get started on this recursion problem.

Heres the problem: "In the language of an alien race, all words take the form of Blurbs. A Blurb is a Whoozit followed by one or more Whatzits. A Whoozit is the character x followed by zero or more y's. A Whatzit is a q followed be either a z or a d, followed by a Whoozit. Design and implement a recursive program that generates random Blurbs in this alien language."

Heres what i have so far
Code:

```//CLASS WHOOZIT import java.util.Random; public class Whoozit {     private String second;     private int num;     private Random rand;     private String Whoozit;     public Whoozit()     {         rand = new Random();         Whoozit = "x";         second = "y";         num = rand.nextInt(100);         makeWhoozit(Whoozit, second, num);     }     private void makeWhoozit(String x, String y, int times)     {         for(int counter = 0; counter < times; counter++)         {             Whoozit += second;         }     }     public String toString()     {         return Whoozit;     } } //CLASS WHATZIT import java.util.Random; public class Whatzit {     private int choice;     private Random rand;     private Whoozit whoozit;     private String whatzit;     public Whatzit()     {         rand = new Random();         whoozit = new Whoozit();         whatzit = "q";         choice = rand.nextInt(2);         makeWhatzit(choice, whoozit);     }     private void makeWhatzit(int num, Whoozit append)     {         if (num == 0)         {             whatzit += "d";         }         else         {             whatzit += "z";         }         whatzit += append;     }     public String toString()     {         return whatzit;     } } //CLASS BLURB import java.util.Random; public class Blurb {     private int num;     private Whoozit who;     private Whatzit what;     private Random rand;     private String blurb;     public Blurb()     {         who = new Whoozit();         what = new Whatzit();         rand = new Random();         num = rand.nextInt(100);         makeBlurb(who, what, num);     }     private void makeBlurb(Whoozit whos,  Whatzit whats, int iterations)     {         blurb = whos.toString();         for(int counter = 0; counter < iterations; counter++)         {             blurb += whats.toString();             whats = new Whatzit();         }     }     public String toString()     {         return blurb;     } }```
How would i make this recursive?
• 03-09-2012, 07:55 PM
Fubarable
Re: Missed school, need help getting started
Quote:

Originally Posted by jcarosella10
Hey guys I've been on vacation the last two weeks ...

This is not really our concern.

Quote:

...and am lost on how to get started on this recursion problem.
....
How would i make this recursive?
What in particular are you confused on? What have you tried? What errors are you seeing?
• 03-09-2012, 07:58 PM
jcarosella10
Re: Missed school, need help getting started
oh, its just that i have no clue where the recursive part should be. I've been reading through my code and cannot find where to put in a recursive statement
• 03-09-2012, 08:02 PM
Norm
Re: Missed school, need help getting started
One thing that would be helpful would be to make a list of what each term in the language means or how it is defined.
When I was learning programming languages, several of them were defined in a format called BNF.
A formal listing of the language's rules could make it easier to see the format and patterns.
• 03-09-2012, 08:04 PM
jcarosella10
Re: Missed school, need help getting started
ok thanks, ill try that now
• 03-09-2012, 08:08 PM
jcarosella10
Re: Missed school, need help getting started
So Blurb = Whoozit + n Whatzits
Whatzit = q + z *or* d + Whoozit
Whoozit = x + n ys
So the recursion needs to be in both the Blurb and the Whoozit classes?
• 03-09-2012, 08:17 PM
Norm
Re: Missed school, need help getting started
Looks like the random bit is the "or more" part of the language.
Can you write a method that generates a Blurb without any "or more" additions?
• 03-09-2012, 08:20 PM
jcarosella10
Re: Missed school, need help getting started
I'll try, its just confusing because i wasnt given a limit on how many trailing appendages there could be.
• 03-09-2012, 08:23 PM
Norm
Re: Missed school, need help getting started
There's an older guy at my sailing club like that. He just rambles on and on and on and on...
• 03-09-2012, 10:19 PM
jcarosella10
Re: Missed school, need help getting started
haha very funny. heres my new code, but when it prints the blurbs it outputs the memory address. Any clues to why?
Code:

```//DRIVER public class driver {     public static void main(String[] args)     {         System.out.println("Here are some random blurbs:");         System.out.println(Blurb.generateRandom());         System.out.println(Blurb.generateRandom());         System.out.println(Blurb.generateRandom());         System.out.println(Blurb.generateRandom());         System.out.println(Blurb.generateRandom());         System.out.println(Blurb.generateRandom());         System.out.println(Blurb.generateRandom());     } } //BLURB CLASS public class Blurb {     private Whoozit whoozit;     private OnePlusWhatzit whatzits;     public Blurb (Whoozit whoozit, OnePlusWhatzit whatzits)     {         this.whoozit = whoozit;         this.whatzits = whatzits;     }     public static Blurb generateRandom()     {         return new Blurb(Whoozit.generateRandom(), OnePlusWhatzit.generateRandom());     }     public String toString()     {         return whoozit.toString() + whatzits.toString();     } } //ABSTRACT CLASS ONEPLUSWHATZIT public abstract class OnePlusWhatzit {     static java.util.Random generate = new java.util.Random();     public static OnePlusWhatzit generateRandom ()     {         boolean doMore = generate.nextBoolean();         if (doMore)             return TwoPlusWhatzit.generateRandomTwo();         else             return OneWhatzit.generateRandomOne();     } } //CLASSTWOWHATZIT class TwoPlusWhatzit extends OnePlusWhatzit {     private Whatzit whatzit;     private OnePlusWhatzit whatzits;     public TwoPlusWhatzit (Whatzit whatzit, OnePlusWhatzit whatzits) {         this.whatzit = whatzit;         this.whatzits = whatzits;     }     public static TwoPlusWhatzit generateRandomTwo ()     {         return new TwoPlusWhatzit(Whatzit.generateRandom(), OnePlusWhatzit.generateRandom());     }     public String toString ()     {         return whatzit.toString() + whatzits.toString();     } } //CLASS ONEWHATZIT class OneWhatzit extends OnePlusWhatzit {     private Whatzit whatzit;     public OneWhatzit (Whatzit whatzit)     {         this.whatzit = whatzit;     }     public static OneWhatzit generateRandomOne ()     {         return new OneWhatzit(Whatzit.generateRandom());     }     public String toString ()     {         return whatzit.toString();     } } //CLASSWHOOZIT public class Whoozit {     private int numberOfYs;     private static java.util.Random generate = new java.util.Random();     public Whoozit(int numberOfYs)     {         this.numberOfYs = numberOfYs;     }     public static Whoozit generateRandom()     {         return new Whoozit(Math.abs(generate.nextInt()));     }     public String toString()     {         return "xy..." + numberOfYs + "...";     } }```
Output:

Here are some random blurbs:
xy...381535951...Whatzit@2961116fWhatzit@23bfa3a2
xy...1929679965...Whatzit@7a8ff303
xy...1452746471...Whatzit@9b88d2Whatzit@6333934dWh atzit@4f0bd71c
xy...560559566...Whatzit@125797cfWhatzit@34afa071
xy...825184086...Whatzit@764e75bcWhatzit@1913c652W hatzit@61413e5a
xy...516538825...Whatzit@264b898
xy...1415322755...Whatzit@22930462
• 03-09-2012, 10:23 PM
Norm
Re: Missed school, need help getting started
Post the output if you have questions about it.
• 03-09-2012, 10:26 PM
jcarosella10
Re: Missed school, need help getting started
just edited it into the last one
• 03-09-2012, 10:32 PM
Norm
Re: Missed school, need help getting started
Quote:

Whatzit@22930462
That is from the default toString() method for the Whatzit method. Add a toString() method that returns what you want to see.
• 03-09-2012, 10:37 PM
jcarosella10
Re: Missed school, need help getting started
That worked, thank you