program - Computer Sciences User Pages

Download Report

Transcript program - Computer Sciences User Pages

Chapter 1 - Introduction
Announcements
Info Sheet
Web Site:
www.cs.wisc.edu/~dakoop/cs302
Lab Hours
Notecards
Pictures
A Simple Program
Most simple program – Hello World

Outputs “Hello, World!” to the screen
public class HelloTester
{
public static void main(String[] args)
{
//display a greeting in the console
System.out.println(“Hello, world!”);
}
}
Questions / Outline
What is a computer program?
What makes a computer program work?
How do you write computer programs?
What does it mean to “program in Java”?
What is object-oriented programming?
Computer Programs
Computers perform operations based on
sequences of instructions
A program is simply a sequence of
instructions
The behavior of a program is what
happens when the computer executes the
instructions
Note: Programs may behave differently
given different user input
Computer Programs
Sophisticated programs require teams of
highly skilled programmers and other
professionals
Computer programs are used to make
many tasks more efficient

There are programs for a wide variety of tasks
Flexibility arises because we program a
specific task for the computer
Computers
A computer is a machine that can
accomplish a set of basic instructions



Stores data
Interacts with devices
Executes programs
A program is a set of instructions and
decisions used to carry out a task
Computers
Computers have come a long way, and will
continue to advance – but they still
maintain the basic components:



CPU
Storage
Input and Output Devices
CPU
Central Processing Unit (CPU) - Brain of the computer



It’s a chip made with millions of transistors
Athlon, Pentium, PowerPC
Executes instructions given by a program
Storage
Storage is where a computer stores
information that helps it execute instructions
3 Major Types



Primary
Secondary
Removable
Primary Storage
Random Access Memory(RAM)


Fast but expensive
All electronic, so it loses info when power is stopped
Secondary
Hard Disk


Less expensive,
but slow
Maintains data
even when the
computer is
turned off
Removable
Floppies, CD-ROMs, Zip Disks, Thumb
Drives
Maintain information without power, plus
you can take them with you
Connections
Network: computers can be interconnected
with each other
Peripheral Devices: how computers and
humans communicate


Input: keyboard, mouse, iris scanner
Output: speakers, screen, printer
Internal Bus: The computer sends and
receives data to it’s different parts via a bus
(think of this as a highway)
Questions
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?
Programming
Very simply, programming is writing a
computer program
While this may sound straightforward, it’s
actually much more complex…
From Instructions to Code
A computer needs to be able to understand what
instructions it needs to execute
There is only one real language a computer
understands:

001010100001001000100100100101010101010
However, there are many languages a program can
be written in



Machine Code
Assembly Language
High Level Languages
Machine Code
0101010101010001000000111111001
The most basic level of instructions
Everything is in binary (ones and zeros)
Humans cannot easily read machine code,
but the only language a computer can
read is machine code
Problems With Machine Code
Very primitve


Commands: Add, subtract, load from memory
Leads to coding of several hundred lines to
perform one simple operation
Not Intuitive

Can you look at machine code and tell what
the program is doing?
Not universal

Each CPU translates different binary strings to
mean different things
Assembly Language
add 1, R2, R3
Not used in Java
Has a direct translation to machine code
Slightly easier to read
Still has only a small set of instructions that it
can perform
Takes a long time to write a big program
Hard to find errors and still not universal
High Level Languages
sum = Math.sqrt(3) + 2 * 5;
High level languages are much more
understandable and expressive

One statement is converted into several
machine code instructions
The conversion is done by the compiler
Compiler: High-Level to Assembly
Assembler: Assembly to Machine Code
The Java Programming Language
Simple – easy to understand
Safe – more secure, less error-prone
Platform-Independent - "write once, run
anywhere")
Rich Library – lots of tasks are already
accomplished for you
Designed with the internet in mind
Java Virtual Machine
Unique to Java
Instead of compiling on processor, Java
compiles to a virtual processor that is the
same for every computer
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.
Flaws
Simple programs takes a little more effort
than with C++ and other languages
Many revisions made – make sure you
update if you work from home to Java 5.0+
Rich libraries  Lots to learn
Programming Tools
There are many tools available for
programming in two categories:


Editor & Compiler Pair
Integrated Development Environment (IDE)
We will be using the Eclipse IDE



Originally developed by IBM
Now an open-source project
Free & available for many platforms
Programming Tips
Become familiar with the environment
you’ll be using
Organize your files in a logical manner
Save and backup your work often


