Chapter 1 - Introduction. Ch 1Goals To understand the activity of programming To learn about the...
-
Upload
marjory-maxwell -
Category
Documents
-
view
220 -
download
2
Transcript of Chapter 1 - Introduction. Ch 1Goals To understand the activity of programming To learn about the...
Chapter 1 - IntroductionChapter 1 - Introduction
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
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?
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
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
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
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
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?
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
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
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
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.
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
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
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.
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
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
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.
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!”);
}}}}
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
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
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)
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 ;
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*/*/
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
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!
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
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
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
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
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.
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
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