Title: CS 3131 Introduction to Programming in Java
1CS 3131 Introduction to Programming in Java
- Rich Maclin
- Computer Science Department
2Course 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 Features
- 1. Basic Language Features (Ch.5, part of Ch. 6)
3- III. Java Language Features (cont)
- B. Event Models (Ch. 6)
- 1. 1.0 Model
- 2. 1.1 Model
- C. Building a full program (Ch. 7)
- D. Advanced Language Features (Ch. 8)
- 1. Arrays
- 2. 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)
4Chapter 1
- Overview of programming languages
- compilers
- interpreters
- Origin of Java
- Application versus Applet
- Object-Oriented Programming
- classes
- inheritance
5Computer Languages
- Machine language
- 01010110 ADD R1,R2
- Assembly language
- ADD R1,R2
- High-level programming language
- XY
6Language 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
7Object Code
- Compilers generally produce machine code
- Some produce intermediate code
- Intermediate code executed by interpreter
- Java compiles to byte-code
- Byte-code interpreted
8Advantages 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
9Disadvantages 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)
10Why 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)
11Why 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
12The Internet
- Originated by defense department
- Mechanism of decentralized communication
- Nodes route communication to others (seamlessly)
13World 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
14HyperText Markup Language (HTML)
- Language used in web documents
- Contains commands enclosed in ltgt
- ltBgt bold lt/Bgt for bold text
- Commands interpreted by machine
- Can contain references to Java programs
- ltAPPLETgt code Name.class lt/APPLETgt
15ltHTMLgt ltHEADgt ltTITLEgtPage Titlelt/TITLEgt
lt/HEADgt ltBODYgt Title on the Page ltHRgt
ltAPPLETgt code Name.class
width 100 height 100 lt/APPLETgt ltHRgt
lt/BODYgt lt/HTMLgt
16Java Programs
- Applications
- stand alone
- compiled, run with interpreter
- Applets
- not intended to run on its own
- embedded in web page
17Application
public class HelloWorld // A simple
application public static void main (String
argv) System.out.println(Hello
world!) produces output Hello world!
18Applet
import 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
19Java 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
20Object-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)
21A 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
22Robot 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
23Inheritance
- 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
24Java 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
25Java 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
26import 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)