csc 110 introduction to computer science for majors summer 2009 introduction to computers and java 1

53
INTRO TO COMPUTERS & JAVA CSC 110 Introduction to Computer Science for Majors Summer 2009 Introduction to Computers and Java 1

Upload: nathan-perkins

Post on 25-Dec-2015

220 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: CSC 110 Introduction to Computer Science for Majors Summer 2009 Introduction to Computers and Java 1

Introduction to Computers and Java1

INTRO TO COMPUTERS & JAVA

CSC 110 Introduction to

Computer Science for

MajorsSummer 2009

Page 2: CSC 110 Introduction to Computer Science for Majors Summer 2009 Introduction to Computers and Java 1

Hardware examples>> 2

Computer Basics

Computer system: Hardware plus softwareHardware: The physical components

◦Examples?Software: Programs (i.e. the instructions

that tell the hardware what to do)◦Examples?

Page 3: CSC 110 Introduction to Computer Science for Majors Summer 2009 Introduction to Computers and Java 1

More hardware examples>> 3

HARDWARE

ComputersMonitorsKeyboardsPDA’sServersCell phones (telephones)SmartCards

Page 4: CSC 110 Introduction to Computer Science for Majors Summer 2009 Introduction to Computers and Java 1

Hardware configuration>> 4

HARDWARE

Floppy and hard drivesPrintersVideo camsBackup devicesScannersModems

Page 5: CSC 110 Introduction to Computer Science for Majors Summer 2009 Introduction to Computers and Java 1

CPU>> 5

TYPICAL HARDWARE CONFIGURATION

KeyboardMonitorChassis (Tower)

◦cpu (processor)◦memory (main and

auxiliary)◦disk drives◦i/o connectors◦cards (i.e. network)

Page 6: CSC 110 Introduction to Computer Science for Majors Summer 2009 Introduction to Computers and Java 1

Memory>>6

CPU (Central Processing Unit)

The computer’s microprocessor chip

The “brain” consists of several million transistors

Pentium is one of better known chips

Carries out “simple” instructions, and can add and subtract

The design is simple

Page 7: CSC 110 Introduction to Computer Science for Majors Summer 2009 Introduction to Computers and Java 1

Bits & bytes>> 7

Two Kinds of Memory

Main or random access memory (RAM)◦ used when computer is running a program◦ i.e. 256 meg of RAM◦ temporarily stores program and working data while

processing Auxiliary (secondary)

◦ floppy & hard disk drives, CDs, tape, USB keychains◦ permanent storage for programs and data◦ used to swap back and forth between RAM

Page 8: CSC 110 Introduction to Computer Science for Majors Summer 2009 Introduction to Computers and Java 1

Base 2 numbering system>> 8

Memory

Bits◦0’s or 1’s, on or off, true or false◦simply a state

Bytes◦8 bits◦main memory holds one byte, at a

numbered address

Page 9: CSC 110 Introduction to Computer Science for Majors Summer 2009 Introduction to Computers and Java 1

Main memory organization>>9

Bits and Bytes

Byte 01001101 = 77◦base 2 numbering system◦all one’s = 255◦adding zero = 256

1 2 4 8 16 32 64 128

2 2 2 2 2 2 2 2

1 0 1 1 0 0 1 101234567

Page 10: CSC 110 Introduction to Computer Science for Majors Summer 2009 Introduction to Computers and Java 1

Auxilliary memory organization>> 10

Main Memory Organization

Byte - Addressable◦ Main memory is

a list of numbered locations that contain one byte of data in each location

◦ A0000 to FFFFF is typical “look”

Number of bytes per data item may vary

Address Data Byte

3021 1111 0000 Item 1: 2 bytes stored

3022 1100 1100

3023 1010 1010 Item 2: 1 byte stored

3024 1100 1110 Item 3: 3 bytes stored

3025 0011 0001

3026 1110 0001

3027 0110 0011 Item 4: 2 bytes stored

3028 1010 0010

3029 … Next Item, etc.

