introduction to the lab computer system and the creation of a simple java program
Download
Skip this Video
Download Presentation
Introduction to the Lab Computer System and the Creation of a Simple Java Program

Loading in 2 Seconds...

play fullscreen
1 / 23

Lab PowerPoint Slides - PowerPoint PPT Presentation


  • 409 Views
  • Uploaded on

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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Lab PowerPoint Slides' - Solomon


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
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
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
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
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
Create and edit a new Java program
  • On the desktop, clickStart >All Programs > Accessories > Notepad
create and edit a new java program6
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 program7
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
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
Compile and execute program
  • On the desktop, clickStart >All Programs > Accessories > Command Prompt
compile and execute program10
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 program11
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
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
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 style14
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 style15
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
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
Open the jGRASP IDE
  • On the desktop, clickStart > All Programs > jGRASP
  • Follow the directions given by your lab instructor
programming style18
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
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 methods20
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

println and print methods21

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
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.

documentation23
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

*/

ad