package com.ibm.compbio;


public abstract class DynamicProgramming {
	
	
   private Cell prevCell;
   private int score;
   private int row;
   private int col;

   protected String sequence1;
   protected String sequence2;
   protected Cell[][] scoreTable;
   protected boolean tableIsFilledIn;
   protected boolean isInitialized;
   
   
   public Cell(int row, int col) {
      this.row = row;
      this.col = col;
   }

   /**
    * @param score
    *           the score to set
    */
   public void setScore(int score) {
      this.score = score;
   }

   /**
    * @return the score
    */
   public int getScore() {
      return score;
   }

   /**
    * @param prevCell
    *           the prevCell to set
    */
   public void setPrevCell(Cell prevCell) {
      this.prevCell = prevCell;
   }

   /**
    * @return the row
    */
   public int getRow() {
      return row;
   }

   /**
    * @return the col
    */
   public int getCol() {
      return col;
   }

   /**
    * @return the prevCell
    */
   public Cell getPrevCell() {
      return prevCell;
   }

   /*
    * (non-Javadoc)
    * 
    * @see java.lang.Object#toString()
    */
   @Override
   public String toString() {
      return "Cell(" + row + ", " + col + "): score=" + score + ", prevCell="
            + prevCell + "]";
   }
   
   

   public DynamicProgramming(String sequence1, String sequence2) {
      this.sequence1 = sequence1;
      this.sequence2 = sequence2;
      scoreTable = new Cell[sequence2.length() + 1][sequence1.length() + 1];
   }

   public int[][] getScoreTable() {
      ensureTableIsFilledIn();

      int[][] matrix = new int[scoreTable.length][scoreTable[0].length];
      for (int i = 0; i < matrix.length; i++) {
         for (int j = 0; j < matrix[i].length; j++) {
            matrix[i][j] = scoreTable[i][j].getScore();
         }
      }

      return matrix;
   }

   protected void initializeScores() {
      for (int i = 0; i < scoreTable.length; i++) {
         for (int j = 0; j < scoreTable[i].length; j++) {
            scoreTable[i][j].setScore(getInitialScore(i, j));
         }
      }
   }

   protected void initializePointers() {
      for (int i = 0; i < scoreTable.length; i++) {
         for (int j = 0; j < scoreTable[i].length; j++) {
            scoreTable[i][j].setPrevCell(getInitialPointer(i, j));
         }
      }
   }

   protected void initialize() {
      for (int i = 0; i < scoreTable.length; i++) {
         for (int j = 0; j < scoreTable[i].length; j++) {
            scoreTable[i][j] = new Cell(i, j);
         }
      }
      initializeScores();
      initializePointers();

      isInitialized = true;
   }

   protected abstract Cell getInitialPointer(int row, int col);

   protected abstract int getInitialScore(int row, int col);

   protected abstract void fillInCell(Cell currentCell, Cell cellAbove,
         Cell cellToLeft, Cell cellAboveLeft);

   protected void fillIn() {
      for (int row = 1; row < scoreTable.length; row++) {
         for (int col = 1; col < scoreTable[row].length; col++) {
            Cell currentCell = scoreTable[row][col];
            Cell cellAbove = scoreTable[row - 1][col];
            Cell cellToLeft = scoreTable[row][col - 1];
            Cell cellAboveLeft = scoreTable[row - 1][col - 1];
            fillInCell(currentCell, cellAbove, cellToLeft, cellAboveLeft);
         }
      }

      tableIsFilledIn = true;
   }

   abstract protected Object getTraceback();

