Introduction to Programming with Java AP Computer Science ASFA.

55
Introduction to Programming with Java AP Computer Science ASFA

Transcript of Introduction to Programming with Java AP Computer Science ASFA.

Page 1: Introduction to Programming with Java AP Computer Science ASFA.

Introduction to Programmingwith Java

AP Computer ScienceASFA

Page 2: Introduction to Programming with Java AP Computer Science ASFA.

Intro-OO 2

What is an Object?

• A person, place, or thing– That knows something about

itself• Has data (attributes, fields)• A cashier has a id, name, and a

password

– And can do something• Has operations (methods)• A cashier can total the items,

take payment, make change

Page 3: Introduction to Programming with Java AP Computer Science ASFA.

Intro-OO 3

What is a Class?

• The type of an object – The way we classify

an object• “The Idiot” by

Dostoevsky is a book• “War and Peace” by

Tolstoy is a book• Caitlin is a cashier• Kheri is a cashier

• Grouping of objects with the same data and operations

Page 4: Introduction to Programming with Java AP Computer Science ASFA.

• We will use the Java language to model our objects on the computer

• Cashier has attributes:– Name – Id number

• Cashier can perform:– Make change– Calculate order total

Page 5: Introduction to Programming with Java AP Computer Science ASFA.

This would be modeled in Java as a Cashier class with memory set aside to store:– Name – Id number

And instructions on how to perform:– Make change– Calculate order total

Alexander and Jefferson are both Cashier objects

Alexander has a different name and id number than Jefferson, but they both make change the same way

Page 6: Introduction to Programming with Java AP Computer Science ASFA.

Now Let’s Look at the Small Steps

• How are we going to make this happen through a computer program?

Page 7: Introduction to Programming with Java AP Computer Science ASFA.

Part 1

What is a computer?

What is this programming thang?

What is Java?

Page 8: Introduction to Programming with Java AP Computer Science ASFA.

What is a Computer?

• A device that performs high-speed mathematical and/or logical operations or that assembles, stores, correlates, or otherwise processes information.

• The first computers were people – who did computations

Page 9: Introduction to Programming with Java AP Computer Science ASFA.

Why Do Computers Keep Getting Cheaper?

• The number of transistors (a major component of the brain of a computer) at the same price doubles every 18 months– making computers faster, smaller, and

cheaper over time• This notion is know as Moore’s Law

– For Gordon Moore, a founder of Intel• This “Law” has held true for decades

– And is predicted to hold true at least one more

Page 10: Introduction to Programming with Java AP Computer Science ASFA.

What are Computers Good At?

• Doing calculations and comparisons• Producing the same answer every time

– Like calculating the sum of hundreds of numbers

• Storing information – They don’t forget information

• Looking up information quickly– Search through a phone book to find the

customer name for a phone number

Page 11: Introduction to Programming with Java AP Computer Science ASFA.

What is Programming?• Creating detailed

instructions that a computer can execute to accomplish some task.– Like writing a recipe

for your favorite dish– Or giving someone

directions to your house

– Or making a robot do what you want

Page 12: Introduction to Programming with Java AP Computer Science ASFA.

Early Programming

• Early computers required the programmer to set switches and move wires– Which represented a

series of 1’s and 0’s

• Later computers were programmed using punched cards

Page 13: Introduction to Programming with Java AP Computer Science ASFA.

Language Evolution

• Early languages were based on how to do instructions on each machine– 1’s and 0’s to add, subtract, read, store, etc

• Assembler allowed you to write programs using names for instructions and memory– But still translated into machine language

• Higher-level languages– Are compiled into machine language or virtual

machine language (Java)

Page 14: Introduction to Programming with Java AP Computer Science ASFA.

Java

• Developed at Sun in the early 1990s– Invented by James Gosling

• Similar to C++ in syntax but easier to use– Less likely to crash– Automatic memory management

• Cross-platform, object-oriented language• One of the fastest adopted technologies of

all time• Current favorite language, for now

Page 15: Introduction to Programming with Java AP Computer Science ASFA.

Which Language?

• All high-level languages are eventually translated into machine language

• You can write the same program in any language– The computer doesn’t care what high-level

language you use• The language matters to the programmer

– How long does it take to write the program?– How hard is it to change the program?– How long does it take to execute?

Page 16: Introduction to Programming with Java AP Computer Science ASFA.

Why Don’t We Just Use English?

• English is good for communication between two intelligent humans– Even then we sometimes don’t

understand

• Computers are very stupid– They basically know how to

add, compare, store, and load– Programs are very detailed

instructions• Everything must be precise

and unambiguous

