39
COMP 110: Introduction to Programming Tyler Johnson January 14, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110: Introduction to Programming Tyler Johnson January 14, 2009 MWF 11:00AM-12:15PM Sitterson 014

Embed Size (px)

Citation preview

Page 1: COMP 110: Introduction to Programming Tyler Johnson January 14, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110:Introduction to Programming

Tyler JohnsonJanuary 14, 2009

MWF 11:00AM-12:15PMSitterson 014

Page 2: COMP 110: Introduction to Programming Tyler Johnson January 14, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110 Spring 20092

Announcements

Hand in HW0 at the front of the room after class

Page 3: COMP 110: Introduction to Programming Tyler Johnson January 14, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110 Spring 20093

Questions?

Page 4: COMP 110: Introduction to Programming Tyler Johnson January 14, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110 Spring 20094

Today in COMP 110

Hardware and MemoryPrograms and CompilingYour first JAVA program!

Page 5: COMP 110: Introduction to Programming Tyler Johnson January 14, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110 Spring 20095

Before Programming

Need to know basics of a computerIf you drive a car you should know it runs on gasoline

What’s in the box?

Page 6: COMP 110: Introduction to Programming Tyler Johnson January 14, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110 Spring 20096

Hardware vs. Software

HardwarePhysical machineCPU, Memory

Software Set of instructions for the machine to execute

Page 7: COMP 110: Introduction to Programming Tyler Johnson January 14, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110 Spring 20097

Hardware

ENIACOne of the first general purpose programmable computers

Visit the computer museum in the first floor lobby of Sitterson Hall

Page 8: COMP 110: Introduction to Programming Tyler Johnson January 14, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110 Spring 20098

Hardware

CPU (Central Processing Unit) Executes instructions specified by a programmer

GHz - number of instructions per second, how fast is the computerDual Core - multiple processing units per CPU

Page 9: COMP 110: Introduction to Programming Tyler Johnson January 14, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110 Spring 20099

Memory

Holds data for the computer to processMain Memory (RAM – Random Access Memory)

Used for intermediate calculationsUsed to store the current program itself!Expensive

Auxiliary Memory (Secondary Memory)Disk drives, CDs, Flash drivesCheap

Page 10: COMP 110: Introduction to Programming Tyler Johnson January 14, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110 Spring 200910

Measuring Memory

Measured in bytesFor example 2 gigabytes (GB) of RAMMegabyte (MB) = 1 million (106) bytes (or 1,048,576 = 220 bytes)Gigabyte (GB) = 1 billion (109) bytes (or 1,073,741,824 = 230 bytes)

Page 11: COMP 110: Introduction to Programming Tyler Johnson January 14, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110 Spring 200911

What is a Byte?

Data, such as numbers and keyboard characters are stored as series of bitsA bit is a digit with value 1 or 0Examples

00111010 is a byte with value 5801000001 is a byte with value 6501100001 is a byte with value 97

A byte is composed of 8 bitsJust large enough to store a keyboard character

Page 12: COMP 110: Introduction to Programming Tyler Johnson January 14, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110 Spring 200912

Encoding Characters

How to encode keyboard characters?

Assign each character a number

Example (ASCII)(01000001) 65 = A(01000010) 66 = B(01000011) 67 = C….

Page 13: COMP 110: Introduction to Programming Tyler Johnson January 14, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110 Spring 200913

Main Memory

Called RAM (Random Access Memory)

Fast accessAccess any location in memory in constant time

AddressableEvery byte has an address that is used when writing or reading data to memory

Page 14: COMP 110: Introduction to Programming Tyler Johnson January 14, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110 Spring 200914

What is a Program?

Set of instructions for a computer to followExample instructions:

Add two numbersDivide two numbersStore a number at a memory location

Page 15: COMP 110: Introduction to Programming Tyler Johnson January 14, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110 Spring 200915

Programs

Programs take data as input and produce a useful resultExample

A spell-checking program takes a text file as input and produces a list of misspelled words as output

Input Program Output

Page 16: COMP 110: Introduction to Programming Tyler Johnson January 14, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110 Spring 200916

Programming Languages

Modern programming languages (Java, C/C++) are designed to be human-readable

Called high-level languages

Computers can’t understand high level languages

A Compiler translates our human-readable program into a machine-readable program

Compiler is a program as well!

Page 17: COMP 110: Introduction to Programming Tyler Johnson January 14, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110 Spring 200917

Role of Compilers

Program Source Code

Compiler

Machine Code

Human-readable, What you will be writing in this class

Machine-readable, What the machine actually executes

Note: Java uses a slightly different approach, which we’ll see later

Page 18: COMP 110: Introduction to Programming Tyler Johnson January 14, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110 Spring 200918

Human- vs Machine Readable

A human-readable instruction to add two numbers

a = b + c;

The equivalent machine-readable instruction might look this!

0010110110110010101010110101010101000111101010100101000101001011101001010101010101101010100

Page 19: COMP 110: Introduction to Programming Tyler Johnson January 14, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110 Spring 200919

Review

What we’ve covered so farHardware & MemoryPrograms and Compiling

Now let’s learn a bit about Java

Page 20: COMP 110: Introduction to Programming Tyler Johnson January 14, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110 Spring 200920

Java

A high-level programming language Java source code is human-readable

Originally envisioned as a programming language for home appliancesIs now the language of choice for internet applications

Example internet application using Java

Page 21: COMP 110: Introduction to Programming Tyler Johnson January 14, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110 Spring 200921

Java

Java is an interpreted languageCompiler produces bytecode instead of machine code• Bytecode is not quite readable by any machine

When a Java program is run, an interpreter translates the bytecode into machine code on-the-flyThe Java interpreter is called the Java Virtual Machine (JVM)

