Link to home
Start Free TrialLog in
Avatar of nsamri
nsamri

asked on

class does not have a main method

Hello,
I have a java package conatain three files. I compiled this package correctly using Netbeans V.6. When I run each file in the package, it gives me a message
 "class org.bluer.cg.filename does not have a main method"!!!

Can anyone help me?

I attached just two files to look at them.
(1)
package org.bluear.cg.hull;

import java.awt.geom.Point2D;
import java.util.Collection;
import java.util.List;

/**
 * Defines a generic convex hull algorithm.
 *
 *
 */
public abstract class HullAlgorithm {
   
    // -------------------------------------------------------------------------
    // Constants
    // -------------------------------------------------------------------------
   
    /**
     * Indicates that the given point is to the left of the segment.
     */
    public static final int LEFT = 0;
   
    /**
     * Indicates that the given point is to the right of the segment.
     */
    public static final int RIGHT = 1;
   
    /**
     * Indicates that the given point is beyond the segment.
     */
    public static final int BEYOND = 2;
   
    /**
     * Indicates that the given point is behind the segment.
     */
    public static final int BEHIND = 3;
   
    /**
     * Indicates that the given point is between the segment end points.
     */
    public static final int BETWEEN = 4;
   
    /**
     * Indicates that the given point is at the origin of the segment.
     */
    public static final int ORIGIN = 5;
   
    /**
     * Indicates that the given point is at the destination of the segment.
     */
    public static final int DESTINATION = 6;

    // -------------------------------------------------------------------------
    // Methods
    // -------------------------------------------------------------------------
   
    /**
     * Returns the array of indices of points that are part of the convex
     * hull.
     *
     * @param points A collection of <code>Point2D</code> objects.
     * @return The list of points that are vertices of a convex hull.
     */
    public abstract List getHullPoints(Collection points);
   
    // -------------------------------------------------------------------------
    // Helper methods
    // -------------------------------------------------------------------------
   
    /**
     * Classifies the location of the point (x2, y2) relatively to the segment
     * defined by points (x0, y0) and (x1, y1). The following illustrates
     * regions considered by this method:
     * <pre>
     *
     *           LEFT
     *                         BEYOND
     *                          .
     *                     DESTINATION
     *                        /
     *                    BETWEEN          
     *                      /             RIGHT
     *                  ORIGIN
     *                 .
     *          BEHIND
     * </pre>
     * @param x0 The x of the starting point of the segment.
     * @param y0 The y of the starting point of the segment.
     * @param x1 The x of the final point of the segment.
     * @param y1 The y of the final point of the segment.
     * @param x2 The x of the point whose position we are testing.
     * @param y2 The y of the point whose position we are testing.
     */
    public static int classify(double x0, double y0,
                               double x1, double y1,
                               double x2, double y2) {
        double dx1 = x1 - x0;
        double dx2 = x2 - x0;
        double dy1 = y1 - y0;
        double dy2 = y2 - y0;
        double sa = dx1 * dy2 - dx2 * dy1;
       
        if (sa > 0.0) {
            return (LEFT);
        }
       
        if (sa < 0.0) {
            return (RIGHT);
        }
       
        if (dx1 * dx2 < 0.0 || dy1 * dy2 < 0.0) {
            return (BEHIND);
        }
       
        if (dx1 * dx1 + dy1 * dy1 < dx2 * dx2 + dy2 * dy2) {
            return (BEYOND);
        }
       
        if (x2 == x0 && y2 == y0) {
            return (ORIGIN);
        }
       
        if (x2 == x1 && y2 == y1) {
            return (DESTINATION);
        }

        return (BETWEEN);                
    }

    /**
     * Returns a code indicating where point <code>c</code> is located with
     * respect to the segment formed by points <code>a</code> and <code>b</code>.
     *
     * @param a The first point of the segment.
     * @param b The second point of the segment.
     * @param c The point classified with respect to the segment.
     * @return The code indicating the relative location of c.
     */    
    public static int classify(Point2D a, Point2D b, Point2D c) {
        return (classify(a.getX(), a.getY(), b.getX(), b.getY(), c.getX(), c.getY()));
    }
   
