Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,430 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,306 people online right now. Registration is fast and FREE... Join Now!




Program Conversion to Java?

 
Reply to this topicStart new topic

Program Conversion to Java?, C++ to Java. Code Inside!

needhelpbadly
7 Feb, 2008 - 08:38 AM
Post #1

New D.I.C Head
*

Joined: 5 Feb, 2008
Posts: 13

Hello, for my first project of this semester I was supposed to make the popular Knight's Tour program in C++, and submit it in both C++ and Java.

ONE PROBLEM.

I dont know Java.

i've been looking up how to create methods and stuff in Java but I'm still not sure how to convert.

I'll post my code ( which works ) then what I have so far in Java.

Can you possibly help me with a few lines? I apologize because this is long.

I just dont know how to convert ( for example )
CODE
void Knight::display_board(int size)
{
  for(int c=0;c<size;c++)
    {
      for(int d=0;d<size;d++)
        {
          if(d==size-1)
            {
              cout<<"| "<<board[c][d]<<" |"<<endl;
            }
          else
{
              cout<<"| "<<board[c][d]<<" ";
            }
        }
    }
}

into a java method. Is it just public void display_board(int size) ?
and to call display_board, I have to make a "Knight kt = new Knight(); and use kt.display_board(size) ?

Thanks so much!!

My C++ Program

CODE
#include<iostream>
using namespace std;



class Knight
{
public:

  bool valid_pos (int row, int col, int size);
  void display_board(int size);
  void recursive (int row, int col, int size, int count);
  int check_board(int size);

};

//Global vars                                                                                                                                                                    
const int size=6;//Board size                                                                                                                                                    
int board[size][size];  //Create board                                                                                                                                            



int main()
{
  Knight check;
  int row, col, count=0;


  for(int a=0;a<=size;a++) //clear board to 0                                                                                                                                    
    {
      for(int b=0;b<=size;b++)
        {
          board[a][b]=0;
        }
    }

  //Take input                                                                                                                                                                    
  cout<<"Enter the start row of the knight: ";
  cin>>row;
  cout<<"Enter the starting column of the knight: ";
  cin>>col;

  check.valid_pos (row, col, size);

  //Output results                                                                                                                                                                

  check.recursive(row, col, size, count);
  check.display_board(size);

  return 0;
}

bool Knight::valid_pos (int row, int col, int size){
  if(row>=size || col>=size ||  row<0 || col<0){//Test for chosen location being within board dimensions                                                                          
    return 1;

  }
  else{//Check to see if location was not previously visited                                                                                                                      
    if((board[row][col]) == 0){
      return 0;

    }
    else{//returns 1 if location previously visited                                                                                                                              
      return 1;

    }
  }
}

void Knight::display_board(int size)
{
  for(int c=0;c<size;c++)
    {
      for(int d=0;d<size;d++)
        {
          if(d==size-1)
            {
              cout<<"| "<<board[c][d]<<" |"<<endl;
            }
          else
{
              cout<<"| "<<board[c][d]<<" ";
            }
        }
    }
}


int Knight::check_board(int size)
{
  int rows, cols, checker;

  for(rows=0;rows<size;rows++)
    {
      for(cols=0;cols<size;cols++)
        {
          if(valid_pos(rows,cols,size)!=1)
            {
              checker=0;
            }
        }
    }
  return(checker);
}

