1 / 23

Introduction to the Lab Computer System and the Creation of a Simple Java Program

Introduction to the Lab Computer System and the Creation of a Simple Java Program. CS 201 Introduction to Object-Oriented Programming Lab Session 1. Prepared by: Robert Tairas modified 8/22/07. Agenda. Access to the lab machines

Solomon
Download Presentation

Introduction to the Lab Computer System and the Creation of a Simple Java Program

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. Introduction to the Lab Computer System and the Creation of a Simple Java Program CS 201 Introduction to Object-Oriented Programming Lab Session 1 Prepared by: Robert Tairas modified 8/22/07

  2. Agenda • Access to the lab machines • Writing a Java program (using notepad, java, and javac on a command Line) • Create and edit a new Java class • Compile and execute program • Identify and correct compile errors • Programming Style • The jGRASP IDE • println() and print() methods • Documentation

  3. Access to the Lab Machines • Use your CIS student account • If you don’t have one, complete the form athttp://www.cis.uab.edu/it/accountApplication.php • For today, use the generic user account

  4. Writing a Java Program • Create and edit a new Java class using Notepad • Compile (with javac) and execute (with java) your program using the command line

  5. Create and edit a new Java program • On the desktop, clickStart >All Programs > Accessories > Notepad

  6. Create and edit a new Java program • Another way to get to Notepad:On the desktop, click Start > Run Type “notepad” and click OK

  7. Create and edit a new Java program • Carefully enter the following Java code to print “Hello, World.” to the standard output (the screen). public class HelloWorld { public static void main (String[] args) { System.out.println("Hello, World."); } }

  8. Create and edit new Java program • From the menu in Notepad, select File > Save As • For File name, type “c:\temp\HelloWorld.java” • Click Save

  9. Compile and execute program • On the desktop, clickStart >All Programs > Accessories > Command Prompt

  10. Compile and execute program • Another way to get to Command Prompt:On the desktop, click Start > Run Type “cmd” and click OK

  11. Compile and execute program • Type “c:” and press Enter • Type “cd temp” and press Enter • Type “javac HelloWorld.java” and press Enter • Type “java HelloWord” and press Enter

  12. Identify and correct compile errors • Edit the program and capitalize println • Now save and compile your program with javac • What happened? • Now correct your program, recompile, and run it again.

  13. Programming Style public class HelloWorld { public static void main (String[] args) { System.out.println("Hello, World."); } } • The Java language is case-sensitive • For example:HelloWorld is different from Helloworldmain is different from MainSystem.out.print is different from System.Out.print

  14. Programming Style public class HelloWorld { public static void main (String[] args) { System.out.println("Hello, World."); } } • Class names: • Required: • No spaces • Should match file name, ie. HelloWorld.java • Recommended: • Capitalize first letter

  15. Programming Style public class HelloWorld { public static void main (String[] args) { System.out.println("Hello, World."); } } • Method names: • Required: • No spaces • Recommended: • Starts with a lowercase letter, e.g. getName()

  16. Writing a Java Program Using jGRASP • Integrated Development Environment • A tool that integrates editing, compiling, building, debugging, and other application development tasks. • We will begin with the jGRASP IDE. There are others such as Eclipse and NetBeans.

  17. Open the jGRASP IDE • On the desktop, clickStart > All Programs > jGRASP • Follow the directions given by your lab instructor

  18. Programming Style • Indentation • Indent to indicate the nesting of code blocks • User 3 or 4 spaces of indentation • Be consistent! public class HelloWorld { public static void main (String[] args) ( System.out.println("Hello, World."); } }

  19. println() and print() methods • The only difference between println() and print() is that println() adds a newline at the end of its output System.out.println("Hello, World."); System.out.println("Hello, World."); Output:Hello, World. Hello, World. System.out.print("Hello, World."); System.out.print("Hello, World."); Output:Hello, World.Hello, World.

  20. println() and print() methods • Edit the code to display the output below: System.out.print("1700 "); System.out.print("University "); System.out.print("Boulevard "); System.out.print("Birmingham "); System.out.print("Alabama "); System.out.print("35294 "); System.out.print("USA "); Output:1700 University Boulevard Birmingham Alabama 35294 USA

  21. OR System.out.print("1700 University Boulevard\n"); System.out.print("Birmingham Alabama 35294\n"); System.out.print("USA\n"); println() and print() methods • How else could you do it? Output:1700 University Boulevard Birmingham Alabama 35294 USA System.out.println("1700 University Boulevard"); System.out.println("Birmingham Alabama 35294"); System.out.println("USA");

  22. Documentation • Document your code using comments • Block comments • Single-Line comments • End-Of-Line comments /* * Here is a block comment. */ /* Here is a single-line comment. */ System.out.print(“Hello”); // An end-of-line comment.

  23. Documentation • JavaDocs commentsJavaDocs example:http://java.sun.com/j2se/1.5.0/docs/api/java/lang/System.html /** * Returns an Image object that can then be painted on the screen. * The url argument must specify an absolute {@link URL}. The name * argument is a specifier that is relative to the url argument. * <p> * This method always returns immediately, whether or not the * image exists. When this applet attempts to draw the image on * the screen, the data will be loaded. The graphics primitives * that draw the image will incrementally paint on the screen. * * @param url an absolute URL giving the base location of the image * @param name the location of the image, relative to the url argument * @return the image at the specified URL * @see Image */

More Related