   public void printScoreTable() {
      ensureTableIsFilledIn();
      for (int i = 0; i < sequence2.length() + 2; i++) {
         for (int j = 0; j < sequence1.length() + 2; j++) {
            if (i == 0) {
               if (j == 0 || j == 1) {
                  System.out.print("  ");
               } else {
                  if (j == 2) {
                     System.out.print("     ");
                  } else {
                     System.out.print("   ");
                  }
                  System.out.print(sequence1.charAt(j - 2));
               }
            } else if (j == 0) {
               if (i == 1) {
                  System.out.print("  ");
               } else {
                  System.out.print(" " + sequence2.charAt(i - 2));
               }
            } else {
               String toPrint;
               Cell currentCell = scoreTable[i - 1][j - 1];
               Cell prevCell = currentCell.getPrevCell();
               if (prevCell != null) {
                  if (currentCell.getCol() == prevCell.getCol() + 1
                        && currentCell.getRow() == prevCell.getRow() + 1) {
                     toPrint = "\\";
                  } else if (currentCell.getCol() == prevCell.getCol() + 1) {
                     toPrint = "-";
                  } else {
                     toPrint = "|";
                  }
               } else {
                  toPrint = " ";
               }
               int score = currentCell.getScore();
               String s = String.format("%1$3d", score);
               toPrint += s;
               System.out.print(toPrint);
            }

            System.out.print(' ');
         }
         System.out.println();
      }
   }

   protected void ensureTableIsFilledIn() {
      if (!isInitialized) {
         initialize();
      }
      if (!tableIsFilledIn) {
         fillIn();
      }
   }
}

I have an error at line 19, which says, "invalid method declaration; return type required". However if I enter a return for that error, there is more error. May I know how to solve this error?

This looks like you're trying to put a constructor for Cell in the DynamicProgramming class. That's not going to work so well.

yes, you need to either rename your class DynamicProgramming into Cell or change the word Cell into DynamicProgramming. You cannot create a constructor for one class in another. Use ECLIPSE IDE and it will show you why you get these errors, try to use IDEs when writing programs :)

Okay. Now, I run the Cell.java and DynamicProgramming.java as 2 different files. It can be compiled already. However, the result can't be displayed on the applet(I created HTML file and use the DynamicProgramming.class file). Is it wrong to use DynamicProgramming.class? I tried using Cell.class, but it doesn't work too. May I know how to display the results?

Ok, so cell is a different file, it DOES have a cnostructor right? Post the 2 different files' code here, and I will take a look and tell. I am not very well versed in Applets, but I can still help you. If you use System.out.println(); it will not work. I believe you have to use drawString in applets() :D

public class Cell {
   private Cell prevCell;
   private int score;
   private int row;
   private int col;
   

   public Cell(int row, int col) {
      this.row = row;
      this.col = col;
   }

   /**
    * @param score
    *           the score to set
    */
   public void setScore(int score) {
      this.score = score;
   }

   /**
    * @return the score
    */
   public int getScore() {
      return score;
   }

   /**
    * @param prevCell
    *           the prevCell to set
    */
   public void setPrevCell(Cell prevCell) {
      this.prevCell = prevCell;
   }

   /**
    * @return the row
    */
   public int getRow() {
      return row;
   }

   /**
    * @return the col
    */
   public int getCol() {
      return col;
   }

   /**
    * @return the prevCell
    */
   public Cell getPrevCell() {
      return prevCell;
   }

   /*
    * (non-Javadoc)
    * 
    * @see java.lang.Object#toString()
    */
   @Override
   public String toString() {
      return "Cell(" + row + ", " + col + "): score=" + score + ", prevCell="
            + prevCell + "]";
   }
   
   
}
public abstract class DynamicProgramming {
	
	
   private Cell prevCell;
   private int score;
   private int row;
   private int col;

   protected String sequence1;
   protected String sequence2;
   protected Cell[][] scoreTable;
   protected boolean tableIsFilledIn;
   protected boolean isInitialized;
    

   public DynamicProgramming(String sequence1, String sequence2) {
      this.sequence1 = sequence1;
      this.sequence2 = sequence2;
      scoreTable = new Cell[sequence2.length() + 1][sequence1.length() + 1];
   }

   public int[][] getScoreTable() {
      ensureTableIsFilledIn();

      int[][] matrix = new int[scoreTable.length][scoreTable[0].length];
      for (int i = 0; i < matrix.length; i++) {
         for (int j = 0; j < matrix[i].length; j++) {
            matrix[i][j] = scoreTable[i][j].getScore();
         }
      }

      return matrix;
   }

   protected void initializeScores() {
      for (int i = 0; i < scoreTable.length; i++) {
         for (int j = 0; j < scoreTable[i].length; j++) {
            scoreTable[i][j].setScore(getInitialScore(i, j));
         }
      }
   }