void Knight::recursive(int row, int col, int size, int count)
{

  int checked;

  board[row][col]=count;

  if(valid_pos (row+2, col+1, size)!=1){
    count++;
    recursive(row+2, col+1, size, count);
    checked=check_board(size);
    if(checked==0){
      board[row+2][col+1]=0;
      count--;
    }
  }
  if(valid_pos (row+2, col-1, size)!=1){
    count++;
    recursive(row+2, col-1, size, count);
    checked=check_board(size);
    if(checked==0){
      board[row+2][col-1]=0;
      count--;

    }
  }
if(valid_pos (row+1, col+2, size)!=1){
    count++;
    recursive(row+1, col+2, size, count);
    checked=check_board(size);
    if(checked==0){
      board[row+1][col+2]=0;
      count--;
    }
  }
  if(valid_pos (row+1, col-2, size)!=1){
    count++;
    recursive(row+1, col-2, size, count);
    checked=check_board(size);
    if(checked==0){
      board[row+1][col-2]=0;
      count--;
    }
  }
  if(valid_pos(row-2, col+1, size)!=1){
    count++;
    recursive(row-2, col+1, size, count);
    checked=check_board(size);
    if(checked==0){
      board[row-2][col+1]=0;
      count--;
    }
  }

if(valid_pos (row-2, col-1, size)!=1){
    count++;
    recursive(row-2, col-1, size, count);
    checked=check_board(size);
    if(checked==0){
      board[row-2][col-1]=0;
      count--;
    }
  }
  if(valid_pos (row-1, col+2, size)!=1){
    count++;
    recursive(row-1, col+2, size, count);
    checked=check_board(size);
    if(checked==0){
      board[row-1][col+2]=0;
      count--;
    }
  }
  if(valid_pos (row-1, col-2, size)!=1){
    count++;
    recursive(row-1, col-2, size, count);
    checked=check_board(size);
    if(checked==0){
      board[row-1][col-2]=0;
      count--;
    }
  }

checked=check_board(size);
  if(checked==0){
    board[row][col]=0;
    count--;
  }
}


What I have in Java:



CODE
public class Knight
{
    //Global variables    
    int size=5;//Board size
    int board[][];

    
    
    public static void main(String []args)
    {
        Knight kt = new Knight();
        bool valid_pos (int row, int col, int size);
        void display_board(int size);
        void recursive (int row, int col, int size, int count);
        int check_board(int size);
    }


    public Knight()
    {
        board = new int[size][size];
    }                                                                                                                                                              
                                                                                                                                          



int main()
{
  Knight check;
  int row, col, count = 0;

  for(int x=0;x<=size;x++) //clear board to 0                                                                                                                                    
    {
      for(int y=0;y<=size;y++)
        {
          board[x][y]=0;
        }
    }

//Starting Coordinates                                                                                                                                                                    
  row = 0;
  col = 0;

//Output results
  check.valid_pos (row, col, size);                                                                                                                                          
  check.recursive(row, col, size, count);
  check.display_board(size);

  return 0;
}

bool Knight::valid_pos (int row, int col, int size){
  if(row>=size || col>=size ||  row<0 || col<0){//Test for chosen location being within board dimensions                                                                          
    return 1;

  }
  else{//Check to see if location was not previously visited                                                                                                                      
    if((board[row][col]) == 0){
      return 0;

    }
    else{//returns 1 if location previously visited                                                                                                                              
      return 1;

    }
  }
}

void Knight::display_board(int size)
{
  for(int c=0;c<size;c++)
    {
      for(int d=0;d<size;d++)
        {
          if(d==size-1)
            {
              cout<<"| "<<board[c][d]<<" |"<<endl;
            }
          else
{
              cout<<"| "<<board[c][d]<<" ";
            }
        }
    }
}


int Knight::check_board(int size)
{
  int rows, cols, checker;

  for(rows=0;rows<size;rows++)
    {
      for(cols=0;cols<size;cols++)
        {
          if(valid_pos(rows,cols,size)!=1)
            {
              checker=0;
            }
        }
    }
  return(checker);
}

void Knight::recursive(int row, int col, int size, int count)
{

  int checked;

  board[row][col]=count;

  if(valid_pos (row+2, col+1, size)!=1){
    count++;
    recursive(row+2, col+1, size, count);
    checked=check_board(size);
    if(checked==0){
      board[row+2][col+1]=0;
      count--;
    }
  }
  if(valid_pos (row+2, col-1, size)!=1){
    count++;
    recursive(row+2, col-1, size, count);
    checked=check_board(size);
    if(checked==0){
      board[row+2][col-1]=0;
      count--;

    }
  }
if(valid_pos (row+1, col+2, size)!=1){
    count++;
    recursive(row+1, col+2, size, count);
    checked=check_board(size);
    if(checked==0){
      board[row+1][col+2]=0;
      count--;
    }
  }
  if(valid_pos (row+1, col-2, size)!=1){
    count++;
    recursive(row+1, col-2, size, count);
    checked=check_board(size);
    if(checked==0){
      board[row+1][col-2]=0;
      count--;
    }
  }
  if(valid_pos(row-2, col+1, size)!=1){
    count++;
    recursive(row-2, col+1, size, count);
    checked=check_board(size);
    if(checked==0){
      board[row-2][col+1]=0;
      count--;
    }
  }

if(valid_pos (row-2, col-1, size)!=1){
    count++;
    recursive(row-2, col-1, size, count);
    checked=check_board(size);
    if(checked==0){
      board[row-2][col-1]=0;
      count--;
    }
  }
  if(valid_pos (row-1, col+2, size)!=1){
    count++;
    recursive(row-1, col+2, size, count);
    checked=check_board(size);
    if(checked==0){
      board[row-1][col+2]=0;
      count--;
    }
  }
  if(valid_pos (row-1, col-2, size)!=1){
    count++;
    recursive(row-1, col-2, size, count);
    checked=check_board(size);
    if(checked==0){
      board[row-1][col-2]=0;
      count--;
    }
  }

checked=check_board(size);
  if(checked==0){
    board[row][col]=0;
    count--;
  }
}
}