Page 11: CSC 110 Introduction to Computer Science for Majors Summer 2009 Introduction to Computers and Java 1

Aux memory organization cont'd>> 11

Auxiliary Memory Organization

A u xilia ry M em ory O rg an iza tion

F iles

F iles

F iles

F iles

S u b d irec to ry

S u b d irec to ry F iles

S u b d irec to ry

F iles

S u b d irec to ry

S u b d irec to ry S u b d irec to ry

M ain (R oot) D irec to ry / F o ld er

Page 12: CSC 110 Introduction to Computer Science for Majors Summer 2009 Introduction to Computers and Java 1

h: drive>> 12

Auxiliary Memory Organization

Files◦bytes grouped in larger units when stored on a

floppy◦has a name w/file extension and can store data◦a java program is stored as a file

(i.e. .java or .class)Directories (i.e. Folders)

◦called both◦files are often grouped in a folder

Page 13: CSC 110 Introduction to Computer Science for Majors Summer 2009 Introduction to Computers and Java 1

SOFTWARE>> 13

h: drive

Create foldersCSC110CSC110/MinilabsCSC110/Programs

Page 14: CSC 110 Introduction to Computer Science for Majors Summer 2009 Introduction to Computers and Java 1

Types>> 14

SOFTWARE - A program that runs (or executes)

Program (mortgage calculator)

ComputerInput

(principle and interest data)

Output(monthly payment)

A Program such as Java, is a set of instructions for a computer to follow:

Page 15: CSC 110 Introduction to Computer Science for Majors Summer 2009 Introduction to Computers and Java 1

Java programs>> 15

Many Types of ProgramsOperating System

◦ DOS, MS- Windows(3.x, 95, 98, NT, XP), Mac OS, UNIXExisting applications

◦ word processors◦ web browsers◦ compilers and assemblers◦ virus protection

User-created applications◦ Java applet for a web site

Page 16: CSC 110 Introduction to Computer Science for Majors Summer 2009 Introduction to Computers and Java 1

A taste of Java>>16

Java ProgramsApplets

◦Small, executable program embedded in a web page

◦Little application◦Sent over Internet, run on client, needs HTML

Applications◦ Installed and run on a computer like any

program◦Larger (i.e. Student Information System)◦One line of code determines application/applet

Page 17: CSC 110 Introduction to Computer Science for Majors Summer 2009 Introduction to Computers and Java 1

Java history>> 17

A Taste of Java!What’s in a name?

◦Sun Microsystems and Project “Green” 1991◦Once developed, called “Oak,” a name already

used◦Java! Marketing scheme, or an accident at a

café?◦Technology puns include JavaBeans, Café,

Mocha (a Java “decompiler” tool)

Page 18: CSC 110 Introduction to Computer Science for Majors Summer 2009 Introduction to Computers and Java 1

Java features>> 18

A Taste of Java!

History◦Intermediate Language for appliances

(compilers are too expensive) in 1991◦1994 first “written-in-Java” web-browser called

HotJava (Note: Mosaic was first browser)◦1995 concept of an interactive web, Java

allowed small programs (applets) to be embedded in the page

Page 19: CSC 110 Introduction to Computer Science for Majors Summer 2009 Introduction to Computers and Java 1

cont'd>>19

Java FeaturesFamiliar, simple, object-oriented

◦Basic syntax modeled after C◦Omits pointers and memory management◦Object interactivity rather than procedural

processingRobust

◦Simplicity reduces errors◦Runs without crashing◦Provides exception handling for unexpected

events

Page 20: CSC 110 Introduction to Computer Science for Majors Summer 2009 Introduction to Computers and Java 1

cont'd>>20

Java Features

Secure◦Implements measures for network

transmission◦JVM unbreakable

Platform independence◦Architecture neutral, portable

Page 21: CSC 110 Introduction to Computer Science for Majors Summer 2009 Introduction to Computers and Java 1

Compiling a Java program>>21

Java FeaturesHigh performance

◦Balancing act between platform independence and performance

◦Multitasking (threads) allows users to input data with graphics executing in the background

