Archive for April, 2007

130 Chapter (Affordable web design) 5 The previous statement attempts to

Monday, April 30th, 2007

130 Chapter 5 The previous statement attempts to invoke a no-argument constructor, but the Radio class shown in Listing 5.2 does not declare a no-argument constructor. A Class with Multiple Constructors If a class has multiple constructors, the new operator can be used for each constructor in the class. For example, the Radio class shown in Listing 5.1 has two constructors. Their signatures are as follows: public Radio() public Radio(float t) This gives us two ways to instantiate a new Radio. To invoke the no-argument constructor, use the new operator with no arguments: Radio x = new Radio(); To invoke the constructor that has a float parameter, pass in a float with the new operator: float station = 100.3F; Radio y = new Radio(station); Study the following ConstructorDemo program and try to determine the output. (Note that the ConstructorDemo program is using the Radio class defined in Listing 5.2.) The program output is shown in Figure 5.5. public class ConstructorDemo { public static void main(String [] args) { System.out.println( Instantiating the first Radio ); Radio x = new Radio(); System.out.println( Instantiating the second Radio ); float station = 100.3F; Radio y = new Radio(station); System.out.println(x.volume + + x.tuning + + x.band); System.out.println(y.volume + + y.tuning + + y.band); } }
Note: In case you are looking for affordable webhost to host and run your web application check Vision cheap hosting services

Web hosting faq - Methods 129 If you do add a constructor

Monday, April 30th, 2007

Methods 129 If you do add a constructor to your class, the compiler does not add the default constructor to your class. Consider the Radio class shown in Listing 5.2. public class Radio { public int volume; public float tuning; public char band; public Radio(int v, char b) { volume = v; band = b; } } Listing 5.2 This Radio class declares a constructor and therefore does not have a default no-argument constructor. The Radio class shown in Listing 5.2 declares a constructor that has two parameters: an int and a char. Because we added a constructor to this Radio class, the compiler does not add another one for us. Using Constructors A constructor must be invoked when an object is instantiated using the new keyword. A class can (and often does) have multiple constructors. You determine which constructor is invoked with the arguments used with the new operator. If a class has one constructor, there is only one way to instantiate an object of that class type. For example, the Radio class shown in Listing 5.2 has only one constructor. The signature of this constructor is the following: public Radio(int v, char b) Therefore, the only way to instantiate a Radio using the class shown in Listing 5.2 is to pass in an int and a char: int volume = 7; char band = A ; Radio radio = new Radio(volume, band); The following statement does not compile with the Radio class shown in Listing 5.2: Radio radio = new Radio(); //invalid!
Note: If you are looking for cheap and reliable webhost to host and run your web application check Vision coldfusion web hosting services

128 Chapter 5 Default Constructor If you write (X web hosting)

Sunday, April 29th, 2007

128 Chapter 5 Default Constructor If you write a class and do not add a constructor, the compiler generates a default constructor in your class. This default constructor is public, has no parameters, and does not do anything. For example, if the Radio class does not declare a constructor, the compiler adds the following: public Radio() {} Notice that this follows the rules of a constructor: The constructor name matches the class name, and there is no return value. Also notice that the constructor contains no statements. . A Class with No Default Constructor If you do not add a constructor to a class, the compiler generates a default constructor for the class. This default constructor does not contain any parameters. If you add one or more constructors to your class, no matter what their parameter lists are, the compiler does not add a default constructor to your class. The following class does not have a default constructor: public class Television { public int channel; public Television(int c) { channel = c; } } Because there is only one constructor in this Television class, the only way to instantiate a new Television object is to pass in an int: Television t1 = new Television(4); The point I want to make with this example is that the following statement does not compile: Television t2 = new Television(); This statement is attempting to invoke a no-argument constructor, but the Television class does not contain a no-argument constructor, thereby causing a compiler error. It is not uncommon to write a class without a no-argument constructor, as demonstrated by many classes in the Java API.
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision tomcat hosting services

Frontpage web hosting - Methods 127 Have you noticed throughout the book

Sunday, April 29th, 2007

