1.
Introduction
[ECE20016/ITP20003] Java Programming
Handong Global University
Agenda
■ Computer Basics
■ The First Java Application
■ Programming Basics
■ Graphics Supplement
Handong Global University 2
Computer
■ Composed of …
■ Input devices (keyboards, mouse, camera, mic,…)
■ Output devices (monitor, printer, speaker, …)
■ Storages (HDD, SSD, flash memory, CD/DVD, …)
■ CPU, main memory, controller, …
Handong Global University 3
CPU and Memory
■ CPU - carries out only very simple instructions
■ Moving data from one place in memory to another
■ Performing some basic arithmetic (+, -, …)
Cf. program: a sequence of instructions to accomplish a task
■ Main memory (RAM) – stores data and instructions
■ Volatile
■ Fast
■ Smaller and more expensive than auxiliary memory
■ The only storage that CPU can access directly.
Handong Global University 4
Main Memory
■ Main memory consists of a long list of numbered bytes.
■ All kinds of data are stored as a series of bits or bytes.
■ The location of a byte is called its address.
■ The address of other memory unit, i.e. WORD(2bytes) or
DWORD(4bytes), is the address of the starting byte.
Handong Global University 5
Programs
■ Program: a sequence of instructions for a computer to
follow.
■ Execution of program
■ Program is executed by computer (+ OS)
■ Program takes input and produces output
Handong Global University 6
Programming Languages
■ Primitive programming languages
■ Machine language - a sequence of machine instructions
□ Machine instruction: primitive instructions CPU can run.
■ Assembly language – a sequence of assembly instruction
□ Assembly instruction: symbolic representation of machine
instruction
□ Needs translation into machine language (assembler)
■ High-level programming languages
■ Human-friendly language to describe the things the computer
should do.
■ Only for human (cannot be executed on computer)
➔ Needs translation into machine language code.
(interpreter/compiler)
Handong Global University 7
Interpreter and Compiler
■ Interpreter - translates and executes each command
alternatively
■ Translates every time the program runs.
■ Interactive
■ Compiler - translates the whole (or a part of) program
into machine code (exceptions: Java, C#, …)
■ Compile once execute often.
■ Fast
Handong Global University 8
Creating and Running C Programs
■ Link
■ Integrating objects and library modules required to execute
Notice! a program can be distributed in multiple source files.
Source 1 Object 1
Source 2 Compil Object 2
ing
…
… Linki Executable
Source n Object n ng
Library
Modules
(printf, scanf,…)
Handong Global University 9
Java Bytecode
■ Java compiler translates Java program into bytecode
rather than machine language.
■ Bytecode: machine language of a hypothetical computer
known as a virtual machine, called JVM.
■ Intermediate form between Java program and machine code.
■ Easy to interpret
Java Byte-c Machine
program ode code
(.java) (.class)
for human for machine
Handong Global University 10
Java Virtual Machine (JVM)
■ JVM interprets bytecode (translation + execution)
■ JVM provides platform-independent environment.
■ There exists JVMs for various H/W’s and OS’s
■ Java bytecode can run on any JVM.
JVM
OS
H/W
Handong Global University 11
Compiling and Running Java
Handong Global University 12
Java Virtual Machine (JVM)
■ JVM provides great portability.
“Compile once, run everywhere!”
Handong Global University 13
Applications and Applets
■ Application: regular program.
■ Run on your computer
□ H/W + OS + VM
■ Applet
■ Sent to another location on the Internet and run there.
□ H/W + OS + VM + Web browser
Handong Global University 14
Agenda
■ Computer Basics
■ The First Java Application
■ Programming Basics
■ Graphics Supplement
Handong Global University 15
The First Java Application Program
import java.util.Scanner;
public class FirstProgram
{
public static void main (String [] args)
{
System.out.println ("Hello out there.");
System.out.println ("I will add two numbers for you.");
System.out.println ("Enter two whole numbers on a line:");
int n1, n2;
Scanner keyboard = new Scanner (System.in);
n1 = keyboard.nextInt ();
n2 = keyboard.nextInt ();
System.out.println ("The sum of those two numbers is");
System.out.println (n1 + n2);
}
}
Handong Global University 16
The First Java Application Program
■ Result
Handong Global University 17
The First Java Application Program
Handong Global University 18
The First Java Application Program
■ import java.util.Scanner;
■ Tells the compiler that this program uses the class Scanner.
■ class FirstProgram
public class FirstProgram
{
...
}
■ The main method
public static void main(String[] args)
{
...
}
Handong Global University 19
The First Java Application Program
■ System.out.println()
■ Displays what is shown in parentheses
■ System.out is an object used to send output to the screen
■ println is the method that performs this action for the object
System.out.
■ int n1, n2; // variable declaration
■ variable: a memory space with a name to store a piece of data.
■ int: data type (integer)
■ n1, n2: variable names
Handong Global University 20
The First Java Application Program
■ Scanner keyboard = new Scanner(System.in);
■ Prepares to read from the keyboard
■ System.in is an object used to read input to the keyboard
■ n1 = keyboard.nextInt();
■ n2 = keyboard.nextInt();
■ Reads integer numbers from the keyboard
Handong Global University 21
Writing a Java Program
■ A Java program is composed of smaller parts, called
classes
■ In the code, we use three classes: FirstProgram, System,
Scanner
■ Each class should be in a separate file with the same filename.
Ex) FirstProgram.java
■ Writing a Java program = writing classes
■ Design the whole program
■ Decompose it into classes
■ Implement each class
Handong Global University 22
Compile and Running a Java Program
■ Compile and Running with JDK (Java Development Toolkit)
■ Compiler + JRE (incl. JVM)
cf: JRE: Java Runtime Environment (JVM + built-in classes + α)
■ Compile: javac FirstProgram.java
■ Run: java FirstProgram
➔ JDK should be installed, and its bin directory should be in PATH.
■ IDE (Integrated Development Environment)
■ Editor + compiler + runtime + debugger + …
Ex) Eclipse, NetBeans, …
■ Background compile
■ Run
□ Menu->Run->Run As->Java Application
□ Menu->Run->Run
□ CTRL-F11
Handong Global University 23
Agenda
■ Computer Basics
■ The First Java Application
■ Programming Basics
■ Graphics Supplement
Handong Global University 24
Object-Oriented Programming
■ Java is an object-oriented programming language,
abbreviated OOP.
■ OOP is a technique that experienced programmers have found
to be extremely helpful.
■ The world is made up of objects.
Ex) people, automobiles, buildings, …
■ Object-oriented programming (OOP) treats a program
as a collection of objects that interact by means of
actions.
Handong Global University 25
Object-Oriented Programming
■ Objects, appropriately, are called objects.
■ Actions are called methods.
■ Objects of the same kind have the same type and
belong to the same class.
■ Objects within a class have a common set of methods and the
same kinds of data
■ But each object can have it’s own data values.
Handong Global University 26
Class, Object, and Methods
■ Class: a type of entities
Ex) Sonata, Genesis, Galaxy Note, i-Pad…
■ Object: a specific entity
Ex) my Sonata (with a specific VIN and plate number)
■ Method: an action an object can perform
Ex) Sonata has go, stop, left_turn, right_turn, …
■ Attribute: component that constructs an object
■Also called fields, member variable, data member, …
Ex) body, engine, wheel, tire, chair, door, trunk, …
Handong Global University 27
OOP Design Principles
■ OOP adheres to three primary design principles:
■ Encapsulation
■ Polymorphism
■ Inheritance
Handong Global University 28
Encapsulation
■ The data and methods associated with any
particular class are encapsulated (“put together
in a capsule”), but only part of the contents is
made accessible.
■ Encapsulation provides a means of using the class,
but it omits the details of how the class works.
Ex) accelerator pedal, brake pedal, steering wheel, …
■ Encapsulation often is called information hiding.
Ex) fuel injectors, automatic braking control system, power steering
pump, …
Handong Global University 29
Polymorphism
■ From the Greek meaning “many forms”
■ The same program instruction adapts to mean different
things in different contexts.
■ A method name produces results that depend on the class of
the object that used the method.
Ex) ‘go’ method of an automobile vs. ‘go’ method of an airplane.
Handong Global University 30
Inheritance
■ Classes can be organized using inheritance.
■ ‘is a’ relation
■ A class at lower levels inherits all the characteristics of
classes above it in the hierarchy.
■ Inherited characteristics do not need to be repeated.
■ New characteristics are added.
Handong Global University 31
Inheritance in Java
■ Used to organize classes
■ New characteristics are added.
Handong Global University 32
Algorithms
■ An algorithm describes a means of performing an action.
■ Algorithm = a series of actions
cf. program = a series of instructions (or commands)
■ An abstracted form of program.
■ For human, not machine
■ Once an algorithm is defined, expressing it in Java (or in
another programming language) usually is easy.
■ An algorithm must be expressed completely and precisely.
■ Algorithms usually are expressed in pseudocode.
Handong Global University 33
Example: Total Cost of All Items
■ Write the number 0 on the
whiteboard.
■ For each item on the list
■ Add the cost of the item to the number
on the whiteboard
■ Replace the number on the whiteboard
with the result of this addition.
■ Announce that the answer is the
number written on the whiteboard.
Handong Global University 34
Reusable Components
■ Most programs are created by combining existing
components.
■ Programs NOT usually created entirely from scratch.
■ Reusing components saves time and money.
■ Reused components are likely to be better developed, and
more reliable.
■ New components should be designed to be reusable by other
applications.
■ Java provides many classes
http://docs.oracle.com/javase/7/docs/api/
Handong Global University 35
Java Platform API
It moved to http://docs.oracle.com/javase/7/docs/api/
Description of
class Scanner
Package names
Class names
Handong Global University 36