   protected void initializePointers() {
      for (int i = 0; i < scoreTable.length; i++) {
         for (int j = 0; j < scoreTable[i].length; j++) {
            scoreTable[i][j].setPrevCell(getInitialPointer(i, j));
         }
      }
   }

   protected void initialize() {
      for (int i = 0; i < scoreTable.length; i++) {
         for (int j = 0; j < scoreTable[i].length; j++) {
            scoreTable[i][j] = new Cell(i, j);
         }
      }
      initializeScores();
      initializePointers();

      isInitialized = true;
   }

   protected abstract Cell getInitialPointer(int row, int col);

   protected abstract int getInitialScore(int row, int col);

   protected abstract void fillInCell(Cell currentCell, Cell cellAbove,
         Cell cellToLeft, Cell cellAboveLeft);

   protected void fillIn() {
      for (int row = 1; row < scoreTable.length; row++) {
         for (int col = 1; col < scoreTable[row].length; col++) {
            Cell currentCell = scoreTable[row][col];
            Cell cellAbove = scoreTable[row - 1][col];
            Cell cellToLeft = scoreTable[row][col - 1];
            Cell cellAboveLeft = scoreTable[row - 1][col - 1];
            fillInCell(currentCell, cellAbove, cellToLeft, cellAboveLeft);
         }
      }

      tableIsFilledIn = true;
   }

   abstract protected Object getTraceback();

   public void printScoreTable() {
      ensureTableIsFilledIn();
      for (int i = 0; i < sequence2.length() + 2; i++) {
         for (int j = 0; j < sequence1.length() + 2; j++) {
            if (i == 0) {
               if (j == 0 || j == 1) {
                  System.out.print("  ");
               } else {
                  if (j == 2) {
                     System.out.print("     ");
                  } else {
                     System.out.print("   ");
                  }
                  System.out.print(sequence1.charAt(j - 2));
               }
            } else if (j == 0) {
               if (i == 1) {
                  System.out.print("  ");
               } else {
                  System.out.print(" " + sequence2.charAt(i - 2));
               }
            } else {
               String toPrint;
               Cell currentCell = scoreTable[i - 1][j - 1];
               Cell prevCell = currentCell.getPrevCell();
               if (prevCell != null) {
                  if (currentCell.getCol() == prevCell.getCol() + 1
                        && currentCell.getRow() == prevCell.getRow() + 1) {
                     toPrint = "\\";
                  } else if (currentCell.getCol() == prevCell.getCol() + 1) {
                     toPrint = "-";
                  } else {
                     toPrint = "|";
                  }
               } else {
                  toPrint = " ";
               }
               int score = currentCell.getScore();
               String s = String.format("%1$3d", score);
               toPrint += s;
               System.out.print(toPrint);
            }

            System.out.print(' ');
         }
         System.out.println();
      }
   }

   protected void ensureTableIsFilledIn() {
      if (!isInitialized) {
         initialize();
      }
      if (!tableIsFilledIn) {
         fillIn();
      }
   }
}

These are the 2 codes. I need to run it as an application. Do you know how to do it?

Use ECLIPSE IDE and it will show you why you get these errors, try to use IDEs when writing programs :)

Dunno about that one, WR. I've gotta put in a word for learning how to use the hammer and saw before you start in with the power tools.
Or maybe the metaphor is more "learning to walk before you put yourself in a wheelchair" - you choose.

SunshinSerene - are you trying to run this as an applet or as an application? If you want it to be an application, you need a main() method somewhere. If you want to run it as an applet, you need to have a JApplet in the mix.
Applets are a little complicated, but not terribly so.

What is this all in aid of, anyway? What's the purpose of the code?

I want to run it as an application. It is dynamic programming.

Okay, in that case you need a main method somewhere. Most likely you'd put it in DynamicProgramming, although some people like to make a separate class - "DynamicProgrammingMain.java" for that purpose. What main() does will depend on what the program is meant to do, but without it, it'll do nothing at all.

Okay. I do have a main method already. However, it has an error: "DynamicProgramming is abstract; cannot be instantiated" What do I do then?

Okay, I don't see a main method in what you've posted, but if you say you've got one I'll buy that.

