Results 1 to 9 of 9
  1. #1
    kraigballa is offline Senior Member
    Join Date
    Oct 2011
    Posts
    115
    Rep Power
    0

    Default Question about Line object w/ switch case

    Alright, I don't really get what is going on here. The code is below: this code is inside the Line class.
    Java Code:
    private int lineQuadrant;
    	private int lineOuterX;
    	private int lineOuterY;
    	private final int midX = 100;
    	private final int midY = 100;
    	private final int lineIncrement = 10;
    	private boolean drawLine = true;
    
    	public Line(int lineOuterX, int lineOuterY, int lineQuadrant) {
    		this.lineQuadrant = lineQuadrant;
    		this.lineOuterX = lineOuterX;
    		this.lineOuterY = lineOuterY;
    	}
    	
    	public void animate() {
    		if (drawLine) {
    			switch(lineQuadrant) {
    				case 4:
    					lineOuterY -= lineIncrement;
    					if (lineOuterY==0) lineQuadrant = 1;
    					break;
    				case 3:
    					lineOuterX -= lineIncrement;
    					if (lineOuterX==0) lineQuadrant = 4;
    					break;
    				case 2:
    					lineOuterY += lineIncrement;
    					if (lineOuterY==200) lineQuadrant = 3;
    					break;
    				case 1:
    					lineOuterX += lineIncrement;
    					if (lineOuterX==200) lineQuadrant = 2;
    					break;
    			}
    		}
    	}
    Now here is the object that is being created: this is inside a different class
    Java Code:
    private Line line;
    line = new Line(0, 0, 1);
    I get out of this that...
    lineOuterY is going to equal (0-10)...so -10 after case 4,
    lineOuterX is going to equal (0-10)...so -10 after case 3,
    lineOuterY is going to equal (0+10)...so 10 after case 2,
    lineOuterX is going to equal (0+10)...so 10 after case 1.

    Why is it that in the if statement it is saying if lineOuterY==0 or lineOuterX==0....then lineOuterY==200 or lineOuterX==200?? The lineIncrement is 10...so those values will never be right? Maybe I am thinking about this all wrong. Any help would be greatly appreciated!!

    Also with Java, when you have classes inside of a package you don't have to 'include' them into each other in order to use those classes? For instance, C++ you have to include a class into another class in order to use it's methods.
    Last edited by kraigballa; 10-10-2011 at 10:59 PM.

  2. #2
    DiamondSoul is offline Member
    Join Date
    Oct 2011
    Posts
    83
    Rep Power
    0

    Default Re: Question about Line object w/ switch case

    I'm not sure I understand the question. Could you be more specific, like "I expect the output to be [x], but it comes out as [y]"?

    Reading your code, what it looks like it should do is move the point (lineOuterX,lineOuterY) around the edge of a 200x200 square in 10 pixel increments. Is it not doing that?

    I don't know the answer to your second question - I've never made a Java project with more than one package. I do know that, if you have multiple files in the same package, the classes in every file will be visible in every other file, without you having to do anything special.

  3. #3
    kraigballa is offline Senior Member
    Join Date
    Oct 2011
    Posts
    115
    Rep Power
    0

    Default Re: Question about Line object w/ switch case

    Ok my question is how does the switch statement work? Hoping my math isn't failing me I don't ever get 0 or 200 like the if statements do...I would think that I am looking at the switch statement wrong because of this.

  4. #4
    DiamondSoul is offline Member
    Join Date
    Oct 2011
    Posts
    83
    Rep Power
    0

    Default Re: Question about Line object w/ switch case

    The switch statement will execute one and only one of the four blocks, depending on the value of lineQuadrant, or none of them if it is not 1, 2, 3, or 4.

  5. #5
    kraigballa is offline Senior Member
    Join Date
    Oct 2011
    Posts
    115
    Rep Power
    0

    Default Re: Question about Line object w/ switch case

    Alright, let me see if I have this down. So Line(0,0,1) is passing 1 as the value for lineQuadrant..then the switch statement is going to look for case 1, which is:

    Java Code:
    case 1:
                        lineOuterX += lineIncrement;
                        if (lineOuterX==200) lineQuadrant = 2;
                        break;
    Then from here is the lineOuterX==200 the lineQuadrant will be two. This will change the switch to go to case 2...and so forth. From there lineIncrement is used to determine when to move onto the next quadrant.

  6. #6
    DiamondSoul is offline Member
    Join Date
    Oct 2011
    Posts
    83
    Rep Power
    0

    Default Re: Question about Line object w/ switch case

    That seems correct. Just to clarify, is this code that you wrote yourself and are trying to fix some bug in it, or is it code written by someone else and you're trying to decipher what is does?

  7. #7
    kraigballa is offline Senior Member
    Join Date
    Oct 2011
    Posts
    115
    Rep Power
    0

    Default Re: Question about Line object w/ switch case

    It is code written by someone else that I am trying to work through and understand.

  8. #8
    DiamondSoul is offline Member
    Join Date
    Oct 2011
    Posts
    83
    Rep Power
    0

    Default Re: Question about Line object w/ switch case

    In that case, it seems you understand it correctly.

  9. #9
    kraigballa is offline Senior Member
    Join Date
    Oct 2011
    Posts
    115
    Rep Power
    0

    Default Re: Question about Line object w/ switch case

    Thank you for your help!!

Similar Threads

  1. about conditonal if else and switch case
    By niksipandit in forum New To Java
    Replies: 6
    Last Post: 09-22-2011, 12:53 PM
  2. Using a switch/case with my if statements
    By coding in forum New To Java
    Replies: 2
    Last Post: 03-07-2011, 09:01 AM
  3. Switch Case statement
    By seanfmglobal in forum New To Java
    Replies: 7
    Last Post: 02-15-2011, 02:18 PM
  4. if else changes to switch-case?
    By noobinoo in forum New To Java
    Replies: 1
    Last Post: 04-23-2010, 06:56 PM
  5. Replies: 2
    Last Post: 02-23-2009, 01:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •