Archive for May, 2007

Methods 135 Lab 5.2: Using Constructors The purpose (Best web hosting)

Thursday, May 3rd, 2007

Methods 135 Lab 5.2: Using Constructors The purpose of this lab is to become familiar with working with constructors. You will add two constructors to your Elevator class. 1. Begin by opening your Elevator class from Lab 5.1 in your text editor. 2. Add a constructor that has a single parameter of type int to represent the current floor of the elevator. Within the constructor, assign the parameter to the appropriate field in your class. Also, display a message using System.out.println() that shows which constructor you are currently in. 3. Add a no-argument constructor that uses the this keyword to invoke your constructor in the previous step, passing in a 1 for the initial floor. Display a message that states you are currently in the no-argument constructor. 4. Modify your ElevatorProgram in Lab 5.1. Instantiate one of the Elevator objects using the no-argument constructor, and instantiate the other Elevator object using the one-argument constructor. 5. Run the ElevatorProgram and ensure that the constructors are working successfully. Lab 5.3: Redesigning the Powerball Lottery In this lab, you will make your Powerball program object oriented by writing a class called Powerball. 1. Write a class named Powerball that contains six fields to represent the five white balls and one red ball. 2. Add a method named play() that simulates the playing of Powerball. (See Lab 3.5 for details of the lottery.) This method should assign a value to each of the six fields. 3. Add a method named displayResults() that prints out the values of the five white balls and one red ball. 4. Save and compile your Powerball class. 5. Write a class named PlayLottery that contains main(). Within main(), instantiate a Powerball object and invoke the methods of the Power- ball class to ensure that they are working successfully.
Note: If you are looking for cheap and reliable webhost to host and run your web application check Vision coldfusion web hosting services

Web hosting domains - 134 Chapter 5 Figure 5.6 Output of the

Wednesday, May 2nd, 2007

134 Chapter 5 Figure 5.6 Output of the ThisDemo program. Lab 5.1: Simulating an Elevator In this lab, you will write an Elevator class containing various methods. Then, you will write a program that creates Elevator objects and invokes the various methods. The purpose of the lab is to help you understand how to write methods and how methods manipulate the data of an object. 1. Using your text editor, write a class named Elevator. Add fields for the following attributes: an int for the current floor, an int for the floor that the elevator is heading to, a boolean to denote whether the elevator is going up or down once it reaches its destination, and a boolean to denote whether the elevator doors are open or closed. 2. Add a method named goToFloor() that changes the floor that the elevator is heading to. Use the System.out.println() method to display a message that you are changing the value. 3. Add methods named openDoors() and closeDoors() that change the appropriate boolean field accordingly. Again, display a message within each method so that you can see when the methods are invoked. 4. Add methods named goingUp() and goingDown() that change the appropriate boolean field accordingly. 5. Save and compile your Elevator class. 6. Write a class named ElevatorProgram that contains main(). 7. Within main(), instantiate two Elevator objects. Invoke the various methods of the Elevator class on these two objects, ensuring that all your Elevator methods work successfully.
Note: If you are looking for reliable webhost to maintain and run your java application check Vision java hosting services

Methods 133 { System.out.println( Instantiating (Shared web hosting) first television ); Television t1

Wednesday, May 2nd, 2007

Methods 133 { System.out.println( Instantiating first television ); Television t1 = new Television(); System.out.println(t1.volume + + t1.channel); int channel = 206; System.out.println( Instantiating second television ); Television t2 = new Television(channel); System.out.println(t2.volume + + t2.channel); int volume = 7; System.out.println( Instantiating third television ); Television t3 = new Television(channel, volume); System.out.println(t3.volume + + t3.channel); } } The first statement within main() outputs the following: Instantiating first television Then, a Television object is instantiated using no arguments. Flow of control jumps to the no-argument constructor of the Television class. The first statement in the following constructor causes flow of control to jump to the two-argument constructor in the Television class: this(4,10); The two-argument constructor executes, then control jumps back to the one- argument constructor, which executes. Therefore, the output is as follows: Inside two-arg constructor Inside no-arg constructor Notice that the default constructor creates a Television object with an initial volume of 10 and channel 4. The second Television object in ThisDemo uses the one-argument constructor, which uses the this keyword to invoke the two- argument constructor, creating the following output: Inside two-arg constructor Inside one-arg constructor The third Television object invokes the two-argument constructor directly, creating the following output: Inside two-arg constructor The entire output is shown in Figure 5.6.
Note: If you are looking for high quality webhost to host and run your jsp application check Vision florida web design services

Cedant web hosting - 132 Chapter 5 To avoid repeating code, you

Tuesday, May 1st, 2007

132 Chapter 5 To avoid repeating code, you can have all the constructors invoke one constructor that does all the work. A constructor can use the this keyword to invoke another constructor within the same class. If a constructor uses the this keyword to invoke another constructor in the class, the this statement must appear as the first line of code in the constructor. (Otherwise, a compiler error will occur.) The following Television class demonstrates using the this keyword to make one constructor invoke another constructor. Notice that the this statement is the first line of code in the constructor. public class Television { public int channel; public int volume; public Television() { this(4, 10); System.out.println( Inside no-arg constructor ); } public Television(int c) { this(c, 10); System.out.println( Inside one-arg constructor ); } public Television(int c, int v) { System.out.println( Inside two-arg constructor ); channel = c; volume = v; } } The this keyword used within a constructor is not the same as the this reference that every object has to itself. The this keyword has two different uses in Java. The following ThisDemo program instantiates three Television objects by using each of the three constructors in the Television class. Study the program carefully and try to determine the output. The output is not obvious, so follow the flow of control carefully. The actual output is shown in Figure 5.6. public class ThisDemo { public static void main(String [] args)
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision shared web hosting services

Methods 131 Figure 5.5 (Vps web hosting) Output of the ConstructorDemo

Tuesday, May 1st, 2007

Methods 131 Figure 5.5 Output of the ConstructorDemo program. Using this in a Constructor The following Television class has three constructors in it. Notice that the three constructors have different parameter lists, but the body of each one essentially does the same thing. public class Television { public int channel; public int volume; public Television() { System.out.println( Inside no-arg constructor ); channel = 4; volume = 10; } public Television(int c) { System.out.println( Inside one-arg constructor ); channel = c; volume = 10; } public Television(int c, int v) { System.out.println( Inside two-arg constructor ); channel = c; volume = v; } } The problem with the Television class is that code is repeated three times. If you needed to change code in one of the previous constructors for some reason, you would need to change the code in three places, which is an undesirable situation in any programming language.
Note: If you are looking for cheap and reliable webhost to host and run your mysql application check Vision professional web hosting services