Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[Moved] Help me with the logic of my Java coding

Status
Not open for further replies.

cool01

Member level 2
Joined
Jan 9, 2009
Messages
52
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,851
hi
i am facing problem while coding can anyone tell me how to come out of it and be a an expert in coding basically i am facing problem in logic..........one way is practice is their any other way

how the things should be constructed and organised, which will be better option (efficient way or method ) while coding................ wat should be used where that should be used and and how..... etc i guess u get me...

thank you plz reply......

Moved to PC Programming and Interfacing [alexan_e]
 

re: coding Java

Can you post what you need exactly?
For which application and in which language you are coding?
specific questions than generic questions will help you to get what you need exactly.
 

re: coding Java

i am basically facing problem in logic.........and i am working on java.....................

eg:
to make a box or to get x y coordinate u can do it by......

x=value%2;
y=value/2;

their much more things in it ......i am coding like a lay man and i want be expert in it.......
i will give two examples it is j2me code......first one is mine and second one is expert he used things which havent come to my mind i mean i was unaware that it can be coded in that way also......


eg1
import java.util.Random;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import java.util.Vector;
import java.lang.Integer;

public class BrainCanvas extends Canvas implements Runnable{
///////////////////////////////Declaration////////////////////////////////////////

private Main midlet = null;Thread myThread = null;

public int canvasW,canvasH;


private Display display;

public final int UP=-1,DOWN=-2,LEFT=-3,RIGHT=-4,ENTER=-5;


// public static int p=-7,q=-7,newP=-7,newQ=-7,dir;

public int a[][]= {{20,40,60,80,100,120,140},
{20,40,60,80,100,120,140},
{20,40,60,80,100,120,140},
{20,40,60,80,100,120,140},
{20,40,60,80,100,120,140},
{20,40,60,80,100,120,140},
{20,40,60,80,100,120,140},};

public int b[][]= {{20,20,20,20,20,20,20},
{40,40,40,40,40,40,40},
{60,60,60,60,60,60,60},
{80,80,80,80,80,80,80},
{100,100,100,100,100,100,100},
{120,120,120,120,120,120,120},
{140,140,140,140,140,140,140}};


// public int value[][]={{1,1,1,1,1,1,1},{1,1,1,1,1,1,1},{1,1,1,1,1,1,1},{1,1,1,2,1,1,1},{1,1,1,1,1,1,1},{1,1,1,1,1,1,1},{1,1,1,1,1,1,1}};
public int value[][]={{0,0,1,1,1,0,0},{0,0,1,1,1,0,0},{1,1,1,1,1,1,1},{1,1,1,2,1,1,1},{1,1,1,1,1,1,1},{0,0,1,1,1,0,0},{0,0,1,1,1,0,0}};


public int width=20;
public int empty=2;
public int select0=3;
public int select=3;
public int select1=4;
boolean flag=true;
int x=60,y=60;
int count=0;


public BrainCanvas(Main midlet){

try{
setFullScreenMode(true);
this.midlet = midlet;

canvasW = getWidth();
canvasH = getHeight();

this.startThread();
}catch(Exception e){}
}
public boolean startThread() {
System.out.println("in start thread");
boolean retVal = false;

try {

myThread = new Thread(this);

myThread.start();

retVal = true;
} catch(Exception e) {

}
return retVal;
}

void initialise() {

int a[][]= {{20,40,60,80,100,120,140},
{20,40,60,80,100,120,140},
{20,40,60,80,100,120,140},
{20,40,60,80,100,120,140},
{20,40,60,80,100,120,140},
{20,40,60,80,100,120,140},
{20,40,60,80,100,120,140},};

int b[][]= {{20,20,20,20,20,20,20},
{40,40,40,40,40,40,40},
{60,60,60,60,60,60,60},
{80,80,80,80,80,80,80},
{100,100,100,100,100,100,100},
{120,120,120,120,120,120,120},
{140,140,140,140,140,140,140}};

int value[][]={{0,0,1,1,1,0,0},{0,0,1,1,1,0,0},{1,1,1,1,1,1,1},{1,1,1,2,1,1,1},{1,1,1,1,1,1,1},{0,0,1,1,1,0,0},{0,0,1,1,1,0,0}};

width=20;
empty=2;
select0=3;
select1=4;
flag=true;
x=60;y=60;
count=0;
}
public void run(){
System.out.println("in run");

try{

while(true){
myThread.sleep(100);

repaint();
}

} catch(Exception e){}
}

public void paint(Graphics g) {

drawmyrect(g);
drawGrid(g);
g.setColor(0,0,255);
g.fillRect(x,y,20,20);
g.setColor(0,255,0);

}


public void keyPressed(int keyCode) {

int i=0,j=0;
if(x>1 && y>1) {
j=(x/width)-1;
i=(y/width)-1;
}
switch(keyCode) {
case -1:
{
if(((x>=20 && x<=40) && y>60 || y>20 && (x>=60 && x<=100)|| y>60 && (x>100 && x<=140)))
y-=20;
}
break;
case -2:
{

if(((x>=20 && x<=40) && y<100 || y<140 && (x>=60 && x<=100)|| y<100 && (x>100 && x<=140)))
y+=20;

}
break;
case -3:
{

if((x>20 && (y>=60 && y<=100))|| x>60 && (y>=20 && y<=40)|| x>60 &&(y>100 && y<=140) )
x-=20;
// System.out.println(x+" in 4 "+y);
}
break;
case -4:
{

if(((y>=20 && y<=40) && x<100 || x<140 && (y>=60 && y<=100)|| x<100 && (y>100 && y<=140)))
x+=20;
}
break;
case -5:
{


System.out.println("back frm check");
if(value[j]==1) {
flag=true;
check();
value[j]=select;
flag=false;
System.out.println("in falg false");
}

if(flag==false) {
if(value[j]==empty) {
value[j]=select1;
Game(i,j);
}
}
if(value[j]==select1){
value[j]=empty;
}
}
break;
}
}
int check() {
System.out.println("in check");
for (int i = 0; i < value.length-1; i++){
for (int j= 0; j < value.length-1; j++){
if(value[j]==select) {
System.out.println("in falg true"+i+" j "+j);
value[j]=1;

}
}
}
return 0;
}

public void clearscreen(Graphics g) {
g.setColor(255,255,255);
g.fillRect(0,0,getWidth(),getHeight());
}

public void drawmyrect(Graphics g) {
g.setColor(255,255,255);
g.fillRect(0,0,canvasW,canvasH);


}
public void drawGrid(Graphics g) {

for(int i=0;i<7;i++) {
for(int j=0;j<7;j++) {
g.drawRect(a[j],b[j],19,19);
}
}
for(int i=0;i<7;i++) {
for(int j=0;j<7;j++) {

if(value[j]==1) {
g.setColor(250,50,250);
g.fillRect(a[j],b[j],19,19);

}
if(value[j]==0) {
g.setColor(255,255,255);
g.fillRect(a[j],b[j],19,19);

}

if(value[j]==empty) {
g.setColor(255,250,0);
g.fillRect(a[j],b[j],19,19);

}
if(value[j]==select) {
g.setColor(0,255,0);
g.fillRect(a[j],b[j],19,19);

}
int count=0;
if(value[j]==select)
count++;
if(count>46) {
g.setColor(0,255,0);
g.drawString("player win",20,20,0);
restart();
}
}
}
}

public void Game(int i,int j) {

if(value[j]==select1){




if( (i>=0 && i<6 && j+2<6 && j+2>=0) && value[j+1]==1 && value[j+2]==select){
System.out.println("i "+i+" j "+j+" x "+x+" y "+y);
value[j]=1;
value[j+1]=empty;
value[j+2]=empty;
// System.out.println("i "+i+" j "+j);
// System.out.println("x "+x+" y "+y);
// System.out.println("value[j] "+value[j]+" value[i+1][j] "+value[j]+" value[i+2][j] "+value[i+2][j]);
} else if( (i+2>=0 && i+2<6 && j<6 && j>=0) && value[i+1][j]==1 && value[i+2][j]==select ) {
System.out.println("i "+i+" j "+j+" x "+x+" y "+y);
value[j]=1;
value[i+1][j]=empty;
value[i+2][j]=empty;
// System.out.println("i "+i+" j "+j);
// System.out.println("x "+x+" y "+y);
// System.out.println("value[j] "+value[j]+" value[j+1] "+value[j+1]+" value[j+2] "+value[j+2]);
} else if( (i>=0 && i<6 && j-2<6 && j-2>=0) &&value[j-1]==1 && value[j-2]==select) {
System.out.println("i "+i+" j "+j+" x "+x+" y "+y);
value[j]=1;
value[j-1]=empty;
value[j-2]=empty;
// System.out.println("i "+i+" j "+j);
// System.out.println("x "+x+" y "+y);
// System.out.println("value[j] "+value[j]+" value[i-1][j] "+value[i-1][j]+" value[i-2][j] "+value[i-2][j]);

}else if( (i-2>=0 && i-2<6 && j<6 && j>=0) &&value[i-1][j]==1 && value[i-2][j]==select) {
System.out.println("i "+i+" j "+j+" x "+x+" y "+y);
value[j]=1;
value[i-1][j]=empty;
value[i-2][j]=empty;
// System.out.println("i "+i+" j "+j);
// System.out.println("x "+x+" y "+y);
// System.out.println("value[j] "+value[j]+" value[j-1] "+value[j-1]+" value[j-2] "+value[j-2]);

}

}
}

}


