Putting the Class to Work
The deed is done. You created a class. Now what?
Remember that as an object oriented programmer, your task is to create classes. Other folks will actually use your class to solve problems. However, there is an important task on your end that you must still conduct. You need to test your class thoroughly so you know you are passing along an effective toolbox.
And to do that, you need to see how to use a Java class. The first thing that must be done is to create a main class so you can conduct the testing. This is a fully separate class containing a main method as shown below. Note that this is the only main class that will be used here. Your abstract data type class (i.e., BoxClass) and all other ADT classes you create will never have a main method in them. Here is the empty shell:
package boxclass_package;
public class BoxClassMain
{
public static void main(String[] args)
{
}
}
You should start by testing all the constructors. Here is a way to test the default constructor.
public static void main(String[] args)
{
BoxClass bc1 = new BoxClass();
String color;
int perimeter, area, length;
color = bc1.getColor();
perimeter = bc1.find2DPerimeter();
area = bc1.find2DArea();
length = bc1.getLength();
conIO.printString( "Box 1 color is " + color + ", its perimeter is " +
perimeter + ", its area is " + area +
", and its length is " + length );
conIO.printEndline();
}
Using References
To start with, remember that to create a new class, you must make the variable (bc1) a reference of the BoxClass type (BoxClass bc1) and then using the new operator with the class name(= new BoxClass();) to tell Java that you want to instantiate an object of that kind of class. Once a reference has been declared, it can be used to manipulate any class of the same type (e.g., in this case BoxClass) using the dot operator. So if a bc2 and a bc3 are declared as BoxClass types (BoxClass bc2, bc3;), and they are assigned to bc1 (bc2 = bc1; bc3 = bc1;), they would then have access to any of the data and operations of the object originally created with the new operator. The use of bc1.find2DArea() would result in the exact same value as bc2.find2DArea() and bc3.find2DArea(). There would not be a different object, there would just be three references that "refer" to that same object.
You have been instantiating the Console_IO_Class regularly up to this point and using the dot operator to program input and output (I/O) so this is not new to you. However, you need to be comfortable with using references and objects as you continue along your programming path. As it turns out, references are a little more complicated than noted here. There will be some more learning about them later on.
Testing Strategy
You do not have to use the Six Step Programming Process or comments for this. However, it is a very good idea for you to put your test list in comments in the main method so you make sure you have tested all the possible conditions. For this one, your test list should look something like the following:
- // test default constructor
- // test initialization constructor
- // test copy constructor
- // test find2DArea
- // test find2DPerimeter
- etc.
This is a short topic to get everyone on board with using the class that you have created. Nevertheless you should watch this video and work along with it so you are sure to be comfortable with instantiating and using objects.