Create a class named Box that includes integer data fields for length, width and
ID: 3639984 • Letter: C
Question
Create a class named Box that includes integer data fields for length, width and height. Create three constructors that require one, two and three arguments, respectively. Code the constructors as follows:? The constructor that takes one argument should assign the value to length and assign zeros to height and width. This constructor should also print a line of text that says, "Line created with length XX". Be sure to replace the 'XXX' with the value of length using the Java keyword this in your code.
? The constructor that takes two arguments should assign the first value to length, the second to width and zero to height. This constructor should also print a line of text that says, "Rectangle created with length XXX and width XXX". Be sure to replace the 'XXX' with the values of length and width using the Java keyword this in your code.
? The constructor that takes three arguments should assign the first value to length, the second to width and the third to height. This constructor should also print a line of text that says, "Box created with length XXX, width XXX, and height XXX". Be sure to replace the 'XXX' with the values of length, width and height using the Java keyword this in your code.
When you are finished writing this class, write a second class called BoxTest that tests each of the constructors and demonstrates that each works correctly. You may use any values for length, width and height in your program or you can prompt the user to enter these values.
Although the output of your program is not required to look this way, it might look something like:
Line created with length 5. Rectangle created with length 10 and width 15. Box created with length 1, width 2, and height 3.
Explanation / Answer
public class Box {
int length;
int width;
int height;
Box(int length) {
this.length = length;
this.height = 0;
this.width = 0;
print("Line created with length " + this.length);
}
Box(int length, int width) {
this.length = length;
this.width = width;
this.height = 0;
print("Rectangle created with length "+this.length+" and width "+this.width);
}
Box(int length, int width, int height) {
this.length = length;
this.width = width;
this.height = height;
print("Box created with length "+this.length+", width "+this.width+", and height "+this.height);
}
void print(String text) {
System.out.println(text+".");
}
}
public class BoxTest {}
public static void main(String[] args) {
Box line = new Box(5);
Box rectangle = new Box(10,15);
Box box = new Box(1,2,3);
}
}
public class Box {
int length;
int width;
int height;
Box(int length) {
this.length = length;
this.height = 0;
this.width = 0;
print("Line created with length " + this.length);
}
Box(int length, int width) {
this.length = length;
this.width = width;
this.height = 0;
print("Rectangle created with length "+this.length+" and width "+this.width);
}
Box(int length, int width, int height) {
this.length = length;
this.width = width;
this.height = height;
print("Box created with length "+this.length+", width "+this.width+", and height "+this.height);
}
void print(String text) {
System.out.println(text+".");
}
}
public class BoxTest {}
public static void main(String[] args) {
Box line = new Box(5);
Box rectangle = new Box(10,15);
Box box = new Box(1,2,3);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.