Can anybody help me with this java code

I am doing Ms bioinformatics in U.K. I have just started with java. Can anybody help me with the following code.

In this exercise the task is to write a alignment class. This will allow two strings to be aligned.

The alignment is a simplification to the general alignment and works in the following way:

Given two strings S1 and S2 and a character c, the alignment will insert padding symbols in to S1 and S2 such that when writing the padded string under each other the occurrences of the character c are aligned.

Example:

S1: aabaababbba.

S2: acadadaadaddaaba

When we align them by/for the character 揳?

The output should be:

a_aba_ababbba

acadada_ad__addaaba

here _(underscore) is the padding symbol.

We are using LINUX(SUSE).

Here we use the Xe mac editor to write codes.

We are being asked to use the following methods:

Class Alignment

java.lang.Object

Alignment

public class Alignment

extends java.lang.Object

Alignment.java Created: Mon Mar 7 08:27:36 2005

Constructor Summary

Alignment()

Default constructor.

Alignment(java.lang.String s1, java.lang.String s2)

Constructor The first and second string are given by the parameter.

Method Summary

void

align(char c)

Aligns the first and second string, such that all occurrences of the given character are aligned.

java.lang.String

getColumn(int i)

Return a string with the symbols of the i'th row in the alignment.

char

getPadSymbol()

Return the current padding symbol

void

setFirstString(java.lang.String s)

Set the first string to the given parameter.

void

setPadSymbol(char c)

Set the padding symbol to the given parameter.

void

setSecondString(java.lang.String s)

Set the second string to the given parameter.

char[][]

toArray()

Return a two-dimensional array of the alignment.

java.lang.String

toString()

Returns the Alignment as String The format is that each column of the alignment is given in a new row.

Methods inherited from class java.lang.Object

clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

Constructor Detail

Alignment

public Alignment()

Default constructor. The first and second string are set to be empty. The default pad symbol is '-'.

Alignment

public Alignment(java.lang.String s1,

java.lang.String s2)

Constructor The first and second string are given by the parameter. The default pad symbol is '-'.

Parameters:

s1 - the first string

s2 - the first string

Method Detail

setFirstString

public void setFirstString(java.lang.String s)

Set the first string to the given parameter.

Parameters:

s - the new first string

setSecondString

public void setSecondString(java.lang.String s)

Set the second string to the given parameter.

Parameters:

s - the new second string

setPadSymbol

public void setPadSymbol(char c)

Set the padding symbol to the given parameter.

Parameters:

c - the new padding symbol

getPadSymbol

public char getPadSymbol()

Return the current padding symbol

Returns:

the current padding symbol

getColumn

public java.lang.String getColumn(int i)

Return a string with the symbols of the i'th row in the alignment.

Parameters:

i - the number of the column

Returns:

the string representing the i'th column of the alignment

align

public void align(char c)

Aligns the first and second string, such that all occurrences of the given character are aligned. Lets call this character c. If one string contains more occurrences of c then the first occurrences will be aligned. See examples

Parameters:

c - the character that will be aligned.

toArray

public char[][] toArray()

Return a two-dimensional array of the alignment. The first row correspond to the first string aligned and the second row to the second string.

toString

public java.lang.String toString()

Returns the Alignment as String The format is that each column of the alignment is given in a new row.

If the class is writted correctly the following test program should compile and work:

If the above class is written correctly the test program below should work;

**

* AlignmentTest.java

*

*

* Created: Mon Mar 7 08:51:09 2005

*

* @author <a href="mailto:">M. Hoffmann</a>

* @version 1.0

*/

public class AlignmentTest{

public static void main(String[] args) {

Alignment ali= new Alignment("aababababcabac","ahasababababasdab");

System.out.println(ali.toString());

ali.align('a');

System.out.println(ali.toString());

char[][] answer = ali.toArray();

for (int i=0; i<answer[0].length; ++i) {

System.out.print("--");

}

System.out.println("-");

for (int i=0; i><answer[0].length; ++i) {

System.out.print("|"+answer[0]);

}

System.out.println("|");

for (int i=0; i><answer[0].length; ++i) {

System.out.print("--");

}

System.out.println("-");

for (int i=0; i><answer[0].length; ++i) {

System.out.print("|"+answer[1]);

}

System.out.println("|");

for (int i=0; i><answer[0].length; ++i) {

System.out.print("--");

}

System.out.println("-");

}

} // AlignmentTest

Please help me out>

[5824 byte] By [pankajjyotibaruaa] at [2007-10-2 13:58:34]
# 1

Read the assignment text well, send email to your instructor about the parts

that are not clear, and start doing it. Only when you encounter a problem that

you can't overcome by reading your notes, the Java tutorial, or the

documentation of standard classes come back here and ask a specific question

with a clear description of the problem you are having with some source code,

what you want it do, what it is doing instead, and possible error message.

Java tutorial: http://java.sun.com/docs/books/tutorial/

Documentation of standard classes: http://java.sun.com/j2se/1.5.0/docs/api/

Examples of how to use them: http://javaalmanac.com

jsalonena at 2007-7-13 12:03:55 > top of Java-index,Java Essentials,Java Programming...
# 2
Thanks for the leads. Actually I am having prob with the align method. Do I need to compare the two strings, character by character and create a new string if there is match and a padded symbol if there is no match.
pankajjyotibaruaa at 2007-7-13 12:03:55 > top of Java-index,Java Essentials,Java Programming...
# 3

> Thanks for the leads. Actually I am having prob with

> the align method. Do I need to compare the two

> strings, character by character and create a new

> string if there is match and a padded symbol if there

> is no match.

Reading the specification, yes, that's what you would seem to need to do.

Note that it's easier and more efficient to work with the StringBuffer (or StringBuilder in Java 5) class than with String. Using StringBuffer you don't have to create new objects in a loop, instead you keep using one that already exists and use helpful methods such as insert() to insert characters or append() to append characters. See here for code examples:

http://javaalmanac.com/egs/java.lang/CreateUsingStringBuffer.html

jsalonena at 2007-7-13 12:03:55 > top of Java-index,Java Essentials,Java Programming...