Page 17: Introduction to Programming with Java AP Computer Science ASFA.

Programming Exercise

• Write down instructions for how to make a paper airplane

• Have your partner read the directions and make an airplane– stop anytime anything

isn’t clear and ask for clarification

Page 18: Introduction to Programming with Java AP Computer Science ASFA.

Summary – Part 1

• Computers perform calculations and comparisons

• A program is a series of instructions to a computer

• Programming has changed from moving wires to writing textual programs that can be stored and executed several times

• Java is a high level popular programming language

Page 19: Introduction to Programming with Java AP Computer Science ASFA.

Part 2

How does the computer do it?

Page 20: Introduction to Programming with Java AP Computer Science ASFA.

Parts of a Computer• User Interface

– monitor (screen), mouse, keyboard, printer

• Brain - Central Processing Unit – can do math and logic

operations• Memory - Storage

– main - RAM– secondary – Disks,

CD-ROMs, DVDs

Page 21: Introduction to Programming with Java AP Computer Science ASFA.

CPU – Brain of the Computer

• Arithmetic/Logic Unit (ALU)– Does math and logic

calculations on numbers in registers

• Control Unit– Reads instructions

from memory and decodes and executes them using the ALU

345

263Add register A to register B

608 Store the value in register C into memory location320843202

Page 22: Introduction to Programming with Java AP Computer Science ASFA.

Fetch, Decode, Execute Cycle

• The control unit reads (fetches) an instruction from memory

• The control unit decodes the instruction and sets up the hardware to do the instruction – like add the values in the A and B registers

and put the result in the C register• The instruction is executed • The program counter is incremented to

read the next instruction

Page 23: Introduction to Programming with Java AP Computer Science ASFA.

Processor Speed• Processors (CPUs) have

a clock• Clock speed is measured

in megahertz (MHz) or gigahertz (GHz)

• Some instructions take just 2-3 clock cycles, some take more

• When the clock speed increases the computer can execute instructions faster

Page 24: Introduction to Programming with Java AP Computer Science ASFA.

Memory• Computer memory is

used to store data • The smallest unit of

memory is a bit (Binary digIT)

• A bit can be off (no voltage) or on (has voltage) which we interpret to be 0 or 1

• Memory is organized into 8 bit contiguous groups called bytes. A megabyte is 1 million bytes. A gigabyte is 1 billion bytes.

Page 25: Introduction to Programming with Java AP Computer Science ASFA.

Types of Memory• Registers

– Very high speed temporary storage areas for use in the CPU– Used for calculations and comparisons

• Cache– High speed temporary storage for use with the CPU

• Main Memory – Random-access Memory (RAM)– High speed temporary storage – Contains programs and data currently being used– Often described in Megabytes (MB)

• Secondary Memory - Disks– Contains programs and data not currently being used– Often described in Gigabytes (GB)

Page 26: Introduction to Programming with Java AP Computer Science ASFA.

Why are there so many types of memory?

• The faster memory is the more it costs– So we reduce the cost by using small

amounts of expensive memory (registers, cache, and RAM) and large amounts of cheaper memory (disks)

• Why do we need cache?– Processors are very fast and need quick

access to lots of data– Cache provides quick access to data from

RAM

Page 27: Introduction to Programming with Java AP Computer Science ASFA.

How does Memory Represent Values?

• The different patterns of the on and off bits in a byte determine the value stored

• Numbers are stored using binary numbers– 101 is 1 * 20 + 0 * 21 + 1 * 22 = 1 + 4 = 5– 1010 is 0 * 20 + 1 * 21 + 0 * 22 + 1 * 23 = 2 + 8 = 10

• Characters are internally represented as numbers– Different numbers represent different characters– There are several systems for assigning numbers to

characters: • ASCII, EBCDIC, and Unicode

Page 28: Introduction to Programming with Java AP Computer Science ASFA.

Binary Exercise

• Count as high as you can using just the fingers on one hand– You have to count up by ones

• No counting by 10s

– The fingers can be up or down • No in-between states

• How high can you count?

Page 29: Introduction to Programming with Java AP Computer Science ASFA.

Binary Numbers

• We usually work with decimal numbers with digits from 0 to 9 and powers of 10

7313 = (7 * 1000 + 3 * 100 + 1 * 10 + 3 * 1)

Or (7 * 103 + 3 * 102 + 1 * 101 + 3 * 100)• The binary number system uses digits 0

and 1 and powers of 20101 = (0 * 8 + 1 * 4 + 0 * 2 + 1 * 1)

Or (0 * 23 + 1 * 22 + 0 * 21 + 1 *20)

= 5

Page 30: Introduction to Programming with Java AP Computer Science ASFA.

