# Testing equation of a line

• February 22nd, 2011, 04:46 PM
TimoElPrimo
Testing equation of a line
definition of an equation of a line
Design the class lineType to store line. To store a line, you need to store values of a, b and c. Your class must contain the following operations:
1) If a line is nonvertical, then determine its slope.
2) Determine if two lines are equal.
3) Determine if two lines are parrallel.
4) Determine if two lines are perpendicular.
5) If two lines are not parrallel, then find the point of intersection.

These is my skeleton with comments of my plans. Need help making a constructor!
Code Java:

```import java.util.*;   public class Linetype { static Scanner ci = new Scanner(System.in);   public class Line{ private double a, b, c; // how to make a contructor here? } public void slope(){ double a=0, b=0, c; if ( a == 0 && b != 0) System.out.println("The slope of the line is horizontal"); else if (b == 0) System.out.println("The slope of the line is vertical " + "and undefined"); else c = (-1*a)/b;}   public boolean equal(){ int a1=0, a2=0, b1=0, b2=0, c1, c2; boolean result; c1 = a1 + b1; c2 = a2 + b2; // replace all these variable with line1==line2 if (a1 == a2 && b1 == b2 && c1 == c2){ result = true;} else result = false; return result; } public class Parallel{ // compare slope if equal then parallel or both vertical // if not parralel find the point of intersection // y=(c-a*x)/b and x=(c-b*y)/a ? } public class Perpendicular{ //slope of line1 times line2 is negative one // or line1 is horizontal and line2 is vertical } public static void main(String[] args) { Line line1 = new Line(); Line line2 = new Line(); // prompt the user to enter the values of a,b and c. Taking suggestions! } }```
• February 22nd, 2011, 04:57 PM
aussiemcgr
Re: Testing equation of a line
Quote:

Originally Posted by TimoElPrimo
definition of an equation of a line
Design the class lineType to store line. To store a line, you need to store values of a, b and c. Your class must contain the following operations:
1) If a line is nonvertical, then determine its slope.
2) Determine if two lines are equal.
3) Determine if two lines are parrallel.
4) Determine if two lines are perpendicular.
5) If two lines are not parrallel, then find the point of intersection.

These is my skeleton with comments of my plans. Need help making a constructor!
Code Java:

```import java.util.*;   public class Linetype { static Scanner ci = new Scanner(System.in);   public class Line{ private double a, b, c; // how to make a contructor here? } public void slope(){ double a=0, b=0, c; if ( a == 0 && b != 0) System.out.println("The slope of the line is horizontal"); else if (b == 0) System.out.println("The slope of the line is vertical " + "and undefined"); else c = (-1*a)/b;}   public boolean equal(){ int a1=0, a2=0, b1=0, b2=0, c1, c2; boolean result; c1 = a1 + b1; c2 = a2 + b2; // replace all these variable with line1==line2 if (a1 == a2 && b1 == b2 && c1 == c2){ result = true;} else result = false; return result; } public class Parallel{ // compare slope if equal then parallel or both vertical // if not parralel find the point of intersection // y=(c-a*x)/b and x=(c-b*y)/a } public class Perpendicular{ //slope of line1 times line2 is negative one // or line1 is horizontal and line2 is vertical } public static void main(String[] args) { Line line1 = new Line(); Line line2 = new Line(); // promt the user to enter the values of a,b and c. } }```

Ok, a few things about your code, because I think you have confused yourself a bit when you made this.

Code java:

```public class Line{ private double a, b, c; // how to make a contructor here? }```
Do you understand that you are creating a new object class named Line that has no constructor (other than the default constructor), no methods, and no variables that can be accessed from outside of the class?

Code java:

```public class Parallel{ // compare slope if equal then parallel or both vertical // if not parralel find the point of intersection // y=(c-a*x)/b and x=(c-b*y)/a } public class Perpendicular{ //slope of line1 times line2 is negative one // or line1 is horizontal and line2 is vertical }```
Do you understand that these two things above are not methods, but new class declarations? The above says you are creating a new object class named Parallel and a new object class named Perpendicular. Both of these classes have no variables or methods.

Code java:

```public class Line{ private double a, b, c; // how to make a contructor here? } public void slope(){ double a=0, b=0, c; ... }```
I would assume you are wanting to use the a,b, and c variables in the Line class, but you cannot do this since those variables cannot be accessed from the slope method because those variables are owned by the Line class and not within the scope of the slope() method.

