Introduction to the lab computer system and the creation of a simple java program
Download
1 / 23

Lab PowerPoint Slides - PowerPoint PPT Presentation


  • 406 Views
  • Updated 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 l.jpg

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 l.jpg
Agenda a Simple Java Program

  • 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 l.jpg
Access to the Lab Machines a Simple Java Program

  • 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 l.jpg
Writing a Java Program a Simple 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 l.jpg
Create and edit a new Java program a Simple Java Program

  • On the desktop, clickStart >All Programs > Accessories > Notepad


Create and edit a new java program6 l.jpg
Create and edit a new Java program a Simple 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 l.jpg
Create and edit a new Java program a Simple 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 l.jpg
Create and edit new Java program a Simple 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 l.jpg
Compile and execute program a Simple Java Program

  • On the desktop, clickStart >All Programs > Accessories > Command Prompt


Compile and execute program10 l.jpg
Compile and execute program a Simple Java Program

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


Compile and execute program11 l.jpg
Compile and execute program a Simple Java 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 l.jpg
Identify and correct compile errors a Simple Java Program

  • 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 l.jpg
Programming Style a Simple Java Program

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 l.jpg
Programming Style a Simple Java Program

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 l.jpg
Programming Style a Simple Java Program

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 l.jpg
Writing a Java Program Using jGRASP a Simple Java Program

  • 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 l.jpg
Open the jGRASP IDE a Simple Java Program

  • On the desktop, clickStart > All Programs > jGRASP

  • Follow the directions given by your lab instructor


Programming style18 l.jpg
Programming Style a Simple Java Program

  • 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 l.jpg
println() and print() methods a Simple Java Program

  • 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 l.jpg
println() and print() methods a Simple Java Program

  • 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 l.jpg

OR a Simple Java Program

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 l.jpg
Documentation a Simple Java Program

  • 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 l.jpg
Documentation a Simple Java Program

  • 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 [email protected] 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