Good morning guys, I would eliminate the ifs and elses using polymorphisms that solves this lot of ifs, could someone help me? How could I solve this?
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Good morning guys, I would eliminate the ifs and elses using polymorphisms that solves this lot of ifs, could someone help me? How could I solve this?
Please post the code you are having problems with and an explanation of what you are trying to do.
If you don't understand my answer, don't ignore it, ask a question.
MasterG (May 31st, 2014)
Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.
MasterG (May 31st, 2014)
// if it is not draggin, see if it touches any draggable
if( !isDragging() ){
for( WorldElement t : getWorld().getDraggables() ){
Rectangle r*emphasized text*t = t.getBounds();
if( rt.intersects( ra ) ){
/* ESTES IFS TODOS TÊM DE DESAPARECER */
/* ESTES IFS TODOS TÊM DE DESAPARECER */
/* ESTES IFS TODOS TÊM DE DESAPARECER */
/* ALL THESE IFS MUST BE REMOVED */
/* ALL THESE IFS MUST BE REMOVED */
/* ALL THESE IFS MUST BE REMOVED */
if( t instanceof Fuel ) {
Fuel f = (Fuel)t;
draggedFuel = f;
f.pickUp();
}
else if( t instanceof Treasure ){
Treasure tre = (Treasure) t;
draggedTreasure = tre;
tre.pickUp();
}
else if( t instanceof Spaceship ){
Spaceship s = (Spaceship)t;
draggedShip = s;
s.pickUp();
}
int dtx = (ra.x - rt.x) + (ra.width - rt.width)/2;
int dty = (ra.y - rt.y) + (ra.height - rt.height);
t.move(dtx, dty );
break;
}
}
}
//---------------------------------outro exemplo----------------------------------------------
// se está a arrastar, verificar se está sobre a drop area
// if it is dragging then check if its over the drop area
if (isDragging()) {
Rectangle drop = getWorld().getMainSpaceship().getDropArea();
/* ESTES IFS TODOS TÊM DE DESAPARECER */
/* ESTES IFS TODOS TÊM DE DESAPARECER */
/* ESTES IFS TODOS TÊM DE DESAPARECER */
/* ALL THESE IFS MUST BE REMOVED */
/* ALL THESE IFS MUST BE REMOVED */
/* ALL THESE IFS MUST BE REMOVED */
if (draggedFuel != null) {
Rectangle rt = draggedFuel.getBounds();
if (drop.intersects(rt)) {
draggedFuel.release();
int dtx = (drop.x - rt.x) + (drop.width - rt.width) / 2;
draggedFuel.move(dtx, 0);
draggedFuel = null;
}
}
if (draggedTreasure != null) {
Rectangle rt = draggedTreasure.getBounds();
if (drop.intersects(rt)) {
draggedTreasure.release();
int dtx = (drop.x - rt.x) + (drop.width - rt.width) / 2;
draggedTreasure.move(dtx, 0);
draggedTreasure = null;
}
}
if (draggedShip != null) {
Rectangle rt = draggedShip.getBounds();
if (drop.intersects(rt)) {
draggedShip.release();
int dtx = (drop.x - rt.x) + (drop.width - rt.width) / 2;
draggedShip.move(dtx, 0);
draggedShip = null;
}
}
}
//---------------exemplo três------------------------------------------------------------
/**
* larga o que está a carregar drops what it is dragging
*/
public void drop() {
if (!isDragging())
return;
/* ESTES IFS TODOS TÊM DE DESAPARECER */
/* ESTES IFS TODOS TÊM DE DESAPARECER */
/* ESTES IFS TODOS TÊM DE DESAPARECER */
/* ALL THESE IFS MUST BE REMOVED */
/* ALL THESE IFS MUST BE REMOVED */
/* ALL THESE IFS MUST BE REMOVED */
if (draggedFuel != null) {
draggedFuel.release();
draggedFuel = null;
} else if (draggedTreasure != null) {
draggedTreasure.release();
draggedTreasure = null;
} else if (draggedShip != null) {
draggedShip.release();
draggedShip = null;
}
}
Good afternoon staff, I would eliminate the ifs and elses using polymorphisms that solves this lot of ifs, could someone help me? How could I solve this?
// if it is not draggin, see if it touches any draggable if( !isDragging() ){ for( WorldElement t : getWorld().getDraggables() ){ Rectangle r*emphasized text*t = t.getBounds(); if( rt.intersects( ra ) ){ /* ESTES IFS TODOS TÊM DE DESAPARECER */ /* ESTES IFS TODOS TÊM DE DESAPARECER */ /* ESTES IFS TODOS TÊM DE DESAPARECER */ /* ALL THESE IFS MUST BE REMOVED */ /* ALL THESE IFS MUST BE REMOVED */ /* ALL THESE IFS MUST BE REMOVED */ if( t instanceof Fuel ) { Fuel f = (Fuel)t; draggedFuel = f; f.pickUp(); } else if( t instanceof Treasure ){ Treasure tre = (Treasure) t; draggedTreasure = tre; tre.pickUp(); } else if( t instanceof Spaceship ){ Spaceship s = (Spaceship)t; draggedShip = s; s.pickUp(); } int dtx = (ra.x - rt.x) + (ra.width - rt.width)/2; int dty = (ra.y - rt.y) + (ra.height - rt.height); t.move(dtx, dty ); break; } } } //---------------------------------outro exemplo---------------------------------------------- // se está a arrastar, verificar se está sobre a drop area // if it is dragging then check if its over the drop area if (isDragging()) { Rectangle drop = getWorld().getMainSpaceship().getDropArea(); /* ESTES IFS TODOS TÊM DE DESAPARECER */ /* ESTES IFS TODOS TÊM DE DESAPARECER */ /* ESTES IFS TODOS TÊM DE DESAPARECER */ /* ALL THESE IFS MUST BE REMOVED */ /* ALL THESE IFS MUST BE REMOVED */ /* ALL THESE IFS MUST BE REMOVED */ if (draggedFuel != null) { Rectangle rt = draggedFuel.getBounds(); if (drop.intersects(rt)) { draggedFuel.release(); int dtx = (drop.x - rt.x) + (drop.width - rt.width) / 2; draggedFuel.move(dtx, 0); draggedFuel = null; } } if (draggedTreasure != null) { Rectangle rt = draggedTreasure.getBounds(); if (drop.intersects(rt)) { draggedTreasure.release(); int dtx = (drop.x - rt.x) + (drop.width - rt.width) / 2; draggedTreasure.move(dtx, 0); draggedTreasure = null; } } if (draggedShip != null) { Rectangle rt = draggedShip.getBounds(); if (drop.intersects(rt)) { draggedShip.release(); int dtx = (drop.x - rt.x) + (drop.width - rt.width) / 2; draggedShip.move(dtx, 0); draggedShip = null; } } } //---------------exemplo três------------------------------------------------------------ /** * larga o que está a carregar drops what it is dragging */ public void drop() { if (!isDragging()) return; /* ESTES IFS TODOS TÊM DE DESAPARECER */ /* ESTES IFS TODOS TÊM DE DESAPARECER */ /* ESTES IFS TODOS TÊM DE DESAPARECER */ /* ALL THESE IFS MUST BE REMOVED */ /* ALL THESE IFS MUST BE REMOVED */ /* ALL THESE IFS MUST BE REMOVED */ if (draggedFuel != null) { draggedFuel.release(); draggedFuel = null; } else if (draggedTreasure != null) { draggedTreasure.release(); draggedTreasure = null; } else if (draggedShip != null) { draggedShip.release(); draggedShip = null; } }
Last edited by MasterG; May 31st, 2014 at 10:13 AM. Reason: indent the code
Please edit your post and wrap your code with code tags:
[code=java]
YOUR CODE GOES HERE
[/code]
to get highlighting and preserve formatting.
--- Update ---
Do you have any small sample programs that show an example of what you want to do?
If you don't understand my answer, don't ignore it, ask a question.
MasterG (May 31st, 2014)
Hello guys, How to Eliminate ifs and elses using polymorphism, so that the code is more compact and easy to understand.
public void update( ){ // is its dead it does nothing if( dead ) return; // see what movement it is doing int dy = rising? -6: 6; if( walking && rising ) move( (dir==LEFT? -6: 6), dy); else if( walking ) move( (dir==LEFT? -4: 4), dy); else move( 0, dy ); if( shooting ) fire(); // see the bouding rectangle to check for colisions Rectangle ra = getBounds(); // if it is not draggin, see if it touches any draggable if( !isDragging() ){ for( WorldElement t : getWorld().getDraggables() ){ Rectangle rt = t.getBounds(); if( rt.intersects( ra ) ){ /* ALL THESE IFS MUST BE REMOVED */ /* ALL THESE IFS MUST BE REMOVED */ /* ALL THESE IFS MUST BE REMOVED */ if( t instanceof Fuel ) { Fuel f = (Fuel)t; draggedFuel = f; f.pickUp(); } else if( t instanceof Treasure ){ Treasure tre = (Treasure) t; draggedTreasure = tre; tre.pickUp(); } else if( t instanceof Spaceship ){ Spaceship s = (Spaceship)t; draggedShip = s; s.pickUp(); } int dtx = (ra.x - rt.x) + (ra.width - rt.width)/2; int dty = (ra.y - rt.y) + (ra.height - rt.height); t.move(dtx, dty ); break; } } } // if it is dragging then check if its over the drop area if( isDragging() ){ Rectangle drop = getWorld().getMainSpaceship().getDropArea(); /* ALL THESE IFS MUST BE REMOVED */ /* ALL THESE IFS MUST BE REMOVED */ /* ALL THESE IFS MUST BE REMOVED */ if( draggedFuel != null ){ Rectangle rt = draggedFuel.getBounds(); if( drop.intersects( rt ) ){ draggedFuel.release(); int dtx = (drop.x - rt.x) + (drop.width - rt.width)/2; draggedFuel.move(dtx, 0 ); draggedFuel = null; } } if( draggedTreasure != null ){ Rectangle rt = draggedTreasure.getBounds(); if( drop.intersects( rt ) ){ draggedTreasure.release(); int dtx = (drop.x - rt.x) + (drop.width - rt.width)/2; draggedTreasure.move(dtx, 0 ); draggedTreasure = null; } } if( draggedShip != null ){ Rectangle rt = draggedShip.getBounds(); if( drop.intersects( rt ) ){ draggedShip.release(); int dtx = (drop.x - rt.x) + (drop.width - rt.width)/2; draggedShip.move(dtx, 0 ); draggedShip = null; } } } // if the ship is full then test if it hits the ship to win the level if( getWorld().getFuelPercentage() == 100 ){ Spaceship n = getWorld().getMainSpaceship(); if( n.getBounds().intersects( getBounds() ) ){ getWorld().completed(); setPosition( new Point(-100,-100) ); } } setJetPacOn( true ); } /*** move the astronaut * @param. The x distance to move * @param. The y distance to move */ public void move(int dx, int dy ) { if( getImage().getPosicao().y + dy < 0 ) dy = -getImage().getPosicao().y; if( getImage().getPosicao().x < 0 ) dx += getWorld().getWidth(); else if( getImage().getPosicao().x > getWorld().getWidth() ) dx -= getWorld().getWidth(); super.move( dx, dy ); // also move the dragged if( isDragging() ){ /* /* ALL THESE IFS MUST BE REMOVED */ /* ALL THESE IFS MUST BE REMOVED */ /* ALL THESE IFS MUST BE REMOVED */ if( draggedFuel != null ) draggedFuel.move( dx, dy ); else if( draggedTreasure != null ) draggedTreasure.move( dx, dy ); else if( draggedShip != null ) draggedShip.move( dx, dy ); } } //} * drops what it is dragging */ public void drop(){ if( !isDragging() ) return; /* ALL THESE IFS MUST BE REMOVED */ /* ALL THESE IFS MUST BE REMOVED */ /* ALL THESE IFS MUST BE REMOVED */ if( draggedFuel != null ){ draggedFuel.release(); draggedFuel = null; } else if( draggedTreasure != null ){ draggedTreasure.release(); draggedTreasure = null; } else if( draggedShip != null ){ draggedShip.release(); draggedShip = null; } }
Please don't post duplicate threads.
Threads merged.
MasterG (May 31st, 2014)
Do you have any small sample programs that show an example of what you want to do?
If you don't understand my answer, don't ignore it, ask a question.