Before we can tackle some of the things you have asked above, we need to fix your code to what you are intending on it doing. Answer the above things for me (based on what you know) and we will proceed from there to help you clean this up. Maybe after we clean it up the questions you are asking will be obvious.
• February 22nd, 2011, 05:36 PM
TimoElPrimo
Re: Testing equation of a line
my parrallel and perpendicular should be a method!
and what I wrote in the comment is my plan but I can not do it without making an constructor inside of Line.
Code Java:

```public class Linetype { static Scanner ci = new Scanner(System.in);   public class Line{ private double a, b, c; // how to make a contructor here? Line(){   } public void slope(){ double a=0, b=0, c; if ( a == 0 && b != 0) System.out.println("The slope of the line is horizontal"); else if (b == 0) System.out.println("The slope of the line is vertical " + "and undefined"); else c = (-1*a)/b;}   public boolean equal(){ int a1=0, a2=0, b1=0, b2=0, c1, c2; boolean result; c1 = a1 + b1; c2 = a2 + b2; // replace all these variable with line1==line2 if (a1 == a2 && b1 == b2 && c1 == c2){ result = true;} else result = false; return result; } public Parallel{ // compare slope if equal then parallel or both vertical // if not parralel find the point of intersection // y=(c-a*x)/b and x=(c-b*y)/a } public Perpendicular{ //slope of line1 times line2 is negative one // or line1 is horizontal and line2 is vertical } } public static void main(String[] args) { Line line1 = new Line(); Line line2 = new Line(); // promt the user to enter the values of a,b and c. } }```
i just put the method inside Line and need help making an constructor. I dont know which directiong to go.
i need to setter and getter for a, b and c?
• February 22nd, 2011, 05:54 PM
TimoElPrimo
Re: Testing equation of a line
I be back on like 3hr re-read about user-defined classes and ADTs then try to write these program.
If the contructor is properly made.
i can write line1.slope(); and get the slop of line1?
• February 22nd, 2011, 07:08 PM
aussiemcgr
Re: Testing equation of a line
Compare the two method declarations you have:
Code java:

```public boolean equal(){...} public Parallel{...}```

Notice how in the second one you forgot 2 things:
1) The return type. This can be any primitive or object, or void. Not sure what you want for your parallel and perpendicular methods.
2) The parameters. Parameters go inside the parentheses after the name of the method and before the curly-bracket. The parentheses could be empty, or contain any amount of parameters you will need in the method.

Constructors
The Constructor's main purpose is to initialize the instance variables and perform all of the necessary actions to prepare the object to be used throughout the program. For example, you have 3 instance variables (a,b,c). If your program is designed in a way that these variable values will be determined at runtime (such as user input), you usually want to send values to the constructor to initialize the instance variables.

Look at the following code:
Code java:

```public class WordPlay { public int value1; public int value2;   public WordPlay(int v1, int v2) { value1 = v1; value2 = v2; }   public static void main(String[] args) { WordPlay wp = new WordPlay(5,10); } }```
This code shows how to create a constructor that accepts multiple variables, how to initialize the instance variables using the variables passed to the constructor, and how to call that constructor from a main.
• February 22nd, 2011, 10:15 PM
helloworld922
Re: Testing equation of a line
Just to clarify/add to aussiemcgr's post, a constructor is a non-static method with no defined return value and has the same name as the class. To call the constructor you must put the new keyword, followed by the constructor call signature. Also, the constructor can be called from pretty much anywhere, not just the main method.
• February 22nd, 2011, 10:35 PM
TimoElPrimo
Re: Testing equation of a line
this is my constructor with method. Why it is not working? i try to call it with dot notation, but it doesnt work.
Code Java:

```public class Line { private double a2,a1,b2,b1,c2,c1; Line(){} public void setLine1(double a, double b ,double c) {a1 = a; b1 = b; c1 = c;} public void setLine2(double a, double b ,double c){ a2 = a; b2 = b; c2 = c;} public String slope1(){ if(b1 == 0 ) return (" Verticle line and the slope is Undefined."); else if (a1 == 0) return (" Horizontal line and the slope is 0."); else{ Double m = Double.parseDouble(Double.toString(a1)); Double m2 = Double.parseDouble(Double.toString(b1)); Double slope1; slope1 = (-1*m)/m2; return(" The slope of the first line is " + slope1 + ".");}} public String slope2(){ if( b2 == 0) return (" Verticle line and the slope is Undefined."); else if ( a2==0) return (" Horizontal line and the slope is 0."); else{ Double m3 = Double.parseDouble(Double.toString(a2)); Double m4 = Double.parseDouble(Double.toString(b2)); Double slope2; slope2 = (-1*m3)/m4; return(" The slope of the second line is " + slope2 + ".");}} public String equal(){ if ( (a1 == a2) && (b1 == b2) &&( c1 == c2)) return (" and equal."); return (" and not equal.");} public String parallel(){ double m = (-1*a1)/b1; double m1 = (-1*a2)/b2; if (m == m1) return (" The lines are parallel"); else if (b1 == 0 & b2 == 0) return(" The lines are parallel"); return(" The lines are not parallel");} public String perpendicular(){ double m = (-1*a1)/b1; double m1 = (-1*a2)/b2; if ((a1 == 0) & (b2 == 0)) return(", perpendicular"); else if ((b1 == 0) & (a2 == 0)) return (", perpendicular"); else if ((m * m1) == -1 ) return (", perpendicular"); return(", not perpendicular");} public double getA1() {return a1;} public double getB1() {return b1;} public double getC1() {return c1;} public double getA2() {return a2;} public double getB2() {return b2;} public double getC2() {return c2;}}```