User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Program Conversion To Java?
7 Feb, 2008 - 10:30 AM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,654



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
QUOTE
Is it just public void display_board(int size) ?


Yes... but it will be defined IN your Knight class definition. Unlike C++ where you can separate the declarations (like in an .h file) and your implementation (like in a cpp file) you put them together. Replace your cout's with System.out.println calls and change your signature. Here is what it may look like...

CODE

public void display_board(int size)
{
  for(int c=0;c<size;c++)
    {
      for(int d=0;d<size;d++)
        {
          if(d==size-1)
            {
              System.out.println("| " + board[c][d] + " |");
            }
          else
      {
              System.out.println("| " + board[c][d] + " ");
          }
        }
    }
}


This would be defined in a class called "Knight" which would have a private member variable "board" which is your 2D array. (As you have defined in your Java so far)

When you call the function you just need to then use display_board(itssize);

You are on the right track. Java and C++ are very similar in ways. The main thing to watch is your printing and your function signatures.

Good luck!

"At DIC we be language converters, of the human code ninja kind!" decap.gif
User is offlineProfile CardPM
+Quote Post

needhelpbadly
RE: Program Conversion To Java?
7 Feb, 2008 - 10:42 AM
Post #3

New D.I.C Head
*

Joined: 5 Feb, 2008
Posts: 13

Here is my new code! I am getting far, but I think I am declaring size and board wrong ( the errors go throughout the whole program )


Everything seems good. Will it still operate the same way?

Thank you so much for reviewing