◦Communication delays are generally accepted on a web page

Page 22: CSC 110 Introduction to Computer Science for Majors Summer 2009 Introduction to Computers and Java 1

Explanation of code>> 22

A Sample Java Programpublic class FirstProgram {

public static void main(String[] args) {

System.out.println("Hello out there.");

System.out.println("Want to talk some more?");

System.out.println("Answer y for yes or n for no.");

char answerLetter;

answerLetter = SavitchIn.readLineNonwhiteChar();

if (answerLetter == 'y')

System.out.println("Nice weather we are having.");

System.out.println("Good-bye.");

System.out.println("Press enter key to end...");

String junk;

junk = SavitchIn.readLine();

}

}

Page 23: CSC 110 Introduction to Computer Science for Majors Summer 2009 Introduction to Computers and Java 1

cont'd>> 23

Lab & Reinforcement of Code ...Create Program in TextPad

Code to begin the program (to be explained later):

public class FirstProgram { public static void main(String[ ] args) { }}

Java applications all have similar code at the beginning◦ The name of the class differs from one program to another.◦ Other information about the class might also be included on

the first line.Save as FirstProgram.java in ClassTonight folder!!!

Note: Type in “exactly” as we explain the code.

Page 24: CSC 110 Introduction to Computer Science for Majors Summer 2009 Introduction to Computers and Java 1

cont'd>> 24

Explanation of Code ... Code to display a text string:System.out.println("Hello out there.");System.out.println("Want to talk some more?"); System.out.println("Answer y for yes or n for no.");

◦ Note the “dot” operator◦ System.out is an object◦ println is a method that it carries out◦ double-quoted text inside the parentheses is an argument to

the method◦ general syntax: Object_Name.Method_Name(Arguments)

Page 25: CSC 110 Introduction to Computer Science for Majors Summer 2009 Introduction to Computers and Java 1

cont'd>> 25

… Explanation of Code ...

Code to create a variable named answerLetter to contain a single character of data:char answerLetter;

This variable is used to store the user’s response.

Page 26: CSC 110 Introduction to Computer Science for Majors Summer 2009 Introduction to Computers and Java 1

cont'd>> 26

… Explanation of Code ... Read a character typed in from the keyboard and store it in the

variable answerLetter: answerLetter = SavitchIn.readLineNonwhiteChar();

◦ SavitchIn is a class used for obtaining input from the keyboard◦ readLineNonwhiteChar() is a method that reads a single, non-

blank character from the keyboard and discards any remaining characters on the line.

◦ the equal sign is not the same as in math; it means “assign the value on the right to the variable on the left;” in this case, store the value read from the keyboard into the variable answerLetter

Be sure to place copy of SavitchIn.class in folder

Page 27: CSC 110 Introduction to Computer Science for Majors Summer 2009 Introduction to Computers and Java 1

cont'd>> 27

… Explanation of Code ...

Question: If “=“ means “assign the value of the expression on the right to the variable on the left,” how do we indicate “equals”?

Answer: use a double equals (“==“)

Example: check to see if the character entered is ‘y’:if (answerLetter == 'y')◦ the value inside the parentheses will be True if the

letter ‘y’ was typed in, otherwise it will be False (if any other letter was typed in)

Page 28: CSC 110 Introduction to Computer Science for Majors Summer 2009 Introduction to Computers and Java 1

cont'd>> 28

… Explanation of Code ...

Code to display the line “Nice weather we are having.” if the user entered the character ‘y’:if (answerLetter == 'y') System.out.println("Nice weather we are having.");◦ Note that the line will not be printed if any letter other than ‘y’ is

entered.

Unconditionally display the line “Good-bye.”:System.out.println("Good-bye.");

◦ only the previous System.out.println is conditionally printed, depending on the value entered; the next instruction is executed regardless of the value entered.

Page 29: CSC 110 Introduction to Computer Science for Majors Summer 2009 Introduction to Computers and Java 1

Syntax rules>> 29

… Explanation of Code

Code to prevent the display from scrolling off the screen before you can read it:

