1 / 18

Computer Science 1 (Studio) 01C-Simple Java

Computer Science 1 (Studio) 01C-Simple Java. Compilation in Java. The Java development toolchain: Source code created with emacs has a .java extension Byte code generated by javac has a .class extension Byte code run by java excludes the extension. % emacs Foo.java.

disabel
Download Presentation

Computer Science 1 (Studio) 01C-Simple Java

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. Computer Science 1 (Studio) 01C-Simple Java CS1s - 01C-Simple Java (v1.00)

  2. Compilation in Java • The Java development toolchain: • Source code created with emacs has a .java extension • Byte code generated by javac has a .class extension • Byte code run by java excludes the extension % emacs Foo.java Other Java bytecode Foo.java Foo.class % javac Foo.java % java Foo Java Virtual Machine Java source code Java compiler Byte code CS1s - 01C-Simple Java (v1.00)

  3. Basic Java Program FirstProgram.java: // This is my first program public class FirstProgram { public static void main (String args[]) { System.out.println(“Hello World!”); System.out.print(“I’m”); System.out.print(“ programming!”); } } CS1s - 01C-Simple Java (v1.00)

  4. FirstProgram • The name of the program is FirstProgram • This source code must be contained in a file named FirstProgram.java • Some lines are terminated with curly braces { } while some are terminated with the semicolon ; • What’s the difference between: System.out.println() System.out.print() • “Literal strings are contained within double quotes.” • // Single line comments begin with forward slashes CS1s - 01C-Simple Java (v1.00)

  5. Creating A Java Source Code File • In class Activity • Go to your ~/cs1/lec/01 directory • Create the FirstProgram.java file using emacs FirstProgram.java: // This is my first program public class FirstProgram { public static void main (String args[]) { System.out.println(“Hello World!”); System.out.print(“I’m”); System.out.print(“ programming!”); “What up!” } } CS1s - 01C-Simple Java (v1.00)

  6. Compiling Java Programs • You must be in the same directory as the source code file, FirstProgram.java • ~/cs1/lec/01 • In class Activity • Compile your program using: % javac FirstProgram.java • There are two possible outcomes: • Your program compiled without any problems and the byte code, FirstProgram.class, was quietly generated • Error messages were printed to standard output and no byte code, FirstProgram.class, was generated CS1s - 01C-Simple Java (v1.00)

  7. The Compiler • The java compiler is responsible for generating byte code from your java source code • Syntax errors in the source code cause the compiler to abort and display error messages • Missing symbols • Illegal instructions • Misspelled words • Your source code must be 100% syntactically correct before you can run your program CS1s - 01C-Simple Java (v1.00)

  8. Fixing Syntax Errors • In class Activity • Load the source file into emacs • Locate the syntax error/s based on the compiler output • To go to a line number in emacs: Edit->Go To->Goto Line… • Make the necessary changes • Save the changes: File->Save (C-x C-s) • Recompile the source code using the same command: % javac FirstProgram.java • Repeat these steps until the compiler no longer reports any errors CS1s - 01C-Simple Java (v1.00)

  9. Running Your Program • To run your program you must invoke the JVM in the same directory as the byte code file • This is the same directory as your source code • The JVM interprets the byte code which was generated by the compiler (FirstProgram.class) % java FirstProgram • In class Activity • Successfully execute the program CS1s - 01C-Simple Java (v1.00)

  10. Coding Standards • Look again at the source code and answer the following questions: • Who is the author or authors of the code? • When was it written? • What is the purpose of the program? • What are the different working revisions and changes to the source? CS1s - 01C-Simple Java (v1.00)

  11. Coding Standards • These questions cannot be answered because the program is not properly documented • Documentation is a very important part of writing code • It allows you or another author to interpret what you intended the code to do (in plain English) • It provides information about the author (ownership) • It documents what was done to the program • In a development group, consistent coding standards increases productivity CS1s - 01C-Simple Java (v1.00)

  12. The DCS Coding Standard for Java • Each program you write in Java, to be submitted as coursework, must follow the DCS Coding Standard • Programs that work correctly, but are not properly documented, will be penalized when graded • In class Activity • Navigate to the Coding Standard link (under Documentation) on the CS1 Studio Homepage • Read through the rules for proper coding CS1s - 01C-Simple Java (v1.00)

  13. Documenting FirstProgram • All source code must begin with a C-style comment that lists the file name and version information: /*  * filename.java  *  * Version:  *     $Id$  *  * Revisions:  *     $Log$  */ CS1s - 01C-Simple Java (v1.00)

  14. Documenting FirstProgram • The following comments will appear before the definition of every class: /**  * A description of what the class does.  *  * @author      Author name  * @author      Contributor 2  * @author      Contributor 3                 .                 .  * @author      Contributor n  */ CS1s - 01C-Simple Java (v1.00)

  15. Documenting FirstProgram • The following comments will appear before every method (including main):  /**   * A description of what the method does   *   * @param       name    description   …   * @param       name    description *   * @return              description *   * @exception   name    description …   * @exception   name    description */ CS1s - 01C-Simple Java (v1.00)

  16. Documenting FirstProgram • In class Activity • Add a source file documentation block • Add a class definition block • Add a method header for the main method • Add a comment inside main to explain what is happening • Save the file, recompile and rerun CS1s - 01C-Simple Java (v1.00)

  17. Writing your Second Program • In class Activity • Log on/off the CS system and get back into ~/cs1/lec/01 • Create a new Java source file called SecondProgram.java: public class SecondProgram { public static void (String args[]) { System.out.println(“Hello world!”) System.out.print(My name is ”); Systemout.println(“ YOUR NAME HERE ”; } • Save the file • Compile the program and make note of the errors • Correct all the errors in the program so that it successfully compiles and prints your name CS1s - 01C-Simple Java (v1.00)

  18. Revision History • Revision History • v1.00, 9/2/2004 4:52 PM, sps Initial revision. CS1s - 01C-Simple Java (v1.00)

More Related