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.

Results 1 to 9 of 9

Thread: Dispose of containers ifs and elses using polymorphism

  1. #1
    Junior Member
    Join Date
    May 2014
    Location
    Portugal
    Posts
    4
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Dispose of containers ifs and elses using polymorphism

    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?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Dispose of containers ifs and elses using polymorphism

    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.

  3. The Following User Says Thank You to Norm For This Useful Post:

    MasterG (May 31st, 2014)

  4. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Dispose of containers ifs and elses using polymorphism

    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.

  5. The Following User Says Thank You to GregBrannon For This Useful Post:

    MasterG (May 31st, 2014)

  6. #4
    Junior Member
    Join Date
    May 2014
    Location
    Portugal
    Posts
    4
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Dispose of containers ifs and elses using polymorphism

    // 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;
    }
    }

  7. #5
    Junior Member
    Join Date
    May 2014
    Location
    Portugal
    Posts
    4
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default How to Eliminate FIs and elses

    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

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Dispose of containers ifs and elses using polymorphism

    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.

  9. The Following User Says Thank You to Norm For This Useful Post:

    MasterG (May 31st, 2014)

  10. #7
    Junior Member
    Join Date
    May 2014
    Location
    Portugal
    Posts
    4
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default How to Eliminate FIs and elses

    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;
    		}
     
    	}

  11. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Dispose of containers ifs and elses using polymorphism

    Please don't post duplicate threads.

    Threads merged.

  12. The Following User Says Thank You to GregBrannon For This Useful Post:

    MasterG (May 31st, 2014)

  13. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Dispose of containers ifs and elses using polymorphism

    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.

Similar Threads

  1. dispose(); not working
    By newtolearningjava in forum What's Wrong With My Code?
    Replies: 63
    Last Post: April 25th, 2014, 07:01 PM
  2. Using a Loop and Nested Ifs to Count Number of Vowels in a String
    By Potat in forum Loops & Control Statements
    Replies: 17
    Last Post: February 21st, 2013, 09:35 PM
  3. modality for Lightweight containers
    By chronoz13 in forum AWT / Java Swing
    Replies: 0
    Last Post: November 20th, 2011, 05:33 AM
  4. Syntax error with dispose()
    By jagnat in forum AWT / Java Swing
    Replies: 1
    Last Post: October 14th, 2011, 11:00 AM
  5. dispose()'ing a JFrame and replacing it
    By musasabi in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 14th, 2010, 02:31 PM

Tags for this Thread