Results 1 to 20 of 22
Thread: Need help with Arrays
- 10-07-2008, 07:17 AM #1
Member
- Join Date
- Oct 2008
- Posts
- 19
- Rep Power
- 0
Need help with Arrays
This is what I am given:
In this exercise we want to simulate the effects of mutations on a DNA string (with at most 1000 characters). For simplicity we will only consider a selected subset of chromosomal mutations. We would simulate the following (simplified) mutations:
Amplification (amp) : this inserts a duplicate copy of the specified region beside it;
Deletion (del) : this deletes the selected region;
Inversion (inv) : this reverses the orientation of the specified region.
The first line of the input consists of a string (with at most 1000 characters) that denotes the DNA sequence. The second line consists of a positive integer n denoting the number of mutations. The next n lines specify the mutations, each on a single line.
Each line representing a mutation consists of (1) the mutation command (which can be amp, del, or inv), (2) the start index (inclusive), and (3) the end index (inclusive). The first character in the DNA sequence has index 0. All commands are in lower case.
For example "amp 2 4" on "AGCTAGATT" results in "AGCTACTAGATT" as CTA is duplicated. The duplicated part is marked in blue for your reference.
The following is always true about the input and the DNA at every point of time:
There will always be a command, followed by a space, followed by an integer value (denoting the start index), followed by a space, followed by an integer value (denoting the end index).
There is always only a single space between the command and the start index.
There is always only a single space between the start index and the end index.
All indices are inclusive.
The start index is always smaller or equal to the end index.
All DNA strings will consist only of 4 possible characters (in uppercase) which are 'A', 'T', 'C', 'G'.
The number of commands is always exactly equals to n.
The length of a DNA string will always be at least 1 and at most 1000 at any point of time.
The input mutation commands are always in lower case.
The mutations have been greatly simplified and you should model your mutations only in the descriptions given here.
The output consists of a single line containing the modified string.
It is a must to work on character array.
And the skeleton i'm provided with:
import java.util.*;
public class Mutations
{
public static void main(String[] args)
{
Scanner stdIn = new Scanner(System.in);
String dna = stdIn.nextLine();
int count = stdIn.nextInt(); // number of mutation commands
int dnaLength = dna.length();
char[] dnaArray = new char[1000];
for (int i = 0; i < dna.length(); i++)
{
dnaArray[i] = dna.charAt(i);
}
/*
* Type your code here.
* Make sure you remember to update the dnaLength variable!
*/
/*
* We cannot simply use the dnaArray because it has a length of 1000.
* Therefore we should only take a substring of its proper length.
*/
String output = new String(dnaArray);
output = output.substring(0, dnaLength);
System.out.println(output);
}
}
- 10-07-2008, 07:45 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
'm not clear your question, where you stuck with.
- 10-07-2008, 07:52 AM #3
Hmm, dietgal provided with a skeleton..That figures! (pun intended).
To get help here, you'll need to be specific about what you're having a problem understanding. I mean, you didn't really think you could dump your assignment here and someone would do your homework for you, now did you?
Take a look at How To Ask Questions The Smart Way, it'll help you to rephrase your question to get useful answers.
db
- 10-07-2008, 07:55 AM #4
Member
- Join Date
- Oct 2008
- Posts
- 19
- Rep Power
- 0
Yea, I know I shouldn't.
But I am really lost in this chapter and although this is supposed to be the easier question, I can't do it, at all.
Would appreciate any help as long as it can get me started. Please.
- 10-07-2008, 08:01 AM #5
To get you started? OK.
The Java™ Tutorials
db
- 10-07-2008, 08:04 AM #6
Member
- Join Date
- Oct 2008
- Posts
- 19
- Rep Power
- 0
I mean, to get me started with this question.
I've googled about character array but still couldn't compile without errors.
- 10-07-2008, 08:12 AM #7
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
- 10-07-2008, 08:31 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Yes, send here to see your error. Seems to me your char type array declaration is fine. So where are you stuck with?
- 10-07-2008, 08:59 AM #9
Member
- Join Date
- Oct 2008
- Posts
- 19
- Rep Power
- 0
This is the changes I made, compliable.Java Code:String mutation = stdIn.nextLine(); int num1 = stdIn.nextInt(); int num2 = stdIn.nextInt(); if (mutation.equals("amp")) { } else if (mutation.equals("del")) { } else (mutation.equals("inv")) { }
However I am not sure how to do the effects that amp, del and inv are supposed to have, thus the blanks within the braces.
And here's the sample run provided:
Java Code:$ javac Mutations.java $ java Mutations AAAGGCCTTAGCTTAGATTACGATCG 1 amp 2 7 AAAGGCCTAGGCCTTAGCTTAGATTACGATCG
- 10-07-2008, 09:06 AM #10
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
Well, at least give it a try, we are not simply going to do it for you. Carefully read the description for what they are to do again, and then at least try. We will help you correct it, or nudge you in the proper direction, but we are not going to do it for you.
I will say, however, that you should make three methods to perform the actions and simply call those methods from within the if blocks rather than doing the actual processing there.
- 10-07-2008, 09:16 AM #11
Member
- Join Date
- Oct 2008
- Posts
- 19
- Rep Power
- 0
How about this? Is my definition of the statement correct?
And I've another question, how do I make use of theJava Code:if (mutation.equals("amp")) { System.arraycopy(dnaArray, num1, dnaArray, num1, num2); }
given in the skeleton?Java Code:int count
By the way, I think I haven't learnt to call methods...
- 10-07-2008, 09:25 AM #12
Member
- Join Date
- Oct 2008
- Posts
- 19
- Rep Power
- 0
And let me know why the above couldn't work. Thanks.Java Code:else if (mutation.equals("del")) { return (s.substring(0,dna.length)+s.substring(num1) && s.substring(0,dna.length)+s.substring(num1)); }
- 10-07-2008, 09:29 AM #13
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
Okay, lets take a look at this. amp means you are insert a copy of the region beside it and num1 is the start index and num2 the end index.
arraycopy's arguments are as follows
1. The source array
2. The starting index in that source array
3. The target array
4. The starting index in that target array
5. The length of the sequence to copy
However, arraycopy does not insert, it overwrites. So, designating num1 as the start index in both occurances will affect nothing. And, the length is not num2. Num2 is the end index, the length is the end index - the start index, is it not? Also, keep track of the index of the last used element. The reason for that will soon be made clear.
What you really need to do is, copy the elements starting from one spot after the "end index to copy" until the last used element leaving a "hole" just large enough to write the sequence that should be copied, then copy the element to copied into that hole.
You need to wrap the code above in a while loop incrementing that count variable each pass through the loop, as each pass through the loop is a mutation.And I've another question, how do I make use of the
given in the skeleton?Java Code:int count
Then ignore that for the moment.By the way, I think I haven't learnt to call methods...
- 10-07-2008, 10:22 AM #14
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
Also, all of this gets a little tricky. Are you sure you are not allowed to redefine the char array. Then I would copy from the beginning of the source array to the end of the copy index, then copy again from the start index to the last used element. The thing with simply pushing the part after the nlock to copy over is, say the block to copy is 4 characters long, and there are six characters after the block to copy. Then by pushing that block over, the first character moved four spaces over would overwrite the character at that position before it has been pushed over.
- 10-07-2008, 01:18 PM #15
Member
- Join Date
- Oct 2008
- Posts
- 19
- Rep Power
- 0
In that case, I should use a loop? I do remember that we are not supposed to have long codes and the codes duplicate.
May I ask how? Any specific array to use?What you really need to do is, copy the elements starting from one spot after the "end index to copy" until the last used element leaving a "hole" just large enough to write the sequence that should be copied, then copy the element to copied into that hole.
Below is my new code, I've inserted the while loop and my "del" and "inv" still have errors.
Java Code:import java.util.*; public class Mutations { public static void main(String[] args) { Scanner stdIn = new Scanner(System.in); String dna = stdIn.nextLine(); int count = stdIn.nextInt(); // number of mutation commands int dnaLength = dna.length(); char[] dnaArray = new char[1000]; for (int i = 0; i < dna.length(); i++) { dnaArray[i] = dna.charAt(i); } /* * Type your code here. * Make sure you remember to update the dnaLength variable! */ while (count >=1) { String mutation = stdIn.nextLine(); int num1 = stdIn.nextInt(); int num2 = stdIn.nextInt(); if (mutation.equals("amp")) { System.arraycopy(dnaArray, num1, dnaArray, num1, (num2-num1)); } else if (mutation.equals("del")) { return (s.substring(0,dna.length)+s.substring(num1) && s.substring(0,dna.length)+s.substring(num1)); } else if (mutation.equals("inv")) { reverse (dnaArray.substring(num1, num2)); System.out.print(dnaArray.substring(0,1)); } /* * We cannot simply use the dnaArray because it has a length of 1000. * Therefore we should only take a substring of its proper length. */ String output = new String(dnaArray); output = output.substring(0, dnaLength); System.out.println(output); } } }
- 10-07-2008, 01:45 PM #16
further study needed.
Loop logic is tricky to get right, but I would rather use a loop that I understand than something from proven code that I do not understand. Given that we have some very specific and detailed information on how the data comes in; what I see is that any point we stay with Character datatype. That determines: as always being a character array type. Most, probably all of the Collections will operate as <char> types.
Usually, but this is just for design reasons. There is only so much the mind can keep up with.
Comments inserted:
Java Code:import java.util.*; public class Mutations { // with insertions, result can possibly be longer than 1000 // unless the specification never longer than 1000 holds true // for resultant work. It may be that a temp work area could // exceed specified lenght during program run, depending on // approach used. char[] dnaArray = new char[1000]; // Why the copy? though that may be the work area possibly. for (int i = 0; i < dna.length(); i++) /* * Type your code here. <- no you type the code here. */ //..... String mutation = stdIn.nextLine(); int num1 = stdIn.nextInt(); int num2 = stdIn.nextInt(); // As given by masijade: "arraycopy does not insert, it overwrites." if (mutation.equals("amp")) { // Would StringBuffer work here? System.arraycopy(dnaArray, num1, dnaArray, num1, (num2-num1)); } // Cannot return a String from main, probably keep working here. else if (mutation.equals("del")) { return (s.substring(0,dna.length)+s.substring(num1) && s.substring(0,dna.length)+s.substring(num1)); } // Where is reverse? All of main should call functions on something. // Can be the enclosing class if you declare a variable of type // Mutations and write methods to do the work. else if (mutation.equals("inv")) { reverse (dnaArray.substring(num1, num2)); } }Why?Java Code:/* * We cannot simply use the dnaArray because it has a length of 1000. * Therefore we should only take a substring of its proper length. */Introduction to Programming Using Java.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
- 10-07-2008, 02:06 PM #17
Member
- Join Date
- Oct 2008
- Posts
- 19
- Rep Power
- 0
Yes, I do note that copyarray overwrites instead of insert. But I am still thinking (and searching) how to insert.
Sadly, in this assignment, we are not allowed to use StringBuffer.
Then how do I get back the new string? Do I create another string to contain the one with the changes?Java Code:// Cannot return a String from main, probably keep working here.
So I need to create a method called reverse is it? I am not too sure about this, as I goggled it.Java Code:// Where is reverse? All of main should call functions on something. // Can be the enclosing class if you declare a variable of type // Mutations and write methods to do the work.
This is provided in the skeleton provided for us.Java Code:/* * We cannot simply use the dnaArray because it has a length of 1000. * Therefore we should only take a substring of its proper length. */
- 10-08-2008, 05:18 AM #18
Member
- Join Date
- Oct 2008
- Posts
- 19
- Rep Power
- 0
Hi all, I have came up with this code, finally compliable but still couldn't pass the dynamic cases when I submitted my code. Please help me out.
Java Code:import java.util.*; public class Mutations { public static void main(String[] args) { Scanner stdIn = new Scanner(System.in); String dna = stdIn.nextLine(); //String dna = "GGCCTTAGCTTAGATTACGATCGGTTCCGAGCTTAGATTACGATCG"; int count = stdIn.nextInt(); // number of mutation commands int dnaLength = dna.length(); char[] dnaArray = new char[1000]; char[] newArray = new char[1000]; for (int i = 0; i < dna.length(); i++) { dnaArray[i] = dna.charAt(i); } /* * Type your code here. * Make sure you remember to update the dnaLength variable! */ for(int i=1;i<=count;i++){ String command = stdIn.next(); int startIndex = stdIn.nextInt(); int endIndex = stdIn.nextInt(); //System.out.println("Command: "+command); //System.out.println("Index[S][E] :["+startIndex+"]["+endIndex+"]"); if(command.equals("amp")){ int difference = endIndex-startIndex+1; dnaLength = dnaLength + difference; for (int k = 0; k < dnaLength; k++) { if(k>endIndex){ newArray[k] = dnaArray[k-difference]; } else{ newArray[k] = dnaArray[k]; } } //System.out.println("old : "+ new String(dnaArray)); //System.out.println("new : "+ new String(newArray)); } if(command.equals("del")){ int difference = endIndex-startIndex+1; dnaLength = dnaLength - difference; for (int k = 0; k < dnaLength; k++) { if(k>=startIndex){ newArray[k] = dnaArray[k+difference]; } else{ newArray[k] = dnaArray[k]; } } //System.out.println("old : "+ new String(dnaArray)); //System.out.println("new : "+ new String(newArray)); } if(command.equals("inv")){ int difference = endIndex-startIndex+1; for (int k = 0; k < dnaLength; k++) { if(k>=startIndex && k<=endIndex){ newArray[k] = dnaArray[endIndex-k+startIndex]; } else{ newArray[k] = dnaArray[k]; } } //System.out.println("old : "+ new String(dnaArray)); //System.out.println("new : "+ new String(newArray)); } for (int l = 0; l < dnaLength; l++) { dnaArray[l] = newArray[l]; } } /* * We cannot simply use the dnaArray because it has a length of 1000. * Therefore we should only take a substring of its proper length. */ String output = new String(dnaArray); output = output.substring(0, dnaLength); System.out.println(""); System.out.println(output); } }
- 10-08-2008, 06:42 AM #19
Member
- Join Date
- Oct 2008
- Posts
- 1
- Rep Power
- 0
Hi there, i have run through your program and i find that it is quite elegantly done, however i would have used an arraylist... as for the dynamic test i think you are failing it because of addtional things being printed out...
Take your second last line of code for example.. System.out.println("");
I would take this line out.
- 10-08-2008, 06:54 AM #20
Member
- Join Date
- Oct 2008
- Posts
- 19
- Rep Power
- 0
Similar Threads
-
Help with Arrays
By bri1547 in forum New To JavaReplies: 4Last Post: 08-01-2008, 05:12 AM -
arrays
By hasysf in forum New To JavaReplies: 12Last Post: 07-28-2008, 02:38 AM -
Help on Arrays...
By cuellar14 in forum New To JavaReplies: 4Last Post: 07-25-2008, 08:16 PM -
need help with arrays
By Jman in forum New To JavaReplies: 17Last Post: 07-21-2008, 02:34 AM -
2D-Arrays
By kbyrne in forum New To JavaReplies: 1Last Post: 02-07-2008, 10:08 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks