COMP 110 Introduction to Programming Tyler Johnson January

  • Slides: 39
Download presentation
COMP 110: Introduction to Programming Tyler Johnson January 14, 2009 MWF 11: 00 AM-12:

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

Announcements Hand in HW 0 at the front of the room after class 2

Announcements Hand in HW 0 at the front of the room after class 2 COMP 110 Spring 2009

Questions? 3 COMP 110 Spring 2009

Questions? 3 COMP 110 Spring 2009

Today in COMP 110 Hardware and Memory Programs and Compiling Your first JAVA program!

Today in COMP 110 Hardware and Memory Programs and Compiling Your first JAVA program! 4 COMP 110 Spring 2009

Before Programming Need to know basics of a computer If you drive a car

Before Programming Need to know basics of a computer If you drive a car you should know it runs on gasoline What’s in the box? 5 COMP 110 Spring 2009

Hardware vs. Software Hardware Physical machine CPU, Memory Software Set of instructions for the

Hardware vs. Software Hardware Physical machine CPU, Memory Software Set of instructions for the machine to execute 6 COMP 110 Spring 2009

Hardware ENIAC One of the first general purpose programmable computers Visit the computer museum

Hardware ENIAC One of the first general purpose programmable computers Visit the computer museum in the first floor lobby of Sitterson Hall 7 COMP 110 Spring 2009

Hardware CPU (Central Processing Unit) Executes instructions specified by a programmer GHz - number

Hardware CPU (Central Processing Unit) Executes instructions specified by a programmer GHz - number of instructions per second, how fast is the computer Dual Core - multiple processing units per CPU 8 COMP 110 Spring 2009

Memory Holds data for the computer to process Main Memory (RAM – Random Access

Memory Holds data for the computer to process Main Memory (RAM – Random Access Memory) Used for intermediate calculations Used to store the current program itself! Expensive Auxiliary Memory (Secondary Memory) Disk drives, CDs, Flash drives Cheap 9 COMP 110 Spring 2009

Measuring Memory Measured in bytes For example 2 gigabytes (GB) of RAM Megabyte (MB)

Measuring Memory Measured in bytes For example 2 gigabytes (GB) of RAM Megabyte (MB) = 1 million (106) bytes (or 1, 048, 576 = 220 bytes) Gigabyte (GB) = 1 billion (109) bytes (or 1, 073, 741, 824 = 230 bytes) 10 COMP 110 Spring 2009

What is a Byte? Data, such as numbers and keyboard characters are stored as

What is a Byte? Data, such as numbers and keyboard characters are stored as series of bits A bit is a digit with value 1 or 0 Examples 00111010 is a byte with value 58 01000001 is a byte with value 65 01100001 is a byte with value 97 A byte is composed of 8 bits Just large enough to store a keyboard character 11 COMP 110 Spring 2009

Encoding Characters How to encode keyboard characters? Assign each character a number Example (ASCII)

Encoding Characters How to encode keyboard characters? Assign each character a number Example (ASCII) (01000001) 65 = A (01000010) 66 = B (01000011) 67 = C …. 12 COMP 110 Spring 2009

Main Memory Called RAM (Random Access Memory) Fast access Access any location in memory

Main Memory Called RAM (Random Access Memory) Fast access Access any location in memory in constant time Addressable Every byte has an address that is used when writing or reading data to memory 13 COMP 110 Spring 2009

What is a Program? Set of instructions for a computer to follow Example instructions:

What is a Program? Set of instructions for a computer to follow Example instructions: Add two numbers Divide two numbers Store a number at a memory location 14 COMP 110 Spring 2009

Programs take data as input and produce a useful result Example A spell-checking program

Programs take data as input and produce a useful result Example A spell-checking program takes a text file as input and produces a list of misspelled words as output Input 15 Program Output COMP 110 Spring 2009

Programming Languages Modern programming languages (Java, C/C++) are designed to be human-readable Called high-level

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! 16 COMP 110 Spring 2009

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

Role of Compilers Note: Java uses a slightly different approach, which we’ll see later Program Source Code Human-readable, What you will be writing in this class Compiler Machine Code 17 Machine-readable, What the machine actually executes COMP 110 Spring 2009

Human- vs Machine Readable A human-readable instruction to add two numbers a = b

Human- vs Machine Readable A human-readable instruction to add two numbers a = b + c; The equivalent machine-readable instruction might look this! 0010110110110010101101010001111 0101010001011101001010101 10100 18 COMP 110 Spring 2009

Review What we’ve covered so far Hardware & Memory Programs and Compiling Now let’s

Review What we’ve covered so far Hardware & Memory Programs and Compiling Now let’s learn a bit about Java 19 COMP 110 Spring 2009

Java A high-level programming language Java source code is human-readable Originally envisioned as a

Java A high-level programming language Java source code is human-readable Originally envisioned as a programming language for home appliances Is now the language of choice for internet applications Example internet application using Java 20 COMP 110 Spring 2009

Java is an interpreted language Compiler produces bytecode instead of machine code • Bytecode

Java is an interpreted language Compiler 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-thefly The Java interpreter is called the Java Virtual Machine (JVM) 21 COMP 110 Spring 2009

Compiling & Running Java Programs Compiling a Java program Humanreadable Java JVMreadable 22 Running

Compiling & Running Java Programs Compiling a Java program Humanreadable Java JVMreadable 22 Running a Java program Bytecode interpreter (Java JVM) Java compiler Machine code Machinereadable Bytecode program COMP 110 Spring 2009

Java Interpreter Why is Java interpreted? The short answer is portability Can run the

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 23 COMP 110 Spring 2009

Your First Java Program Source code on the next slide Displays a welcome message

Your First Java Program Source code on the next slide Displays a welcome message Asks the user to input two numbers Displays the sum of the numbers 24 COMP 110 Spring 2009

Sample Java Program (Section 1. 2) import java. util. Scanner; public class First. Program

Sample Java Program (Section 1. 2) import java. util. Scanner; public class First. Program { 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 n 1, n 2; Scanner keyboard = new Scanner(System. in); n 1 = keyboard. next. Int(); n 2 = keyboard. next. Int(); } 25 } System. out. println("The sum of those two numbers is"); System. out. println(n 1 + n 2); COMP 110 Spring 2009

