I am working on a homework asisgnment from school and need some help. I am stuck on coding to set a private field to my Box class with three constructors.. It's kind of confusing when dealing with more than one constructor. The following is the code for the box class progam. Can any one assist me. Please follow the comments Thanks! //two arg constructor that sets length and width
public Box(int l, int w) {

// write code to set the private field

}

/** The Box class with three constructors */

    public class Box{
 // three class variables
      private int length;
      private int width;
      private int height;
   
//one arg constructor should take one parameter and set the length equal to that. Width and height should be set to zero.
       public Box( int l) {
         length = l;
         width =0;
         height=0;
}
//two arg constructor that sets length and width
       public Box(int l, int w) {

        // write code to set the private field
		
	} 
//three arg constructor that sets lenght, width and height   
       public Box(int l, int w, int h) {
          
	// write code to set the private fields
      }
   	
// get and set methods for all the private data ie, length, width and height. I have written one method. You need to write for all three 
       public int getLength() {
         return length;
      }
   
       public void setLength(int l){
         length=l;
      }
   // Write get and set methods for width
		public int getWidth()
		{
			return width;
		}

		public void setWidth(int w) {
		width = 0;
	}

   // Write get and set methods for height   
		public int getHeight()
		{
			return height;
		}

		public void setHeight(int h) {
		height = 0;
	}

   }

Recommended Answers

All 4 Replies

where is the confusion, 2nd and 3rd constructors will work like 1st one only. Whatever arguments you are passing in constructors that you can set to length/width/height.
for ex for the second constructor:

public Box(int l, int w) {
     length = l;
     width =w;
     height=0;      
}

and setters and getters will not have any extra logic.

public void setWidth(int w) {
    width = 0;
}

above code is wrong. it will always be

public void setWidth(int w) {
    width = w;
}

Ok thanks for your help, i have to figure out something here.

Hi I have tried to figure the box program but now I get nothing but the length, width and height..

/** The Box class with three constructors */

    public class box{
 // three class variables
      private int length;
      private int width;
      private int height;
   
//one arg constructor should take one parameter and set the length equal to that. Width and height should be set to zero.
       public box( int l) 
	   {
         length = l;
         width =0;
         height=0;
	   }

		private void Box1()
		{
			System.out.println("Line created with length 1");

		}
//two arg constructor that sets length and width
		public box(int l, int w)
		{
			length = 1;
			width = 2;
			height = 0;
		}
			// write code to set the private field	
		private void Box2()
		{
			System.out.println("Rectangle created with length 1" + " and width 2");

		}
//three arg constructor that sets lenght, width and height   
       public box(int l, int w, int h) {
		   length = 1;
		   width = 2;
		   height = 3;
	   }
		
	// write code to set the private fields
		private void Box3()
		{
			System.out.println("Box created with length 1" + "width 2" + "and height 3");

      }
   	
// get and set methods for all the private data ie, length, width and height. I have written one method. You need to write for all three 
       public int getLength() {
         return length;
      }
   
       public void setLength(int l){
         length=l;
      }
   // Write get and set methods for width
		public int getWidth()
		{
			return width;
		}

		public void setWidth(int w) {
		width = 2;
	}

   // Write get and set methods for height   
		public int getHeight()
		{
			return height;
		}

		public void setHeight(int h) {
		height = 3;
	}

   }

Here is my Main:
public class boxTest {

	public static void main(String[] args)
	{
		// create three Box objects
		box b1 = new box(1);
		box b2 = new box(2);
		box b3 = new box(3);
		// printing code goes here
		System.out.println("Length " + "Width " + "Height ");
	}
}


This is the result of the above code.

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\Administrator>cd c:\java

C:\java>javac boxTest.java

C:\java>java boxTest
Length Width Height

C:\java>

try to understand below code and revert in case of any confusion..
m sure u will have some confusions

class box{                                                                                                            
 // three class variables                                                                                                     
 private int length;                                                                                                          
 private int width;                                                                                                           
 private int height;                                                                                                          
 //one arg constructor should take one parameter and set the length equal to that. Width and height should be set to zero.    
 public box( int l)                                                                                                           
 {    
//you can call other constructor with this key word, 	
 this(l,0,0);                                                                                                                   
 }                                                                                                                            
 //two arg constructor that sets length and width                                                                             
 public box(int l, int w)                                                                                                     
 {                                                                                                                            
	 this(l,w,0);                                                                                                                  
 }                                                                                                                            
 // write code to set the private field                                                                                       
 public box(int l, int w, int h) {                                                                                            
 length = l;                                                                                                                  
 width = w;                                                                                                                   
 height = h;      
 System.out.println("length="+l+", width = "+w+", heigth="+h);
 }                                                                                                                            
 // get and set methods for all the private data ie, length, width and height. I have written one method. You need to write fo
 public int getLength() {                                                                                                     
 return length;                                                                                                               
 }                                                                                                                            
 public void setLength(int l){                                                                                                
 length=l;                                                                                                                    
 }                                                                                                                            
 // Write get and set methods for width                                                                                       
 public int getWidth()                                                                                                        
 {                                                                                                                            
 return width;                                                                                                                
 }                                                                                                                            
 public void setWidth(int w) {                                                                                                
 width = w;                                                                                                                   
 }                                                                                                                            
 // Write get and set methods for height                                                                                      
 public int getHeight()                                                                                                       
 {                                                                                                                            
 return height;                                                                                                               
 }                                                                                                                            
 public void setHeight(int h) {                                                                                               
 height = h;                                                                                                                  
 }                                                                                                                            
 }                                                                                                                            
 public class boxTest {                                                                                                       
 public static void main(String[] args)                                                                                       
 {                                                                                                                            
 // create three Box objects with different constructors                                                                                                   
 box b1 = new box(1);                                                                                                         
 box b2 = new box(23,43);                                                                                                         
 box b3 = new box(12,32,43);                                                                                                         
                                                                                                                              
 }                                                                                                                            
                                                                                                                              
 }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.