Download Intro to Programming

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Introduction to Programming
The Programming Process
Compile Errors?
Create/Edit
Program
Source
Program
Compile
Program
Run-Time Errors?
Object
Program
Execute
Program
Programming



No matter what kind of computer you
have, it works by following specific
steps
These steps are called the computer’s
program
A large part (but not all) of CS 21a is
about how to write programs
Object-Oriented Programming



Several years ago, Ateneo was the first school
in the Philippines (and one of the first in the
world) to teach Java as the first programming
language for CS and MIS majors
Now Java is a well-accepted teaching
language, and very highly in-demand in
industry
Among others … one advantage of Java is
that it makes learning Object-Oriented
Programming (OOP) easier than with other
languages
What is Object-oriented
Programming?

Programs are written as systems of
interacting objects




Objects are “things” that can have state and
properties and can “do” certain actions
Began in the 1960’s with Simula, a language
for writing simulations
Popularized in the 80’s by Smalltalk, then
C++ in the late 80’s, and then Java in the
late-90’s
Allows you to write very complex and large
systems, as well as maximize reuse and
flexibility
Object-oriented Programming


Object-oriented design makes it easy to
understand and define the program
Makes writing big, complex programs
easier


Little pieces compose complex systems
Just like in real life (e.g., cells make up
living things)
Introduction to Java
Java

1991


1993



Sun Microsystems develops a language
(based on C) for consumer electronic
devices
WWW explodes in popularity
increased need for “dynamic” Web pages
1995

Sun formally announces Java for web use
What is Java?
Java is a general purpose programming
language that is:






object-oriented
interpreted, architecture-neutral, portable
distributed (network-aware), secure
simple, robust
multi-threaded
high-performance (?)
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
Simple Java Application
File: Hello.java
// Hello World application
public class Hello
{
public static void main( String
args[] )
{
System.out.println( “Hello world”
);
}
}
Creation, Compilation, and
Execution

Create Java program
C:\> 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
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 );
}
}
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
Sample .html Document
File: HA.html
<h1> My Sample Applet </h1>
<applet code=“HelloAgain.class” height=200
width=100>
</applet>
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)
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
Applet)
contains method/function declarations
Related documents