I need help with the following code. I have the correct production rules but can
ID: 3820240 • Letter: I
Question
I need help with the following code. I have the correct production rules but can't figure out how to correctly determine the next generation if the current generation isn't: A, B, +, or -. If the current generation is A my code will correctly display the next generation as: +B-A-B+. However, if I start of with +B-A-B+ as the current generation, the next generation is only computed as: +. I understand I need to implement a for loop for the getProd() method but I can't seem to figure this out and I have been going crazy because I can't figure it out.
Arrowhead Curve get Prod public class ArrowheadCurve Given the character, return the appropriate production for that character. Implements the Sierpinski Arrowhead Curve refer to https://en. Wikipedia.org/wiki/L-system Example 5 Sierpinski triangle (make sure to scroll down to the part about the Sierpinski Arrovhead Curve) eparam c the character to expand eretum the appropriate production for the given character. public static String get Prod char c) TODO: replace with your inplementation Scanner reader new Scanner System.in) reader next charAt (0) return "c"; end get Prod Given the String representing the current generation, return a new String that is the result of applying the get Prod rules to each character in the current generation. eparam curGen a String that is the current generation ereturn a new String that is the result of applying the get Prod rules to each character in the current generation. public static String nextGen (String curGen) TODO: replace with your inplementation return end nextGenExplanation / Answer
No need of a loop you can use the for loop you can use nextLine() method for read the full interger if you are using the .next().charAt(0) returns the 0th element of that string thats why this things happening. This code is for the entire line you get in the string .
public static String getProd()
{
// string for storing the input
String c=null;
Scanner reader=new Scanner(System.in);
//reading the line and storing it in to c
c=reader.nextLine();
return c;
}
if the input is like that :
john merry
then it store like this
c="john merry"
if you want word by word you can use next()
public Static String[] getProd()
{
//string array for storing the value
String[] c=null;
// scanner object
Scanner reader=new Scanner(System.in);
int i=0;
while(reader.hasNext())
{
// reading each word form the input each word is stored in separate position in array
c[i]=reader.next();
// incrementing i
i++;
}
return c;
}
eg
if the input is like that :
john merry
then it store like this
c[0]="john"
c[1]="merry"