Results 1 to 9 of 9
- 10-10-2011, 09:56 PM #1
Senior Member
- Join Date
- Oct 2011
- Posts
- 115
- Rep Power
- 0
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.
Now here is the object that is being created: this is inside a different classJava 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; } } }
I get out of this that...Java Code:private Line line; line = new Line(0, 0, 1);
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 09:59 PM.
- 10-11-2011, 01:34 AM #2
Member
- Join Date
- Oct 2011
- Posts
- 83
- Rep Power
- 0
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 #3
Senior Member
- Join Date
- Oct 2011
- Posts
- 115
- Rep Power
- 0
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 #4
Member
- Join Date
- Oct 2011
- Posts
- 83
- Rep Power
- 0
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 #5
Senior Member
- Join Date
- Oct 2011
- Posts
- 115
- Rep Power
- 0
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:
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.Java Code:case 1: lineOuterX += lineIncrement; if (lineOuterX==200) lineQuadrant = 2; break;
- 10-11-2011, 07:44 PM #6
Member
- Join Date
- Oct 2011
- Posts
- 83
- Rep Power
- 0
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 #7
Senior Member
- Join Date
- Oct 2011
- Posts
- 115
- Rep Power
- 0
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 #8
Member
- Join Date
- Oct 2011
- Posts
- 83
- Rep Power
- 0
Re: Question about Line object w/ switch case
In that case, it seems you understand it correctly.
- 10-11-2011, 08:00 PM #9
Senior Member
- Join Date
- Oct 2011
- Posts
- 115
- Rep Power
- 0
Similar Threads
-
about conditonal if else and switch case
By niksipandit in forum New To JavaReplies: 6Last Post: 09-22-2011, 11:53 AM -
Using a switch/case with my if statements
By coding in forum New To JavaReplies: 2Last Post: 03-07-2011, 08:01 AM -
Switch Case statement
By seanfmglobal in forum New To JavaReplies: 7Last Post: 02-15-2011, 01:18 PM -
if else changes to switch-case?
By noobinoo in forum New To JavaReplies: 1Last Post: 04-23-2010, 05:56 PM -
[SOLVED] Is it possible to have If in a switch case?
By sfe23 in forum New To JavaReplies: 2Last Post: 02-23-2009, 12:34 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks