Download COP3502 Programming Fundamentals for CIS Majors 1

Document related concepts
no text concepts found
Transcript
COP3502
Programming
Fundamentals
for CIS Majors 1
Instructor: Dr. Parisa Rashidi
Objectives
 Computer
basics
 What is Java?
 API,
 Write
IDE, and JDK
a simple program in Java
 Console
output
 GUI
 Understand
the basic syntax of Java
Computing Basics
Hardware, software, etc.
Computer Anatomy



Hardware
Operating Systems
Software
From Wikipedia, the free encyclopedia
Hardware Anatomy

Basic hardware components of a computer
 CPU
 Memory
 Storage
 I/O (Input/Output)
 Communication devices
Storage
Devices
Memory
Communication
Devices
CPU
BUS
Input
Devices
Output
Devices
CPU
 CPU
= Central Processing Unit
 Speed
1
measured in MHz
MHz = 10^6 pulses per second
 Executes
instructions retrieved from memory
Memory
 We
are talking about RAM
 Random
Access Memory
 Volatile
 Stores
A
data
sequence of bytes
 Each
byte = 8 bits
 Each
bit can be 0 or 1
23
100
67
A
P
P
L
E
X
32
2
@
$
76
X
89
120
D
T
D
Byte
0 0 0 0 0 0 1 0 (8 bits)
Bit
Memory
(sequence of bytes)
Memory
 All
data is encoded as 0-1
 Byte = minimum storage unit
 Large numbers are stored in more than 1 byte
Memory Address Memory Content
.
.
.
.
.
2000
0 1 0 0 1 0 1 0 Encoding for character ‘J’
2001
0 1 1 0 0 0 0 1 Encoding for Character ‘a’
2002
0 1 1 1 0 1 1 0 Encoding for character ‘v’
2003
0 1 1 0 0 0 0 1 Encoding for Character ‘a’
2004
0 0 0 0 0 0 1 0 Encoding for number 2
.
.
.
.
Memory
 Quick
 Byte
reminder
= minimum storage unit
 KB = 10^3 Bytes
 MB = 10^6 Bytes
 GB = 10^9 Bytes
 TB = 10^12 Bytes
 PB = 10^15 Bytes
Storage
 Memory
 Store
is volatile
programs & data on non-volatile devices
 Hard
disks (now TB)
 CD (700 MB)
 DVD (4.7 GB)
 Blu-ray (25-100 GB)
 USB Flash drive (now 256 GB)
Output
 Monitor
Display
 Quality
 Resolution
 number
of pixels per square inch
1024 by 768
 Dot pitch
 E.g.
 amount
of space between pixels
Dot pitch
Communication
 Modem


Uses a phone line
Speed = 56,000 bps (bits per second)
 DSL


Uses phone line
20 times faster than modem
 Cable



modem
Uses TV cable line
Speed same as DSL
NIC


Used in local area networks
E.g. 10BaseT has speed of 10 Mbps
Operating System
Controls
all programs
Manages hardware resources
Examples
Windows
Ubuntu
MacOS
7
Class Issues
Textbook
 Lab sections
 Mailing List

W. 8-9 (3-5 pm) W. 10-11 (5-7pm)
 Th. 2-3 (3-5 pm)  Th. 10-11 (5-7 pm)
 any section  W. 2-3

Programming Languages Basics
Programming Languages
 Every
operating system, application and
mobile app has been written in some
programming language
Popular High level Languages
There
are many programming languages
COBOL (1959)
 FORTRAN (1957)
 BASIC (1964)
 Visual Basic (1991)
 Pascal (1970)
 Delphi (1986)
 Ada (1980)
 C (1972)
 C++ (1983)
 Objective C (1983)
 C# (2001, a Java-like language by Microsoft)
…

Java

We will be using Java (1991)
Duke: Java’s Mascot
Some Java Programs

Some famous applications written in Java
Mars exploration rover
 Hubble telescope
 Vuze
 Minecarft
 Android (mostly)

Programming Languages


High level language (Java)
Assembly Language
Machine language
Difficulty of programming

• High level languages
• Assembly Language
• Machine Language
Machine Language
 Machine language
 The language of CPU
 Each CPU family has its own instruction set
 Patterns of bits corresponding to different commands
 E.g. To add two numbers instruction:

1101101010011010
Assembly Language
 Assembly language
 Easier than machine language
 E.g.

To add two numbers instruction:
mov ax, a
add ax, b
mov c, ax
 Yet
not so easy!
Assembler
Assembly
Code
mov ax, a
add ax, b
mov c, ax
Machine
Language
Code
0000111
0000101
1100011
0011111
1110000
High level Languages
 High level languages
 Easiest to program
 English like syntax
 E.g. To add two numbers in Java

c = a + b;
Library Code
Machine
Language
Code
High Level
Source Code
Compiler
Machine
Language
Code
Linker
Example Program
//This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
Run
Run Program
1.
Compile:

javac Welcome.java
Run
2.


java Welcome
Caution: no .class at the end of Welcome!
Java Basics
Why Java?
Allows
you to develop and deploy applications on
Internet for servers
 desktop computers
 small hand-held devices

Why Java?

Applets
Java History

Developed by James Gosling at Sun Microsystems
 First
named Oak
 Later
named Java 1995
 HotJava

The first Java-enabled Web browser
 Write
once, run anywhere
Some Terms
 Java


= Language specification + API
Specification: technical definition of the language
(semantic + syntax)
API: contains predefined libraries for developing java
programs
 JVM
= Java Virtual Machine
 JDK = Java Development Kit
 IDE = Integrated Development Environment
Java IDE
 Makes
writing & managing large scale programs
 Eclipse
Open Source by IBM
easier
 NetBeans Open Source by Oracle
 BlueJ
…
JDK Versions
 JDK
= Java Development Kit
 JDK 1.02 (1995)
 JDK 1.1 (1996)
 JDK 1.2 (1998)
 JDK 1.3 (2000)
 JDK 1.4 (2002)
 JDK 1.5 (2004) a. k. a. JDK 5 or Java 5
 JDK 1.6 (2006) a. k. a. JDK 6 or Java 6
 JDK 1.7 (2010) a. k. a. JDK 7 or Java 7
JDK Editions
1.
2.
3.
Java Standard Edition (Java SE)
 client-side standalone applications or applets.
Java Enterprise Edition (Java EE)
 server-side applications such as Java servlets and
Java Server Pages (JSP).
Java Micro Edition (Java ME).
 applications for mobile devices
We use Java SE.
Java is
 Simple
 Object-Oriented
 Distributed
 Robust
 Secure
 Architecture-Neutral
 Portable
 Performance
Agenda
 Homework

1 is posted
Due next Friday
 Programming

assignment 1 is posted
Due next Friday
 Source
code available on website
 No class on Monday (Holiday)
Last Class
 Programming
languages
 Java
 Simple


program
javac nameOfSourceFile.java
java nameOfClassFile
Example Program
//This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
Run
Programming in Java
Today



Object Oriented Programming ( OOP)
main method
Programming syntax
OOP
 Object



Oriented Programming ( OOP)
A software methodology
Great flexibility, modularity, clarity, and reusability
Uses “encapsulation”, “inheritance”, and “polymorphism”
 Everything
is Java is an “Object”
Object
 Real

world objects
Dog, TV set, desk, etc.
 Every

object has “state” and “behavior”
Dog object
 State:
name, color, breed, hungry
 Behavior: barking, fetching, wagging tail
 In


OOP
State = “field”
Behavior = “method”
OOP
 E.g.

Dog class
class Dog { ...description of a dog goes here... }
 Each



object (class) has
State (Properties or data fields)
Behavior (methods)
Can have instances
Instances
Dog
-
max
2
30
Bark()
Eat ()
Class
Name
Properties
Methods
Dog
-
Name
Age
Weight
Bark()
Eat ()
Dog
-
cooper
3.5
34
Bark()
Eat ()
Class Example
 Here

is another example of a class:
class Window { ... }
 Here
are some examples of Windows:
Class Definition
 Example:
class Dog {
String name;
int age;
...rest of the class...
}
Data usually goes first in a class
Class
Class Definition
 Example:
class Dog {
…
Class
Methods usually
go after the data
void bark() {
System.out.println("Woof!");
}
}
A
class may contain methods that describe the
behavior of objects
First Program
Run
Packages
 Groups
related classes in the same category
 How to declare a class is part of a package?
package packagename;
package MyMathPackage;
 Unique
name
 Hierarchal
package book.chapter1;
Packages
 Many





packages in Java API
javax.swing
java.lang
java.util
Java.net
…
 How
to use a package?
import packagename;
import book.chapter1.Welcome;
Only “Welcome”
class is imported.
import book.chapter1.*;
All classes in
chapter1
imported.
Run Program
Create/Modify Source Code
Source code (developed by the programmer)
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
Byte code (generated by the compiler for JVM
to read and interpret, not for you to understand)
…
Method Welcome()
0 aload_0
…
Method void main(java.lang.String[])
0 getstatic #2 …
3 ldc #3 <String "Welcome to
Java!">
5 invokevirtual #4 …
8 return
Saved on the disk
Source Code
Compile Source Code
i.e., javac Welcome.java
If compilation errors
stored on the disk
Bytecode
Run Byteode
i.e., java Welcome
Result
If runtime errors or incorrect result
JVM

Usually: the source program must be recompiled on
another machine


Because the object program can only run on a specific
machine.
With Java:


Compile the source program into bytecode.
The bytecode can then run on any computer

Which has Java Virtual Machine (JVM)


A software that interprets Java bytecode.
JVM


Class loader
Bytecode verifier
Java Bytecode
Java Virtual
Machine
Any
Computer
Program Detail
Enter main method
//This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
More Examples
Welcome1
Run
ComputeExpression
Run
Configuration

Set path to JDK bin directory

set path=c:\Program Files\java\jdk1.7.0\bin
Set classpath to include the current
directory


set classpath=.
Programming Constructs
Program Anatomy
Comments
 Reserved words
 Modifiers
 Statements
 Blocks
 Classes
 Methods
 The main method

Comments

3 types of comments in Java
Line
comment //
Example

// This is a line comment!
Paragraph
comment /* */
Example

/* This is a paragraph comment.
It includes multiple lines. */

JavaDoc comments (automatic
documentation) /** */
JavaDoc


JavaDoc comments (automatic
documentation) /** */
Tags starting with @
Reserved Words



Reserved words (or keywords)
Specific meaning to the compiler
Cannot be used for other purposes

Keywords in our previous examples




class
public
static
void
Modifiers



A subset of keywords
Specify the properties of the data,
methods, and classes and how they can
be used.
Examples we have seen



public
static
Other example modifiers




private
final
abstract
protected
Statement



Represents an action or a
sequence of actions
Every statement in Java ends with a
semicolon ;
Examples
System.out.println("Hello World!");
a = 8 * 3.14;
Blocks
A pair of braces in a program
grouping components of a
program

public class Test {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
Class block
Method block
Class

The essential Java construct

A template or blueprint for objects
A program is defined by using one
or more classes.

Method



A collection of statements that
performs a sequence of
operations
Might accept input
Might provide output
Input
1
Input
2
Input
3
Output
Method

What does it look like?

Example
public returnType name(input1, input2, …)
{
// Statements;
}
Method

What does it look like?

Example
Modifier
Return
Value
name
Input
public int addTwoNumbers(int a, int b)
{
int c = a + b;
return c;
}
Method

What does it look like?

Example
Modifier Modifier
Return
Value
name
Input
public static void main(String[] args)
{
// Statements;
}
Graphical User Interface
Graphical User Interface (GUI)
 Use the showMessageDialog
method in JOptionPane class.
 JOptionPane is one of the many
predefined classes in the Java
system, which can be reused
rather than “reinventing the
wheel.”

Graphical User Interface

Graphical User Interface (GUI)

Example
WelcomeInMessageDialogBox
Run
Graphical User Interface

Graphical User Interface (GUI)

Example
JOptionPane.showMessageDialog (null,
"Welcome to Java!",
"Display Message",
JOptionPane.INFORMATION_MESSAGE);
Graphical User Interface
Several ways to use showMessageDialog

method.
1.
Use a statement as shown in the example:
JOptionPane.showMessageDialog(null, x, y,
JOptionPane.INFORMATION_MESSAGE);
where x is a string for the text to be displayed,
and y is a string for the title of the message
dialog box.
2.
The other is to use a statement like this:
JOptionPane.showMessageDialog(null, x);
where x is a string for the text to be displayed.