CS 3131 Introduction to Programming in Java Rich

  • Slides: 26
Download presentation
CS 3131 Introduction to Programming in Java Rich Maclin Computer Science Department

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.

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 Features 1. Basic Language Features (Ch. 5, part of Ch. 6)

III. Java Language Features (cont) B. Event Models (Ch. 6) 1. 1. 0 Model

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)

Chapter 1 • Overview of programming languages – compilers – interpreters • Origin of

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 R 1, R 2 • Assembly

Computer Languages • Machine language – 01010110 ADD R 1, R 2 • Assembly language – ADD R 1, R 2 • High-level programming language – X+Y

Language Translation • High-level code mapped to low-level code • Can have multiple steps

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

Object Code • • • Compilers generally produce machine code Some produce intermediate code

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 –

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

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

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

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

The Internet • Originated by defense department • Mechanism of decentralized communication • Nodes route communication to others (seamlessly)

World Wide Web • Based on the internet • Idea: distributed hypermedia • Pages

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

Hyper. Text Markup Language (HTML) • Language used in web documents • Contains commands

Hyper. Text 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 =

<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

Java Programs • Applications – stand alone – compiled, run with interpreter • Applets – not intended to run on its own – embedded in web page

Application public class Hello. World { // A simple application public static void main

Application public class Hello. World { // A simple application public static void main (String argv[]) { System. out. println(“Hello world!”); } } produces output: Hello world!

Applet import java. applet. *; import java. awt. *; public class Hello. World extends

Applet import java. applet. *; import java. awt. *; public class Hello. World extends Applet { // A simple Applet public void paint (Graphics g) { g. draw. String(“Hello world!”, 30); } } Produces output on a web page

Java files • Class. Name. java - Java source code • Class. Name. class

Java files • Class. Name. java - Java source code • Class. Name. class - compiled byte code for Class. Name. java • Web. Page. Name. html - web page which may contain references to Class. Name. class

Object-Oriented Programming • Program consists of objects from classes • A class is a

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 Map. Long - number indicating longitudinal location Map. Lat

A Robot Class Robot Data Map. Long - number indicating longitudinal location Map. Lat - number indicating latitude location Battery. Level - number indicating battery level Methods Move. Forward(x) - move forward x yards Turn. Right(x) - turn right x degrees

Robot Instances • Each instance has values for the instance variables – R 1

Robot Instances • Each instance has values for the instance variables – R 1 is at long, lat 36. 12, 38. 34 • Each method can be applied to each robot by sending that robot a message – Ask R 1 to move forward 5 meters

Inheritance • Can define robot class with features all robots share • Then subclasses

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

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

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. *;

import java. applet. *; // add classes from applet pack. import java. awt. *; // add classes from awt package public class Hello. World extends Applet { // New class name Hello. World // 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. draw. String(“Hello world!”, 30); } }