Help with multiple file projects

H

Herman

Hi everyone, I have a multiple file project for a class assignment.
It consists of a class Point, then a class Polygon which uses the
Point object, then a class Picture that uses the Polygon object.
Unfortunately, I cannot compile Polygon.java, because it cannot
recgonise the Point object. Any ideas? (I'm using the Sun SDK 1.4.2)
Thanks! (By the way, the drawing package and Canvas class were
created by my instructor to make drawing to Graphics easier)

Point.java:
**
* Point.java
* ----------
* Defines the base class for the drawing exercises
*/

package csc812;

import drawing.*;

class Point
{
private int m_iXValue;
private int m_iYValue;

// Default constructor
public Point()
{
this.m_iXValue = 0;
this.m_iYValue = 0;
}

// values constructor
public Point(int x,int y)
{
this.m_iXValue = x;
this.m_iYValue = y;
}

// Copy constructor
public Point(Point P)
{
this.m_iXValue = P.m_iXValue;
this.m_iYValue = P.m_iYValue;
}

// Calculates the distance between this and a given point
public double distance(Point p)
{
return Math.sqrt((m_iXValue-p.m_iXValue)*(m_iXValue-p.m_iXValue) +
(m_iYValue-p.m_iYValue)*(m_iYValue-p.m_iYValue));
}

// Draws a line between this and the given point
public void DrawLine(Point p,Canvas c)
{
c.drawLine(m_iXValue,m_iYValue,p.m_iXValue,p.m_iYValue);
}

// Outputs the coordinates - primarily for debugging
public void dump()
{
System.out.println("(" + m_iXValue + "," + m_iYValue + ")");
}
}

Polygon.java:
/**
* Polygon.java
* ------------
* This class uses the Point class to draw a polygon based on the
given points
* One critical assumption we make is that element 0 of the array list
is the
* starting point of the drawing
*
*/

package csc812;

import java.lang.*;
import java.util.*;
import java.awt.Color;
import drawing.*;
import csc812.Point;

class Polygon extends Point
{
// Declare an ArrayList, since we cannot predetermine how many
elements we will have
private ArrayList m_List;
// foreground colour is part of the polygon
private Color m_ForeColour;

// Default constructor
public Polygon()
{
m_List = new ArrayList();
m_ForeColour = Color.BLACK;
}

// Constructor
public Polygon(ArrayList List,Color cl)
{
if (List.contains(Point))
{
m_List = new ArrayList();
m_List = List;
}
else
{
System.out.println("Polygon constructor error");
System.out.println("The ArrayList passed is not of type Point, so
cannot construct object");
}
m_ForeColour = cl;
}

// Copy constructor
public Polygon(Polygon p)
{
m_List = new ArrayList();
m_List = p.m_List;
m_ForeColour = p.m_ForeColour;
}

// Draws the polygon
public void Display(Canvas c)
{
// Declare array for use
Point [] pArray = new Point[m_List.size()];
pArray = m_List.toArray();

c.setForegroundColor(m_ForeColour);
for (int i = 0;i < pArray.length;i++)
{
try
{
Point p1 = new Point(pArray);
Point p2 = new Point(pArray[i+1]);
p1.DrawLine(p2,g);
}
catch(ArrayIndexOutOfBoundsException e)
{
break;
}
}
}

// Sets foreground colour
public void SetColour(Color f)
{
m_ForeColour = f;
}

// Output for debugging
public void Dump()
{
// Declare array for use
Point [] pArray = new Point[m_List.size()];
pArray = m_List.toArray();

for (int i = 0;i < pArray.length;i++)
{
Point p = new Point(pArray);
p.Dump();
}
}
}

Error messages:
Polygon.java:19: cannot resolve symbol
symbol : class Point
location: package csc812
import csc812.Point;
^
Polygon.java:21: cannot resolve symbol
symbol : class Point
location: class csc812.Polygon
class Polygon extends Point
^
Polygon.java:38: cannot resolve symbol
symbol : variable Point
location: class csc812.Polygon
if (List.contains(Point))
^
Polygon.java:63: cannot resolve symbol
symbol : class Point
location: class csc812.Polygon
Point [] pArray = new Point[m_List.size()];
^
Polygon.java:63: cannot resolve symbol
symbol : class Point
location: class csc812.Polygon
Point [] pArray = new Point[m_List.size()];
^
Polygon.java:66: cannot resolve symbol
symbol : method setForegroundColor (java.awt.Color)
location: class drawing.Canvas
c.setForegroundColor(m_ForeColour);
^
Polygon.java:71: cannot resolve symbol
symbol : class Point
location: class csc812.Polygon
Point p1 = new Point(pArray);
^
Polygon.java:71: cannot resolve symbol
symbol : class Point
location: class csc812.Polygon
Point p1 = new Point(pArray);
^
Polygon.java:72: cannot resolve symbol
symbol : class Point
location: class csc812.Polygon
Point p2 = new Point(pArray[i+1]);
^
Polygon.java:72: cannot resolve symbol
symbol : class Point
location: class csc812.Polygon
Point p2 = new Point(pArray[i+1]);
^
Polygon.java:73: cannot resolve symbol
symbol : variable g
location: class csc812.Polygon
p1.DrawLine(p2,g);
^
Polygon.java:92: cannot resolve symbol
symbol : class Point
location: class csc812.Polygon
Point [] pArray = new Point[m_List.size()];
^
Polygon.java:92: cannot resolve symbol
symbol : class Point
location: class csc812.Polygon
Point [] pArray = new Point[m_List.size()];
^
Polygon.java:97: cannot resolve symbol
symbol : class Point
location: class csc812.Polygon
Point p = new Point(pArray);
^
Polygon.java:97: cannot resolve symbol
symbol : class Point
location: class csc812.Polygon
Point p = new Point(pArray);
^
15 errors
 
S

Sudsy

Herman said:
Hi everyone, I have a multiple file project for a class assignment.
It consists of a class Point, then a class Polygon which uses the
Point object, then a class Picture that uses the Polygon object.
Unfortunately, I cannot compile Polygon.java, because it cannot
recgonise the Point object. Any ideas? (I'm using the Sun SDK 1.4.2)
Thanks! (By the way, the drawing package and Canvas class were
created by my instructor to make drawing to Graphics easier)

You're using packages and you're not defining your classes as public.
Read this: <http://www.sudsy.net/technology/java-classes.html>
 
D

Dan Nuttle

I can't see any immediate cause for the compile error. Both classes are in
the same package, so you actually don't need to import Point in Polygon, but
doing so doesn't hurt anything. I pulled your code into Eclipse, using JDK
1.3.1, commented out things like references to objects in your drawing
package, and the code compiled for me.

So there's something else at work that you're not showing; unless there's
some big difference between 1.3.1 and 1.4.0 that I don't know about; I
don't think that's the case.
 
H

Herman

Dan Nuttle said:
I can't see any immediate cause for the compile error. Both classes are in
the same package, so you actually don't need to import Point in Polygon, but
doing so doesn't hurt anything. I pulled your code into Eclipse, using JDK
1.3.1, commented out things like references to objects in your drawing
package, and the code compiled for me.

So there's something else at work that you're not showing; unless there's
some big difference between 1.3.1 and 1.4.0 that I don't know about; I
don't think that's the case.

I compiled it in the BlueJ IDE and it worked fine. Someone mentioned
that in order to compile using packages with the Sun SDK, you needed a
makefile or something like that. Anyone know how to compile multiple
..java files into a package using the Sun SDK?
 
H

hiwa

I compiled it in the BlueJ IDE and it worked fine. Someone mentioned
that in order to compile using packages with the Sun SDK, you needed a
makefile or something like that. Anyone know how to compile multiple
.java files into a package using the Sun SDK?

If your class A that belongs to package P does refer to class B that
also belongs to package P, then, when you compile A.java in the
current directory P,

javac -classpath .. A.java

If you compile A.java in the current directory ../P, which is the root
directory of package P,

javac -classpath . A.java

In order to learn classpath setting for Java package, read the J2SE
basic documents for javac compiler.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top