System.out.println("Press enter key to end program.");String junk;junk = SavitchIn.readLine();

◦ junk is a variable that can contain a string of characters.◦ readLine() is a method to read in an entire line of text.◦ The program halts until a character is entered.◦ Any character entered will make the program continue.◦ The character entered is assigned to the variable junk, but

is ignored (it is not used).◦ There are no more lines of code, so the program terminates.

Page 30: CSC 110 Introduction to Computer Science for Majors Summer 2009 Introduction to Computers and Java 1

Good programming practice>> 30

Syntax Rules for IdentifiersIdentifier - the name of something (e.g. a variable, object, or method)

used in a Java program.Identifiers:

◦ cannot use reserved words (e.g. “if,” “for”, etc.) (see App. 1)◦ must contain only letters, digits, and the underscore character, _.◦ cannot have a digit for the first character.

$ is allowed but has special meaning, so do not use it.◦ have no official length limit (there is always a finite limit, but it is

very large and big enough for reasonable names) . ◦ are case sensitive!

junk, JUNK, and Junk are three valid and different identifiers, so be sure to be careful in your typing!

◦ Note that no spaces or dots are allowed.

Page 31: CSC 110 Introduction to Computer Science for Majors Summer 2009 Introduction to Computers and Java 1

Compiling a program>> 31

Good Programming Practice:Identifier Naming Conventions

Always use meaningful names, e.g. finalExamScore, instead of something like x, or even just score.

Use only letters and digits. Capitalize interior words in multi-word names, e.g. answerLetter.

Names of classes start with an uppercase letter.◦ every program in Java is a class as well as a

program. Names of variables, objects, and methods start with a

lowercase letter.

Page 32: CSC 110 Introduction to Computer Science for Majors Summer 2009 Introduction to Computers and Java 1

Running a Java program>> 32

Compiling a Java ProgramAssuming the java compiler is already set up and all the files

are in the same folder (subdirectory): Each class used in a program should be in a separate file The name of the file should be the same as the class except

with “.java” added to it First compile each class definition used in the program

◦ e.g. SavitchIn in the sample program for Sun Microsystems’ JDK (Java Development Kit), typejavac SavitchIn.java

◦ a byte-code file is created with the name SavitchIn.class Next compile the program file:

◦ javac <file>.java (which creates <file>.class)

Page 33: CSC 110 Introduction to Computer Science for Majors Summer 2009 Introduction to Computers and Java 1

OOP>> 33

Running a Java Program at a Command Prompt (& TextPad) Only the class with public static void main(String[] args)can be run◦ the critical word to look for is main

For Sun Microsystems’ JDK (Java Development Kit), typejava <file>◦ <file> is the same name used in the original source file <file>.java

◦ use just <file>; do not use <file>.java or <file>.class

Note that you compile in a separate step andinvoke the Java interpreter and linker when you run the program.

Page 34: CSC 110 Introduction to Computer Science for Majors Summer 2009 Introduction to Computers and Java 1

OOP examples>> 34

Object-Oriented Programming (OOP)

Objects◦ Trees, cars, buildings, people, jobs, traffic lights◦ Nouns◦ Can perform actions (i.e. traffic light turns green, its behavior)◦ Can effect other objects (i.e. cars accelerate, another

behavior)◦ Has data (variables) that determines its state, or its

attributes Methods

◦ The actions an object can take that determines its behavior◦ Verbs (light turns green, car accelerates)

Classes◦ Type of objects◦ Objects of the same kind (type) belong to a class

Page 35: CSC 110 Introduction to Computer Science for Majors Summer 2009 Introduction to Computers and Java 1

State and behavior>> 35

Use Case Example – Math Solutions

Scenario◦ Student enters and requests assistance regarding a math

problem◦ Receptionist summons math tutor with expertise in subject

matter◦ Tutor provides solution to student’s request

Objects◦ Student, receptionist, tutor, math solution◦ Perform actions, effects other objects, has data (information)

Methods◦ Student requests assistance, receptionist summons, tutor