But that sound you just heard was my palm hitting my forehead - of course, you've got DynamicProgramming declared as abstract, which means it can't be instantiated. Basically, an abstract class is a class which is meant to serve as a template for inheritance. You either need to make a class that extends this one and instantiate that, or make this one not abstract (removing every instance of the word "abstract" should do it, but I haven't read it over too carefully - there may be further problems)

If I remove every abstract words it doesn't work. I don't know how to do it.

Grumble grumble grumble.... why was this abstract in the first place?

I guess I gotta actually read your code now or something if I'm going to find out. That would be a lot easier ( == I'm more likely to actually do it) if I knew what it was you were trying to do with the code. What's the goal here?

To get the whole program working. Be able to compile without any error. I think I need to write a main or init() that can display the results. In that sense, a platform to show the results. Can you help me with that?

Okay. From your code:

protected abstract Cell getInitialPointer(int row, int col);
protected abstract int getInitialScore(int row, int col);
protected abstract void fillInCell(Cell currentCell, Cell cellAbove,
Cell cellToLeft, Cell cellAboveLeft);

These are abstract methods. Right now they're just promises to implement something here. Any "DynamicProgramming" object (ie, any object extending this class) has to implement these in some fashion. This is why it did no good to just strip out the "abstract" declarations - a method with a null body has to be abstract.


So what you need (and this is probably in your assignment) is to write a class that extends this abstract class, filling in these two abstract methods with code that will return a Cell (the "InitialPointer") and an int (the "InitialScore").

That'll be something like

public class Dibble extends DynamicProgramming
{

protected abstract Cell getInitialPointer(int row, int col)
{
// your code here
return initialPointer;
}

protected abstract int getInitialScore(int row, int col)
{
// your code here
return initialScore;
}

protected abstract void fillInCell(Cell currentCell, Cell cellAbove,
  Cell cellToLeft, Cell cellAboveLeft)
{
//code here
}
}

What goes in there? I dunno. You have to figure out what it is that "getInitialPointer" and "getInitialScore" mean, and how to get those values.

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

