CS 3131 Introduction to Programming in Java Rich Maclin Computer Science Department.
-
Upload
paulina-jackson -
Category
Documents
-
view
222 -
download
0
Transcript of CS 3131 Introduction to Programming in Java Rich Maclin Computer Science Department.
![Page 1: CS 3131 Introduction to Programming in Java Rich Maclin Computer Science Department.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649ece5503460f94bda8dd/html5/thumbnails/1.jpg)
CS 3131 Introduction to Programming in Java
Rich Maclin
Computer Science Department
![Page 2: CS 3131 Introduction to Programming in Java Rich Maclin Computer Science Department.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649ece5503460f94bda8dd/html5/thumbnails/2.jpg)
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)
![Page 3: CS 3131 Introduction to Programming in Java Rich Maclin Computer Science Department.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649ece5503460f94bda8dd/html5/thumbnails/3.jpg)
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)
![Page 4: CS 3131 Introduction to Programming in Java Rich Maclin Computer Science Department.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649ece5503460f94bda8dd/html5/thumbnails/4.jpg)
Chapter 1
• Overview of programming languages– compilers– interpreters
• Origin of Java• Application versus Applet• Object-Oriented Programming
– classes– inheritance
![Page 5: CS 3131 Introduction to Programming in Java Rich Maclin Computer Science Department.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649ece5503460f94bda8dd/html5/thumbnails/5.jpg)
Computer Languages
• Machine language– 01010110 ADD R1,R2
• Assembly language– ADD R1,R2
• High-level programming language– X+Y
![Page 6: CS 3131 Introduction to Programming in Java Rich Maclin Computer Science Department.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649ece5503460f94bda8dd/html5/thumbnails/6.jpg)
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
![Page 7: CS 3131 Introduction to Programming in Java Rich Maclin Computer Science Department.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649ece5503460f94bda8dd/html5/thumbnails/7.jpg)
Object Code
• Compilers generally produce machine code
• Some produce intermediate code
• Intermediate code executed by interpreter
• Java compiles to byte-code
• Byte-code interpreted
![Page 8: CS 3131 Introduction to Programming in Java Rich Maclin Computer Science Department.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649ece5503460f94bda8dd/html5/thumbnails/8.jpg)
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
![Page 9: CS 3131 Introduction to Programming in Java Rich Maclin Computer Science Department.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649ece5503460f94bda8dd/html5/thumbnails/9.jpg)
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)
![Page 10: CS 3131 Introduction to Programming in Java Rich Maclin Computer Science Department.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649ece5503460f94bda8dd/html5/thumbnails/10.jpg)
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)
![Page 11: CS 3131 Introduction to Programming in Java Rich Maclin Computer Science Department.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649ece5503460f94bda8dd/html5/thumbnails/11.jpg)
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
![Page 12: CS 3131 Introduction to Programming in Java Rich Maclin Computer Science Department.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649ece5503460f94bda8dd/html5/thumbnails/12.jpg)
The Internet
• Originated by defense department• Mechanism of decentralized communication• Nodes route communication to others (seamlessly)
Workstation
Workstation
Workstation
WorkstationWorkstation
![Page 13: CS 3131 Introduction to Programming in Java Rich Maclin Computer Science Department.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649ece5503460f94bda8dd/html5/thumbnails/13.jpg)
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
![Page 14: CS 3131 Introduction to Programming in Java Rich Maclin Computer Science Department.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649ece5503460f94bda8dd/html5/thumbnails/14.jpg)
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>
![Page 15: CS 3131 Introduction to Programming in Java Rich Maclin Computer Science Department.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649ece5503460f94bda8dd/html5/thumbnails/15.jpg)
<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>
![Page 16: CS 3131 Introduction to Programming in Java Rich Maclin Computer Science Department.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649ece5503460f94bda8dd/html5/thumbnails/16.jpg)
Java Programs
• Applications– stand alone– compiled, run with interpreter
• Applets– not intended to run on its own– embedded in web page
![Page 17: CS 3131 Introduction to Programming in Java Rich Maclin Computer Science Department.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649ece5503460f94bda8dd/html5/thumbnails/17.jpg)
Applicationpublic class HelloWorld {
// A simple application
public static void main (String argv[]) {
System.out.println(“Hello world!”);
}
}
produces output:
Hello world!
![Page 18: CS 3131 Introduction to Programming in Java Rich Maclin Computer Science Department.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649ece5503460f94bda8dd/html5/thumbnails/18.jpg)
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
![Page 19: CS 3131 Introduction to Programming in Java Rich Maclin Computer Science Department.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649ece5503460f94bda8dd/html5/thumbnails/19.jpg)
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
![Page 20: CS 3131 Introduction to Programming in Java Rich Maclin Computer Science Department.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649ece5503460f94bda8dd/html5/thumbnails/20.jpg)
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)
![Page 21: CS 3131 Introduction to Programming in Java Rich Maclin Computer Science Department.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649ece5503460f94bda8dd/html5/thumbnails/21.jpg)
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
![Page 22: CS 3131 Introduction to Programming in Java Rich Maclin Computer Science Department.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649ece5503460f94bda8dd/html5/thumbnails/22.jpg)
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
![Page 23: CS 3131 Introduction to Programming in Java Rich Maclin Computer Science Department.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649ece5503460f94bda8dd/html5/thumbnails/23.jpg)
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
![Page 24: CS 3131 Introduction to Programming in Java Rich Maclin Computer Science Department.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649ece5503460f94bda8dd/html5/thumbnails/24.jpg)
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
![Page 25: CS 3131 Introduction to Programming in Java Rich Maclin Computer Science Department.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649ece5503460f94bda8dd/html5/thumbnails/25.jpg)
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
![Page 26: CS 3131 Introduction to Programming in Java Rich Maclin Computer Science Department.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649ece5503460f94bda8dd/html5/thumbnails/26.jpg)
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);
}
}