You only need to backup source files
Know which copy of your program you’re
working on
Writing a Simple Program
Recall our simple program:
public class HelloTester
{
public static void main(String[] args)
{
//display a greeting in the console window
System.out.println(“Hello, world!”);
}
}
Prints “Hello, world!” to the screen
Simple Program
What does each line mean?



Class Declaration
Method Declaration
Method Body
Comments
Program Statements
Class Declaration
public class HelloTester



A class is an essential part of a Java program
– all instructions are contained within a class
Every program contains one or more classes
Important: If we create a class named
HelloTester, the filename must be
HelloTester.java (case-sensitive!)
Method Declaration
public static void main(String[] args)
{
}





Classes contains both data and methods
Methods are sequences of instructions
For example, a Robot class might contain
methods for walking, talking, and grabbing
Every program must have a method called main
A program always starts by executing the
sequence of instructions in the main method
Method Declaration
public static void main(String[] args)
{
}

String[] args is a parameter of the main

method, meaning it is data the method is
given as input
public and static are modifiers that
describe what kind of method main is
void is the return type of the method

Method Body
//display a greeting in the console window
System.out.println(“Hello, world!”);



Contains the sequence of instructions that the
method should execute
Also contains comments which are descriptions
of what a line or a sequence of lines is trying to
accomplish
Each instruction ends in a semi-colon (;), but
comments do not have such restrictions
Comments
//display a greeting in the console window




“//” signifies not to execute this line of code
Anything written after “//” is ignored
The main purpose of comments is to explain
code to anyone reading your code (including
yourself!)
Other varieties of comments:
/* Anything in here is ignored */
/** Anything in here is ignored, but can be
used for automatic documentation */
Sequence of Instructions
System.out.println(“Hello, world!”);



The only thing we are trying to do is print
“Hello, world!” to the screen
This takes only one instruction – usually we
have many more
The type of instruction is a method call on an
object
Notes
Java is case-sensitive


Cannot call the method Main instead of main
Cannot call system.out.println() instead of
System.out.println()
Java 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…
Programming Errors
Two types of errors

Syntax Errors = compile-time errors
These are caught when you ask Java to convert
your program to machine code
Your program must follow a set of rules called a
grammar (just like a sentence in English)

Logic Errors = run-time errors
Not problems in the syntax, but problems in logic
Program does not accomplish what the
programmer intended
Programming & Compiling
The sequence of instructions that a
programmer writes is called source code

Saved as a .java file
A compiler takes source code and creates
machine code

Saved by compiler as .class file
Java combines previously written code
(libraries) with the source code when it
runs the program
From Editing to Executing
Create source code (.java files)
Compiler converts source code to machine
code (.class files)
Class files are integrated with library files
(files that define a set of useful classes)
To run the program, JVM combines class
files with library files and runs the program
Edit-Compile-Test
Edit: Write or revise your source code
Compile: Run the compiler program to
translate source code to machine code

If there are syntax errors, go back and edit
Test: Run your program, see if it works,
and try to break

If there are logic errors, go back and edit
Object Oriented Programming
This is an introduction to programming
with Java
It is not simply a course in Java syntax
One of the big selling points of Java is the
fact that it is object-oriented
We will cover the concepts of OOP later,
but it is helpful to have some background
of this paradigm earlier
What is Object Oriented
Programming?
It is an evolutionary form of modular
programming with more formal rules that
allow pieces of software to be reused and
interchanged between programs.
A style of computer programming which
entails building of independent pieces of
code which interact with each other.
What are the advantages to OOP?
Cohesive
Efficient
Clearer
Objects and Classes
Analogy: class : object as blueprint : house
Objects simply represent an entity



Think of objects as nouns (eg a robot)
Objects have data (eg name=Fred) and
methods (eg moveRight(), turnLeft() )
To use objects, simply call the method on the
object:
robot.moveRight();
robot.getName();
Objects and Classes
Classes are blueprints to build objects




They are a template/mold for an object, but
they aren’t the actual objects
They define what objects know (eg their
name) and what they can do (eg moveRight)
Just like you can’t water the front lawn of a
blueprint, you can’t accomplish anything with
a class
You use classes to build objects
Methods
Each method call executes the code in
that method (it’s a mini-program)
You can call methods on objects


Methods may do different things for different
objects
You can only call methods that are defined by
the class on the object