Methods 127 Have you noticed throughout the book so far that parentheses appear when using the new keyword to instantiate an object? For example, a new Radio object is instantiated as follows: Radio r = new Radio(); I have not explained those empty parentheses, but I am ready to now. Any time you see parentheses, it looks as if a method is being invoked. This is exactly what is happening when you use the new keyword, except that a method is not invoked. One of the constructors in the class is invoked. In fact, the only time you can invoke a constructor is when the object is being instantiated. Constructors are similar to methods, but keep in mind that they are not methods. They behave quite differently, as you will see in Chapter 6, Understanding Inheritance. Classroom Q & A Q: Wait a minute. We haven t added a constructor to any of the classes we have seen up until now. Do you have to add a constructor to every class? A: You are right. We must not have to add a constructor because our classes seem to be fine without one, so the answer to your question is no. If you do not add a constructor to your class, the compiler writes one for you. This free constructor is known as the default constructor. Q: How does the compiler know what I want my default constructor to do? A: The compiler doesn t know. The default constructor that you get for free has an empty parameter list and doesn t do anything. Q: Then why bother? It seems like a waste of code to have a constructor that doesn t do anything. A: Because every class must have a constructor. When you instantiate a class, the new operator must invoke a constructor. The fact that the compiler adds a default constructor to your class is purely for convenience. In almost all development situations you find yourself in, you will add at least one constructor yourself to all of your classes. Before we write our own constructors, though, let s take a quick look at this default constructor.
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision personal web hosting services

Web hosting - 126 Chapter 5 Remember that when an object

Sunday, April 29th, 2007

126 Chapter 5 Remember that when an object is instantiated using the new keyword, the memory is allocated and zeroed out. Therefore, the initial values of the fields of an object are zero values (see Table 4.1). Without a constructor, you have to go in and initialize all the fields so that the object has meaningful data. A constructor provides an opportunity to construct the object so that its fields have meaningful data while the object is being instantiated. What makes a constructor different from a method is that a constructor satisfies the following two properties: The name of the constructor must match the name of the class. A constructor does not declare a return value, not even void. For example, if we want to add a constructor to the Radio class discussed earlier, the name of the constructor has to be Radio and no return value is declared. Listing 5.1 shows the Radio class with two constructors added. public class Radio { public int volume; //0-10 public float tuning; //Current station tuned in public char band; // A for AM or F for FM public Radio() { System.out.println( Inside no-argument constructor ); tuning = 80.0F; band = F ; volume = 5; } public Radio(float t) { System.out.println( Inside float constructor ); tuning = t; band = A ; volume = 8; } //The remainder of the class definition… } Listing 5.1 This Radio class has two constructors. When adding multiple constructors to a class, the rules of method overloading apply. Each constructor must have a unique parameter list that makes it distinguishable from the other constructors.
Note: In case you are looking for affordable webhost to host and run your servlet application check Vision mysql5 web hosting services

Web file server - Methods 125 In the first call to multiply()

Saturday, April 28th, 2007

Methods 125 In the first call to multiply() in the OverloadDemo program, a and b are passed in. Because a and b are both ints, the following version of multiply() in the Calculator class is invoked: public int multiply(int x, int y) When the two doubles d1 and d2 are passed in to multiply(), the corresponding overloaded version in Calculator is invoked: public double multiply(double x, double y) In the following statement, only a single int is passed in, so the version of multiply that takes in a single int is invoked: intAnswer = calc.multiply(b); Similarly, invoking multiply() with three int arguments causes the corresponding multiply() with three int parameters to be invoked. I want to make an observation about the following statement in the OverloadDemo program: doubleAnswer = calc.multiply(b, f); The arguments are of type int and float, in that order. There is no multiply() method in the Calculator class that has a parameter list with an int and a float. However, because a float can be promoted to a double, notice that the multiply() method that gets invoked is the following: public double multiply(int x, double y); This situation in which the float is passed in to a double arises all the time when invoking methods (not just when a method is overloaded). When an argument does not exactly match a parameter, but the argument can be promoted to match a parameter, then the promotion will occur automatically. Constructors A constructor is a special method in a class that is invoked when the object gets instantiated. The purpose of a constructor is to allow the fields of the object to be initialized when the object is instantiated.
Note: In case you are looking for affordable and reliable webhost to host and run your business application check Vision php5 hosting services

124 Chapter 5 Of the five multiply() methods

Saturday, April 28th, 2007

124 Chapter 5 Of the five multiply() methods in the Calculator class, the parameter lists are different for each one. Therefore, this is an example of valid method overloading. The following OverloadDemo program instantiates a Calculator object and invokes the various multiply() methods. Study the OverloadDemo program carefully and try to determine the output. The actual output is shown in Figure 5.4. public class OverloadDemo { public static void main(String [] args) { System.out.println( Instantiating a Calculator… ); Calculator calc = new Calculator(); System.out.println( Initializing some variables… ); int a = 5; int b = 8; double d1 = 2.5; double d2 = -1.0; float f = 4.0F; int intAnswer = 0; double doubleAnswer = 0.0; intAnswer = calc.multiply(a, b); System.out.println(a + * + b + = + intAnswer); doubleAnswer = calc.multiply(d1, d2); System.out.println(d1 + * + d2 + = + doubleAnswer); intAnswer = calc.multiply(b); System.out.println(b + * + b + = + intAnswer); intAnswer = calc.multiply(a, b, a); System.out.println(a + * + b + * + a + = + intAnswer); doubleAnswer = calc.multiply(b, f); System.out.println(b + * + f + = + doubleAnswer); doubleAnswer = calc.multiply(d2, f); System.out.println(d2 + * + f + = + doubleAnswer); } } Figure 5.4 Output of the OverloadDemo program.
Note: If you are looking for reliable webhost to maintain and run your java application check Vision java hosting services

Web design rates - Methods 123 System.out.println( Multiply int * int ); return x

Friday, April 27th, 2007

Methods 123 System.out.println( Multiply int * int ); return x * y; } public double multiply(double x, double y) { System.out.println( Multiply double * double ); return x * y; } public double multiply(int x, double y) { System.out.println( Multiply int * double ); return x * y; } public int multiply(int x) { System.out.println( Multiply int * itself ); return x * x; } public int multiply(int x, int y, int z) { System.out.println( Multiply three ints ); return x * y * z; } } . Method Overloading You can overload a method as long as the parameter lists are distinct enough for the compiler to be able to distinguish which method you want to invoke. Certainly if the number of parameters is different, the overloading is valid, as shown in the following two method signatures: public float computePay(double d, int x); //Two parameters public float computePay(double d); //One parameter If you simply change the name of the parameter, it is not valid. For example, the following two methods could not appear in the same class because this is not valid overloading: public void setDay(int x, int y, long z); public boolean setDay(int a, int b, long c) //No! The preceding two methods have the same name and the same number of parameters, and the parameters appear in the same order. The compiler would not be able to distinguish between the two methods and would generate a compiler error. Note that changing the return value does not affect whether the overloading is valid or not. However, changing the order of parameters is just like changing the parameter list. For example, the following two methods demonstrate valid method overloading: public void setDay(int x, int y, long z ; public boolean setDay(long a, int b, int c);
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision web and email hosting services

