13
Introduction to Java Programming CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University

Introduction to Java Programming CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University

Embed Size (px)

Citation preview

Page 1: Introduction to Java Programming CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University

Introduction to Java Programming

CS 21a: Introduction to Computing I

Department of Information Systems

and Computer ScienceAteneo de Manila University

Page 2: Introduction to Java Programming CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

L1: Intro JavaSlide 2

Programming

Recall that a program is defined as:a sequence of instructions for a computer

A large part (but not all) of CS 21a is about how to write programs in a programming language (Java)

Page 3: Introduction to Java Programming CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

L1: Intro JavaSlide 3

The JavaProgramming Language

Java: an object-oriented programming language that is simple safe platform independent designed for the internet

Many universities use Java as the introductory programming language for beginning programmers Ateneo adopted Java for CS 21a in 1997

Page 4: Introduction to Java Programming CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

L1: Intro JavaSlide 4

Java (a brief history) 1991

Sun Microsystems develops a language (based on C) for consumer electronic devices

1993 WWW explodes in popularity increased need for “dynamic” Web pages

1995 Sun formally announces Java for web use

Page 5: Introduction to Java Programming CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

L1: Intro JavaSlide 5

Two types of Java programs:

Applications general-purpose programs standalone executed through the operating system

Applets programs meant for the WWW embedded in a Web page normally executed through a browser

Page 6: Introduction to Java Programming CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

L1: Intro JavaSlide 6

Simple Java Application

File: Hello.java

// Hello World application

public class Hello

{

public static void main( String args[] )

{

System.out.println( “Hello world” );

}

}

Page 7: Introduction to Java Programming CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

L1: Intro JavaSlide 7

The Programming Process

Create/EditProgram

CompileProgram

ExecuteProgram

Compile Errors? Run-Time Errors?

SourceProgram

ObjectProgram

Page 8: Introduction to Java Programming CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

L1: Intro JavaSlide 8

Creation, Compilation, and Execution

Create Java programC:\> edit Hello.java Hello.java file is created

Compile using javac (compiler)C:\> javac Hello.java Hello.class file is produced

Execute using java (interpreter)C:\>java Hello requires a Hello.class file

Page 9: Introduction to Java Programming CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

L1: Intro JavaSlide 9

Simple Java AppletFile: HelloAgain.java

import javax.swing.*;import java.awt.*;public class HelloAgain extends JApplet{ public void paint( Graphics g ) { g.drawString( “Hello”, 50, 50 ); }}

Page 10: Introduction to Java Programming CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

L1: Intro JavaSlide 10

Executing Applets

After compiling the java program:

Embed an “applet tag” in an .html document that references the .class file

Open the .html document using a browser or the appletviewer

Page 11: Introduction to Java Programming CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

L1: Intro JavaSlide 11

Sample .html Document

File: HA.html

<h1> My Sample Applet </h1><applet code=“HelloAgain.class” height=200

width=100></applet>

Page 12: Introduction to Java Programming CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

L1: Intro JavaSlide 12

What is HTML?

Hypertext Markup Language Underlying language of Web pages A means of providing formatting

instructions for presenting content Text-based .html documents:

collection of content and controls (tags)

Page 13: Introduction to Java Programming CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University

Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

L1: Intro JavaSlide 13

Java Program Structure

Java Program (optional) import declarations class declaration

Class class name should match its file name may extend an existing class

(such as JApplet) contains method/function declarations