Java the easy way James A. Rome Tennessee Governor's Academy August 2010 Why Java? • Java is the most in-demand language (= job opportunities) • It is a modern object-oriented language • It is similar to C and C++ ⇒ It leaves out pointers, multiple inheritance, operator overloading • Write once, run anywhere ⇒ From supercomputers to cell phones, Macs, Windows, Linux • It is faster than any language other than Fortran or Assembler • There are oodles of libraries • Free excellent development tools Java programmers are in demand http://www.eweek.com/c/a/IT- Management/Programming- Development-Skills-In-Demand-505665 In the WSJ recently, there was a long article on how the language you use affects how you think. Computer languages foster logical thought. • But many scientific calculations do not fit nicely into existing computer languages • Remember that digital computers do discrete mathematics and computations: calculations are done at a finite number of points. -> round-off and truncation errors -> most floating point numbers cannot be represented exactly in binary Tools you will need (installed I hope) • The Java development kit http://www.oracle.com/technetwork/java/javase/downloads/i ndex-jsp-138363.html • The NetBeans IDE. Some people prefer Eclipse http://www.netbeans.org • A programmer's text editor such as http://www.crimsoneditor.com/ • We may get to databases, so I asked for MySQL http://www.mysql.com/downloads/mysql/ http://dev.mysql.com/downloads/workbench/#downloads Our foray into Java Purpose: Get your feet wet so you can understand how scientists use computers and teach yourself more Java. • The book is really good, so please read it. • I want to concentrate on what is not in the book ⇒ How to do a graphical user interface (GUI) ⇒ How to model a real problem and get it into a form a computer can solve ⇒ How to teach yourself • Along the way we will use and learn many Java Classes and techniques Some basic syntax • Assignment uses an equals sign ⇒ int b = 4; // assigns 4 to the integer variable b • Testing for equality uses == ⇒ if(b == 4) { /* do something when b is 4 */} • // Comments to the end of the line • /* . . .*/ Comments blocks, which can be many lines • /** . . . */ A JavaDoc comment ⇒ Java is self documenting if you put these comments into your code they get converted to nice html • Formatting is optional but important for readability • All statements must end in a semicolon • Braces {} group lines of code and define a scope ⇒ Things defined in the braces are not accessible outside them. • Names must start with a letter Java syntax Built-in types for variables: • char, int, short, byte, long, float, double, boolean ⇒ These are not Objects. But all have wrapper classes that turn them into Objects int a = 5; Integer aInt = new Integer(a); ⇒ Everything else is an Object (think noun, its a thing) ⇒ Unlike in C or C++, these are the same on all platforms • Otherwise (without ints, floats,…) , you could not write a = b + c; Loops int j = 0; for(int i = 0, j = 5; i < 3; i++) { j += i; // becomes 5, 6, 8 (same as j = j + i) } // j = 8 at end of loop // i no longer defined here because outside the {} while(j < 10) { // Test at start of loop System.out.println("j=" + ++j); // preincrement } // Prints out j=9, j=10 do { // Test at end of loop System.out.println("j=" + j++); } while (j < 20); // prints out j=10, j=20 starting values do while at end of loop do this Class methods or fields are referenced using "." System.out returns a PrintStream object, which has a method println() Logic (make decisions) int a = 3; while(a < 12) { if(a == 5) // == is a test for equality System.out.println("Hit a=5"); else if(a < 10) System.out.println("a < 10"); else System.out.println("a >= 10"); a++; // if you forget this, you get an infinite loop! } // Ternary operator int b = ((a > 7) ? 0 : 1); // Usually a good idea to put if(b != 5) b += 7; // Parens around the condition Case statements char c = 'b'; switch(c) { case 'a': System.out.println("a"); break; // if break is missing, this “falls through” case 'b': System.out.println("b"); break; default: System.out.println("not a or b"); } The argument you switch on must be an int, char, short, or byte. Used to improve logic and speed decision trees with a hash. [...]... privileges because they belong with each other • They avoid name collision • By convention, you use a reverse DN (e.g., com.sun .java. utility) ⇒ We will use mypets (in lc) • Right-click the MyPets Source Package name and select “New, Java Package” Create the Pet Class Right-click the mypets Package and select “New, Class” • Note that the file is always classname .java, and it is stored in the package directory... Call the Pet methods Make a PetMaster Class Run the project The project needs to know where the main() method is • Right-click the Project, and select Properties • Note that you must specify the package as well as the class name in the Run settings Run your project by either clicking the green arrow, or selecting Run Main Project on the Run Menu Inheritance Our class Pet will help us learn yet another... System.out.println(petReaction); myFish.sleep(); } } Run the code again with your fish How to run without NetBeans Taken from README.TXT in the MyPets/Dist folder: When you build an Java application project that has a main class, the IDE automatically copies all of the JAR files on the projects classpath to your projects dist/lib folder The IDE also adds each of the JAR files to the Class-Path element in the application JAR files... files manifest file (MANIFEST.MF) To run the project from the command line, go to the dist folder and type the following: java -jar "MyPets.jar" To distribute this project, zip up the dist folder (including the lib folder) and distribute the ZIP file User interfaces The cartoon Lifted was shown along with Ratatouille It is all about user interfaces © Pixar Note the thousands of identical and unlabeled... Tools/Plugins/I nstalled Tab ⇒ Make sure that Java SE is activated Set your NetBeans options Under Tools/Options • In General: Pick your favorite Web Browser • Under Options/Formatting, set the indent and tab sizes to 3 characters • You can look at the other options at your leisure Install the JavaDocs • The Java documents come in a zip file: jdk-7u??docs.zip [download it from the JDK page] • NetBeans is smart... Classes must be instantiated In order to do anything with a class you must “create an instance of the object.” This uses the class definition to make a copy of the object described by the class and to put it in the computer’s memory Pet myPet = new Pet(); myPet is the name of the instance and is a member of the class Pet We need code to instantiate Pet public class PetMaster { class signature public...Everything in Java is part of a class Classes represent objects from the real world Think of a class as The definition of a set where each member of the set exhibits the same behavior and has the same attributes and associations.” • Good programmers decide what classes they need to write before starting to write a program! • Classes in Java may have methods and attributes ⇒... you must always remember the user! Making a GUI application using NetBeans • Open NetBeans and do File, New Project ⇒ Select Java Application from the Wizard and hit Next ⇒ Uncheck Create Main Class ⇒ Enter the project Name YourName and hit Finish • The YourName project will appear in the left Package Window, with a package, files, and a blank GUI NetBeans has a new GUI framework Make a new Java package... (Fish inherits all of the methods of Pet) Making a subclass Creation of subclasses in NetBeans is a piece of cake! Right-click the package and select New, Class, and type Fish as the name of the class Then put in “extends Pet” before the first { Let’s not forget, however, that we’re creating a subclass of a Pet to add some new features that only fish have, and to reuse some of the code that we wrote... application with a GUI There are several methods of making graphical user interfaces in Java — AWT, Swing, SWT We will concentrate on Swing • Swing is built into all modern JDKs ⇒ A Swing book is on your CD • It is an easy- to-use high-level java interface • Swing components are Java Beans ⇒ A Java Bean Object has get and set methods ⇒ It adheres to certain naming conventions ⇒ Java Bean properties can . Java the easy way James A. Rome Tennessee Governor's Academy August 2010 Why Java? • Java is the most in-demand language (= job opportunities) • It. characters • You can look at the other options at your leisure Install the JavaDocs • The Java documents come in a zip file: jdk-7u??- docs.zip [download it from the JDK page] • NetBeans is. lc) • Right-click the MyPets Source Package name and select “New, Java Package” Create the Pet Class Right-click the mypets Package and select “New, Class” • Note that the file is always classname .java,