Introduction To Computers and Java
Introduction To Computers and Java
and Java
Module 1
Objectives
overview computer hardware and software
introduce program design and object-oriented
programming
overview the Java programming language
Hardware and Software
Computer systems consist of hardware and
software.
Hardware includes the tangible parts of computer
systems.
Software includes programs - sets of instructions
for the computer to follow.
Familiarity with hardware basics helps us
understand software.
Hardware and Memory
Most modern computers have similar
components including
input devices (keyboard, mouse, etc.)
output devices (display screen, printer, etc.)
a processor
two kinds of memory (main memory and auxiliary
memory).
The Processor
also called the CPU (central processing unit)
or the chip (e.g. Pentium processor)
The processor processes a programs
instructions.
It can process only very simple instructions.
The power of computing comes from speed
and program intricacy.
Memory
Memory holds
programs
data for the computer to process
the results of intermediate processing.
two kinds of memory
main memory
auxiliary memory
Main memory
working memory used to store
the current program
the data the program is using
the results of intermediate calculations
usually measured in megabytes (e.g. 256
megabytes of RAM)
RAM is short for random access memory
a byte is a quantity of memory
Auxiliary Memory
also called secondary memory
disk drives, diskettes, CDs, etc.
more or less permanent (nonvolatile)
usually measured in gigabytes (e.g. 50
gigabyte hard drive)
Bits, Bytes, and Addresses
A bit is a digit with a value of either 0 or 1.
A byte consists of 8 bits.
Each byte in main memory resides at a
numbered location called its address.
Addresses
Storing Data
Data of all kinds (numbers, letters, strings of
characters, audio, video, even programs) are
encoded and stored using 1s and 0s.
When more than a single byte is needed,
several adjacent bytes are used.
The address of the first byte is the address of the
unit of bytes.
Files
Large groups of bytes in auxiliary memory are
called files.
Files have names.
Files are organized into groups called
directories or folders.
Java programs are stored in files.
Programs files are copied from auxiliary
memory to main memory in order to be run.
0s and 1s
Machines with only 2 stable states are easy
to make, but programming using only 0s and
1s is difficult.
Fortunately, the conversion of numbers,
letters, strings of characters, audio, video,
and programs is done automatically.
Programs
A program is a set of instructions for a
computer to follow.
We use programs almost daily (email, word
processors, video games, bank ATMs, etc.).
Following the instructions is called running or
executing the program.
Input and Output
Normally, a computer received two kinds of
input:
the program
the data needed by the program.
The output is the result(s) produced by
following the instructions in the program.
Running a Program
javac HelloWorld.java
java HelloWorld
Hello, world!
Line-by-line Analysis
The first line of the class,
public class HelloWorld {
declares a Java class named HelloWorld.
declared public - available to any other class.
public static void main(String[] args) {
begins a Java method named main. Java will start
executing the program from main method.
args is a method parameter
The method must be both public and static
No alternatives to: static, args in this course
Sum of 2
numbers
class
FirstProgram
Some Terminology
The person who writes a program is called
the programmer.
The person who interacts with the program is
called the user.
A package is a library of classes that have
been defined already.
import java.util.*
Some Terminology, cont.
The item(s) inside parentheses are called
argument(s) and provide the information
needed by methods.
A variable is something that can store data.
an instruction to the computer is called a
statement; it ends with a semicolon.
The grammar rules for a programming
language are called the syntax of the
language.
Printing to the Screen
System.out.println (Whatever you want to
print);
System.out is an object for sending output to
the screen.
println is a method to print whatever is in
parentheses to the screen.
The object is said to invoke or call the method
using.
objectName.methodName(argumentsTheMethodNeeds);
Compiling a Java Program or
Class
A Java program consists of one or more
classes, which must be compiled before
running the program.
You need not compile classes that
accompany Java (e.g. System and Scanner).
Each class should be in a separate file.
The name of the file should be the same as
the name of the class.
Compiling and Running
Use an IDE (integrated development
environment) which combines a text editor
with commands for compiling and running
Java programs.
When a Java program is compiled, the byte-
code version of the program has the same
name, but the ending is changed from .java
to .class.
Compiling and Running, cont.
A Java program can involve any number of
classes.
The class to run will contain the words
public static void main(String[] args)
near the beginning of the file.
Object-Oriented Programming
Our world consists of objects (people, trees,
cars, cities, airline reservations, etc.).
Objects can perform actions which effect
themselves and other objects in the world.
Object-oriented programming (OOP) treats a
program as a collection of objects that
interact by means of actions.
OOP Terminology
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 its own data values.
OOP Design Principles
OOP adheres to three primary design
principles:
encapsulation
polymorphism
inheritance.
Introduction to 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.
Encapsulation often is called information hiding.
Accessibility Example
An automobile consists of several parts and
pieces and is capable of doing many useful
things.
Awareness of the accelerator pedal, the brake
pedal, and the steering wheel is important to the
driver.
Awareness of the fuel injectors, the automatic
braking control system, and the power steering
pump is not important to the driver.
Introduction to Polymorphism
from the Greek meaning many forms
The same program instruction adapts to
many different things in different contexts.
A method name, used as an instruction, produces
results that depend on the class of the object that
used the method.
everyday analogy: take time to recreate causes
different people to do different activities
more about polymorphism later
Introduction to Inheritance
Classes can be organized using inheritance.
Introduction to Inheritance,
cont.
A class at lower levels inherits all the
characteristics of classes above it in the
hierarchy.
At each level, classifications become more
specialized by adding other characteristics.
Higher classes are more inclusive; lower
classes are less inclusive.
Inheritance in Java
used to organize classes
Inherited characteristics do not need to be
repeated.
New characteristics are added.
more about inheritance later
Number Systems
The base value of a number system determines
The number of symbols in the system
The place value of each digit
Decimal (base 10)
Symbols (10): 0,1,2,....9
Place: ..,104,103,102,101,100
132: (1x102)+(3x101)+(2x100)
Number Systems
Binary (base 2)
Symbols (2): 0,1
Place: ..,23,22,21,20
To convert decimal 132 to binary:
(1x27)+(0x26)+(0x25)+(0x24)+(0x23)+(1x22)+(0x21)+(0
x20) or 100001002
Number Systems
To convert from any base to decimal
1. Write the number in its expanded form
2. Sum each term
10012
(1x23) + (0x22) + (0x21) + (1x20)
8+0+0+1=9
Number Systems
To convert from decimal to any base
1. Write the place representation for the base using 1 as
the multiplier for each term
2. Calculate the value of each term
3. Determine the number required for each term
Decimal (5710) to binary
(1x26) + (1x25) + (1x24) + (1x23) + (1x22) + (1x21) +
(1x20)
64 32 16 8 4 2 1
0 1 1 1 0 0 1
5710 = 1110012