-------------------------------------------------------------------------------------------------------------------------------------------------------
eg2


import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import java.util.Vector;
import java.util.Random;
import java.lang.Boolean;
public class Marbles extends Canvas implements Runnable {

Thread thread=null;

Image red_image=null,brown_image=null,blank_image=null;

private static int marble_size=30;

Vector marble_alive=new Vector(1); //

private static Random random= new Random();

private static int init; //position of initially dead marble
private static int x_size; //number of marble on X-AxIS
private static int y_size; //number of marble on Y-AxIS
private static int line_x; //Coordinate of select line
private static int line_y; // " "
private static boolean selected;
private static int selected_i;
private static boolean finish;
private static int count;

public Marbles() {
try{
red_image=Image.createImage("/Red_Marble.PNG");
brown_image=Image.createImage("/Brown_Marble.PNG");
blank_image=Image.createImage("/Blank.PNG");
} catch(Exception e) {
System.out.println("Exception in run----"+e);
}
thread=new Thread(this);
thread.start();
x_size=getWidth()/marble_size;
y_size=getHeight()/marble_size;
gameInitialization();
}

public void gameInitialization() {
count=0;
finish=true;
selected=false;
line_x=2*marble_size;
line_y=0;
for(int k=x_size*y_size-1;k>=0;k--) {
marble_alive.addElement(new Boolean(true));
}

init=2*x_size+random.nextInt(marble_alive.size()-1-4*x_size);
marble_alive.setElementAt(new Boolean(false),init);

}
public void run() {
try {
while(true) {
thread.sleep(100);
repaint();
}
} catch(Exception ex) {

System.out.println("Exception in run----"+ex);
}
}

public void paint(Graphics g) {

g.setColor(255,255,255); //back ground color--BLACK
g.fillRect(0,0,getWidth(),getHeight());
count=0;
finish=true;
for(int k=marble_alive.size()-1;k>=0;k--) {

int x=(k%x_size)*marble_size;
int y=(k/x_size)*marble_size;
if(((Boolean)marble_alive.elementAt(k)).booleanValue()) {
count++;

g.drawImage(red_image,x,y,0);
if((x==0 || x==marble_size || x==(x_size-2)*marble_size || x==(x_size-1)*marble_size)) {
if((y<2*marble_size)||y>(y_size-3)*marble_size) {
//g.fillRect(x,y,marble_size,marble_size);
marble_alive.setElementAt(new Boolean(false),k);
}
}




if(finish) {
if(k>=x_size && k<=marble_alive.size()-1-x_size) {

if(((Boolean)marble_alive.elementAt(k-1)).booleanValue() || ((Boolean)marble_alive.elementAt(k+1)).booleanValue() || ((Boolean)marble_alive.elementAt(k-x_size)).booleanValue() || ((Boolean)marble_alive.elementAt(k+x_size)).booleanValue()) {

finish=false;
}
} else if(k>0 && k<x_size) {
if(((Boolean)marble_alive.elementAt(k-1)).booleanValue() || ((Boolean)marble_alive.elementAt(k+1)).booleanValue() || ((Boolean)marble_alive.elementAt(k+x_size)).booleanValue())
finish=false;
} else if(k>marble_alive.size()-1-x_size && k<marble_size) {
if(((Boolean)marble_alive.elementAt(k-1)).booleanValue() || ((Boolean)marble_alive.elementAt(k+1)).booleanValue() || ((Boolean)marble_alive.elementAt(k+x_size)).booleanValue())
finish=false;
}
}
} else {
g.drawImage(blank_image,x,y,0);

}
if((x==0 || x==marble_size ||x==(x_size-2)*marble_size || x==(x_size-1)*marble_size)) {
if((y<2*marble_size)||y>(y_size-3)*marble_size) {
g.fillRect(x,y,marble_size,marble_size);
//marble_alive.setElementAt(new Boolean(false),k);
}
}


}

if(selected) {
int x=(selected_i%x_size)*marble_size;
int y=(selected_i/x_size)*marble_size;
g.drawImage(brown_image,x,y,0);
}
g.setColor(0,0,255); //select line color-
g.drawRect(line_x,line_y,marble_size,marble_size);
if(finish) {
g.setColor(0); //back ground color--BLACK
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(255,255,255); //set color white
g.drawString("Game Over",100,100,0);
if(count<3)
g.drawString("You Did Excellent",100,120,0);
else if(count<5)
g.drawString("Well Done",100,120,0);
else if(count<7)
g.drawString("Good",100,120,0);
else
g.drawString("Try Again",100,120,0);
}
}

protected void keyPressed(int keyCode) {
if(keyCode==-1) //up scroll key
{
if((line_x<2*marble_size || line_x>(x_size-3)*marble_size) && line_y+marble_size==3*marble_size);
else if(line_y>0)
line_y-=marble_size;
}

if(keyCode==-2) //down scroll key
{
if((line_x<2*marble_size ||line_x>(x_size-3)*marble_size) && line_y+marble_size==(y_size-2)*marble_size);
else if(line_y+marble_size<y_size*marble_size)
line_y+=marble_size;
}
if(keyCode==-3) //left scroll key
{
if((line_y+marble_size<3*marble_size || line_y+marble_size>(y_size-2)*marble_size) && line_x==2*marble_size);
else if(line_x>0)
line_x-=marble_size;
}

if(keyCode==-4) //right scroll key
{
if((line_y+marble_size<3*marble_size || line_y+marble_size>(y_size-2)*marble_size) && line_x==(x_size-3)*marble_size);
else if(line_x+marble_size<x_size*marble_size)
line_x+=marble_size;
}

if(keyCode==-5) //enter key
{

if(!selected) {
selected_i=x_size*(line_y/marble_size)+line_x/marble_size;
if(((Boolean)marble_alive.elementAt(selected_i)).booleanValue() ) //to select the marble
selected=true;
} else {
int i=x_size*(line_y/marble_size)+line_x/marble_size;

if(!((Boolean)marble_alive.elementAt(i)).booleanValue()) {
if(selected_i-i==2*x_size) {
marble_alive.setElementAt(new Boolean(false),selected_i-x_size);
marble_alive.setElementAt(new Boolean(false),selected_i);
selected=false;
marble_alive.setElementAt(new Boolean(true),i);
} else if(i-selected_i==2*x_size) {
marble_alive.setElementAt(new Boolean(false),selected_i+x_size);
marble_alive.setElementAt(new Boolean(false),selected_i);
selected=false;
marble_alive.setElementAt(new Boolean(true),i);
} else if(selected_i-i==2) {
marble_alive.setElementAt(new Boolean(false),selected_i-1);
marble_alive.setElementAt(new Boolean(false),selected_i);
selected=false;
marble_alive.setElementAt(new Boolean(true),i);
} else if(i-selected_i==2) {
marble_alive.setElementAt(new Boolean(false),selected_i+1);
marble_alive.setElementAt(new Boolean(false),selected_i);
selected=false;
marble_alive.setElementAt(new Boolean(true),i);
}

//marble_alive.setElementAt(new Boolean(false),selected_i);
} else if(((Boolean)marble_alive.elementAt(i)).booleanValue())
selected_i=i;
}
}

}


}
 

