Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Testing equation of a line

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Location
    Philadelphia
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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!
    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!
        }
    }
    Last edited by TimoElPrimo; February 22nd, 2011 at 05:52 PM.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,707
    Thanks
    25
    Thanked 338 Times in 313 Posts

    Default Re: Testing equation of a line

    Quote Originally Posted by TimoElPrimo View Post
    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!
    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.

    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?

    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.

    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.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Junior Member
    Join Date
    Feb 2011
    Location
    Philadelphia
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Testing equation of a line

    yeah sorry about that!
    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.
    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?

  4. #4
    Junior Member
    Join Date
    Feb 2011
    Location
    Philadelphia
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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?
    Last edited by TimoElPrimo; February 22nd, 2011 at 07:20 PM.

  5. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,707
    Thanks
    25
    Thanked 338 Times in 313 Posts

    Default Re: Testing equation of a line

    Compare the two method declarations you have:
    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:
    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.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  6. The Following User Says Thank You to aussiemcgr For This Useful Post:

    TimoElPrimo (February 23rd, 2011)

  7. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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.

  8. The Following User Says Thank You to helloworld922 For This Useful Post:

    TimoElPrimo (February 23rd, 2011)

  9. #7
    Junior Member
    Join Date
    Feb 2011
    Location
    Philadelphia
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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.
    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
    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);}
        }
        }
    Last edited by TimoElPrimo; February 23rd, 2011 at 12:46 AM.

  10. #8
    Junior Member
    Join Date
    Feb 2011
    Location
    Philadelphia
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Testing equation of a line

    i got it XD lol
    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);}}}
    Last edited by TimoElPrimo; February 23rd, 2011 at 12:54 AM.

  11. #9
    Junior Member
    Join Date
    Feb 2011
    Location
    Philadelphia
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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

Similar Threads

  1. Linear Equation Help !!!
    By thangavel in forum Algorithms & Recursion
    Replies: 1
    Last Post: January 13th, 2011, 06:32 AM
  2. Help with Quadratic forumla equation in java please.
    By taylor6132 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 27th, 2010, 08:27 PM
  3. Testing the DataSource with MySQL
    By srinivasan_253642 in forum JDBC & Databases
    Replies: 0
    Last Post: January 9th, 2010, 02:23 AM
  4. Reading a file line by line using the Scanner class
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: April 17th, 2009, 08:34 AM
  5. How to Read a file line by line using BufferedReader?
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: May 19th, 2008, 07:32 AM