-
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
-
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);
}
}
-
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.
-
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.