Chapter 1 - Introduction

fencinghuddleΤεχνίτη Νοημοσύνη και Ρομποτική

14 Νοε 2013 (πριν από 3 χρόνια και 8 μήνες)

80 εμφανίσεις

Chapter 1


Ch 1Goals

To understand the
activity of programming

To learn about the

of computers

To learn about
machine code

high level
programming languages

To become familiar with your computing


and your


compile and run

your first Java program

recognize syntax and logic errors

1.1 What is Programming?

Computers are programmed to perform

Different tasks = different programs


Sequence of basic operations executed in

Contains instruction sequences for all tasks it
can execute

How do we produce this program?

1.2 Anatomy of a Computer

A computer is a machine that can do
certain tasks

Stores data

Interacts with devices

Executes programs


Central Processing Unit (CPU)

Heart of the

Made of a chip (AMD, Intel, etc), millions of transistors

Executes instructions given by a program

1.2 Anatomy of a Computer


where data is kept

3 types

primary, secondary, removable

1.2 Anatomy of a Computer

Computers communicate through a

Peripheral devices

allow interaction with

Speakers, screen, printer, scanner

RAM, Disks, peripherals connected to CPU
via the

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

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

1.3 Translating Human
Programs to Machine Code

There are actually multiple levels upon which
a program can be written

Machine Instructions


High Level Language

Machine Code

The most basic level of instructions

Everything is represented with numbers (read
in binary)

Looks like this:

00010101 00101000 (binary)

21 40 (decimal)

This is the only language the CPU knows

What’s the problem?

CPUs perform very primitive operations only

Add, subtract, load from memory

Leads to coding of several hundred lines to
perform one simple operation

Not Intuitive

Can you look at 1000 numbers and tell what the
program is doing?

Each CPU uses a different set of instructions

Each CPU translates different digits to mean
different things


A slight step up from machine

Uses words and numbers

load 40

Still needs as many instructions as machine
language, but is

easier to read.

High Level

High level is much more expressive

One statement is converted into several
machine code instructions

The conversion is done by the

Very complicated and sophisticated, not the
focus of this course

1.4 The Java Programming Language



more secure, easier to catch


independent ("write once, run


operating system and


Rich library (packages)

Designed for the Internet

Java Virtual Machine

Unique to Java

Instead of compiling for a particular
processor, Java compiles to a virtual
processor that is the same for every

Lesson: The code is the same no matter
what computer you are on, so you don’t
have to make a Mac version and a PC
version, etc.


Fully object
oriented nature can be
confusing to beginning programmers

Many revisions made

make sure you
update if you work from home to Java 5.0

Rich libraries

So much to learn

1.5 Becoming Familiar w/ your

Very important to familiarize yourself with your

Understand how to find files, what they are, how do
you move them around and name them

Understand files and folders

Programs are kept in files

File: a collection of items of information that are kept

Files have names, and the rules for legal names differ

Files are stored in folders or directories

1.6 Compiling a Simple Program

So how do we actually make a program?

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

This will help us find mistakes more

Simple Program

Most simple program

Outputs a sentence to the console screen

public class FirstProgram


public static void main(String[] args)


//display a greeting in the console window

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



Essential Components

What does each line mean?

public class

A class is an essential part of a java program,
all instructions are contained within a

We create a class called FirstProgram, the file
must be called


public static void main(String[] args)


// Code goes here


Classes contains methods

instructions that can be called

Ex. Robot needs method called walk

public void walk() {


Every program must have a method called

it is the method called first


public static void main(String[] args)

String[] args
is a parameter

input given
to a method (will learn about more later


The instructions are the body of a method

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

An instruction to print a message to the

Each instruction ends in a ;


//display a greeting in the console window

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

“//” signifies not to execute this line of code

Has many uses

here it is to explain what
the code is supposed to do

Another type of comments:

/* Multiple lines

of comments about

what is going on


Method Call

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


Java is case sensitive

class FirstProgram

class firstprogram

It is free form

the amount of white space
is irrelevant to the computer (although it is
important for readability)

You could have all instructions on one line,
but not very useful to a human

We will grade on it!

1.7 Errors

Two types of errors

Compile Time Errors

Syntax errors

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

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

Run Time Errors

Not problems in the syntax, but problems in logic

Program does not accomplish what the
programmer intended

1.8 Compilation Process

The code a programmer creates is called
the source code

Written in Java

Saved as a .java file

Must have the same name as the class

When converted by the compiler, a class
file is created

Machine instructions

Saved by compiler as .class file

Coding loop

test loop

Your programming begins with editing a
file by writing instructions

When done, compile the program.

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

If there are runtime errors, go back to fix

Object Oriented Programming

What is the point of this course?

Not Java

its just a tool

Methodology of programming


Objects & Classes

2 Main


an entity, something we can
imagine (typically a noun)

Consists of data values and operations for
manipulating those values

Interactions between objects is the heart of
OO style

Ex. Bank accounts

need Customer,
Account, Bank, etc.



Instructions for creating objects

Defines what objects can and cannot do

how they behave and what info they hold

Like a mold, template, or blueprint

Objects are instances of classes

Ex. Class is Student, instance is Eric Lantz

Ex2. Class is University, instances could be
University of Wisconsin and Cornell

Messages and Methods

In order to use objects and classes, coder
needs to communicate with them


Ex. Tell Robot to move forward 1 foot

Can you send any message to any object?

No, the object must understand the message

The class defines the messages that the
object can understand