Chapter 1 - Introduction. Ch 1Goals To understand the activity of programming To learn about the...

35
Chapter 1 - Chapter 1 - Introduction Introduction

Transcript of Chapter 1 - Introduction. Ch 1Goals To understand the activity of programming To learn about the...

Page 1: Chapter 1 - Introduction. Ch 1Goals To understand the activity of programming To learn about the architecture of computers To learn about machine code.

Chapter 1 - IntroductionChapter 1 - Introduction

Page 2: Chapter 1 - Introduction. Ch 1Goals To understand the activity of programming To learn about the architecture of computers To learn about machine code.

Ch 1GoalsCh 1Goals

To understand the To understand the activity of programmingactivity of programming

To learn about the To learn about the architecturearchitecture of computers of computers

To learn about To learn about machine codemachine code and and high level high level

programming languagesprogramming languages

To become familiar with your computingTo become familiar with your computing

environmentenvironment and yourand your compilercompiler

To To compile and runcompile and run your first Java program your first Java program

To To recognize syntax and logic errorsrecognize syntax and logic errors

Page 3: Chapter 1 - Introduction. Ch 1Goals To understand the activity of programming To learn about the architecture of computers To learn about machine code.

1.1 What is Programming?1.1 What is Programming?Computers are programmed to perform Computers are programmed to perform tasks tasks

Different tasks = different programsDifferent tasks = different programs

Program Program Sequence of basic operations executed in Sequence of basic operations executed in

succession succession Contains instruction sequences for all tasks it Contains instruction sequences for all tasks it

can execute can execute

How do we produce this program?How do we produce this program?

Page 4: Chapter 1 - Introduction. Ch 1Goals To understand the activity of programming To learn about the architecture of computers To learn about machine code.

1.2 Anatomy of a Computer1.2 Anatomy of a Computer

A computer is a machine that can do A computer is a machine that can do certain taskscertain tasks Stores dataStores data Interacts with devicesInteracts with devices Executes programsExecutes programs

Page 5: Chapter 1 - Introduction. Ch 1Goals To understand the activity of programming To learn about the architecture of computers To learn about machine code.

CPUCPUCentral Processing Unit (CPU) - Heart of the Central Processing Unit (CPU) - Heart of the computercomputer Made of a chip (AMD, Intel, etc), millions of transistorsMade of a chip (AMD, Intel, etc), millions of transistors Executes instructions given by a programExecutes instructions given by a program

Page 6: Chapter 1 - Introduction. Ch 1Goals To understand the activity of programming To learn about the architecture of computers To learn about machine code.

1.2 Anatomy of a Computer1.2 Anatomy of a ComputerStorage – where data is keptStorage – where data is kept 3 types – primary, secondary, removable3 types – primary, secondary, removable

Page 7: Chapter 1 - Introduction. Ch 1Goals To understand the activity of programming To learn about the architecture of computers To learn about machine code.

1.2 Anatomy of a Computer1.2 Anatomy of a Computer

Computers communicate through a Computers communicate through a networknetwork

Peripheral devicesPeripheral devices allow interaction with allow interaction with humanshumans Speakers, screen, printer, scannerSpeakers, screen, printer, scanner

RAM, Disks, peripherals connected to CPU RAM, Disks, peripherals connected to CPU via the via the busbus

Page 8: Chapter 1 - Introduction. Ch 1Goals To understand the activity of programming To learn about the architecture of computers To learn about machine code.
Page 9: Chapter 1 - Introduction. Ch 1Goals To understand the activity of programming To learn about the architecture of computers To learn about machine code.

Where is a program stored when it is not Where is a program stored when it is not currently running? currently running?

Which part of the computer carries out Which part of the computer carries out arithmetic operations, such as addition arithmetic operations, such as addition and multiplication? and multiplication?

Page 10: Chapter 1 - Introduction. Ch 1Goals To understand the activity of programming To learn about the architecture of computers To learn about machine code.

1.3 Translating Human-Readable 1.3 Translating Human-Readable Programs to Machine CodePrograms to Machine Code

There are actually multiple levels upon which There are actually multiple levels upon which a program can be writtena program can be written Machine InstructionsMachine Instructions Assembly Assembly High Level LanguageHigh Level Language