Binary Addition• To add two decimal numbers you add the digits and if the

total is greater than ten you carry the one into the next column

• To add two binary numbers – 0 + 0 = 0– 0 + 1 and 1 + 0 = 1– 1 + 1 = 0 with a carry of 1 into the next column to the left

00 10 111

01 01 001

---- --- ------

01 11 1000

00111001010

01010101101

-------------------

10001110111

Page 31: Introduction to Programming with Java AP Computer Science ASFA.

It’s Hard to Work in Binary

• Convert to Octal– Base 8

– For example:– A is 65 in decimal and

0101 in octal

– What is q in octal?

0161

• Or Hexidecimal– Base 16

– For example:– A is 65 in decimal and

0x41 in hexidecimal

– What is a space in hex?

0x20

Page 32: Introduction to Programming with Java AP Computer Science ASFA.

Decimal Number Storage

• How do you think a computer stores: 3205.406?– It uses an IEEE 754 format – Converts the number into a value between 0

and 1 raised to a power of 2– Stores 3 things

• A bit for the sign• A number between 0 and 1 (the mantissa)• The power of 2 (the exponent)

Page 33: Introduction to Programming with Java AP Computer Science ASFA.

Encode and Decode Exercise

• Use Unicode to write a secret message in decimal and then exchange it with another person– See Appendix B in the Java Concepts

textbook for the decimal values for characters

Page 34: Introduction to Programming with Java AP Computer Science ASFA.

Encodings Make Computer Powerful

• Voltages are interpreted as numbers• Numbers can be interpreted as characters• Characters can be interpreted to be part of

a link to Sun’s Java Site (for example)

0100 0001

off on off off off off off on

a

<a href=http://java.sun.com>Sun’s Java Site </a>

Page 35: Introduction to Programming with Java AP Computer Science ASFA.

Summary – Part 2• Computers are commonplace and very

important to modern life• Computers are made up of parts

– CPU – calculation and comparisons– Memory – temp storage– Disk – permanent storage– Monitor – Display – Keyboard and mouse – User input

• All data in a computer is stored in bits– More data takes more bits– All characters can be represented as a series of 0s and 1s

Page 36: Introduction to Programming with Java AP Computer Science ASFA.

Introduction to Java, and DrJavapart 3

Page 37: Introduction to Programming with Java AP Computer Science ASFA.

Part 3

Introduction to Java, and DrJava

Page 38: Introduction to Programming with Java AP Computer Science ASFA.

What is DrJava?• DrJava is a free

integrated development environment for doing Java programming– From Rice University– It is written in Java

• It has several window panes in it– For creating programs

(definitions pane)– For trying out Java code

(interactions pane)– Listing of open files (files

pane)

Page 39: Introduction to Programming with Java AP Computer Science ASFA.

Dr. Java

• We will use Dr. Java to create our Java programs

• But, first, we will use it to try out some mathematical operations

Page 40: Introduction to Programming with Java AP Computer Science ASFA.

Math Operators in Java (+ * / - %)• Addition

3 + 4• Multiplication

3 * 4• Division

3 / 4 • Subtraction

3 – 4 • Negation

-4• Modulo (Remainder)

10 % 2 and 11 % 2

Page 41: Introduction to Programming with Java AP Computer Science ASFA.

Math Operators Exercise

• Open DrJava and do the following in the interactions pane– Subtract 7 from 9– Add 7 to 3– Divide 3 by 2– Divide 4.6 by 2– Multiply 5 by 10– Find the remainder

when you divide 10 by 3

Page 42: Introduction to Programming with Java AP Computer Science ASFA.

Why is the result of 3 / 2 = 1?• Java is a strongly typed language

– Each value has a type associated with it– Tells the computer how to interpret the number

• It is an integer, floating point, letter, etc

• The compiler determines the type if it isn’t specified (literals)– 3 is an integer– 3.0 is a floating point number (has a fractional part)

• The result of an operation is in the same type as the operands– 3 and 2 are integers so the answer is an integer 1

Page 43: Introduction to Programming with Java AP Computer Science ASFA.

Casting• There are other ways to solve the problem of 3 /

2 has a result of 1• You can make one of the values floating point by

adding .0– 3.0 / 2– 3 / 2.0

• The result type will then be floating point• Or you can cast one of the values to the

primitive types: float or double– (double) 3 / 2– 3 / (float) 2

Page 44: Introduction to Programming with Java AP Computer Science ASFA.

Casting Exercise

• Use casting to get the values right for splitting up a bill for 3 people of 19 dollars.

• Try it first by hand• Try it in DrJava without casting • Try it in DrJava with casting

