Machine Problem in Java

Create a class named Box that includes integer data fields for length,
width, and height. Create three Constructors that requires one, two,
and three parameters, respectively.

When one argument is passed to the constructor, assign it to length, assign zeros to height
and width, and display “Line Created”.

When two arguments are used, assign them to length and width, assign zero
to height, and display “Rectangle created”.

When three arguments are used, assign them to the three variables and display “Box created”.
Save this file as Box.java. Create an application named Text Box that demonstrates each method works correctly.
Save the application as TestBox.java

Java Programming sixth edition Chapter 4
By Joyce Farrell

Chapter 4 Problem 4E

Program Listing

Box.java

package test;

/*
*  Box.java
* 
* Jake Rodriguez Pomperada, MAED-IT, MIT
* www.jakerpomperada.com  and www.jakerpomperada.blogspot.com
* jakerpomperada@gmail.com
* Bacolod City, Negros Occidental Philippines
* 
* Machine Problem
* 
* Create a class named Box that includes integer data fields for length, 
 width, and height. Create three Constructors that requires one, two, 
and three parameters, respectively. When one argument is passed to
 the constructor, assign it to length, assign zeros to height 
 and width, and display "Line Created". 
 
 When two arguments are used, assign them to length and width, 
 assign zero to height, and
 display "Rectangle created". 
 
 When three arguments are used,  
 assign them to the three variables and display "Box created". 
 Save this file as Box.java. Create an application named Text Box that 
 demonstrates each method works correctly.
 Save the application as TestBox.java
 
* 
*/

class Box {

    int length;
    int width;
    int height;
    
   
    Box(int length)
    {
    	 this.length = length;
         this.height = 0;
         this.width  = 0;
         System.out.println();
         System.out.print("\tLine Created");
         
     }
     
    
    Box(int length, int width)
    {
    	 this.length = length;
         this.width  = width;
         this.height = 0;
         System.out.println();
         System.out.print("\tRectangle created");
     }
     
    
    Box(int length, int width, int height)
    {
    	 this.length = length;
         this.width  = width;
         this.height = height;
         System.out.println();
         System.out.print("\tBox created");
     }
    
    
    public int getLenght() {
  	  return length;
     }
    
    public int getLenght_getWidth() {
    	  return(length * width);
       }
      
    public int  getLenght_getWidth_getHeight()
    {
    	 return(length * width * height);
    }
    
   
  
}


TextBox.java

package test;

/*
*  TextBox.java
* 
* Jake Rodriguez Pomperada, MAED-IT, MIT
* www.jakerpomperada.com  and www.jakerpomperada.blogspot.com
* jakerpomperada@gmail.com
* Bacolod City, Negros Occidental Philippines
* 
* Machine Problem
* 
* Create a class named Box that includes integer data fields for length, 
 width, and height. Create three Constructors that requires one, two, 
 and three parameters, respectively. When one argument is passed to
 the constructor, assign it to length, assign zeros to height 
 and width, and display "Line Created". 
 
 When two arguments are used, assign them to length and width, 
 assign zero to height, and
 display "Rectangle created". 
 
 When three arguments are used,  
 assign them to the three variables and display "Box created". 
 Save this file as Box.java. Create an application named Text Box that 
 demonstrates each method works correctly.
 Save the application as TestBox.java
 
* 
*/



public class TestBox {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		    System.out.println();
	        System.out.println("\tBox Constructors in Java");
	  
		
	     /* create boxes using the various constructors */
        Box mybox1 = new Box(12);
        Box mybox2 = new Box(4,2);
        Box mybox3 = new Box(5,6,8);
        
      
        mybox1.getLenght();
        mybox2.getLenght_getWidth();
        mybox3.getLenght_getWidth_getHeight();
        
        System.out.println("\n");
        System.out.print("\tEnd of Program \n");  
        System.out.println();
	}

}

Leave a Reply

Your email address will not be published.