This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.

can anyone help me to figure out what is wrong with this code? I am trying to write a code that will find the intersection of two lines something I can use in a j-unit test case. here is what i have so far. I am using netbeans.

public class intercept
float line;
{

public intercept(float m1, float m2, float c1, float c2)

Array[] line = new Array;

array[0] = [m2 - m1]/ [c2-c1]
array[1] = [m2-m1]/[c2-c1]
if(float i=0; i=line; i++ ){
line = i;
}
system.out.println(line)
else
system.out.println(" the line don't meet");

this is suppose to be the line. i i thought since i declared the as parameters then i could say they were some number. i was going for the point slope formula.

When you post a question, you need to TellTheDetails(⇐click) about the problems you're having. We're not mind-readers, after all.

Also, when you post code, please UseCodeTags(⇐click) so it will be readable.

As for this:

I think you need to go back and review loops and if statements. That's just a hot mess.

Finally, a couple of notes about floating point arithmetic. There's really no good reason to use float. Always use double. And when looking for equality, due to natural errors inherent in floating point numbers, you can't check for exact equality. You need to see if the value is with in +/- some error tolerance that you decide on.

Although, for calculating the intercept, you don't actually need to check for equality anyway, but you will want to test your results and see if they're what you expected.

tito ortizz wrote:this is suppose to be the line. i i thought since i declared the as parameters then i could say they were some number. i was going for the point slope formula.

But look at these two lines:

They're setting both array elements to the exact same value. Why are you doing that?

i was thinking the same thing, but i needed to only use 4 variable so i thought i would use the array to run them twice then the result would give me the intersection. how can i fix this problem? and the if and looping are wrong? why? I have just got on here and dont know my way around. sorry for not post all the information.

tito ortizz wrote:i was thinking the same thing, but i needed to only use 4 variable so i thought i would use the array to run them twice then the result would give me the intersection.

I have no idea what you're saying here.

how can i fix this problem?

I don't know what that code is supposed to actually represent, so I can't answer that.

and the if and looping are wrong? why?

The code you provided won't even compile. That code I pointed out is neither a valid loop nor a valid if statement. Like I said, you need to study those concepts. Once you do, you'll be able to produce a correct loop or a correct if statement, whichever one you need in a particular situation.

Write a program block to compute interaction point between given two lines. Each line is represented in a parametric form –

Y = mX + c

Where m is slop of the line while c is y intercept. In turn your function takes four parameters, m1, c1, m2, and c2 as floating point numbers. The program computes the intersection point and returns its result to the caller. Once completed, perform unit testing of your routine using the method covered in the lab. You must select input values for the unit test using following methods –

·Equivalent partitioning

·Basic path coverage

Provide your analysis for selecting input values in a separate document.

Write a program block to compute interaction point between given two lines. Each line is represented in a parametric form –

Y = mX + c

Where m is slop of the line while c is y intercept. In turn your function takes four parameters, m1, c1, m2, and c2 as floating point numbers. The program computes the intersection point and returns its result to the caller. Once completed, perform unit testing of your routine using the method covered in the lab. You must select input values for the unit test using following methods –

·Equivalent partitioning

·Basic path coverage

Provide your analysis for selecting input values in a separate document.

Okay, so what is your specific question at this point?

And have you figured out how to find the intersection withoutJava? That has to be your first step.

Then, when you get a bazillion errors, gut it and start from scratch, adding a tiny bit at a time. Make sure each piece compiles and executes properly before moving on to the next. (For instance, you don't appear to have studied ifs and loops yet.)

Victor M. Pereira
Ranch Hand

Joined: Mar 02, 2012
Posts: 50

posted

0

fred perez wrote:yea, i know that you have to set each point slope form to each other:
0 =(m1 - m2)x + (c1 - c2)

Set y1 to the slope of line 1 plus the intercept of line1.
Set y2 to the slope of line 2 plus the intercept of line2.
Then throw away the value of that second step and set y2 equals to the same value as y1.

That doesn't look even remotely related to finding the intersection of two lines.

And this:

That says that your intersection method returns a single number. You do know that the intersection will be a point, right, and that a point has 2 coordinates?

Honestly, it seems like you're just taking random bits of code and throwing them together and hoping that will magically produce the result. I'll say it again: You need to thoroughly understand the problem without any regard to Java at all, and you need to be able to describe your solution clearly and precisely in English and/or pseudocode before writing a line of Java.

So, given two lines, one described by slope m1 and intercept c1, and the other described by slope m2 and intercept c2, how do you find the point of intersection of those lines, without Java?

in math term: if i had an equation solve for a single variable, and then plug it in to the found variable and solve for the other one. then use those found and plug them into the other. but they are all variable. i could move everything onto one side but outside of that, what else can i do?

fred perez wrote:in math term: if i had an equation solve for a single variable, and then plug it in to the found variable and solve for the other one. then use those found and plug them into the other. but they are all variable.

That's very far from precise. I would never be able to write code from that description

i could move everything onto one side but outside of that, what else can i do?

That's exactly what you should do.

As I already said, you know that the end result of your method needs to be a point right? And that it will have an X and a Y coordinate?

So, again, given m1 and c1 to define one line and m2 and c2 to define the second line, what are the equations to solve for the X and Y coordinates of the intersection?

fred perez wrote:please help me with this code. i have been at this for the last 2 day and i am having nightmares. is this code right?

I'm not going to give you that answer. It would be ridiculous to hand in code without testing it, so get your test program working. If what's stopping you is a simple compile error, then you definitely need the practice anyway.

For that matter, does the code you posted even compile? It doesn't look like it will. If it doesn't compile, then obviously it's not right.

this is my last attempt to write this code because this is due tonight by midnight. can anyone tell me if i am close or should give up? i have studied for loops and ifs statement and array and have all the individual parts but cant put it all together. help please!

[code=java][
package intersection;

public class Intersection {
float point [];
float x = 0;
float y = 0;
public static void main(String args [] ){

}
public void intersection ( float m1,float m2, float c1, float c2)
{
float x1 [] = new float [2];
point [0] = x;
point [1] = y;

x = (c2 - c1) / (m1 - m2);
y = (m2 * x + c2);

for ( float line : x1){
System.out.print(point[0] + point[1]);
}

Why do you continue to refuse to test your code? If you want to know if code is correct, testing it is an absolute MUST. Your assignment even says, "Once completed, perform unit testing of your routine..."

And why do you have a main() method with nothing in it?

i toke out the system.out.println
and ran a junit test and it gave me a bunch of error that i cant not understand. what i am suppose to do with that? it failed, now what?

fred perez wrote:i toke out the system.out.println
and ran a junit test and it gave me a bunch of error that i cant not understand. what i am suppose to do with that? it failed, now what?

Change the code so that you don't get errors and it doesn't fail.
Unless you TellTheDetails that's about the best anyone can tell you.