Introduction to the Lab Computer System and the Creation of a Simple Java Program - PowerPoint PPT Presentation

1 / 37
About This Presentation
Title:

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

Description:

Introduction to the Lab Computer System and the Creation of a Simple Java Program ... Create and edit a new Java class. Compile and execute program ... – PowerPoint PPT presentation

Number of Views:89
Avg rating:3.0/5.0
Slides: 38
Provided by: robert794
Category:

less

Transcript and Presenter's Notes

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


1
Introduction to the Lab Computer System and the
Creation of a Simple Java Program
CS 201 LAB Introduction to Object-Oriented
Programming Lab Session 1
Prepared by Robert Tairas
2
Agenda
  • Access to the lab machines
  • Writing a Java program (Notepad Command Line)
  • Create and edit a new Java class
  • Compile and execute program
  • Writing a Java program (Eclipse IDE)
  • Create a new Java project
  • Create and edit a new Java class
  • Compile and execute program
  • Identify and correct compile errors
  • println() and print() methods
  • Documentation

3
Access to the lab machines
  • Use your CIS student account
  • If you dont have one, complete the form
    athttp//www.cis.uab.edu/it/accountApplication.ph
    p
  • For today, use the generic user account

4
Writing a Java Program
  • Create and edit a new Java class using Notepad
  • Compile and execute program using the Command
    Line (Prompt)

5
Create and edit new Java program
  • On the desktop, clickStart gtAll Programs gt
    Accessories gt Notepad

6
Create and edit new Java program
  • Another way to get to NotepadOn the desktop,
    click Start gt Run Type notepad and click OK

7
Create and edit new Java program
  • Write the following code to print Hello, World.
    to the standard output

public class HelloWorld public static void
main(String args) System.out.println("He
llo, World.")
8
Create and edit new Java program
  • From the menu in Notepad, select File gt Save As
  • For File name, type c\temp\HelloWorld.java
  • Click Save

9
Compile and execute program
  • On the desktop, clickStart gtAll Programs gt
    Accessories gt Command Prompt

10
Compile and execute program
  • Another way to get to Command PromptOn the
    desktop, click Start gt 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
Writing a Java Program
  • Integrated Development Environment
  • A tool that integrates editing, compiling,
    building, debugging, and other application
    development tasks.
  • Can be downloaded at http//www.eclipse.org

13
Open the Eclipse IDE
  • On the desktop, clickStart gt All Programs gt
    eclipse

14
Open the Eclipse IDE
  • Select workspace where projects will be stored
  • For Workspace, type C\temp\workspace

15
Create a new Java project
  • On the menu bar,chooseFile gt New gt Project

16
Create a new Java project
  • For Wizards,selectJava Project
  • Click Next

17
Create a new Java project
  • For Project name,type Lab 1
  • For Contents,select Createnew project
    inworkspace
  • For JDK Compliance,select Use a
    projectspecific complianceand select 5.0
    fromthe dropdown options

18
Create a new Java project
  • (...continued fromprevious slide)
  • For Project layout,select Use project folder as
    root for sources and class files
  • Click Finish

19
Create a new Java project
  • You may be asked to switch to the Java
    perspective. If so, click Yes
  • A perspective determines the initial layout of
    Eclipse, which includes the menu and tool bars
    and window views
  • You can switch to different perspectives by
    selecting Window gt Open Perspective on the menu
    bar

20
Create and edit a new Java class
menu bar
tool bar
OR
OR
right click on project
21
Create and edit a new Java class
  • For Name,type HelloWorld
  • Make sure publicstatic void main(Strings
    args)is checked
  • Click Finish

22
Create and edit a new Java class
  • Write a Java statement to print Hello, World.
    to the standard output

public class HelloWorld public static void
main(String args) System.out.println("Hello,
World.")
23
Compile and execute program
menu bar
OR
24
Compile and execute program
tool bar
OR
OR
25
Compile and execute program
right click on the file
OR
26
Compile and execute program
  • Under Save Resources,click OK

27
Compile and execute program
  • The output message will be displayed in the
    Console window at the bottom

28
Programming Style
public class HelloWorld public static void
main(String args) System.out.println("Hello,
World.")
  • The Java language is case-sensitive
  • For exampleHelloWorld is different from
    Helloworldmain is different from Main
    System.out.print is different from
    System.Out.print

29
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

30
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()

31
Programming Style
  • Indentation
  • Indent to indicate the nesting of code blocks
  • User 2, 3, 4 spaces of indentation or tabs
  • Be consistent

public class HelloWorld public static void
main(String args) System.out.println("Hello,
World.")
IDENTATION
IDENTATION
IDENTATION
32
Identify and correct compile errors
  • Edit the program and capitalize println
  • Println will automatically be underlined in red
  • The Problems window will display the error
  • Indicators (circled above) will also display
    error

33
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.pr
intln("Hello, World.")
OutputHello, World. Hello, World.
System.out.print("Hello, World.") System.out.prin
t("Hello, World.")
OutputHello, World.Hello, World.
34
println() and print() methods
  • Edit the code to display the output below

System.out.print("1700 ") System.out.print("Unive
rsity ") System.out.print("Boulevard
") System.out.print("Birmingham
") System.out.print("Alabama ") System.out.print
("35294 ") System.out.print("USA ")
Output1700 University Boulevard Birmingham
Alabama 35294 USA
35
println() and print() methods
  • How else could you do it?

Output1700 University Boulevard Birmingham
Alabama 35294 USA
System.out.println("1700 University
Boulevard") System.out.println("Birmingham
Alabama 35294") System.out.println("USA")
36
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.
37
Documentation
  • JavaDocs commentsJavaDocs
    examplehttp//java.sun.com/j2se/1.5.0/docs/api/j
    ava/lang/System.html

/ Returns an Image object that can then be
painted on the screen. The url argument must
specify an absolute _at_link URL. The name
argument is a specifier that is relative to the
url argument. ltpgt 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.
_at_param url an absolute URL giving the base
location of the image _at_param name the
location of the image, relative to the url
argument _at_return the image at the
specified URL _at_see Image /
Write a Comment
User Comments (0)
About PowerShow.com