-1

I want to stress that I know how to solve this this problem if only I could modify the code that my instructor has given me, but that is not an option. I should also stress that I don't have very much experience with Java.

So I have been given this assignment to complete a project where the skeleton code is provided for me. This is the output I am getting from running it:

Genomic sequence: [C@4aa298b7
Sequence length: 25
Array of exon positions: [1, 5, 8, 10, 13, 16]

As you can see, the "Genomic sequence" field is printing a pointer instead of valid data which should look more like "AAAGGTTATTA..."

This is where the information is printed out:

public class BioSeqData
{
  public static void main(String[] args)
  {
    String demodna = new String("AATGCCAGTCAGCATAGCGTAGACT");
    int[] ardemo = {1, 5, 8, 10, 13, 16};
    GenomicDNASequence gdemo = new     GenomicDNASequence(demodna.toCharArray());
    System.out.println( "Genomic sequence: " + gdemo );
    ...

If I could modify this class in the assignment to print out what the pointer is pointing to instead of the pointer itself, I would, but that is not an option. How can I change GenomicDNASequence to return the char array instead of a pointer?

Here are the relevant parts of Genomic DNA Sequence:

public class GenomicDNASequence extends DNASequence
{
  public boolean[] iscoding; // made public instead of private for grading.
  public GenomicDNASequence(char[] gdnaarr)
  {
    super(gdnaarr);
  }
...

And the class that it extends:

public class DNASequence extends Sequence                                                                                                                                                                                                                                        
{                                                                                                                                                                                                                                                                                
  public DNASequence(char[] dnaarr)                                                                                                                                                                                                                                              
  {                                                                                                                                                                                                                                                                              
    super(dnaarr);                                                                                                                      
  }
...

And the class that that extends:

public class Sequence
{
  public char[] seqarr; // made public instead of protected for grading.
  public Sequence(char[] sarr)
  {
        for(int i=0;i<sarr.length;i++){
                if (!isValidLetter(sarr[i])){
                        throw new IllegalArgumentException("Invalid sequence of letters for class edu.iastate.cs228.hw1.Sequence");
                }
        }
        seqarr=sarr;
...
Display name
  • 721
  • 2
  • 11
  • 29
  • It's printing the default `toString` of a character array. That should be enough to get you started. – Dave Newton Sep 11 '17 at 01:56
  • I know that, please refer to the text in bold at the beginning of the question – Display name Sep 11 '17 at 02:03
  • And I'm saying that since you know what the data is you have enough information to take that data and get the output you want. Since it's coursework it's an opportunity to learn (which was taken from you, but that's not your fault). – Dave Newton Sep 11 '17 at 02:42
  • What was taken from the OP? Asking questions on StackOverflow is a great opportunity to learn. The community chips in a lot of discussion often well beyond the sometimes small and focused learning objectives that come from individual assigned problems. Obviously discovering a solution on one's own is desirable but when stuck it's great to get an answer with a good deal of elaboration and background information that can be more valuable to a learner than just doing the minimal amount of work to solve an assigned problem. Cheers. – Ray Toal Sep 14 '17 at 06:47

1 Answers1

-1

From what I can tell, you have access to GenomicDNASequence but not any of the other classes. When you try to print a GenomicDNASequence object, you are somehow seeing output that looks like an array.

This suggests that one of the ancestor classes of GenomicDNASequence has a toString method that uses the default conversion of an array to a string. In Java, if a is an array, then a.toString() looks weird. You need to override this so that your GenomicDNASequence object's toString method returns a cleaner instance of the sequence as a string.

In particular I would go with

public class GenomicDNASequence extends DNASequence {
  public boolean[] iscoding; // made public instead of private for 

  @Override
  public String toString() {
    return new String(seqarr);
  }
  ...
}

This makes a character array like ['G', 'C', 'A', 'T'] turn into the string "GCAT".

ADDENDUM

It is important to use new String and NOT Arrays.toString(). For example:

import java.util.Arrays;
public class MyClass {
    public static void main(String args[]) {
        char[] c = {'G', 'C', 'A', 'T'};
        System.out.println(c.toString());
        System.out.println(Arrays.toString(c));
        System.out.println(new String(c));
    }
}

produces

[C@2a139a55
[G, C, A, T]
GCAT
Ray Toal
  • 86,166
  • 18
  • 182
  • 232