|
-
Mar 16th, 2010, 06:52 PM
#1
Thread Starter
Member
Java errors
Here is the entire code, and the new error. Does the main method need to be in the "Box" class? I need the second class to test the constructors to make sure they all work properly, that is what the book wants me to do.
Code:
// Program that ues constructors to assig values (length, width and height) to a box
import javax.swing.JOptionPane;
// Create "Box" class
public class Box
{
// Declare variables
private int length;
private int width;
private int height;
// Constructors to set length, width, and height
public Box(int length)
{
this.length = length;
this.width = 0;
this.height = 0;
// Display box length
JOptionPane.showMessageDialog(null, "Line created with length " + length);
}
public Box(int length, int width)
{
this.length = length;
this.width = width;
this.height = 0;
// Display box length and width
JOptionPane.showMessageDialog(null, "Rectangle created with length " + length + " and width " + width);
}
public Box(int length, int width, int height)
{
this.length = length;
this.width = width;
this.height = height;
// Display box length and width
JOptionPane.showMessageDialog(null, "Box created with length " + length + ", width " + width + ", and height " + height);
System.exit(0);
}
}
public class BoxTest
{
// Main method
public void main(String[] args)
{
Box boxOne = new Box(1);
Box boxTwo = new Box(1,2);
Box boxThree = new Box(1,2,3);
}
}
Error:
Box.java:53: class BoxTest is public, should be declared in a file named BoxTest.java
public class BoxTest
^
1 error
Last edited by CarlMartin10; Mar 16th, 2010 at 07:11 PM.
-
Mar 16th, 2010, 07:57 PM
#2
Re: Java errors
The error is telling you exactly what is wrong.
Although there are ways to have multiple classes in one file in Java, it compiles faster and is typically considered good practice to have each class in its own file that has the same name.
So your "Box" class should be in Box.java and your "BoxTest" class should be in BoxTest.java.
You can tie the two classes together by having them in the same package.
Here is your code, split into 2 files:
Box.java
Code:
package smitty.com;
//Program that ues constructors to assig values (length, width and height) to a box
import javax.swing.JOptionPane;
// Create "Box" class
public class Box
{
// Declare variables
private int length;
private int width;
private int height;
// Constructors to set length, width, and height
public Box(int length)
{
this.length = length;
this.width = 0;
this.height = 0;
// Display box length
JOptionPane.showMessageDialog(null, "Line created with length " + length);
}
public Box(int length, int width)
{
this.length = length;
this.width = width;
this.height = 0;
// Display box length and width
JOptionPane.showMessageDialog(null, "Rectangle created with length " + length + " and width " + width);
}
public Box(int length, int width, int height)
{
this.length = length;
this.width = width;
this.height = height;
// Display box length and width
JOptionPane.showMessageDialog(null, "Box created with length " + length + ", width " + width + ", and height " + height);
System.exit(0);
}
}
BoxTest.java
Code:
package smitty.com;
public class BoxTest
{
// Main method
public void main(String[] args)
{
Box boxOne = new Box(1);
Box boxTwo = new Box(1,2);
Box boxThree = new Box(1,2,3);
}
}
-
Mar 16th, 2010, 10:32 PM
#3
Thread Starter
Member
Re: Java errors
I got it all in one file. Thanks for the help. I am getting another error, though now.
ERROR (on run, not compile...there are no compile errors):
----jGRASP exec: java Box
java.lang.NoSuchMethodError: main
Exception in thread "main"
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
-
Mar 18th, 2010, 07:34 AM
#4
Re: Java errors
Looks to me like you're trying to run your "Box" class which has no main, so the program doesn't know where to start.
You need to be running your BoxTest class, since it is the entry point for the program.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|