Importing Packages import java. util. Scanner; Tells the compiler that this program uses the

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 26 COMP 110 Spring 2009

Begin the Program public class First. Program { … } Begin the class “First.

Begin the Program public class First. Program { … } Begin the class “First. Program” A class is just a piece of code that we give a name to Everything within the curly braces will be part of “First. Program” 27 COMP 110 Spring 2009

Main Method public static void main(String[] args) { … } Begin the method called

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 body Every Java program has a method called main 29 COMP 110 Spring 2009

Program Set-Up So far we have import java. util. Scanner; public class First. Program

Program Set-Up So far we have import java. util. Scanner; public class First. Program { public static void main(String[] args) { … } } As of yet, the code performs no action Now for the body of the “main” method where the action occurs 30 COMP 110 Spring 2009

Output to Screen System. out. println("Hello out there. "); System. out. println("I will add

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 screen Example System. out. println("Hello out there. "); Causes “Hello out there. ” to be printed out to screen 31 COMP 110 Spring 2009

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

Methods and Objects What does “System. out. println” mean? “System. out” is an object Java programs use objects to perform actions The 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 32 COMP 110 Spring 2009

Invoking Methods on Objects my. Car. start() Objects Methods airplane. land() System. out. println(“Hi”.

Invoking Methods on Objects my. Car. start() Objects Methods airplane. land() System. out. println(“Hi”. ) Argument 33 COMP 110 Spring 2009

Variable int n 1, n 2; Declares “n 1” and “n 2” as variables

Variable int n 1, n 2; Declares “n 1” and “n 2” as variables that will be used to store data int is the data type that will be used for n 1 and n 2 int indicates an integer (whole number) 34 COMP 110 Spring 2009

Create Scanner Object Scanner keyboard = new Scanner(System. in); Class Object Enables the program

Create Scanner Object Scanner keyboard = new Scanner(System. in); Class Object Enables the program to read data from the keyboard Creates an object of Scanner class called “keyboard” 35 COMP 110 Spring 2009

Call Method on Object Method n 1 = keyboard. next. Int(); Invoke/Call Read an

Call Method on Object Method n 1 = keyboard. next. Int(); Invoke/Call Read an integer from the keyboard and store it in n 1 36 COMP 110 Spring 2009

Output to Screen System. out. println("The sum of those two numbers is"); System. out.

Output to Screen System. out. println("The sum of those two numbers is"); System. out. println(n 1 + n 2); Add n 1 and n 2 Print the sum to the screen 37 COMP 110 Spring 2009

Sample Java Program (Section 1. 2) import java. util. Scanner; public class First. Program

Sample Java Program (Section 1. 2) import java. util. Scanner; public class First. Program { 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 n 1, n 2; Scanner keyboard = new Scanner(System. in); n 1 = keyboard. next. Int(); n 2 = keyboard. next. Int(); } 38 } System. out. println("The sum of those two numbers is"); System. out. println(n 1 + n 2); COMP 110 Spring 2009

Friday Recitation j. GRASP Your first Java program Bring Laptop (fully charged) Textbook Download

Friday Recitation j. GRASP Your first Java program Bring Laptop (fully charged) Textbook Download Java Development Kit (JDK) and j. GRASP before lab (see webpage) Finish reading Sections 1. 1, 1. 2 39 COMP 110 Spring 2009

Announcements Hand in HW 0 at the front of the room after class 40

Announcements Hand in HW 0 at the front of the room after class 40 COMP 110 Spring 2009