CS 3131 Introduction to Programming in Java
Rich Maclin
Computer Science Department
Course Overview
I. Introduction to Java (Ch. 1)
II. Graphical User Interfaces (GUIs)A. Graphics commands (Ch. 2)
B. Widgets (Ch. 3)
C. Layouts (Ch. 4)
III. Java Language Features1. Basic Language Features (Ch.5, part of Ch. 6)
III. Java Language Features (cont)B. Event Models (Ch. 6)
1. 1.0 Model2. 1.1 Model
C. Building a full program (Ch. 7)D. Advanced Language Features (Ch. 8)
1. Arrays2. Loops
E. Exceptions (Ch. 9)F. Input/Output Streams (Ch. 10)
IV. The Next Step (living on the Web)A. The World Wide Wait(Web) (Ch. 12)B. Threads (Ch. 11)
Chapter 1
• Overview of programming languages– compilers– interpreters
• Origin of Java• Application versus Applet• Object-Oriented Programming
– classes– inheritance
Computer Languages
• Machine language– 01010110 ADD R1,R2
• Assembly language– ADD R1,R2
• High-level programming language– X+Y
Language Translation
• High-level code mapped to low-level code• Can have multiple steps
– high-level to assembly
– assembly to machine language
• Compiler itself is a program
CompilerSourceCode
ObjectCode
Object Code
• Compilers generally produce machine code
• Some produce intermediate code
• Intermediate code executed by interpreter
• Java compiles to byte-code
• Byte-code interpreted
Advantages of Interpreters
• Different machines can have interpreters specific to that machine– machine independence– no need for specific compiler for each machine– code compiled on any machine can be run on
any with an interpreter
Disadvantages of Interpreters
• Resulting code slower (10-100 times)
• Still need an interpreter for each machine
• Limited to abilities all machines can produce (lose graphics abilities of SGIs)
Why Java?
• Why not?• Similar moves had happened before, the
difference is the World Wide Web• Java provides a programming tool for web
pages that– has graphics capabilities– is machine independent– small language (easy to implement)
Why NOT Java?
• Still very young (undergoing constant change)– Visual J++ already dated– So is our textbook (published in 1998)– Language is growing in size
• It is slow
The Internet
• Originated by defense department• Mechanism of decentralized communication• Nodes route communication to others (seamlessly)
Workstation
Workstation
Workstation
WorkstationWorkstation
World Wide Web
• Based on the internet
• Idea: distributed hypermedia
• Pages placed in known locations on machines connected to internet
• Browsers can look at those pages
• Pages contain links to other pages/info
HyperText Markup Language (HTML)• Language used in web documents
• Contains commands enclosed in <>– <B> bold </B> for bold text
• Commands interpreted by machine
• Can contain references to Java programs– <APPLET> code = “Name.class” </APPLET>
<HTML>
<HEAD>
<TITLE>Page Title</TITLE>
</HEAD>
<BODY>
Title on the Page
<HR>
<APPLET> code = “Name.class”
width = 100 height = 100
</APPLET>
<HR>
</BODY>
</HTML>
Java Programs
• Applications– stand alone– compiled, run with interpreter
• Applets– not intended to run on its own– embedded in web page
Applicationpublic class HelloWorld {
// A simple application
public static void main (String argv[]) {
System.out.println(“Hello world!”);
}
}
produces output:
Hello world!
Appletimport java.applet.*;
import java.awt.*;
public class HelloWorld extends Applet {
// A simple Applet
public void paint (Graphics g) {
g.drawString(“Hello world!”,30,30);
}
}
Produces output on a web page
Java files
• ClassName.java - Java source code
• ClassName.class - compiled byte code for ClassName.java
• WebPageName.html - web page which may contain references to ClassName.class
Object-Oriented Programming
• Program consists of objects from classes
• A class is a set of objects (instances) with a common structure (and purpose)
• Objects consists of– data values (instance variables)– methods (class procedures)
A Robot Class
Robot
Data
MapLong - number indicating longitudinal location
MapLat - number indicating latitude location
BatteryLevel - number indicating battery level
Methods
MoveForward(x) - move forward x yards
TurnRight(x) - turn right x degrees
Robot Instances
• Each instance has values for the instance variables– R1 is at long,lat 36.12,38.34
• Each method can be applied to each robot by sending that robot a message– Ask R1 to move forward 5 meters
Inheritance
• Can define robot class with features all robots share
• Then subclasses of robots with specific features (e.g., Robots with IR sensors)
• Specific class can “inherit” the things defining regular robots
Java and Inheritance
• Java relies heavily upon inheritance
• Java provides lots of classes that perform useful actions
• Our job is to produce specialized classes to perform those aspects we are interested in
Java packages
• java.applet - basic applet tools
• java.awt - abstract window toolkit (buttons, text, etc.)
• java.io - input/output classes
• java.lang - language features
• java.net - classes for working with networks
• java.util - other useful utility classes
import java.applet.*; // add classes from applet pack.
import java.awt.*; // add classes from awt package
public class HelloWorld extends Applet {
// New class name HelloWorld
// Class is accessible by all (public)
// Class extends (inherits from) class Applet
public void paint (Graphics g) {
// Paint is a method (we are overriding an existing
// method)
g.drawString(“Hello world!”,30,30);
}
}