introduction to java programming cs 21a: introduction to computing i first semester, 2013-2014

13
Introduction to Java Programming CS 21a: Introduction to Computing I First Semester, 2013-2014

Upload: michael-lucas

Post on 25-Dec-2015

217 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Introduction to Java Programming CS 21a: Introduction to Computing I First Semester, 2013-2014

Introduction to Java

ProgrammingCS 21a: Introduction to

Computing IFirst Semester, 2013-2014

Page 2: Introduction to Java Programming CS 21a: Introduction to Computing I First Semester, 2013-2014

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 First Semester, 2013-2014

The Java Programming 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 First Semester, 2013-2014

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 First Semester, 2013-2014

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 First Semester, 2013-2014

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 First Semester, 2013-2014

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 First Semester, 2013-2014

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 First Semester, 2013-2014

Simple Java Applet

File: 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 First Semester, 2013-2014

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 First Semester, 2013-2014

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 First Semester, 2013-2014

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 First Semester, 2013-2014

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