Page 11: Chapter 1 - Introduction. Ch 1Goals To understand the activity of programming To learn about the architecture of computers To learn about machine code.

Machine CodeMachine Code

The most basic level of instructionsThe most basic level of instructions Everything is represented with numbers (read Everything is represented with numbers (read

in binary)in binary) Looks like this:Looks like this:

00010101 00101000 (binary)00010101 00101000 (binary)

21 40 (decimal)21 40 (decimal)

This is the only language the CPU knowsThis is the only language the CPU knows

Page 12: Chapter 1 - Introduction. Ch 1Goals To understand the activity of programming To learn about the architecture of computers To learn about machine code.

What’s the problem?What’s the problem?CPUs perform very primitive operations onlyCPUs perform very primitive operations only Add, subtract, load from memoryAdd, subtract, load from memory Leads to coding of several hundred lines to Leads to coding of several hundred lines to

perform one simple operationperform one simple operation

Not IntuitiveNot Intuitive Can you look at 1000 numbers and tell what the Can you look at 1000 numbers and tell what the

program is doing?program is doing?

Each CPU uses a different set of instructionsEach CPU uses a different set of instructions Each CPU translates different digits to mean Each CPU translates different digits to mean

different thingsdifferent things

Page 13: Chapter 1 - Introduction. Ch 1Goals To understand the activity of programming To learn about the architecture of computers To learn about machine code.

AssemblyAssembly

A slight step up from machineA slight step up from machine

Uses words and numbersUses words and numbers load 40load 40

Still needs as many instructions as machine Still needs as many instructions as machine language, but is language, but is slightlyslightly easier to read. easier to read.

Page 14: Chapter 1 - Introduction. Ch 1Goals To understand the activity of programming To learn about the architecture of computers To learn about machine code.

High LevelHigh Level

High level is much more expressiveHigh level is much more expressive One statement is converted into several One statement is converted into several

machine code instructionsmachine code instructions

The conversion is done by the The conversion is done by the compilercompiler Very complicated and sophisticated, not the Very complicated and sophisticated, not the

focus of this coursefocus of this course

Page 15: Chapter 1 - Introduction. Ch 1Goals To understand the activity of programming To learn about the architecture of computers To learn about machine code.

1.4 The Java Programming Language1.4 The Java Programming Language

Simple Simple

Safe – more secure, easier to catch Safe – more secure, easier to catch errorserrors

Platform-independent ("write once, run Platform-independent ("write once, run anywhere") – operating system and anywhere") – operating system and architecturearchitecture

Rich library (packages)Rich library (packages)

Designed for the Internet Designed for the Internet

Page 16: Chapter 1 - Introduction. Ch 1Goals To understand the activity of programming To learn about the architecture of computers To learn about machine code.

Java Virtual MachineJava Virtual Machine

Unique to JavaUnique to JavaInstead of compiling for a particular Instead of compiling for a particular processor, Java compiles to a virtual processor, Java compiles to a virtual processor that is the same for every processor that is the same for every computercomputerLesson: The code is the same no matter Lesson: The code is the same no matter what computer you are on, so you don’t what computer you are on, so you don’t have to make a Mac version and a PC have to make a Mac version and a PC version, etc.version, etc.

Page 17: Chapter 1 - Introduction. Ch 1Goals To understand the activity of programming To learn about the architecture of computers To learn about machine code.

FlawsFlaws

Fully object-oriented nature can be Fully object-oriented nature can be confusing to beginning programmersconfusing to beginning programmers

Many revisions made – make sure you Many revisions made – make sure you update if you work from home to Java 5.0 update if you work from home to Java 5.0 (1.5)(1.5)

Rich libraries Rich libraries So much to learn So much to learn

Page 18: Chapter 1 - Introduction. Ch 1Goals To understand the activity of programming To learn about the architecture of computers To learn about machine code.

1.5 Becoming Familiar w/ your 1.5 Becoming Familiar w/ your ComputerComputer

Very important to familiarize yourself with your Very important to familiarize yourself with your computercomputer Understand how to find files, what they are, how do Understand how to find files, what they are, how do

you move them around and name themyou move them around and name them

Understand files and folders Understand files and folders Programs are kept in files Programs are kept in files File: a collection of items of information that are kept File: a collection of items of information that are kept

