week01topics - Computing Sciences

advertisement
Week 1
Introduction to Computer Science
and Object-Oriented Programming
COMP 111
George Basham
Week 1 Topics
1.1.1 What is Programming?
1.1.2 The Anatomy of a Computer
1.1.3 Translating Human-Readable
Programs to Machine Code
1.1.4 The Java Programming
Language
1.1.5 Becoming Familiar with Your
Computer
1.1.6 Compiling a Simple Program
1.1.7 Errors
1.1.8 The Compilation Process
1.1.1 What is Programming?
• A computer must be programmed to
perform tasks. Different tasks require
different programs.
• A computer executes a sequence of very
basic operations in rapid succession
• A program can balance a checkbook;
process words; play a game
• A sophisticated program is composed of
extremely primitive operations
1.1.1 What is Programming Cont.
• A typical primitive operation may be:
– Put a red dot onto this screen position
– Send the letter A to the printer
– Get a number from this location in memory
– Add up two numbers
– If this value is negative, continue the program at
that instruction
• The creation of a sophisticated program may
require a team of many highly skilled
programmers, graphic artists, and other
professionals
1.1.2 The Anatomy of a Computer
• At the heart of a computer lies the central
processing unit (CPU). It consists of a single
chip (integrated circuit) or a small number of
chips.
• The computer keeps data and programs in
storage. There are two kinds of storage.
• Primary storage, also called random-access
memory (RAM) or simply memory, is fast and
expensive and is made from memory chips. It
loses all its data when the power is turned off.
1.1.2 The Anatomy of a Computer Cont.
• Secondary storage, usually a hard disk, provides
less expensive storage and persists without
electricity.
• Some computers are self-contained units,
whereas others are interconnected through
networks. Home computers are usually
connected to the internet via a dialup or
broadband connection.
• Most computers have removable storage
devices that can access data or programs such
as floppy disks, flash drives, tapes or compact
discs (CDs)
1.1.2 The Anatomy of a Computer Cont.
• The CPU, the RAM and the electronics
controlling the hard disk and other devices are
interconnected through a set of electrical lines
called a bus
• A motherboard contains the CPU, the RAM and
connectors to peripheral devices (monitor,
speakers, printer, mouse, keyboard, etc.)
• The CPU reads machine instructions from
memory which direct it to communicate with
memory, secondary storage and peripheral
devices
1.1.3 Translating Human-Readable
Programs to Machine Code
• A CPU executes machine instructions
• Machine instructions are very simple, are
encoded as numbers and stored in memory, and
can be executed very quickly
• Because machine instructions are encoded as
numbers, it is difficult to write programs in
machine code
• High-level programming languages such as
Java allow you to describe tasks at a higher
conceptual level than machine code
1.1.3 Translating Human-Readable
Programs to Machine Code Cont.
• These machine instructions:
– Load the contents of memory location 40
– Load the value 100
– If the first value is greater than the second value,
continue with the instructions that is stored in
memory location 240
might be encoded something like this:
21 40
16 100
163 240
1.1.3 Translating Human-Readable
Programs to Machine Code Cont.
• This Java code (high-level instructions):
if (intRate > 100)
System.out.println(“Interest rate error”);
might be compiled and translated into
21 40 16 100 163 240 . . .
• A compiler is a sophisticated program that
translates programs written in a high-level
language into machine code
1.1.4 The Java Programming
Language
• Java was originally designed for programming
consumer devices, but it was first successfully
used to write internet applets
• Java was designed to be safe and portable,
benefiting both internet users and students
• Java has a very large library
• There are packages in the Java library for
graphics, user interface design, cryptography,
networking, sound, database storage, and many
other purposes.
1.1.4 The Java Programming Language
Cont.
• Even expert Java programmers cannot hope to
know the contents of all of the packages
• Focus on learning those parts of the library that
you need for your programming projects
• Although you will learn a good deal about the
Java language and the most important library
packages, keep in mind that the central goal of
this course is not to make you memorize Java
minutiae, but to teach you how to think about
programming.
1.1.5 Becoming Familiar with Your
Computer
• Learn how to log in
• Learn how to use a Java Integrated
Development Environment (IDE)
• Understand how to work with files and file
folders (directories)
• Begin by writing a simple Java program
• Save your work frequently and make backup copies of your important files
1.1.6 Compiling a Simple Program
File HelloTester.java:
public class HelloTester
{
public static void main(String[] args)
{
// Display a greeting to the console window
System.out.println(“Hello World!”);
}
}
Output: Hello, World!
1.1.6 Compiling a Simple Program Cont.
• Java is case sensitive. You must be careful
about distinguishing between upper and
lowercase letters.
• Lay out your programs so that they are easy to
read
• Classes are the fundamental building blocks of
Java programs
• Almost every Java application contains a class
with a main method. When the application
starts, the instructions in the main method are
executed.
1.1.6 Compiling a Simple Program Cont.
• Each class contains definitions of methods
• Each method contains a sequence of
instructions
• Use comments to help human readers
understand your program
• A method is called by specifying an object, the
method name, and the method parameters
• A string is a sequence of characters enclosed in
quotation marks
1.1.6 Compiling a Simple Program Cont.
Calling a Method:
Object
Method
Parameters
System.out.println(“Hello, World!”);
1.1.7 Errors
• A syntax error is a violation of the rules of
the programming language. The compiler
detects syntax errors.
• System.ouch.println(“Hello, World!”);
• A logic error causes a program to take an
action that the programmer did not intend.
You must test your programs to find logic
errors.
• System.out.println(“Hello, Word!”);
1.1.8 The Compilation Process
• You use an editor to write your Java program
statements
• When you save your program statements, it
becomes source code and will have a .java
extension, for example HelloTester.java
• A successful compile of your source code will
create virtual machine instructions called class
files and will have a .class extension, for
example HelloTester.class
1.1.8 The Compilation Process Cont.
From Source Code to Running Program
Editor
Source File
Compiler
Class files and
Library files
Virtual Machine
Running Program
Reference: Big Java 2nd Edition by Cay
Horstmann
1.1.1 What is Programming? (section 1.1 in Big Java)
1.1.2 The Anatomy of a Computer (section 1.2 in Big
Java)
1.1.3 Translating Human-Readable Programs to
Machine Code (section 1.3 in Big Java)
1.1.4 The Java Programming Language (section 1.4 in
Big Java)
1.1.5 Becoming Familiar with Your Computer (section
1.5 in Big Java)
1.1.6 Compiling a Simple Program (section 1.6 in Big
Java)
1.1.7 Errors (section 1.7 in Big Java)
1.1.8 The Compilation Process (section 1.8 in Big
Java)
PC Block Diagram from: http://www.vaughns-1pagers.com/computer/pc-block-diagram.htm
Download