How to mutate a String with certain random values?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JimmyJava694
    New Member
    • Oct 2007
    • 6

    How to mutate a String with certain random values?

    Hey everyone,

    I need help trying to mutate a String.

    For example.. I ask the person to input a number.
    Then I ask them to input a String.
    (The string they input can be any length, but must consist of only letters (A,C,G, or T)

    The number they input is the amount of 'random' implications applied to that String.. and replaces a letter in a random position with either (A,C,G, or T)

    I can assume that person types in the correct string (Exceptions don't matter)
    Finally, I must Initialize this random number with the random seed value
    “3735928559” (eg create “... = new Random(37359285 59L);’


    So basically I know I have to implement some code to randomly select a position in the String, and "switch" it with a random character (A,C,G, or T)
    It can be switched as many times as "n" is.. and "A" could in the end be switched to "A".

    The only output should be a single line displaying the completed mutation.
    Any help would be appreciated, I'll post my code once I've made a bit more progress.

    Thanks!
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    Well, a String is, in itself, immutable. So there are 2 ways I can think to tackle this problem:

    1) Every time you want to switch a character, you have to create a new String. Bad idea - it'll get messy.

    2) Get the original string and create a character array out of it. When switches are made, make them in the array. Finally, create a new String with the modified array.

    Comment

    • JimmyJava694
      New Member
      • Oct 2007
      • 6

      #3
      Originally posted by Ganon11
      Well, a String is, in itself, immutable. So there are 2 ways I can think to tackle this problem:

      1) Every time you want to switch a character, you have to create a new String. Bad idea - it'll get messy.

      2) Get the original string and create a character array out of it. When switches are made, make them in the array. Finally, create a new String with the modified array.

      Hmm, , but can't I create a new String which consists of the character(s) in the old String, with the character to be mutated replaced by the mutated character?

      My code is:

      [PHP]import java.util.Scann er;
      import java.util.Rando m;
      public class Mutation
      {
      public static void main(String[]args)
      {

      Random rand = new Random(37359285 59L);
      Scanner scan = new Scanner(System. in);
      System.out.prin tln("Please choose a number:");

      int number = scan.nextInt();
      number = rand.nextInt();

      System.out.prin tln("Please input a string:");
      String Line = scan.next();

      for (int count =0; count <= Line.length(); count++)
      {
      char Lines = Line.charAt(ran d.nextInt(Line. length()));
      }
      System.out.prin tln(Line);

      }
      }[/PHP]

      Comment

      • JimmyJava694
        New Member
        • Oct 2007
        • 6

        #4
        By the way, arrays cannot be implemented as I'm in Java basics
        i.e (They have not been taught; cannot be used).

        Comment

        • JimmyJava694
          New Member
          • Oct 2007
          • 6

          #5
          My code now is this.. thanks!

          [PHP]import java.util.Scann er;
          import java.util.Rando m;
          public class Mutation
          {
          public static void main(String[]args)
          {

          Random generator = new Random(37359285 59L);
          Scanner scan = new Scanner(System. in);
          System.out.prin tln("Please choose a number:");
          int number = scan.nextInt();

          double r = generator.nextD ouble();
          System.out.prin tln("Please input a string:");
          String Line = scan.next();
          String Line2 = Line;
          int position = generator.nextI nt(Line.length( ));
          int length = Line.length();


          char chars= (char)(generato r.nextInt(lengt h));



          System.out.prin tln(number);
          System.out.prin tln(length);

          System.out.prin tln(chars);
          System.out.prin tln(Line2);
          }
          }
          [/PHP]

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Have a look at the StringBuffer or StringBuilder classes.

            kind regards,

            Jos

            Comment

            • JimmyJava694
              New Member
              • Oct 2007
              • 6

              #7
              Originally posted by JosAH
              Have a look at the StringBuffer or StringBuilder classes.

              kind regards,

              Jos

              Is this the only way to do this?

              The program needs to do the following (n) times
              - Picks a random position in the String
              - Changes the letter in that position to one of A, C, G, and T, selected at random. Note that it is possible for the letter to stay the same (for example, A is randomly selected as the new letter for a position which the letter was already A)

              Once the n mutations have been applied to the original String, the program displays the result.

              Is there a more basic way this could be completed? Please show/tell me.

              Comment

              • Ganon11
                Recognized Expert Specialist
                • Oct 2006
                • 3651

                #8
                Originally posted by Ganon11
                1) Every time you want to switch a character, you have to create a new String. Bad idea - it'll get messy.
                This is a simpler way to code it, and will give you the correct solution. You'll have to look up the String's .substring method (try a Google search for String.java) to use it, but this function isn't too tough to learn.

                I think it's a bad solution because it's going to create (n) Strings, but for your purposes, this probably won't change much.

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Originally posted by JimmyJava694
                  Is there a more basic way this could be completed? Please show/tell me.
                  I think it's pretty basic already: given a String you can construct a StringBuilder.
                  Given the length of the String you can find a random number in the range 0 to the
                  length of the String. You can generate a random number in the range 0, 1, 2, 3
                  and pick the corresponding character from the String "ACGT" and put that in the
                  StringBuilder.

                  Also you could use a char[] but if I remember well you don't want that.

                  kind regards,

                  Jos

                  Comment

                  • JimmyJava694
                    New Member
                    • Oct 2007
                    • 6

                    #10
                    Originally posted by JosAH
                    I think it's pretty basic already: given a String you can construct a StringBuilder.
                    Given the length of the String you can find a random number in the range 0 to the
                    length of the String. You can generate a random number in the range 0, 1, 2, 3
                    and pick the corresponding character from the String "ACGT" and put that in the
                    StringBuilder.

                    Also you could use a char[] but if I remember well you don't want that.

                    kind regards,

                    Jos

                    Is there any way this can be implemented with the code I have currently?

                    [PHP]import java.util.Scann er;
                    import java.util.Rando m;
                    public class Mutate
                    {
                    public static void main(String[]args)
                    {
                    String choices = "ACGT", input = "", output = "";
                    Random generator = new Random(37359285 59L);
                    Scanner scan = new Scanner(System. in);
                    System.out.prin tln("Please choose a number:");
                    int number = scan.nextInt();
                    scan.nextLine() ;

                    System.out.prin tln("Please input a string:");
                    input = scan.nextLine() ;

                    for (int i = 0; i < number; i++)
                    {
                    char randomChar = choices.charAt( generator.nextI nt(choices.leng th()));
                    char randomPos = input.charAt(ge nerator.nextInt (input.length() ));

                    System.out.prin t(randomPos);

                    System.out.prin t(randomChar);
                    }


                    }
                    }[/PHP]

                    I think I have the program nearly completed, what else would I have to put in for it to do what I need?

                    Could somebody supply the code, as I generally have an easy time understanding the concept, but the actual mechanics of Java code is what I need to know. Thank you JosAH, and Ganon

                    Comment

                    Working...