together together Files have names, and the rules for legal names differ Files have names, and the rules for legal names differ Files are stored in folders or directoriesFiles are stored in folders or directories

Page 19: Chapter 1 - Introduction. Ch 1Goals To understand the activity of programming To learn about the architecture of computers To learn about machine code.

1.6 Compiling a Simple Program1.6 Compiling a Simple Program

So how do we actually make a program?So how do we actually make a program?

In this course, we will use an IDE In this course, we will use an IDE (Integrated Development Environment) (Integrated Development Environment) called Eclipsecalled Eclipse

This will help us find mistakes more This will help us find mistakes more quickly.quickly.

Page 20: Chapter 1 - Introduction. Ch 1Goals To understand the activity of programming To learn about the architecture of computers To learn about machine code.

Simple ProgramSimple Program

Most simple program Most simple program Outputs a sentence to the console screenOutputs a sentence to the console screen

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

{{//display a greeting in the console //display a greeting in the console

windowwindowSystem.out.println(“My First Program!”);System.out.println(“My First Program!”);

}}}}

Page 21: Chapter 1 - Introduction. Ch 1Goals To understand the activity of programming To learn about the architecture of computers To learn about machine code.

Essential ComponentsEssential Components

What does each line mean?What does each line mean?

public class public class ClassNameClassName A class is an essential part of a java program, A class is an essential part of a java program,

all instructions are contained within a all instructions are contained within a classclass

We create a class called FirstProgram, the file We create a class called FirstProgram, the file must be called FirstProgram.javamust be called FirstProgram.java

Page 22: Chapter 1 - Introduction. Ch 1Goals To understand the activity of programming To learn about the architecture of computers To learn about machine code.

MethodMethodpublic static void main(String[] args)public static void main(String[] args){{// Code goes here// Code goes here

}}

Classes contains methods – specific Classes contains methods – specific instructions that can be calledinstructions that can be called Ex. Robot needs method called walkEx. Robot needs method called walk

public void walk() {public void walk() {}}

Every program must have a method called Every program must have a method called main – it is the method called firstmain – it is the method called first

Page 23: Chapter 1 - Introduction. Ch 1Goals To understand the activity of programming To learn about the architecture of computers To learn about machine code.

ParametersParameters

public static void main(String[] args)public static void main(String[] args)

String[] args String[] args is a parameter – input given is a parameter – input given to a method (will learn about more later to a method (will learn about more later on)on)

Page 24: Chapter 1 - Introduction. Ch 1Goals To understand the activity of programming To learn about the architecture of computers To learn about machine code.

BodyBody

The instructions are the body of a methodThe instructions are the body of a method

System.out.println(“My First Program!”);System.out.println(“My First Program!”);

An instruction to print a message to the An instruction to print a message to the screenscreen

Each instruction ends in a ;Each instruction ends in a ;

Page 25: Chapter 1 - Introduction. Ch 1Goals To understand the activity of programming To learn about the architecture of computers To learn about machine code.

CommentsComments

//display a greeting in the console window//display a greeting in the console windowSystem.out.println(“My First Program!”);System.out.println(“My First Program!”);

““//” signifies not to execute this line of code//” signifies not to execute this line of codeHas many uses – here it is to explain what Has many uses – here it is to explain what the code is supposed to dothe code is supposed to doAnother type of comments:Another type of comments:/* Multiple lines/* Multiple lines