122 Chapter 5 (Web site builder) Figure 5.3 The println() method

Friday, April 27th, 2007

122 Chapter 5 Figure 5.3 The println() method is overloaded 10 times in the PrintStream class. The PrintStream class has 10 println() methods. How does the compiler know which one you want to invoke? If you look carefully, you will also notice that the parameter list is different for each version of println(). For example, calling println() and passing in an int invokes that overloaded version whose signature is as follows: public void println(int x) Invoking println() and passing in a String invokes the following version: public void println(String x) If method overloading were not an option, the println() methods would each have to use a unique name such as printlnInt(), printlnString(), println- Double(), and so on. In this case, method overloading simplifies both the writing of the PrintStream class and the usage of the class. Developers do not need to remember 10 different names for printing a line of text to the system output; they can simply remember that the method to use is println(), and it is overloaded for every data type. Let s look at an example using method overloading. The following Calculator class contains five multiply() methods. Study the method signatures carefully and determine whether this is valid method overloading. public class Calculator { public int multiply(int x, int y) {
Note: In case you are looking for affordable webhost to host and run your web application check Vision http web server services

Methods 121 . A Class with No main() (Web site designers)

Thursday, April 26th, 2007

Methods 121 . A Class with No main() Method? A common point of confusion that new OOP students of mine have arises when they write a class that does not have main() method in it. A Java class without main() is not a program. For example, you cannot execute the Radio class. The Radio class is a description (albeit a simple one) of an AM/FM radio. If you try to run Radio by entering the following command, you will get an error message from the JVM, stating that no main() method was found: java Radio The Radio class is meant to be used by other classes that need a radio. In a large Java application with dozens or even hundreds of classes, you might define main() methods all over the place. However, it is likely that only one class has main() in it. If the Java application is using other Java technologies such as Servlets or Enterprise JavaBeans, no class will have a main() method in it. On a similar note, just because Radio has a bunch of nice methods in it, it does not mean that the methods are automatically invoked. If you want a method to execute, you need to explicitly invoke it. Similarly, if you do not want a method to execute, don t invoke it. For example, the ListenToRadio program creates a Radio object, but at no point is the turnDown() method invoked. We could have invoked turnDown() if we wanted to because it is available to us, but we do not have to and it is not invoked automatically at any point in time. Overloading Methods Java allows a method to be overloaded. Method overloading occurs when a class has two or more methods with the same name but different parameter lists. Having more than one method with the same name might seem unnecessary, but method overloading is used quite frequently in Java (and other programming languages). Method overloading is used most commonly with constructors, which are discussed in the next section. For example, the println() method that we used throughout the book so far is an overloaded method of the java.io.PrintStream class, as you can see by the documentation of PrintStream shown in Figure 5.3.
Note: If you are looking for reliable webhost to maintain and run your java application check Vision java hosting services