Page 22: COMP 110: Introduction to Programming Tyler Johnson January 14, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110 Spring 200922

Compiling & Running Java Programs

Java program

Java compiler

Bytecode program

Bytecode interpreter(Java JVM)

Machine code

Compiling a Java program

Running a Java program

Human-readable

Machine-readable

Java JVM-readable

Page 23: COMP 110: Introduction to Programming Tyler Johnson January 14, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110 Spring 200923

Java Interpreter

Why is Java interpreted?The short answer is portability

Can run the same byte code on any machine!• No need to recompile for Windows or Mac OS X

Ideal for internet applications

Page 24: COMP 110: Introduction to Programming Tyler Johnson January 14, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110 Spring 200924

Your First Java Program

Source code on the next slideDisplays a welcome messageAsks the user to input two numbersDisplays the sum of the numbers

Page 25: COMP 110: Introduction to Programming Tyler Johnson January 14, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110 Spring 200925

Sample Java Program (Section 1.2)

import java.util.Scanner;public class FirstProgram{ public static void main(String[] args) { System.out.println("Hello out there."); System.out.println("I will add two numbers for you."); System.out.println("Enter two whole numbers on a line:");

int n1, n2;

Scanner keyboard = new Scanner(System.in); n1 = keyboard.nextInt(); n2 = keyboard.nextInt();

System.out.println("The sum of those two numbers is"); System.out.println(n1 + n2); }}

Page 26: COMP 110: Introduction to Programming Tyler Johnson January 14, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110 Spring 200926

Importing Packages

import java.util.Scanner;

Tells the compiler that this program uses the class “Scanner” in the package “java.util”

A class is a piece of code that we give a name to

So that when the Scanner class is used later in the program, the compiler knows what we’re referring to

A package is a library of classes that have already been defined for you

“java.util” – for various utilities such as reading input from the keyboard

Page 27: COMP 110: Introduction to Programming Tyler Johnson January 14, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110 Spring 200927

Begin the Program

public class FirstProgram{ …

}

Begin the class “FirstProgram”A class is just a piece of code that we give a name toEverything within the curly braces will be part of “FirstProgram”

Page 28: COMP 110: Introduction to Programming Tyler Johnson January 14, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110 Spring 200929

Main Method

public static void main(String[] args){…}

Begin the method called “main”Everything between the curly braces is part of the method “main”Methods contain lines of code that actually perform some action (statements)The group of statements within a method make up the method bodyEvery Java program has a method called main

Page 29: COMP 110: Introduction to Programming Tyler Johnson January 14, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110 Spring 200930

Program Set-Up

So far we have

import java.util.Scanner;public class FirstProgram{ public static void main(String[] args) { …

}}

As of yet, the code performs no actionNow for the body of the “main” method where the action occurs

Page 30: COMP 110: Introduction to Programming Tyler Johnson January 14, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110 Spring 200931

Output to Screen

System.out.println("Hello out there.");System.out.println("I will add two numbers for you.");System.out.println("Enter two whole numbers on a

line:");

These statements prints what is in quotes out to the screenExample

System.out.println("Hello out there.");

Causes “Hello out there.” to be printed out to screen

Page 31: COMP 110: Introduction to Programming Tyler Johnson January 14, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110 Spring 200932

Methods and Objects

What does “System.out.println” mean?“System.out” is an object

Java programs use objects to perform actionsThe actions are defined by methods

“println” is a method that prints a message to the screen

The println method is part of the System.out object

Page 32: COMP 110: Introduction to Programming Tyler Johnson January 14, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110 Spring 200933

Invoking Methods on Objects

myCar.start()

airplane.land()

System.out.println(“Hi”.)

Objects

Methods

Argument

Page 33: COMP 110: Introduction to Programming Tyler Johnson January 14, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110 Spring 200934

Variable

int n1, n2;

Declares “n1” and “n2” as variables that will be used to store dataint is the data type that will be used for n1 and n2int indicates an integer (whole number)

Page 34: COMP 110: Introduction to Programming Tyler Johnson January 14, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110 Spring 200935

Create Scanner Object

Scanner keyboard = new Scanner(System.in);

Enables the program to read data from the keyboard

Creates an object of Scanner class called “keyboard”

Class Object Class

Page 35: COMP 110: Introduction to Programming Tyler Johnson January 14, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110 Spring 200936

Call Method on Object

n1 = keyboard.nextInt();

Read an integer from the keyboard and store it in n1

Object Method

Invoke/Call

Page 36: COMP 110: Introduction to Programming Tyler Johnson January 14, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110 Spring 200937

Output to Screen

System.out.println("The sum of those two numbers is");System.out.println(n1 + n2);

Add n1 and n2Print the sum to the screen

Page 37: COMP 110: Introduction to Programming Tyler Johnson January 14, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110 Spring 200938

Sample Java Program (Section 1.2)

import java.util.Scanner;public class FirstProgram{ public static void main(String[] args) { System.out.println("Hello out there."); System.out.println("I will add two numbers for you."); System.out.println("Enter two whole numbers on a line:");

int n1, n2;

Scanner keyboard = new Scanner(System.in); n1 = keyboard.nextInt(); n2 = keyboard.nextInt();

System.out.println("The sum of those two numbers is"); System.out.println(n1 + n2); }}

Page 38: COMP 110: Introduction to Programming Tyler Johnson January 14, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110 Spring 200939

Friday

RecitationjGRASPYour first Java program

Bring Laptop (fully charged) Textbook

Download Java Development Kit (JDK) and jGRASP before lab (see webpage)Finish reading Sections 1.1, 1.2

Page 39: COMP 110: Introduction to Programming Tyler Johnson January 14, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110 Spring 200940

Announcements

Hand in HW0 at the front of the room after class