CODE
public class Knight
{
  boolean valid_pos;
//Global vars                                                                                                                                                                    
int size=6;//Board size                                                                                                                                                    
int board[size][size];  //Create board

public static main(String []args)
{
  Knight check = new Knight;
  int row, col, count=0;


  for(int a=0;a<=size;a++) //clear board to 0                                                                                                                                    
    {
      for(int b=0;b<=size;b++)
        {
          board[a][b]=0;
        }
    }

  //Take input
  row = 0;
  col = 0;

  check.valid_pos(row, col, size);

  //Output results                                                                                                                                                                

  check.recursive(row, col, size, count);
  check.display_board(size);

  return 0;
}

public boolean valid_pos(int row, int col, int size){
  if(row>=size || col>=size ||  row<0 || col<0){//Test for chosen location being within board dimensions                                                                          
    return true;

  }
  else{//Check to see if location was not previously visited                                                                                                                      
    if((board[row][col]) == 0){
      return false;

    }
    else{//returns 1 if location previously visited                                                                                                                              
      return true;

    }
  }
}

public void display_board(int size)
{
  for(int c=0;c<size;c++)
    {
      for(int d=0;d<size;d++)
        {
          if(d==size-1)
            {
              System.out.println("| "+board[c][d]+" |");
            }
          else
{
              System.out.println("| "+board[c][d]+" ");
            }
        }
    }
}


public int check_board(int size)
{
  int rows, cols, checker;

  for(rows=0;rows<size;rows++)
    {
      for(cols=0;cols<size;cols++)
        {
          if(valid_pos(rows,cols,size)!= true)
            {
              checker=0;
            }
        }
    }
  return(checker);
}

public void recursive(int row, int col, int size, int count)
{

  int checked;

  board[row][col]=count;

  if(valid_pos (row+2, col+1, size)!= true)
  {
    count++;
    recursive(row+2, col+1, size, count);
    checked=check_board(size);
    if(checked==0){
      board[row+2][col+1]=0;
      count--;
    }
  }
  if(valid_pos(row+2, col-1, size)!= true)
  {
    count++;
    recursive(row+2, col-1, size, count);
    checked=check_board(size);
    if(checked==0){
      board[row+2][col-1]=0;
      count--;

    }
  }
if(valid_pos (row+1, col+2, size)!= true)
{
    count++;
    recursive(row+1, col+2, size, count);
    checked=check_board(size);
    if(checked==0){
      board[row+1][col+2]=0;
      count--;
    }
  }
  if(valid_pos (row+1, col-2, size)!= true)
  {
    count++;
    recursive(row+1, col-2, size, count);
    checked=check_board(size);
    if(checked==0){
      board[row+1][col-2]=0;
      count--;
    }
  }
  if(valid_pos(row-2, col+1, size)!= true)
  {
    count++;
    recursive(row-2, col+1, size, count);
    checked=check_board(size);
    if(checked==0){
      board[row-2][col+1]=0;
      count--;
    }
  }

if(valid_pos (row-2, col-1, size)!= true)
{
    count++;
    recursive(row-2, col-1, size, count);
    checked=check_board(size);
    if(checked==0){
      board[row-2][col-1]=0;
      count--;
    }
  }
  if(valid_pos (row-1, col+2, size)!= true)
  {
    count++;
    recursive(row-1, col+2, size, count);
    checked=check_board(size);
    if(checked==0){
      board[row-1][col+2]=0;
      count--;
    }
  }
  if(valid_pos (row-1, col-2, size)!= true)
  {
    count++;
    recursive(row-1, col-2, size, count);
    checked=check_board(size);
    if(checked==0){
      board[row-1][col-2]=0;
      count--;
    }
  }

checked=check_board(size);
  if(checked==0){
    board[row][col]=0;
    count--;
  }
}
}

User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Program Conversion To Java?
7 Feb, 2008 - 10:53 AM
Post #4

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,654



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
You might want to start taking this conversation and questions over to the Java forum. Getting you over to Java is fine but when you start having the problems with the Java itself they might be able to help more. smile.gif

I help in both, but just thought the other java experts will want to jump in on your help actually getting your java working.
User is offlineProfile CardPM
+Quote Post

needhelpbadly
RE: Program Conversion To Java?
7 Feb, 2008 - 11:19 AM
Post #5

New D.I.C Head
*

Joined: 5 Feb, 2008
Posts: 13

I appreciate the help. I posted a topic over on the java boards.


If someone here knows, please don't hesitate to post. I'll be checking both posts. smile.gif thanks again!
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 11:37AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month