230 likes | 685 Views
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
E N D
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 • 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
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
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
Create and edit a new Java program • On the desktop, clickStart >All Programs > Accessories > Notepad
Create and edit a new Java program • Another way to get to Notepad:On the desktop, click Start > Run Type “notepad” and click OK
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."); } }
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
Compile and execute program • On the desktop, clickStart >All Programs > Accessories > Command Prompt
Compile and execute program • Another way to get to Command Prompt:On the desktop, click Start > Run Type “cmd” and click OK
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
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.
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
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
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()
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.
Open the jGRASP IDE • On the desktop, clickStart > All Programs > jGRASP • Follow the directions given by your lab instructor
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."); } }
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.
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
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");
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.
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 */