my main method
Code Java:

```import javax.swing.*; import java.util.*; public class Linetype {   public static void main(String[] args) { Scanner ci = new Scanner(System.in); String s, s1, s2, s3, s4,s5; double a1, a2, b1, b2, c1, c2; int response = JOptionPane.showConfirmDialog(null, "Would you like to enter two equation of a " + "line in standard form?", "Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); while(response==0){ s=JOptionPane.showInputDialog("Enter the coefficient A of AX+BY=C"); a1 = Double.parseDouble(s); s1=JOptionPane.showInputDialog("Enter the coefficient B of AX+BY=C"); b1 = Double.parseDouble(s1); s2=JOptionPane.showInputDialog("Enter the coefficient C of AX+BY=C"); c1 = Double.parseDouble(s2); s3=JOptionPane.showInputDialog("Enter another coefficient A of AX+BY=C"); a2 = Double.parseDouble(s3); s4=JOptionPane.showInputDialog("Enter another coefficient B of AX+BY=C"); b2 = Double.parseDouble(s4); s5=JOptionPane.showInputDialog("Enter another coefficient C of AX+BY=C"); c2 = Double.parseDouble(s5); Line.setLine1(a1,b1,c1); // this is not working why? response = JOptionPane.showConfirmDialog(null, "Would you like to enter another two equation of a line" + "in standard form?", "Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);} } }```
• February 22nd, 2011, 11:13 PM
TimoElPrimo
Re: Testing equation of a line
i got it XD lol
Code Java:

```import javax.swing.*; import java.util.*; public class Linetype { public static void main(String[] args) { Scanner ci = new Scanner(System.in); Line line1 = new Line(); String s, s1, s2, s3, s4,s5; double a1, a2, b1, b2, c1, c2; int response = JOptionPane.showConfirmDialog(null, "Would you like to enter two equation of a " + "line in standard form?", "Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); while(response==0){ s=JOptionPane.showInputDialog("Enter the coefficient A of AX+BY=C"); a1 = Double.parseDouble(s); s1=JOptionPane.showInputDialog("Enter the coefficient B of AX+BY=C"); b1 = Double.parseDouble(s1); s2=JOptionPane.showInputDialog("Enter the coefficient C of AX+BY=C"); c1 = Double.parseDouble(s2); s3=JOptionPane.showInputDialog("Enter another coefficient A of AX+BY=C"); a2 = Double.parseDouble(s3); s4=JOptionPane.showInputDialog("Enter another coefficient B of AX+BY=C"); b2 = Double.parseDouble(s4); s5=JOptionPane.showInputDialog("Enter another coefficient C of AX+BY=C"); c2 = Double.parseDouble(s5); line1.setLine1(a1,b1,c1); line1.setLine2(a2,b2,c2); System.out.println("You have enter the following"); System.out.println("First equation A is " + line1.getA1() +", B is " + line1.getB1() +" and C is " + line1.getC1()); System.out.println("Second equation A is " + line1.getA2() +", B is " + line1.getB2() +" and C is " + line1.getC2()); System.out.println(line1.slope1() + line1.slope2()); System.out.println(line1.parallel() + line1.perpendicular() + line1.equal()); response = JOptionPane.showConfirmDialog(null, "Would you like to enter another two equation of a line" + "in standard form?", "Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);}}}```
• February 22nd, 2011, 11:40 PM
TimoElPrimo
Re: Testing equation of a line
only mistake i c is the equal method 2 equation are equal if the are multiple of each other rite?
but how would u write that on java?
a1=ka2, b1=kb2, c1=kc2 for somne real number k