provides Classes

◦ Type of student (or….type of person, i.e. inheritance!), type of tutor, type of solution

Page 36: CSC 110 Introduction to Computer Science for Majors Summer 2009 Introduction to Computers and Java 1

OOP definition>> 36

State and Behavior

State (determined with attributes)◦Student is a MAT 120 (intermediate algebra)

requestor◦Math tutor’s state is algebra expertise

(attribute)◦Determined with associated data

Behavior◦Algebra student makes request (actions)◦Math tutor provides solution (actions)◦Implemented with methods

Page 37: CSC 110 Introduction to Computer Science for Majors Summer 2009 Introduction to Computers and Java 1

OOP design>> 37

OOP Definition

A programming methodology that consists of objects that interact with each other by means of actions.

• Combines the actions (methods), and the data into objects (encapsulation).

• Everything an object knows is captured in its variables (data)

• Everything it can do is expressed in its methods

Page 38: CSC 110 Introduction to Computer Science for Majors Summer 2009 Introduction to Computers and Java 1

Algorithm>> 38

OOP Design Principles Encapsulation

◦ Hides the details of what’s inside the object◦ Sometimes called information hiding◦ Many details about the student, but only the type is

necessary for the receptionist object to interact Polymorphism

◦ Greek term meaning “many forms”◦ Human communication is “polymorphic” (i.e. ask same

question to several people and you will get several answers)

◦ One method instruction (message) causes different actions depending upon object

Inheritance◦ Organizes classes (objects) with common properties in

groups so that they are only defined once

Page 39: CSC 110 Introduction to Computer Science for Majors Summer 2009 Introduction to Computers and Java 1

OOP summary>> 39

Algorithms

A strategy or plan for carrying out an action◦ A set of instructions◦ Complete and precise◦ Can be a diagram, such as a flow chart◦ Usually expressed in English and programming

language as pseudocode◦ Example:1. Print student profile2. Set time (t) to nearest quarter hour3. i.e. time = 15:30;

Page 40: CSC 110 Introduction to Computer Science for Majors Summer 2009 Introduction to Computers and Java 1

Reusable components>> 40

Summary - OOPA different style of programmingMay look “procedural” because of

algorithmic programming on top and bottom layers

OOP objects and classes are used for the middle layers

Focus is on data and interaction between objects, not on algorithm steps

Page 41: CSC 110 Introduction to Computer Science for Majors Summer 2009 Introduction to Computers and Java 1

Testing and debugging>> 41

Reusable Components

Design objects for reusePerson object

◦General◦Do not design for one project◦Specify exactly how this object interacts with

other objects (encapsulate)◦Through inheritance, can be a nurse, doctor,

staff

Page 42: CSC 110 Introduction to Computer Science for Majors Summer 2009 Introduction to Computers and Java 1

cont'd>> 42

Testing and DebuggingBug

◦ A mistake in the program◦ Debugging eliminates bugs in the program

Syntax errors◦ Grammatical mistake (i.e. no semicolon)

Run-time errors◦ Occurs during execution

Logic errors◦ Wrong output with no error message◦ Hardest to troubleshoot

Page 43: CSC 110 Introduction to Computer Science for Majors Summer 2009 Introduction to Computers and Java 1

Programming languages and compilers>>

43

Testing and Debugging

Test program◦Reliable data to measure output◦Can detect errors when changes are made to

code

Page 44: CSC 110 Introduction to Computer Science for Majors Summer 2009 Introduction to Computers and Java 1

Machine language>>44

Programming Languages & Compilers

Page 45: CSC 110 Introduction to Computer Science for Majors Summer 2009 Introduction to Computers and Java 1

Assembly language>> 45

The highs and lows of programming languages ...

High-Level Language (HLL)◦ natural language◦ words, numbers, and

math symbols◦ not directly understood

by hardware◦ “portable” source code

(hardware independent)

◦ Java, C, C++, COBOL, FORTRAN, BASIC, Lisp, Ada, etc.

Machine Language(lowest level)◦ least natural language for

