|
|
Assignment
- The purpose of a constructor is to "construct" or create an object.
One primary step in this process is to initialize the instance
variables.
- The definition of a constructor is:
- public/private constructorName (dataType explicitParameterName, dataType explicitParameterName)
- Here is what all this means:
- public/private
- if public is used, the constructor can be used by anyone. If
private is used, the constructor can only be used within the object
itself. Typically constructors are defined as public.
- constructorName - the name of the constructor. Typically this is the same name as the class.
- (dataType explicitParameterName, dataType explicitParameterName)
- these are the values that are passed to the constructor and used when
initializing the instance variables. If the parenthesis are empty
(), no values are passed and the instance variables are set to default
values, such as 0 or null.
- The first part of the explicit parameter is the data type of the value.
- The second part is the name that the constructor will use when referring to this value.
- **Notice
that there is not a void or data type between the public/private and
constructorName. A constructor does not return a value, it just
creates an object. Void is not necessary as the action of a
constructor is always the same.
- To call a constructor to create an object, the following format is used:
- ClassName objName = new constructorName(explicit parameters);
- Assume you have a constructor definition of:
- public Dog (String tmpBreed, String tmpName, int tmpAge)
- Using the format above to create an object of type Dog you would have:
- ClassName objName = new constructorName(explicit parameters);
- ClassName - the class is Dog
- Dog objName = new constructorName(explicit parameters);
- objName - anything you chose that is meaningful and correctly follows the Java conventions
- Dog newDog = new constructorName(explicit parameters);
- new - this is a keyword that indicates this is a brand new object instance of the class Dog
- Dog newDog = new constructorName(explicit parameters);
- constructorName - this is always the same as the class
- Dog newDog = new Dog(explicit parameters);
- Explicit
parameters - in the definition for the Dog constructor given above, 3
explicit parameters are indicated, the first two of data type String
and the last of data type integer. It is important that the
explicit parameters are entered in the exact order in which they are
specified in the constructor definition.
- Dog newDog = new Dog("poodle", "Spike", 4);
- Above
literal (actual) values were used. Most of the time, however, the
values will be contained in variables as they are supplied by a user
through a program.
- String inBreed = "poodle";
- String inName = "Spike";
- int inAge = 4;
- Dog newDog = new Dog(inBreed, inName, inAge);
- Methods are behaviors or actions that are executed on objects. A
method may be an accessors method, meaning that it accesses the values
of instance variables, but not does change them, or a method may be a
mutator method, meaning that it mutates or changes the value of an
instance variable.
- The definition of a method is:
- public/private void/datatype methodName (dataType explicitParameterName, dataType explicitParameterName, etc.)
- Here what all this means:
- public/private
- if public is used, the method can be used by anyone. If private
is used, the method can only be used within the object itself.
- void/dataType
- void is specified when nothing is returned from the method, if a
dataType is specified such as String or int, a value of that data type
will be returned when the method completes
- methodName - the
name of the method. This should start with a lower case character
and should be meaningful to the action that will be completed by the
method.
- (dataType
explicitParameterName, dataType explicitParameterName) - these are the
values that are passed to the method. If the parenthesis
are empty (), no values are passed.
- To call a method, the following format is used:
- If the return type specified for the method is void:
- objName.methodName(explicit parameters);
- If the return type specified is a data type:
- dataType varName = objName.methodName(explicit parameters);
- Assume you have a mutator method definition of:
- public void setName (String tmpName)
- Using the format above to call the method you would have:
- objName.methodName(explicit parameters);
- assume an object of Dog with an object name of newDog
- newDog.methodName(explicit parameters);
- methodName - the name of the method is setName
- newDog.setName(explicit parameters);
- Explicit
parameters - There is just 1 explicit parameter of data type String.
- newDog.setName("King");
- Assume you have an accessor method definition of:
- Using the format above to call the method you would have:
- dataType varName = objName.methodName();
- The data type that will be returned when this method is called is String.
- String varName = objName.methodName();
- Choose a name to hold the returned value.
- String dogName = objName.methodName();
- assume an object of Dog with an object name of newDog
- String dogName = newDog.methodName();
- methodName - the name of the method is getName
- String dogName = newDog.getName();
- There are no explicit parameters for this method.
- When writing classes or programs it is very important to test those
classes and program to ensure they are working as expected. In
this class we are encouraging the use of Test Driven Development.
This type of development encourages the writing of tests FIRST
and then writing the code. I know, I know. This seems
backwards. How can you write test cases for code that has not
even been written yet? The reality is that as you are thinking
about your test cases - all the ways a method might fail - you are
essentially working out the various components of your class and as a
result, when it comes time to write your class you will find it to be
much easier.
- Unit tests consist of special methods that return a
boolean value of either true, if the test passes and false if it does
not pass.
- Unit test methods:
- assertNotNull(objName)
- This
method checks to make sure the object is not equal to null. If
the object is equal to null, then the object was not correctly
constructed - the constructor did not work.
- assertEquals(testValue, actualValue)
- This
method compares two values. If they are equal the test passes.
This is used when comparing String, integer, or char values.
- assertEquals(testValue, actualValue, delta)
- This
method works the same as above, but is used for double values.
The delta that is included indicates how many decimal places
should be used when comparing the two values. For example, if
.001 is used then the two values will be considered equal if they match
to the 3rd decimal place.
- assertTrue(testValue) or assertFalse(testValue)
- This method checks to see if the passed value is equal to true or false depending upon which is used.
- When testing a constructor - making sure it works as expected, you would test the following:
- make sure the object has been created (not null)
- make sure the instance variables contain the correct values
- Before
you can do either of the above you must first create the object, then
you can check to see if the object was successfully created.
- Assume the following constructor and method definitions:
- public Dog (String tmpBreed, String tmpName, int tmpAge)
- public String getBreed ()
- public String getName ()
- public int getAge ()
- To test this constructor, you first need to create the object:
- Dog tmpDog = new Dog("poodle", "Spike", 4);
- Now you want to test and make sure the object is not null - meaning it was created.
- Next test to make sure the instance variables were set equal to the values that were passed to the constructor.
- assertEquals("poodle", tmpDog.getBreed());
- The
getBreed method is called to return the value of the breed and this is
compared against the value that was passed to the constructor to make
sure they contain the same value.
- assertEquals("Spike", tmpDog.getName());
- assertEquals(7, tmpDog.getAge());
- In
addition to testing constructors, all mutator methods should also be
tested as well as any accessor methods that include computations or
produce output.
- As in the case of the constructor test cases
above, the first step is to create the object. Then call the
method that is to be tested and finally check the values of the output
or instance variable to ensure they contain what you expect.
- Assume the following constructor and method definitions:
- public Dog (String tmpBreed, String tmpName, int tmpAge)
- public String getBreed ()
- public void setBreed(String newBreed)
- To test the setBreed() method, you would first create a Dog object.
- Dog tmpDog = new Dog("poodle", "Spike", 4);
- Then call the method you are testing:
- tmpDog.setBreed("Shepherd");
- Lastly, check to make sure the method worked correctly.
- assertEquals("Shepherd", tmpDog.getBreed());
- If
the method worked as intended you would expect that the getBreed()
method would now return the value "Shepherd" and not the value "poodle".
- instance variable - belongs to the object and is defined at the very beginning or end of the class, outside of any method
- Belongs to the object as a whole and is available to be used inside of all methods or other constructors of the object
- Created when the object is created.
- Continues to exist as long as the object exists.
- Usually includes the keyword this. in front of the name when used inside the object.
- local variable
- belongs to the construct within which it is created. If
created inside a method belongs to the method, if created inside an if
statement belongs to the if statement, etc.
- Created when the method/if statement is called
- Ceases to exist when the method/if statement completes
- Problem 6
- You
will be writing your own test class, class, and driver program.
The outline of the class has been provided, with the name of the
class, constructor, and methods. Use what you have learned above
to create your classes, starting first with the test class, then the
class, and finally the driver.
|