these assignments

advertisement
Chapter 11 Create your own objects
11.1 Class definitions and object types
Read the first sentence of this section very slowly.
Many Java programs consist of several files, most of which are class definitions, like in the Pong
game.
As you saw, class definitions allow the programmer to create instances of the class (objects),
sometimes in the one file of the package that has a main method.
Every object is of an object type, and is an instance of the class used to create it.
When new is used to create an object, Java invokes a special method called a constructor, which is
where the variables are declared and/or initialized. The name of the constructor method is the
same as its class name, so if you have a class called Time, the constructor for it will also be called
Time.
It is not uncommon for the constructor method to be overloaded in the class definition. This
means your class definition may have more than one constructor.
Recall that overloading is where a single method is defined in more than one way. For instance, in a program you may
often have the find the area of a rectangle, but sometimes the rectangle is a square, and you only know the length of a
single side. You could overload an areaOfRectangle method by accounting for both instances:
public static int areaOfRectangle(int length, int width) {
return (length * width);
}
public static int areaOfRectangle(int side) {
return (side * side);
}
//from elsewhere you could invoke this method in more than one way (see below)
//as long as the data type and/or number of parameters are unique for each overloaded method
//definition, you can overload as much as needed, it simply adds flexibility to the program
int area = areaOfRectangle(5);
int area = areaOfRectangle(3,13);
After the constructor(s) are made, other methods, which are designed to operate on the object,
may also be defined for the class.
Class names should be capitalized. In a program/package of multiple files of class definitions, a
startup class is designated, which contains the main method used to start the application.
11.2 Time
Slowly read the first sentence of this section of the reading. (Yeah, it is missing the 'o' in object,
but this sentence sums things up pretty well.)
* You are asked to make an object of your own, Time. Open a new Java file and name it: Time.java
Enter the code below, save the program, but do not run it.
package Time;
class Time {
int hour, minute; double second; // these are the instance variables of this class
} //ends class Time
// these variables will help you write the
constructors
11.3 Constructors
*Add a Time constructor to the class definition.
public Time() {
this.hour = 0;
this.minute = 0;
this.second = 0.0;
} //ends the default Time constructor method
The word "this" is a keyword in Java which refers to the object being created, and as you will see,
allows the programmer to access its variable values by using this.* (dot notation) syntax. (In
Python, the word 'self' is used.)
11.4 More constructors
As stated in the book, it is common to have one constructor that takes no arguments (like 11.3)
and one that takes a parameter list identical to the list of instance variables declared at the top of
the class definition.
*Add the second constructor
public Time(int hour, int minute, double second) {
this.hour = hour;
this.minute = minute;
this.second = second;
} //ends custom Time constructor method
What is the difference between the two constructors? Well, one can be invoked with new just to
get a blank Time object (defaulted to midnight), and the other can be created with new and told
what time to be upon creation.
For instance, if this was done in main:
Time time1 = new Time();
Time time2 = new Time(13,30,0.0);
// then time1 holds midnight
// then time2 holds 1:30.0.0 pm
11.5 Creating a new object
*Add a main method to this class definition, as indicated in this section of the reading.
*Save and run the program. Note that it does not print the times, but rather characters that
represent the objects created, not the contents of the object themselves. Printing the time comes
next.
11.6 Printing objects
*Add the printTime method to Time.java, then invoke it with both times. Save the file, run and see
that you obtain similar results to that shown in the reading. (Note the last sentence of the second
to last paragraph of this section.)
11.7 Operations on objects
Pure function, modifier and fill-in method are three kinds of methods that may be created to
operate on an object.
11.8 Pure functions
A pure function does not alter anything (like the variables of an object passed to it) and only
returns a new value or new object.
*Stay with the author here. Read the code at the top of page 138. Consider the questions posed.
You do not need to enter and test this code.
*Look carefully at addTime on the middle of page 138. This is a fruitful function, which returns a
new Time object using the default constructor.
*Add the addTime method to your Time class.
*What all do you now have to do in main in order to invoke this fruitful method and show the
three times of currentTime, breadTime and sum/doneTime? See the reading in this section. Make
that happen in your program. Save and test the program.
*Read page 139 and update the addTime method in your Time class according to the reading.
11.9 Modifiers
*Read the one-page section. As the name indicates, this kind of object method simply adjusts
objects and instance variables that are passed to it. There is technically no return so these
methods are void, but what was passed has been altered.
11.10 Fill-in methods
Here, instead of creating a new object that is passed back like a pure function, a fill-in takes an
object provided by the caller (you have to pass an object to the method in which to store the
results, which are not technically passed back). Thus these methods are void. Kind of creepy, I
think.
The author says just use pure methods as a first choice, ones that create a new object and return it.
11.11 Incremental development and planning
Want to be smarter? Read this section and then adjust your program accordingly.
*Regardless, at this point please submit Time.java via e-mail to the instructor.
11.12 Generalization
11.13 Algorithm
11.14 Glossary
11.15 Exercises
Please do all of Exercise 11.1. Submit with subject "Scrabble."
Please do all of Exercise 11.2. Submit with subject "Birthday."
Please do all or as much as you can of Exercise 11.3. Submit with subject "Rational."
Download