As I mentioned earlier in the Book Example Updated for JavaFX Script SDK: HelloJFX post, several of the upcoming blog posts are going to contain the examples from my JavaFX Script book, rewritten to work with the JavaFX SDK Technology Preview Release. For your convenience, I'm placing these posts in the JavaFX Script Book Examples category. Today's post contains the SequenceExample program, which was developed to demonstrate creating and manipulating sequences. Here is the console output when running the program:
The third planet is:Earth
Mercury is a planet in our solar system
Venus is a planet in our solar system
Earth is a planet in our solar system
Mars is a planet in our solar system
Saturn is a planet in our solar system
Neptune is a planet in our solar system
Inserting Uranus before Neptune
Inserting Jupiter after Mars
Inserting Pluto as the last planet
Inserting the Sun into the beginning
Sun is a planet in our solar system
Mercury is a planet in our solar system
Venus is a planet in our solar system
Earth is a planet in our solar system
Mars is a planet in our solar system
Jupiter is a planet in our solar system
Saturn is a planet in our solar system
Uranus is a planet in our solar system
Neptune is a planet in our solar system
Pluto is a planet in our solar system
Deleting the Sun in position 0
Querying for the indexof Pluto
Pluto is in position 8
Deleting Pluto
[ Neptune, Uranus, Saturn, Jupiter, Mars, Earth, Venus, Mercury ]
The 5 planets with more than 5 characters are:
MercuryJupiterSaturnUranusNeptune
The 4 planets in the second half of the alphabet are:
Venus,Saturn,Uranus,Neptune,
By the way, the eBook version of the JavaFX Script book is available for download from this Apress site.
The Updated Code for SequenceExample
Here is the code, updated by a colleague and JavaFX developer named Matt Shirey:
/*
* SequenceExample.fx - An example creating a sequence literal and using
* some sequence related features of JavaFX to work
* with it.
*
* Developed 2007 by James L. Weaver (jim.weaver at lat-inc dot com)
*
* Updated July 2008 by Matt Shirey (matt.shirey at lat-inc dot com)
* for JavaFX SDK Technology Preview 1
*/
package jfx_book;
import java.lang.System;
// Literally define the planets sequence, leaving out Jupiter and Uranus
var planets = ["Mercury", "Venus", "Earth", "Mars",
"Saturn", "Neptune"];
// Print out the third element in the array, which is element 2
// since arrays are zero-based.
System.out.println("The third planet is:{planets[2]}");
// Print out all planets by iterating over the planets sequence
for (planet in planets) {
System.out.println("{planet} is a planet in our solar system");
}
System.out.println("");
// Insert Uranus before Neptune (which is currently in position 5)
System.out.println("Inserting Uranus before Neptune");
insert "Uranus" before planets[5];
// Insert Jupiter after Mars (which is currently in position 3)
System.out.println("Inserting Jupiter after Mars");
insert "Jupiter" after planets[3];
// Add Pluto to the end of the sequence.
System.out.println("Inserting Pluto as the last planet");
insert "Pluto" into planets;
// Add Sun to the beginning of the sequence
System.out.println("Inserting the Sun into the beginning");
insert "Sun" before planets [0];
// Print out all planets by iterating over a sequence that is the same
// size as the planets sequence, and reference the corresponding element
// in the planets array.
for (i in [0.. sizeof planets - 1]) {
System.out.println("{planets[i]} is a planet in our solar system");
}
System.out.println("");
// Delete the Sun (which is currently in position 0)
System.out.println("Deleting the Sun in position 0");
delete planets[0];
// Query for the indexof Pluto and delete it
System.out.println("Querying for the indexof Pluto");
var indices = for (planet in planets
where planet == "Pluto") indexof planet;
System.out.println("Pluto is in position {indices}");
if (sizeof indices >= 0) {
System.out.println("Deleting Pluto");
delete "Pluto" from planets;
}
// Query for and print all of the planets run together in reverse order
var allPlanetsRunTogether = reverse planets;
System.out.println(allPlanetsRunTogether);
System.out.println("");
// Use for to query for all of the planets with more than 5 characters
var somePlanets:String[];
somePlanets = for (planet in planets where planet.length() > 5) planet;
System.out.println("The {sizeof somePlanets} planets with more than 5 characters are:
{somePlanets}");
System.out.println("");
// Use for to query for all of the planets that begin with
// the letter "N" or later
somePlanets = for (planet in planets where planet.compareTo("N") >= 0) "{planet},";
System.out.println("The {sizeof somePlanets} planets in the second half of the alphabet are:
{somePlanets}");
Running this Example
The JavaFX SDK Technology Preview branch of the compiler build may be downloaded here. This branch is what will become the JavaFX SDK Preview Release. After adding the openjfx-compiler-tp1/dist/bin directory to your PATH environment variable, and verifying that you have the Java Runtime Environment (JRE) 6 installed, use the following command at your operating system prompt to compile the program:
javafxc -d . SequenceExample.fx
To run the program, use the following command:
javafx jfx_book.SequenceExample
Have fun, and please post a comment if you have any questions!
Jim Weaver
JavaFX Script: Dynamic Java Scripting for Rich Internet/Client-side Applications
Immediate eBook (PDF) download available at the book's Apress site
Everyone has a "favourite" book, and then one day, another book comes along and just blows the other one out of the water, and you have a problem, because now your favourite book isnt so good anymore. Which book was THAT book for you?
Posted by: cialis online | April 22, 2010 at 09:31 AM
talk about the planet is something that helps us raise awareness of what we live day by day, since the change of society, to things like pollution, in short, everything is bound to one way or another
Posted by: viagra online | April 21, 2010 at 11:10 AM