//public abstract class DynamicProgramming extends JApplet implements ActionListener{
	public class Dibble extends DynamicProgramming{
	

   protected String sequence1;
   protected String sequence2;
   protected Cell[][] scoreTable;
   protected boolean tableIsFilledIn;
   protected boolean isInitialized;
   
   TextArea outputArea;
	JButton button;
	JButton reset;
	JButton gapB;
	JButton dot;

	JTextField tF1;
	JTextField tF2;
	JTextField m1F;
	JTextField m2F;
	JTextField g1F;

	JLabel l1;
	JLabel l2;
	JLabel m1;
	JLabel m2;
	JLabel g1;

	String s1;
	String s2;
	String mat;
	String mis;
	String gap;

	int matI;
	int misI;
	int gapI;
	int counter;
	int no;
	int no1;
	int no2;
	int no3;
	int super1;
	int super2;
	int super0;
	int super4;
	int super3;
	int super5;
	int super6;
	int super7;
	int mystery;
	int score;

	String output;

	char a1[];
	char a2[];
	int main[][];

	int i,j,k,l,addUp;
   
   
   public static void main(String[] args) 
	{
  		Frame f = new Frame();
  		f.addWindowListener(new java.awt.event.WindowAdapter() 
  		{
       	public void windowClosing(java.awt.event.WindowEvent e) 
       	{
       	System.exit(0);
      	};
     	});

 		DynamicProgramming ut = new DynamicProgramming();
  		ut.setSize(700,700); // same size as defined in the HTML APPLET
  		f.add(ut);
  		f.pack();
  		ut.init();
  		f.setSize(700,700 + 100); // add 20, seems enough for the Frame title,
  		f.show();
  	}
  	
  	
  	public void init()
	{
		Container c = getContentPane();
		c.setLayout(new FlowLayout());

		outputArea = new TextArea(38,85);
		Font font = new Font("Courier", Font.PLAIN, 12);
		outputArea.setFont(font);
		outputArea.setEditable(false);

		button = new JButton("No Gap");
		button.addActionListener(this);

		reset = new JButton(" Reset ");
		reset.addActionListener(this);

		gapB = new JButton("Gap");
		gapB.addActionListener(this);

		dot = new JButton("Dotplot");
		dot.addActionListener(this);

		tF1 = new JTextField(55);
		tF2 = new JTextField(55);
		m1F = new JTextField(3);
		m2F = new JTextField(3);
		g1F = new JTextField(3);

		m1F.setText("1");
		m2F.setText("0");
		g1F.setText("-1");

		l1 = new JLabel("Enter Seq1:");
		l2 = new JLabel("Enter Seq2:");
		m1 = new JLabel("Match Score:");
		m2 = new JLabel("Mismatch Score:");
		g1 = new JLabel("Gap Penalty:");

		c.add(l1);
		c.add(tF1);
		c.add(l2);
		c.add(tF2);
		c.add(button);
		c.add(gapB);
		c.add(reset);
		c.add(dot);
		c.add(m1);
		c.add(m1F);
		c.add(m2);
		c.add(m2F);
		c.add(g1);
		c.add(g1F);
		c.add(outputArea);


	}
	

   public DynamicProgramming(String sequence1, String sequence2) {
      this.sequence1 = sequence1;
      this.sequence2 = sequence2;
      scoreTable = new Cell[sequence2.length() + 1][sequence1.length() + 1];
      
   }

   public int[][] getScoreTable() {
      ensureTableIsFilledIn();

      int[][] matrix = new int[scoreTable.length][scoreTable[0].length];
      for (int i = 0; i < matrix.length; i++) {
         for (int j = 0; j < matrix[i].length; j++) {
            matrix[i][j] = scoreTable[i][j].getScore();
         }
      }

      return matrix;
   }

   protected void initializeScores() {
      for (int i = 0; i < scoreTable.length; i++) {
         for (int j = 0; j < scoreTable[i].length; j++) {
            scoreTable[i][j].setScore(getInitialScore(i, j));
         }
      }
   }

   protected void initializePointers() {
      for (int i = 0; i < scoreTable.length; i++) {
         for (int j = 0; j < scoreTable[i].length; j++) {
            scoreTable[i][j].setPrevCell(getInitialPointer(i, j));
         }
      }
   }

   protected void initialize() {
      for (int i = 0; i < scoreTable.length; i++) {
         for (int j = 0; j < scoreTable[i].length; j++) {
            scoreTable[i][j] = new Cell(i, j);
         }
      }
      initializeScores();
      initializePointers();

      isInitialized = true;
   }

   protected abstract Cell getInitialPointer(int row, int col)
   {
   	for (int i = 0; i < scoreTable.length; i++) {
         for (int j = 0; j < scoreTable[i].length; j++) {
            scoreTable[i][j].setPrevCell(getInitialPointer(i, j));
         }
         return InitialPointer;
   }
   }

   protected abstract int getInitialScore(int row, int col)
   {
   	for (int i = 0; i < scoreTable.length; i++) {
         for (int j = 0; j < scoreTable[i].length; j++) {
            scoreTable[i][j].setScore(getInitialScore(i, j));
         }
      }
      return InitialScore;
   }

   protected abstract void fillInCell(Cell currentCell, Cell cellAbove, Cell cellToLeft, Cell cellAboveLeft)
   {
   	for (int row = 1; row < scoreTable.length; row++) {
         for (int col = 1; col < scoreTable[row].length; col++) {
            Cell currentCell = scoreTable[row][col];
            Cell cellAbove = scoreTable[row - 1][col];
            Cell cellToLeft = scoreTable[row][col - 1];
            Cell cellAboveLeft = scoreTable[row - 1][col - 1];
            fillInCell(currentCell, cellAbove, cellToLeft, cellAboveLeft);
         }
   }
	
   }
   protected void fillIn() {
      for (int row = 1; row < scoreTable.length; row++) {
         for (int col = 1; col < scoreTable[row].length; col++) {
            Cell currentCell = scoreTable[row][col];
            Cell cellAbove = scoreTable[row - 1][col];
            Cell cellToLeft = scoreTable[row][col - 1];
            Cell cellAboveLeft = scoreTable[row - 1][col - 1];
            fillInCell(currentCell, cellAbove, cellToLeft, cellAboveLeft);
         }
      }

      tableIsFilledIn = true;
   }

   protected abstract Object getTraceback();

   public void printScoreTable() {
      ensureTableIsFilledIn();
      for (int i = 0; i < sequence2.length() + 2; i++) {
         for (int j = 0; j < sequence1.length() + 2; j++) {
            if (i == 0) {
               if (j == 0 || j == 1) {
                  System.out.print("  ");
               } else {
                  if (j == 2) {
                     System.out.print("     ");
                  } else {
                     System.out.print("   ");
                  }
                  System.out.print(sequence1.charAt(j - 2));
               }
            } else if (j == 0) {
               if (i == 1) {
                  System.out.print("  ");
               } else {
                  System.out.print(" " + sequence2.charAt(i - 2));
               }
            } else {
               String toPrint;
               Cell currentCell = scoreTable[i - 1][j - 1];
               Cell prevCell = currentCell.getPrevCell();
               if (prevCell != null) {
                  if (currentCell.getCol() == prevCell.getCol() + 1
                        && currentCell.getRow() == prevCell.getRow() + 1) {
                     toPrint = "\\";
                  } else if (currentCell.getCol() == prevCell.getCol() + 1) {
                     toPrint = "-";
                  } else {
                     toPrint = "|";
                  }
               } else {
                  toPrint = " ";
               }
               int score = currentCell.getScore();
               String s = String.format("%1$3d", score);
               toPrint += s;
               System.out.print(toPrint);
            }

            System.out.print(' ');
         }
         System.out.println();
      }
   }

   protected void ensureTableIsFilledIn() {
      if (!isInitialized) {
         initialize();
      }
      if (!tableIsFilledIn) {
         fillIn();
      }
   }
}