of comments aboutof comments about what is going onwhat is going on*/*/

Page 26: Chapter 1 - Introduction. Ch 1Goals To understand the activity of programming To learn about the architecture of computers To learn about machine code.

Method CallMethod Call

Here, we are calling the method println() on the Here, we are calling the method println() on the object System.outobject System.out

Page 27: Chapter 1 - Introduction. Ch 1Goals To understand the activity of programming To learn about the architecture of computers To learn about machine code.

NotesNotes

Java is case sensitiveJava is case sensitive

class FirstProgram class FirstProgram ≠≠ class firstprogram class firstprogram

It is free form – the amount of white space It is free form – the amount of white space is irrelevant to the computer (although it is is irrelevant to the computer (although it is important for readability)important for readability) You could have all instructions on one line, You could have all instructions on one line,

but not very useful to a humanbut not very useful to a human We will grade on it!We will grade on it!

Page 28: Chapter 1 - Introduction. Ch 1Goals To understand the activity of programming To learn about the architecture of computers To learn about machine code.

1.7 Errors1.7 Errors

Two types of errorsTwo types of errors Compile Time Errors – Syntax errorsCompile Time Errors – Syntax errors

These are caught when you ask Java to convert These are caught when you ask Java to convert your program to machine codeyour program to machine code

Your program must follow a set of rules (just like a Your program must follow a set of rules (just like a sentence in English)sentence in English)

Run Time ErrorsRun Time ErrorsNot problems in the syntax, but problems in logicNot problems in the syntax, but problems in logic

Program does not accomplish what the Program does not accomplish what the programmer intendedprogrammer intended

Page 29: Chapter 1 - Introduction. Ch 1Goals To understand the activity of programming To learn about the architecture of computers To learn about machine code.

1.8 Compilation Process1.8 Compilation Process

The code a programmer creates is called The code a programmer creates is called the source codethe source code Written in JavaWritten in Java Saved as a .java fileSaved as a .java file Must have the same name as the classMust have the same name as the class

When converted by the compiler, a class When converted by the compiler, a class file is createdfile is created Machine instructionsMachine instructions Saved by compiler as .class fileSaved by compiler as .class file

Page 30: Chapter 1 - Introduction. Ch 1Goals To understand the activity of programming To learn about the architecture of computers To learn about machine code.
Page 31: Chapter 1 - Introduction. Ch 1Goals To understand the activity of programming To learn about the architecture of computers To learn about machine code.

Coding loopCoding loop

Edit-compile-test loopEdit-compile-test loop

Your programming begins with editing a Your programming begins with editing a file by writing instructionsfile by writing instructions

When done, compile the program.When done, compile the program.

If no compile errors, test the program. If If no compile errors, test the program. If compile errors, go back to editingcompile errors, go back to editing

If there are runtime errors, go back to fix If there are runtime errors, go back to fix themthem

Page 32: Chapter 1 - Introduction. Ch 1Goals To understand the activity of programming To learn about the architecture of computers To learn about machine code.

Object Oriented ProgrammingObject Oriented Programming

What is the point of this course?What is the point of this course? Not Java – its just a toolNot Java – its just a tool Methodology of programming – OOPMethodology of programming – OOP

Page 33: Chapter 1 - Introduction. Ch 1Goals To understand the activity of programming To learn about the architecture of computers To learn about machine code.

Objects & Classes – 2 Main Objects & Classes – 2 Main ConceptsConcepts

ObjectObject – an entity, something we can – an entity, something we can imagine (typically a noun)imagine (typically a noun) Consists of data values and operations for Consists of data values and operations for

manipulating those valuesmanipulating those values Interactions between objects is the heart of Interactions between objects is the heart of

OO styleOO style Ex. Bank accounts – need Customer, Ex. Bank accounts – need Customer,

Account, Bank, etc.Account, Bank, etc.

Page 34: Chapter 1 - Introduction. Ch 1Goals To understand the activity of programming To learn about the architecture of computers To learn about machine code.

ClassesClasses

ClassClass – Instructions for creating objects – Instructions for creating objects Defines what objects can and cannot do – Defines what objects can and cannot do –

how they behave and what info they holdhow they behave and what info they hold Like a mold, template, or blueprintLike a mold, template, or blueprint

Objects are instances of classesObjects are instances of classes Ex. Class is Student, instance is Eric LantzEx. Class is Student, instance is Eric Lantz Ex2. Class is University, instances could be Ex2. Class is University, instances could be

University of Wisconsin and CornellUniversity of Wisconsin and Cornell

Page 35: Chapter 1 - Introduction. Ch 1Goals To understand the activity of programming To learn about the architecture of computers To learn about machine code.

Messages and MethodsMessages and Methods

In order to use objects and classes, coder In order to use objects and classes, coder needs to communicate with them - needs to communicate with them - MessagesMessages Ex. Tell Robot to move forward 1 footEx. Tell Robot to move forward 1 foot

Can you send any message to any object?Can you send any message to any object? No, the object must understand the messageNo, the object must understand the message The class defines the messages that the The class defines the messages that the

object can understandobject can understand