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 4 of 4

Thread: Eclipse not recognizing my method for class.

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Eclipse not recognizing my method for class.

    I am using Eclipse IDE. I have created a class called LineSegment. I have to implement this class in my program I am writing called LineSegmentTesterApp.
    However, when trying to use the accessor method getX1, getX2, getY1, etc... I get an error saying that the method is undefined. I think I may be looking over something. Thanks in advance for th help. P.s. my first time having to create and implement my own class.

    package linesegmentapp;
     
    /**
     * File: LineSegment.java
     * @author Jarrod Rotolo
     * Purpose: This class describes a line segment
     * Date: March 6, 2012
     * Course: CSC 1350
     */
     
    public class LineSegment 
    {
        /**
         * The X coordinate of the start point of the line segment.
         */
        private double x1;
     
        /**
         * The X coordinate of the end point of the line segment.
         */
        private double x2;
     
        /**
         * The Y coordinate of the start point of the line segment.
         */
        private double y1;
     
        /**
         * The Y coordinate of the end point of the line segment.
         */
        private double y2;
     
        /**
         * Constructs and initializes a Line with coordinates (0,0)->(0,0).
         */
        public LineSegment()
        {
            x1 = 0;
            y1 = 0;
            x2 = 0;
            y2 = 0;
        }
     
        /**
         * Constructs and initializes a Line2D from the specified coordinates.
         * @param x1Val - the X coordinate of the start point
         * @param y1Val - the Y coordinate of the start point
         * @param x2Val - the X coordinate of the end point
         * @param y2Val - the Y coordinate of the end point
         */
        public LineSegment(double x1Val, double y1Val, double x2Val, double y2Val) 
        {
            x1 = x1Val;
            y1 = y1Val;
            x2 = x2Val;
            y2 = y2Val;           
        }
     
        /**
         * Returns the X coordinate of the start point in double precision.
         * @return the X coordinate of the start point in double precision.
         */
        public double getX1() 
        {
            return x1;
        }
     
        /**
         * Returns the X coordinate of the end point in double precision.
         * @return the X coordinate of the end point in double precision.
         */
        public double getX2() 
        {
            return x2;
        }
     
        /**
         * Returns the Y coordinate of the start point in double precision.
         * @return the Y coordinate of the start point in double precision.
         */
        public double getY1() 
        {
            return y1;
        }
     
        /**
         * Returns the Y coordinate of the end point in double precision.
         * @return the Y coordinate of the end point tin double precision.
         */
        public double getY2() 
        {
            return y2;
        }
     
        /**
         * Computes the length of this line segment.
         * @return the length of this line segment
         */
        public double length() 
        {
             return Math.sqrt((x2-x1)+(y2-y1));
        }
     
        /**
         * Sets the location of the end points of this Line2D to the specified double coordinates.
         */
        public void setLine(double x1Val, double y1Val, double x2Val, double y2Val) 
        {
            x1 = x1Val;
            y1 = y1Val;
            x2 = x2Val;
            y2 = y2Val;
        }                
    }

    package linesegmentapp;
     
    /**
     * File: LineSegmentTester.java
     * @author Jarrod Rotolo
     * Purpose: This program creates a triangle using the implemented class LineSegment
     * Date: March 6, 2012
     * Course: CSC 1350
     */
     
    import java.util.*;
    import linesegmentapp.LineSegment;
     
     
     
    public class LineSegmentTester 
    {
        public static void main(String[] args) 
        {
        	double x1, x2, x3;
        	double y1, y2, y3;
     
            Scanner keyb = new Scanner(System.in);
            System.out.println("Enter the x- and y- coordinates of the first point> ");
            x1 = keyb.nextDouble();
            y1 = keyb.nextDouble();
            System.out.println("Enter the x- and y- coordinates of the second point> ");
            x2 = keyb.nextDouble();
            y2 = keyb.nextDouble();
            System.out.println("Enter the x- and y- coordinates of the third point> ");
            x3 = keyb.nextDouble();
            y3 = keyb.nextDouble();
     
            LineSegment line1 = new LineSegment(x1,y1,x2,y2);
            LineSegment line2 = new LineSegment(x2,y2,x3,y3);
            LineSegment line3 = new LineSegment(x3,y3,x1,y1);
     
            System.out.println("The sides of the triangle are");
            System.out.println("line 1:[("+getX1()+","+getY1()+")->("+getX2()+","+getY2()+")]");
            System.out.println("line 2:[("+getX2()+","+getY2()+")->("+getX3()+","+getY3()+")]");
            System.out.println("line 3:[("+getX3()+","+getY3()+")->("+getX1()+","+getY1()+")]");
     
            double perimeter = line1.length() + line2.length() + line3.length();
            double s = perimeter/2;
            double area = Math.sqrt(s*(s-line1.length())*(s-line2.length())*(s-line3.length()));
     
     
     
     
     
     
     
     
        }
    }


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    660
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Eclipse not recognizing my method for class.

    System.out.println("line 1:[("+getX1()+","+getY1()+")->("+getX2()+","+getY2()+")]"); implies you're trying to use static methods called getX1()... which belong to the class LineSegmentTester.
    Obviously, as these don't exist... you get errors. What I suspect you want to do is invoke those methods on your LineSegment objects.

    Obj name = new Ojb(...)
    name.invokeMethod();
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Eclipse not recognizing my method for class.

    Ah that makes sense. So I can probably get rid of the x3 y3 variables since each object will have its own coordinates [(x1,y1),(x2,y2)]

  4. #4
    Junior Member cj_in_seattle's Avatar
    Join Date
    Feb 2012
    Posts
    7
    My Mood
    Tired
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Also, make comments with // it's much cleaner

    Unless you're writing a lot like method descriptions and class descriptions, but not necessary for instance fields.

Similar Threads

  1. Call method(s) within the same class
    By mwr76 in forum Object Oriented Programming
    Replies: 8
    Last Post: September 26th, 2011, 12:58 AM
  2. How to edit .class files in Eclipse
    By xbill in forum Java IDEs
    Replies: 2
    Last Post: April 22nd, 2011, 02:31 PM
  3. Method & Class confusion
    By jog98 in forum Object Oriented Programming
    Replies: 8
    Last Post: December 8th, 2010, 10:28 AM
  4. Accessing a method of one class in another class
    By Sai in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 23rd, 2010, 04:06 PM
  5. Help Calling Method From Another Class
    By CheekySpoon in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 15th, 2010, 09:24 AM