    /**
     * Returns whether or not the tree given points are co-linear.
     *
     * @param p1 The first point.
     * @param p2 The second points.
     * @param p3 The third point.
     * @return Whether or not the three points are co-linear.
     */
    public static boolean colinear(Point2D p1, Point2D p2, Point2D p3) {
        return (p1.getX()*(p2.getY() - p3.getY())
                + p2.getX()*(p3.getY() - p1.getY())
                + p3.getX()*(p1.getY() - p2.getY())) == 0.0;
    }
}

(2)

package org.bluear.cg.hull;

import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;


public class GrahamScanHull extends HullAlgorithm {

      public List getHullPoints(Collection points) {
        int i;
       
        // deal with abnormal cases first ...
        if (points == null || points.size() <= 0) {
            return (Collections.EMPTY_LIST);
        }
       
        int n = 0;
       
        // count the non-null points ...
        for (Iterator iter = points.iterator(); iter.hasNext(); ) {
            Object obj = iter.next();
           
            if (obj instanceof Point2D) {
                ++ n;
            }
        }
       
        if (n <= 0) {
            return (Collections.EMPTY_LIST);
        }
       
        Point2D[] pt = new Point2D[n];
        i = 0;
       
        for (Iterator iter = points.iterator(); iter.hasNext(); ) {
            Object obj = iter.next();
           
            if (obj instanceof Point2D) {
                pt[i ++] = (Point2D) obj;
            }
        }
       
        ArrayList hull = new ArrayList();

        // Graham scan does not do too well if there are only 1 or 2 points ...
        if (n < 3) {
            hull.add(pt[0]);
           
            if (n == 2) {
                hull.add(pt[1]);            
            }
        }
        else {
       
            // Step 1: find extreme point p0
            int m = 0;
            Point2D pi, p0 = pt[0];
       
            for (i = 1; i < n; i++) {
                pi = pt[i];
           
                if (pi.getY() < p0.getY() || (pi.getY() == p0.getY() && pi.getX() < p0.getX())) {
                    m = i;
                    p0 = pi;
                }
            }
       
            if (m != 0) {
                pt[m] = pt[0];
                pt[0] = p0;
            }
       
            // Step 2: sort points by their polar coordinate relative to p0
            Arrays.sort(pt, 1, n, new PolarComparator(p0));
       
            // Step 3: Find the second point of the hull ...
            i = 1;
       
            while (i+1 < n && colinear(p0, pt[i], pt[i+1])) {
                ++ i;
            }
       
            hull.add(p0);
            hull.add(pt[i]);
       
            // Step 4: Grow the current hull until it includes all points
            for (i = i + 1; i < n; ++ i) {
                pi = pt[i];
                Point2D p1 = (Point2D) hull.get(hull.size() - 1);
                p0 = (Point2D) hull.get(hull.size() - 2);
           
                while (LEFT != classify(p0, p1, pi)) {
                    p1 = p0;
                    hull.remove(hull.size() - 1);
                    p0 = (Point2D) hull.get(hull.size() - 2);
                }
           
                hull.add(pi);
            }
        }

            return (hull);
      }

    /**
     * Local class used by the sort method to classify points based on their
     * polar coordinates with respect to the specified pivot point, p0.
     *
     * @author Bo Majewski
     */
    private static class PolarComparator implements Comparator {
       
        private Point2D p0;
       
        /**
         * Creates a new comparator that used the given pivot point.
         *
         * @param p0 The pivot point.
         */
        public PolarComparator(Point2D p0) {
            this.p0 = p0;
        }

