# Question about Line object w/ switch case

• 10-10-2011, 09:56 PM
kraigballa
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.
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
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.
• 10-11-2011, 01:34 AM
DiamondSoul
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.
• 10-11-2011, 08:41 AM
kraigballa
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.
• 10-11-2011, 03:45 PM
DiamondSoul
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.
• 10-11-2011, 07:37 PM
kraigballa
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:

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.
• 10-11-2011, 07:44 PM
DiamondSoul
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?
• 10-11-2011, 07:45 PM
kraigballa
Re: Question about Line object w/ switch case
It is code written by someone else that I am trying to work through and understand.
• 10-11-2011, 07:58 PM
DiamondSoul
Re: Question about Line object w/ switch case
In that case, it seems you understand it correctly.
• 10-11-2011, 08:00 PM
kraigballa
Re: Question about Line object w/ switch case