re: coding Java

Hi cool01,
i am not java developer, but as i gone through your both examples.its clear that you missing few general points for coding.
- Be clear with functionality and divide in to smaller modules before starting to code.
-use correct naming conventions to avoid confusion.

Check out the following link it might be helpful for you as the programming strategies are given clearly
Java Programming Style Guidelines
 
  • Like
Reactions: cool01

    cool01

    Points: 2
    Helpful Answer Positive Rating
re: coding Java

thank you sir/madam
i know about the name convention but i write it casualy but the main problem is wih coding have you seen his coding and my coding. while i was coding things are not coming to my mind and the things are getting more and more complicated........i do more hard coding not standard coding and even it is not optimised........lengthy wat more to write in praise .......hope ur getting wat i meant................
 

re: coding Java

ya cool01,but its an easy task once you familiar in integrating functionality of the codes before starting to code.
just give a try so by taking more time for planning than coding.
 
  • Like
Reactions: cool01

    cool01

    Points: 2
    Helpful Answer Positive Rating
Re: coding Java

what i am doing now is seeing ready made codes.......is this practice helpful to me or i should adopt some other way.......as i want to get aware and used to /familiar with coding....... but how should i read and understand code so that i get the gist of coding technique and understand which could be best way of particular code...............and one more thing may i get your email id so i keep in touch with you
 

Re: Help me with the logic of my Java coding

even i commited very silly or basic mistake like i redeclare the variables as i want to reinitialise the variables which is wrong.........even i declare a array and havent initialise because of which the compiler was giving out of bound error........like wise commited many silly mistake can you help me out coming out of this silly mistake........right now i am coding on try and error basis checking whether this way will work or not i am not aware of various ways of writing the same code.........and which one will be best suitable and effective
 

Re: Help me with the logic of my Java coding

Hi Cool01,
First read the book suggested in the post it will have step by step explanation, You cannot code and make a fully functional robot of IRON MAN overnight. First read the book and follow the exercises given in the book.
If you want to just know a little about programming, find a program to find sum of two numbers and get over with that program alone.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top