            /**
             * Compares object o1 to object o2. This method returns a negative
         * value if o1 lies to the left of o2 with respect to the pivot p0.
         *
             * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
             */
            public int compare(Object o1, Object o2) {
            Point2D p1 = (Point2D) o1;
            Point2D p2 = (Point2D) o2;
           
            // take care of degenerate cases first ...
            if (p1.equals(this.p0)) {
                return (p2.equals(this.p0)? 0 : -1);
            }
           
            if (p2.equals(this.p0)) {
                return (1);
            }
           
            // this is when neither p1 nor p2 is identical to pivot ...
            double d = ((p2.getX() - p0.getX())*(p1.getY() - p0.getY())
                       - (p1.getX() - p0.getX())*(p2.getY() - p0.getY()));
           
            return (d < 0.0? -1 : d > 0.0? 1 : 0);
            }
    }
}
Avatar of krakatoa
krakatoa
Flag of United Kingdom of Great Britain and Northern Ireland image

You can compile without a main method, but to instantiate individually from the command line, you need one.
Avatar of nsamri
nsamri

ASKER

Thank you so much for the help. I didn't get your point when you said from the command line. What do you mean by that?cna explain it to me in a little more details.  

by the way, I used these files as an applet file .Is this true? or Do i have to chage it to another type of java file?
>>by the way, I used these files as an applet file .Is this true? <<

Don't know how you managed that exactly. There's no applet API imported and the class doesn't extend Applet AFAICS.

>> I didn't get your point when you said from the command line.  <<

If you - directly via the command line in a console window - or your IDE tries to instantiate a class standalone by itself, then there has to be an entry point for that class to be instantiated and to run, and that is the reason why the class, under those circumstances, would need the main() entry point method.

If you have by contrast a class that you are going to instantiate from another class - such as a worker or helper thread or something similar, then that class doesn't necessarily need a main() method, because you can instantiate an object of that type by calling its constructor.
ASKER CERTIFIED SOLUTION
Avatar of sciuriware
sciuriware

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nsamri

ASKER

Thanks all for the help.
<<Is there a manual or guide with it?>>
no it is not. I can send you the folder that contains the whole classes If you like.
I'm headache with these files.
I have a question:
Is there another way to combine this package I mean three classes in one class.


SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This is the HullAlgorithm class, which compiles and runs (but it doesn't do anything - you'll have to do that bit - and it is no longer abstract). ;


import java.awt.geom.Point2D;
import java.util.Collection;
import java.util.List;
import java.util.*;

/**
 * Defines a generic convex hull algorithm.
 *
 *
 */
public  class HullAlgorithm {

      public static void main(String[] args){new HullAlgorithm();}
   
    // -------------------------------------------------------------------------
    // Constants
    // -------------------------------------------------------------------------
   
    /**
     * Indicates that the given point is to the left of the segment.
     */
    public static final int LEFT = 0;
   
    /**
     * Indicates that the given point is to the right of the segment.
     */
    public static final int RIGHT = 1;
   
    /**
     * Indicates that the given point is beyond the segment.
     */
    public static final int BEYOND = 2;
   
    /**
     * Indicates that the given point is behind the segment.
     */
    public static final int BEHIND = 3;
   
    /**
     * Indicates that the given point is between the segment end points.
     */
    public static final int BETWEEN = 4;
   
    /**
     * Indicates that the given point is at the origin of the segment.
     */
    public static final int ORIGIN = 5;
   
    /**
     * Indicates that the given point is at the destination of the segment.
     */
    public static final int DESTINATION = 6;

    // -------------------------------------------------------------------------
    // Methods
    // -------------------------------------------------------------------------
   
    /**
     * Returns the array of indices of points that are part of the convex
     * hull.
     *
     * @param points A collection of <code>Point2D</code> objects.
     * @return The list of points that are vertices of a convex hull.
     */
    public List getHullPoints(Collection points){return new ArrayList();}
   
    // -------------------------------------------------------------------------
    // Helper methods
    // -------------------------------------------------------------------------
   
