Results 1 to 4 of 4

Thread: Java errors

Threaded View

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2009
    Posts
    44

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width