473,406 Members | 2,390 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

C to Java...

Hi,
Could sme one please translate the following code from C to Java for me?

Code :
Expand|Select|Wrap|Line Numbers
  1. /*
  2.       Polytechnic University of the Philippines
  3.          Sta.Mesa, Manila
  4. College of Computer Management and Information Technology
  5.  
  6.             A
  7.             CASE STUDY
  8.             in
  9.           DATA STRUCTURES
  10.             AND
  11.             ALGORITHM
  12.  
  13. */
  14. #include<stdio.h>
  15. #include<conio.h>
  16. #include<ctype.h>
  17. #include<string.h>
  18. #include<stdlib.h>
  19.  
  20. #define MAXROW 15
  21. #define MAXCOL 30
  22.  
  23. #define ALIVE 1
  24. #define DEAD 0
  25.  
  26. struct conway{
  27.   int organism;
  28.   int neighbor;
  29. }cell[MAXROW+2][MAXCOL+2];
  30.  
  31. void twolinebox(int x1,int y1,int x2,int y2){
  32.  int x,y;
  33.  gotoxy(x1,y1); printf("É"); //alt-201
  34.  gotoxy(x2,y1); printf("»"); //alt-187
  35.   for(y=y1+1;y<y2;y++){
  36.     gotoxy(x1,y); printf("º"); //alt-186
  37.     gotoxy(x2,y); printf("º"); //alt-186
  38.   }
  39.  gotoxy(x1,y2); printf("È"); //alt-200
  40.  gotoxy(x2,y2); printf("¼"); //alt-188
  41.   for(x=x1+1;x<x2;x++){
  42.     gotoxy(x,y1); printf("Í"); //alt-205
  43.     gotoxy(x,y2); printf("Í"); //alt-205
  44.   }
  45.  gotoxy(x1+1,y1+1);
  46. }
  47.  
  48. void printxy(int x,int y,char string[]){
  49.  gotoxy(x,y); printf("%s",string);
  50. }
  51.  
  52. void center(int y,char string[]){
  53. int x=(80-strlen(string)+1)/2;
  54. gotoxy(x,y);printf("%s",string);
  55. }
  56.  
  57.  
  58. void initializeMatrix(void){
  59. int row,col;
  60.    for(row=-1;row<MAXROW+1;row++){
  61.      for(col=-1;col<MAXCOL+1;col++){
  62.      cell[row][col].organism=DEAD;
  63.      cell[row][col].neighbor=0;
  64.      }//end of inner for loop
  65.    }//end of outer for loop
  66. }//end of initializeMatrix
  67.  
  68. void showMatrix(void){
  69.   int row,col;
  70.   twolinebox(1,1,80,24);
  71.    for(row=0;row<MAXROW;row++){
  72.      for(col=0;col<MAXCOL;col++){
  73.     gotoxy(5+col,5+row); printf("²"); //alt-178
  74.     if(cell[row][col].organism==ALIVE){
  75.        gotoxy(5+col,5+row);
  76.        printf("%c",cell[row][col].organism);
  77.     }//end of if
  78.      }//end of inner for loop
  79.    }//end of outer for loop
  80.  
  81. }//end of showMatrix
  82.  
  83. void inputOrganism(void){
  84.   int org,row,col,err,a;
  85.      initializeMatrix();
  86.      do{
  87.        clrscr();
  88.        showMatrix();
  89.        gotoxy(5,3); printf("Input no. of organism to display(1-30 
  90. only):
  91. ");
  92.        gotoxy(50,3); scanf("%d",&org);
  93.      }while(org<1||org>30);
  94.  
  95.      for(a=1;a<=org;a++){
  96.       clrscr();
  97.       showMatrix();
  98.       do{
  99.        do{
  100.      gotoxy(36,6); printf("Input cell row position of organism %d",a);
  101.      printxy(36,7,"row:(1-15)only\>     ");
  102.      gotoxy(55,7); scanf("%d",&row); row--;
  103.        }while(row>15||row<0);
  104.        do{
  105.      gotoxy(36,9); printf("Input cell column position of organism 
  106. %d",a+1);
  107.      gotoxy(36,10); printf("col:(1-30)only\>     ");
  108.      gotoxy(55,10); scanf("%d",&col); col--;
  109.        }while(col>30||col<0);
  110.        if(cell[row][col].organism==ALIVE){
  111.      gotoxy(36,20); printf("cell[%d][%d] is already 
  112. occupied!",row+1,col+1);
  113.      gotoxy(36,21); printf("Try other cell...");
  114.      getch();
  115.      gotoxy(36,20); clreol();
  116.      gotoxy(36,21); clreol();
  117.        }//end of if
  118.       }while(cell[row][col].organism==ALIVE);
  119.        cell[row][col].organism=ALIVE;
  120.        showMatrix();
  121.      }//end of for loop
  122.  
  123. }//end of inputOrganism
  124.  
  125. void checkCell(void){
  126.    int row,col;
  127.  
  128.    //in this part the neighboring cells are checked,
  129.    //if the neighborng cell is an occupied cell
  130.    //then the neighbor element of the structure cell is increased by 1
  131.  
  132.      for(row=0;row<MAXROW;row++){
  133.       for(col=0;col<MAXCOL;col++){
  134.          cell[row][col].neighbor=0;    //initialize neighbor to 0
  135.       if(cell[row-1][col-1].organism==ALIVE)
  136.          cell[row][col].neighbor++;
  137.       if(cell[row-1][col].organism==ALIVE)
  138.          cell[row][col].neighbor++;
  139.       if(cell[row][col-1].organism==ALIVE)
  140.          cell[row][col].neighbor++;
  141.       if(cell[row][col+1].organism==ALIVE)
  142.          cell[row][col].neighbor++;
  143.       if(cell[row+1][col].organism==ALIVE)
  144.          cell[row][col].neighbor++;
  145.       if(cell[row+1][col-1].organism==ALIVE)
  146.          cell[row][col].neighbor++;
  147.       if(cell[row-1][col+1].organism==ALIVE)
  148.          cell[row][col].neighbor++;
  149.       if(cell[row+1][col+1].organism==ALIVE)
  150.          cell[row][col].neighbor++;
  151.       }//end of inner for loop
  152.      }//end of outer for loop
  153.  
  154.     //in this part we will check the cell's no. of neighbor then
  155.     //place or kill organism in that particular cell
  156.     //based on its neighboring cell according to Conway's Rule
  157.  
  158.      for(row=0;row<MAXROW;row++){
  159.     for(col=0;col<MAXCOL;col++){
  160.       if(cell[row][col].organism==ALIVE){  //rule2 and 3 if and occupied 
  161. cell
  162.          if(cell[row][col].neighbor<2) //rule2: has less than 2 neighbor
  163.         cell[row][col].organism=DEAD; //organism will die in that cell
  164.          else if(cell[row][col].neighbor>3) //rule3: has greater than 3
  165.         cell[row][col].organism=DEAD; //organism will die in that cell
  166.       }
  167.       else //if(cell[row][col].organism!=1) //rule1. if an empty cell
  168.          if(cell[row][col].neighbor==3) //has exactly 3 neigbor
  169.         cell[row][col].organism=ALIVE;  //an organism is born in that cell
  170.  
  171.     }//end of inner for loop
  172.      }//end of outer for loop
  173.  
  174. }//end of checkCell
  175.  
  176. void showGeneration(void){
  177.   int gen,g;
  178.     clrscr();
  179.       showMatrix();
  180.       gotoxy(5,3); printf("Enter no. of generation you want to 
  181. see...");
  182.       gotoxy(5,4); printf("gen:\>     ");
  183.       gotoxy(12,4); scanf("%d",&gen);
  184.       for(g=1;g<=gen;g++){
  185.      clrscr();
  186.      showMatrix();
  187.      gotoxy(36,10); printf("GENERATION %d",g);
  188.      checkCell();
  189.        getch();
  190.       }//end of for loop
  191. }//end of showGeneration
  192.  
  193.  
  194. void welcome(void){
  195.        clrscr();
  196.        twolinebox(1,1,80,24);
  197.        gotoxy(35,8); printf("WELCOME");
  198.        gotoxy(37,10); printf("to");
  199.        gotoxy(35,12); printf("CONWAY'S");
  200.        gotoxy(36,13); printf("GAME");
  201.        gotoxy(38,14); printf("of");
  202.        gotoxy(37,15); printf("LIFE");
  203.        gotoxy(26,23); printf("press any key to continue...");
  204.        getch();
  205.        clrscr();
  206.        twolinebox(1,1,80,24);
  207.        center(3,"CONWAYS GAME OF LIFE");
  208.        printxy(5,5,"RULE OF THE GAME");
  209.        printxy(5,7,"If in one generation,");
  210.        printxy(10,8,"an empty cell has exactly three neighboring cells
  211. containing");
  212.        printxy(10,9,"organisms, then a new organism is born in that 
  213. cell
  214. in the ");
  215.        printxy(10,10,"next generation.");
  216.        printxy(5,11,"If in one generation,");
  217.        printxy(10,12,"an organism has fewer than two neighboring cells
  218. containing");
  219.        printxy(10,13,"organisms, then it dies from isolation before 
  220. the");
  221.        printxy(10,14,"next generation and its cell become empty.");
  222.        printxy(5,15,"If in one generation,");
  223.        printxy(10,16,"an organism has more than three neighboring cells
  224. containing");
  225.        printxy(10,17,"organisms, then it dies from overcrowding before
  226. the");
  227.        printxy(10,18,"next generation and its cell become empty.");
  228.        printxy(5,19,"All other organisms survive unchanged to the next
  229. generation.");
  230.        center(21,"Press any key to continue...");
  231.        getch();
  232. }
  233.  
  234. void quit(void){
  235.        clrscr();
  236.        twolinebox(1,1,80,24);
  237.        center(7,"PROGRAMMED");
  238.        center(8,"BY");
  239.        center(9,"BSCS 2-2");
  240.        center(11,"Frederick Badion");
  241.        center(12,"Michelle Baylon");
  242.        center(13,"Kirby Adriano");
  243.        center(14,"Jamil Soller");
  244.        center(15,"Sweet Joan Zamora");
  245.        getch();
  246. }
  247.  
  248. void main(){
  249. char opt;
  250.  textmode(BW80);
  251.  do{
  252.    clrscr();
  253.    welcome();
  254.    inputOrganism();
  255.    showGeneration();
  256.    gotoxy(5,22);
  257.    printf("Try again? [Y/N] ");
  258.    opt=getch();
  259.  }while(toupper(opt)=='Y');
  260. quit();
  261. exit(1);
  262. }//end of main
Nov 17 '09 #1
3 2703
Dököll
2,364 Expert 2GB
Hey alireza6485!

Looks like you have your hands full. There are certain pieces of software out there that can do what you need. Care to fetch all of Google!

Good luck with the porject!
Nov 18 '09 #2
I figured it out...........
Nov 24 '09 #3
Hi I'm Frederick Badion.

I created that program...

Thanks for copying it! BUT I would appreciate it more if you converted the program to JAVA yourself, then share it to everyone here...

Students like you are not good learner.. because they rely mostly to Internet rather than figuring out problems themselves. just like this one.. It is already created you only have to convert it to JAVA. why bother other people to convert it for you.
Feb 18 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Ravi Tallury | last post by:
Hi We are having issues with our application, certain portions of it stop responding while the rest of the application is fine. I am attaching the Java Core dump. If someone can let me know what...
1
by: ptaz | last post by:
Hi I'm trying to run a web page but I get the following error. Ca anyone please tell me a solution to this. Thanks Ptaz HTTP Status 500 - type Exception report
11
by: DrUg13 | last post by:
In java, this seems so easy. You need a new object Object test = new Object() gives me exactly what I want. could someone please help me understand the different ways to do the same thing in...
0
by: mailkhurana | last post by:
Hii , I am trying to use a type 2 driver to connect to DB2 0n AIX 5 I have a small java test to class to establish a conneciton with the db .. I am NOT using WAS or any appserver When I try to...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
12
by: Mark Fink | last post by:
I wrote a Jython class that inherits from a Java class and (thats the plan) overrides one method. Everything should stay the same. If I run this nothing happens whereas if I run the Java class it...
0
by: jaywak | last post by:
Just tried running some code on Linux (2.4.21-32.0.1.EL and Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_04-b05)) and Windows XPSP2 (with Java HotSpot(TM) Client VM (build...
1
by: jaimemartin | last post by:
hello, I want to validate an xml by means of a schema (xsd). To do that first of all I´m using a SchemaFactory. The problem is that if I run the code in Windows all works fine, but If I run it in...
0
oll3i
by: oll3i | last post by:
package library.common; import java.sql.ResultSet; public interface LibraryInterface { public ResultSet getBookByAuthor(String author); public ResultSet getBookByName(String name);
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.