Now line 147 has an error. What they mean by return type? I don't think I need to return anything.

Ok listen man, I dont know why you are doing all this, but you obviously do not even know the basics of Java. Please don't be offended by what I am saying, but you really should be understanding Object Oriented Programming and the principles of OOP, as well as some top-down design before you start trying to do things like this. You put the constructor of one class, in another. The JVM is not expecting a constructor for any other class but Dibble in the class Dibble. Therefore when you write:

public DynamicProgramming(String sequence1, String sequence2) {
  this.sequence1 = sequence1;
  this.sequence2 = sequence2;
  scoreTable = new Cell[sequence2.length() + 1][sequence1.length() + 1];

}

It has no meaning. The NVM is looking for a return type of VOID or something like an int. Like I said, honestly, you should really be understanding the basic principles of Java as well as OOP before you do this. Good luck, and I have to refute the previous quote. Whether you know Java or not, if you are going to try your hand at it, it will ease the pain by using an IDE. IDEs have tutorials for learning the language, as well as the debugger which highlights potential compilation errors, and it even explain to you how to fix them, and why it occurs. Eclipse is definitely a good place to start. But honestly, do not start programming right away, you need to learn some stuff before you do things like this. Learn to crawl before you walk :)

WR - we can fight about Eclipse another time. It'll be fun. At least we agree that without an understanding of inheritance, it doesn't matter what coding environment you use, you're going to be pretty helpless.

@Sunshineserene - this is not what I meant. Please look up some tutorials on inheritance and the use of the "extends" keyword to derive classes. Until you do understand this, you won't be able to finish your assignment, because you won't understand what you're writing. Does your Java course have a textbook? If so, opening that would be a start.

Yes, I dont want to argue and I definitely agree with that lol. I was saying is that it will help him understand what he does better is all. Or maybe its just me, but ya no need to argue :).

I cannot agree with jon more, like I said before, you REALLY should not be doing this, honestly I feel like this was either a code you copied or that was handed to you, yet again no offence, but without the knowledge of how constructors work, I highly doubt this was written by you.

What I highly suggest is, try and write a simpler version of this program, something along the lines of 3 classes, with one class with a main, another with a variable of the third class, and try and edit fields in the 3rd class through the use of the second. Get the feel fow how OOP works, and how the internals and _MOST IMPORTANTLY_ the logic of the program itself. you MUST<- CANNOT EMPHASIZE MORE understand how OOP works. Good luck in your course :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.