scripter73
Programmer
Hi,
I have two simple programs:
- Box.java : defines a class for a box
- boxtest.java: uses the Box class
I have a directory where I've been compiling java progs:
c:\test>
Here are my steps:
c:\test> javac Box.java ...compiles fine
c:\test> javac boxtest.java
Generates error:
boxtest.java:1:'.' expected
import Box;
Here's are my two separate files. Both of these files are in c:\test>.
----------------
import java.io.*;
class Box
{
//instance variables
double width;
double length;
double height;
//constructors
Box (double w, double l, double h)
{
width = w;
length = l;
height = h;
}
Box (double side)
{
width = side;
length = side;
height = side;
}
//methods
double volume()
{
return width * length * height;
}
double area()
{
return 2 * (width + length + height);
}
}
----------------
import Box;
class boxtest
{
public static void main (String[] args)
{
//Create box with sides 2.5, 3.0, and 5.0
Box box1 = new Box(2.5, 3.0, 5.0);
//Create a box with all sides 3.0
Box box2 = new Box(3.0);
//Show the area/volume for both boxes.
System.out.println("Area for box1 is " + box1.area() + ". Volume for box1 is " + box1.volume());
System.out.println("Area for box2 is " + box2.area() + ". Volume for box2 is " + box2.volume());
}
}
----------------
I've also tried changing the import statement to : import Box.*; but then the compiler looks for a package. What's the problem? I thought we can use classes we create in other programs as long as their in the same directory?
Thanks in advance for your help.
scripter73
Change Your Thinking, Change Your Life.
I have two simple programs:
- Box.java : defines a class for a box
- boxtest.java: uses the Box class
I have a directory where I've been compiling java progs:
c:\test>
Here are my steps:
c:\test> javac Box.java ...compiles fine
c:\test> javac boxtest.java
Generates error:
boxtest.java:1:'.' expected
import Box;
Here's are my two separate files. Both of these files are in c:\test>.
----------------
import java.io.*;
class Box
{
//instance variables
double width;
double length;
double height;
//constructors
Box (double w, double l, double h)
{
width = w;
length = l;
height = h;
}
Box (double side)
{
width = side;
length = side;
height = side;
}
//methods
double volume()
{
return width * length * height;
}
double area()
{
return 2 * (width + length + height);
}
}
----------------
import Box;
class boxtest
{
public static void main (String[] args)
{
//Create box with sides 2.5, 3.0, and 5.0
Box box1 = new Box(2.5, 3.0, 5.0);
//Create a box with all sides 3.0
Box box2 = new Box(3.0);
//Show the area/volume for both boxes.
System.out.println("Area for box1 is " + box1.area() + ". Volume for box1 is " + box1.volume());
System.out.println("Area for box2 is " + box2.area() + ". Volume for box2 is " + box2.volume());
}
}
----------------
I've also tried changing the import statement to : import Box.*; but then the compiler looks for a package. What's the problem? I thought we can use classes we create in other programs as long as their in the same directory?
Thanks in advance for your help.
scripter73
Change Your Thinking, Change Your Life.