Download presentation
Presentation is loading. Please wait.
Published byJason Dalton Modified over 9 years ago
1
Introduction to Programming with Java AP Computer Science ASFA
2
Intro-OO2 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
3
Intro-OO3 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
4
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
5
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
6
Now Let’s Look at the Small Steps How are we going to make this happen through a computer program?
7
Part 1 What is a computer? What is this programming thang? What is Java?
8
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 people –who did computations
9
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 monthstransistors –making computers faster, smaller, and cheaper over time This notion is know as Moore’s LawMoore’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
10
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
11
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
12
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
13
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)
14
Java Developed at Sun in the early 1990s –Invented by James GoslingJames 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
15
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?
16
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
17
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
18
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
19
Part 2 How does the computer do it?
20
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
21
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 263 Add register A to register B 608 Store the value in register C into memory location 320843202
22
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
23
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
24
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.
25
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)
26
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
27
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 * 2 0 + 0 * 2 1 + 1 * 2 2 = 1 + 4 = 5 –1010 is 0 * 2 0 + 1 * 2 1 + 0 * 2 2 + 1 * 2 3 = 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
28
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?
29
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 * 10 3 + 3 * 10 2 + 1 * 10 1 + 3 * 10 0 ) The binary number system uses digits 0 and 1 and powers of 2 0101 = (0 * 8 + 1 * 4 + 0 * 2 + 1 * 1) Or (0 * 2 3 + 1 * 2 2 + 0 * 2 1 + 1 *2 0 ) = 5
30
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
31
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
32
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)
33
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
34
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 (formerly)Sun’s Java Site 0100 0001 off on off off off off off on a Sun’s Java Site http://java.sun.com
35
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
36
Introduction to Java, and jGRASP part 3
37
Part 3 Introduction to Java, and jGRASP
38
What is jGRASP? jGRASP is a free integrated development environment for doing Java programming –From Auburn University
39
jGRASP We will use jGRASP to create our Java programs But, first, we will use it to try out some mathematical operations
40
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
41
Math Operators Exercise Open jGRASP 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 remainder when you divide 10 by 3
42
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
43
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
44
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 jGRASP without casting Try it in jGRASP with casting
45
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
46
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
47
Sizes of Primitive Types byte 8 bits short int float long 8 bits double 8 bits char 8 bits
48
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
49
Floating Point Numbers Numbers with a fractional part –6170.20389 Stored as binary numbers in scientific notation -52.202 is -5.2202 x 10 1 –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
50
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
51
Comparison Operators Exercise In jGRASP –Try out the comparison operators in the interactions pane with numbers 3 < 4 4 <= 4 5 < 4 6 == 6.0 with characters (single alphabet letter) Put single quote around a character ‘a’ < ‘b’ ‘b’ < ‘a’ ‘a’ == ‘a’
52
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
53
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?
54
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
55
Hello World!! Our first Java program public class FirstProgram { public static void main( String[] args) { System.out.println(“ Hello World !!”); }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.