Page 45: Introduction to Programming with Java AP Computer Science ASFA.

Java Primitive Types– Integers (numbers without fractional parts) are

represented by• The types: int or short or long• 235, -2, 33992093, etc

– Floating point numbers (numbers with fractional parts) are represented by• The types: double or float• 3.233038983 -423.9, etc

– A single character is represented by • The type: char• ‘a’ ‘b’ ‘A’ etc

– True and false values are represented by• The type: boolean• true or false

Page 46: Introduction to Programming with Java AP Computer Science ASFA.

Why so Many Different Types?

• They take up different amounts of space• They have different precisions• Usually use int, double, and boolean

– byte uses 8 bits (1 byte) 2’s compliment– short uses 16 bits (2 bytes) 2’s compliment– int uses 32 bits (4 bytes) 2’s compliment– long uses 64 bits (8 bytes) 2’s compliment– float uses 32 bits (4 bytes) IEEE 754– double uses 64 bits (8 bytes) IEEE 754– char uses 16 bits (2 bytes) Unicode format

Page 47: Introduction to Programming with Java AP Computer Science ASFA.

Sizes of Primitive Types

byte 8 bits

short

int

float

long

8 bits 8 bits

8 bits 8 bits 8 bits 8 bits

8 bits 8 bits 8 bits 8 bits

8 bits 8 bits 8 bits 8 bits 8 bits 8 bits 8 bits 8 bits

double 8 bits 8 bits 8 bits 8 bits 8 bits 8 bits 8 bits 8 bits

char 8 bits 8 bits

Page 48: Introduction to Programming with Java AP Computer Science ASFA.

Types Exercise

• Which type(s) take up the most space?• Which type(s) take up the least space?• What type would you use for

– The number of people in your family– A grade– The price of an item– The answer to do you have insurance– The number of people in the class– The number of people in your school– The number of people in your state

Page 49: Introduction to Programming with Java AP Computer Science ASFA.

Floating Point Numbers

• Numbers with a fractional part– 6170.20389

• Stored as binary numbers in scientific notation -52.202 is -5.2202 x 101

– The sign (1 bit)– The digits in the number (mantissa)– The exponent (8 bits)

• Two types– float – 6-7 significant digits accuracy– double – 14-15 significant digits accuracy

Page 50: Introduction to Programming with Java AP Computer Science ASFA.

Comparison (Relational) Operators• Greater than >

– 4 > 3 is true– 3 > 3 is false– 3 > 4 is false

• Less than < – 2 < 3 is true – 3 < 2 is false

• Equal ==– 3 == 3 is true– 3 == 4 is false

• Not equal !=– 3 != 4 is true– 3 != 3 is false

• Greater than or equal >=– 3 >= 4 is true– 3 >= 3 is true– 2 >= 4 is false

• Less than or equal <=– 2 <= 3 is true– 2 <= 2 is true– 4 <= 2 is false

Page 51: Introduction to Programming with Java AP Computer Science ASFA.

Comparison Operators Exercise

• In DrJava– Try out the comparison operators in the

interactions pane• with numbers

3 < 44 <= 45 < 46 == 6.0

• with characters (single alphabet letter)Put single quote around a character‘a’ < ‘b’‘b’ < ‘a’‘a’ == ‘a’

Page 52: Introduction to Programming with Java AP Computer Science ASFA.

Operator Order

• The default evaluation order is – Negation -– Multiplication *– Division /– Modulo (remainder) %– Addition +– Subtraction -

• The default order can be changed– By using parenthesis– (3 + 4) * 2 versus 3 + 4 * 2

Page 53: Introduction to Programming with Java AP Computer Science ASFA.

Math Operator Order Exercise – Dr. Java

• Try 2 + 3 * 4 + 5• Add parentheses to make it clear what is

happening first• How do you change it so that 2 + 3

happens first?• How do you change it so that it multiplies

the result of 2 + 3 and the result of 4 + 5?

Page 54: Introduction to Programming with Java AP Computer Science ASFA.

Summary – Part 3• Computers

– Can do math– Can execute billions of instructions per second– Keep getting faster, smaller, and cheaper

• Java has typical math and relational operators• Java is a strongly typed language

– This can lead to odd results • integer division gives a integer result

– You can use casting to solve this• Java has primitive types to represent integer and floating

point numbers• Math operations have a default order

– You can specify the order with parentheses

Page 55: Introduction to Programming with Java AP Computer Science ASFA.

Georgia Institute of Technology

Hello World!! Our first Java program

public class FirstProgram

{

public static void main( String[] args)

{

System.out.println(“ Hello World !!”);

}

}