    /**
     * Classifies the location of the point (x2, y2) relatively to the segment
     * defined by points (x0, y0) and (x1, y1). The following illustrates
     * regions considered by this method:
     * <pre>
     *
     *           LEFT
     *                         BEYOND
     *                          .
     *                     DESTINATION
     *                        /
     *                    BETWEEN          
     *                      /             RIGHT
     *                  ORIGIN
     *                 .
     *          BEHIND
     * </pre>
     * @param x0 The x of the starting point of the segment.
     * @param y0 The y of the starting point of the segment.
     * @param x1 The x of the final point of the segment.
     * @param y1 The y of the final point of the segment.
     * @param x2 The x of the point whose position we are testing.
     * @param y2 The y of the point whose position we are testing.
     */
    public static int classify(double x0, double y0,
                               double x1, double y1,
                               double x2, double y2) {
        double dx1 = x1 - x0;
        double dx2 = x2 - x0;
        double dy1 = y1 - y0;
        double dy2 = y2 - y0;
        double sa = dx1 * dy2 - dx2 * dy1;
       
        if (sa > 0.0) {
            return (LEFT);
        }
       
        if (sa < 0.0) {
            return (RIGHT);
        }
       
        if (dx1 * dx2 < 0.0 || dy1 * dy2 < 0.0) {
            return (BEHIND);
        }
       
        if (dx1 * dx1 + dy1 * dy1 < dx2 * dx2 + dy2 * dy2) {
            return (BEYOND);
        }
       
        if (x2 == x0 && y2 == y0) {
            return (ORIGIN);
        }
       
        if (x2 == x1 && y2 == y1) {
            return (DESTINATION);
        }

        return (BETWEEN);                
    }

    /**
     * Returns a code indicating where point <code>c</code> is located with
     * respect to the segment formed by points <code>a</code> and <code>b</code>.
     *
     * @param a The first point of the segment.
     * @param b The second point of the segment.
     * @param c The point classified with respect to the segment.
     * @return The code indicating the relative location of c.
     */    
    public static int classify(Point2D a, Point2D b, Point2D c) {
        return (classify(a.getX(), a.getY(), b.getX(), b.getY(), c.getX(), c.getY()));
    }
   
    /**
     * Returns whether or not the tree given points are co-linear.
     *
     * @param p1 The first point.
     * @param p2 The second points.
     * @param p3 The third point.
     * @return Whether or not the three points are co-linear.
     */
    public static boolean colinear(Point2D p1, Point2D p2, Point2D p3) {
        return (p1.getX()*(p2.getY() - p3.getY())
                + p2.getX()*(p3.getY() - p1.getY())
                + p3.getX()*(p1.getY() - p2.getY())) == 0.0;
    }
}
- now you can call it with main() of course. ;)
Avatar of nsamri

ASKER

I appericiate your cooperation.
I did modify what you wrote above. And added the main method in GrahamScanHull class like this:
public static void main(String[] args)
{
    new GrahamScanHull();
}

both classes compiled and ran but it gave nothing.
Why that happened?What should I do to make this program works.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
{
    new GrahamScanHull();
    getHullPoints(Collection points);
}
sorry -

getHullPoints(points); //where points is type Collection
Avatar of nsamri

ASKER

I added  "getHullPoints(points)" /  "getHullPoints(Collection points)" in GrahamScanHullAlgorithm and they didn't work.
I'm confused...Did you mean adding one of those in GrahamScanHull or in HullAlgorithm?


please, need an urgent help.
'points' has to be an already-instantiated Collection.
Avatar of nsamri

ASKER

I'll try to figure it out
Where did you get this package from in the first place? Did you write it, ( ;) ), or was it given to you to complete an assignment? If so, what is the assignment?
Looks like it's from here:
http://bo.majewski.name/bluear/cg/hull/hull.html

and the source code for one of the classes can be found here
http://bo.majewski.name/bluear/cg/hull/GrahamScanHull.html

OP, the zip file from that site doesn't contain the actual applet (and GUI) that you see on the page. You will probably have to do that yourself. All these java files do is calculate and/or implement a mathematical algorithm.

In fact, the GrahamScanHull class only has one usable function - getHullPoints() , where you provide a set of points. It also inherits a few functions from the HullAlgorithm  abstract class - classify() and colinear().
IMHO, all 3 of us (krakatoa, sciuriware and myself) gave valid answers to the OP
Cheers!