1 / 16

Programming without BlueJ Week 12

Programming without BlueJ Week 12. Programming without BlueJ. CONCEPTS COVERED THIS WEEK. Running Java without BlueJ The main Method. Programming with BlueJ. The BlueJ directory structure. project: clock-display. package.bluej ClockDisplay .java ClockDisplay .class

cale
Download Presentation

Programming without BlueJ Week 12

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming without BlueJ Week 12

  2. Programming without BlueJ CONCEPTS COVERED THIS WEEK • Running Java without BlueJ • The main Method

  3. Programming with BlueJ The BlueJ directory structure project: clock-display package.bluej ClockDisplay.java ClockDisplay.class ClockDisplay.ctxt NumberDisplay.java NumberDisplay.class NumberDisplay.ctxt ClockDisplay NumberDisplay

  4. Programming with BlueJ The BlueJ file structure package.bluej Stores BlueJ project details ClassName.ctxt BlueJ class context file ClassName.java Java source code file ClassName.class Java bytecode file

  5. Programming without BlueJ Standard Java Files ClassName.java Java source files contain the source code in readable form, as typed in by the programmer. ClassName.class Java class files contain bytecode (a machine readable version of the class). They are generated by the compiler from the source file.

  6. Programming without BlueJ Standard Java Edit-Compile-Run Cycle 011010 110101 1001 10 source file class file 011010 110101 010001 1 1 1 0111 0110110 compiler (javac) virtual machine(java) editor

  7. Programming without BlueJ Editing Java Source Files • A Java file can be edited using any text editor e.g. Notepad • Careful with using Word as, by default, Word does not save in text format • Be sure to save changes before compiling!

  8. Programming without BlueJ Command Line Invocation • Compilation and execution of Java in JDK are done from a command line. • Windows: DOS shell, i.e. Command Prompt: • Ensure that the directory for the Java compiler (javac) and Java runtime (java) is set in the command path e.g. • set path=C:\Program Files (x86)\Java\jdk1.6.0_33\bin

  9. Programming without BlueJ Java Compilation • The name of the JDK compiler is javac • To compile a Java class definition:javac<source name> • javac compiles the source file as well as all other classes the source file depends upon. • Example usage of javac: • X:\>cd My Documents\Java\Picture • X:\My Documents\Java\Picture>javac Picture.java

  10. Programming without BlueJ Java Compilation (Error Messages) Example usage of javac: X:\>cd My Documents\Java\Picture X:\My Documents\Java\Picture>javac Picture.java Picture.java:14: ';' expected. private square wall ^ 1 error X:\My Documents\Java\Picture> The programmer has to open the Java file in the editor, find the line number, fix the error and recompile.

  11. Programming without BlueJ Java Program Execution Example usage of java: X:\My Documents\Java\Picture>java Picture • “java” starts the Java virtual machine (JVM). • The named class is loaded and execution is started. • Other classes are loaded as needed. • Only possible if class has been compiled.

  12. Programming without BlueJ Java Program Execution – Problem: Execute What? Example usage of java: X:\My Documents\Java\Picture>java Picture Exception in thread "main" java.lang.NoSuchMethodError: main How does the system know which of the methods of the class to execute?

  13. Programming without BlueJ Java Program Execution – The Main Method Example usage of javac: X:\My Documents\Java\Picture>java Picture Exception in thread "main" java.lang.NoSuchMethodError: main The solution: The Java system always executes a class method called main with a specific signature.

  14. Programming without BlueJ Java Program Execution – The Main Method Fixed signature for the main method: public static void main(String[] args){ ...} • For this to work, a main method must exist in one (and only one) of the classes being called! • It is possible to place all of an application’s code in a class with just a main method! • But this is absolutely NOT RECOMMENDED!

  15. Programming without BlueJ Java Program Execution – The Main Method Example of a main method in a class called Game: public static void main(String[] args){ Game game = new Game(); game.play(); } • In general the main method should ONLY • create an object • call the first method

  16. Creating a Java program • Writing the Java code • Any editor (such as Notepad) can be used to write a Java program. However, most professional programmers use a software application called an Integrated Development Environment or IDE • JCreator • http://www.jcreator.com/

More Related