humans, most natural language for hardware

◦ 0’s and 1’s◦ directly understood by

hardware◦ not portable (hardware

dependent)

Page 46: CSC 110 Introduction to Computer Science for Majors Summer 2009 Introduction to Computers and Java 1

Source to machine code>> 46

Assembly Language(middle level)

a more or less human readable version of machine language

words, abbreviations, letters and numbers replace 0s and 1s

easily translated from human readable to machine executable code

like machine code, not portable (hardware dependent)

Page 47: CSC 110 Introduction to Computer Science for Majors Summer 2009 Introduction to Computers and Java 1

Interpreters>> 47

Getting from Sourceto Machine Code

“Compiling a program” = translating from a high-level language source code to machine (object, or executable) code.

“Compiler” = a program that translates HLL source code to machine (object, or executable) code.

“Assembly” = translating from assemble language source code to machine (object, or executable) code.

“Assembler” = a program that translates assembly source code to machine (object, or executable) code.

Compilers need to know the specific target hardware, i.e. what type computer CPU the program will run on.

Page 48: CSC 110 Introduction to Computer Science for Majors Summer 2009 Introduction to Computers and Java 1

Java program translation>> 48

Compilers vs. Assemblers vs. Interpreters

Compilers and Assemblers◦ translation is a separate user step◦ translation is “off-line,” i.e. not at run time

Interpreters - another way to translate source to object code◦ interpretation (from source to object code) is not a separate user

step◦ translation is “on-line,” i.e. at run time

Compiler,

Assembler, or

Interpreter

SourceCode

ObjectCode

Page 49: CSC 110 Introduction to Computer Science for Majors Summer 2009 Introduction to Computers and Java 1

Java linker>> 49

Java Program Translation

Both Compilation and Interpretation

Intermediate Code:“Byte Code”◦ portable low-level

code◦ similar to

assembly code,but hardware independent

◦ invisible to Java programmer

Interpreter translates from generic byte code to hardware-specific machine code

Java Program Data for Java Program

Java Compiler

Byte-CodeProgram

Byte-Code Interpreter

Machine-LanguageInstructions

Computer Executionof Machine-Language Instructions

Output of Java Program

JavaVirtual

Machine

JVM

Page 50: CSC 110 Introduction to Computer Science for Majors Summer 2009 Introduction to Computers and Java 1

Advantages>> 50

Java Linker

Java Program Data for Java Program

Java Compiler

Byte-CodeProgram

Byte-Code Interpreter

Machine-LanguageInstructions

Computer Executionof Machine-Language Instructions

Output of Java Program

JavaVirtual

Machine

Linker

Previously Compiled Helper Programs

Page 51: CSC 110 Introduction to Computer Science for Majors Summer 2009 Introduction to Computers and Java 1

INTERNET>> 51

Advantages

“Platform Independence” - Will run an any machine

“New Compiler not Necessary with New Machine” - manufacturers need only design a “simple” interpreter, much easier than writing a compiler

“Portability” - byte-code sent over the Internet

Page 52: CSC 110 Introduction to Computer Science for Majors Summer 2009 Introduction to Computers and Java 1

cont'd>> 52

SummaryPart 1

A computer’s main memory holds both the program that is currently running and its data.

Main memory is a series of numbered locations, each one containing a single byte.

Auxiliary memory is for more or less permanent storage. A compiler is a program that translates a high-level

language, like java, into a lower level format (“byte-code” for java).

Actual translation of Java byte-code to the hardware’s specific machine code occurs at run time (it is interpreted).

Page 53: CSC 110 Introduction to Computer Science for Majors Summer 2009 Introduction to Computers and Java 1

Introduction to Computers and Java 53

SummaryPart 2

An algorithm is a set of instructions for solving a problem (it must be complete and precise).

An object is something that has both data and actions (methods) associated with it.

A class defines a type of object; all objects of the same class have the same methods.

Three OOP design principles are encapsulation, polymorphism, and inheritance.

In a java program, a method invocation has the general form Object_Name.Method_Name(Arguments)