Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Programming with Java

Similar presentations


Presentation on theme: "Introduction to Programming with Java"— Presentation transcript:

1 Introduction to Programming with Java
AP Computer Science ASFA

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 Point out objects in the room (like a book, pen, computer, person, etc). Talk about what type of object it is, what data it has, what it can do. Go through your example real-world scenarios. Point out the objects, their types, data, and operations. The picture is a cashier in a music store. The data is his name and password. The operations are how to “ring up” and “take payment”, and “make change”. 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 Objects are instances of a class. This slides shows two book objects of the class book. Intro-OO

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 And instructions on how to perform:
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 Aiden and Jordan are both Cashier objects Aiden has a different name and id number than Jordan, 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 What is a computer? What is this programming thang? What is Java?
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 who did computations The picture shows the Harvard Computers which were women who did astronomical computations at Harvard University. See See See and for some of the history of computers See for a history of computers. See for some computers you may not be familiar with. Also see

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 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 See for more information on transistors. See for more information on Moore’s Law. See for a picture of the first transistor. See for a history of transistors.

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 Notice that these are things that people aren’t great at.

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 Put your favorite recipe on the coweb ( See if someone else can follow the directions. Write down the directions to your house. See if someone else can follow them to find it. Use LEGO Mindstorms RIS to build a robot and use Robocode to program it.

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 The punch cards had first been used to program looms for weaving see For a short history of programming see

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 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 For more information on Java see See for a sample program. See for Sun’s Java web site. It has documentation and tutorials. See for more on James Gosling.

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 Try doing other direction exercises. Write down directions for how to get to a classroom in the school. Write down directions for how to use a computer program.

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 How does the computer do it?
Part 2 How does the computer do it?

20 Parts of a Computer User Interface Brain - Central Processing Unit
monitor (screen), mouse, keyboard, printer Brain - Central Processing Unit can do math and logic operations Memory - Storage main - RAM secondary – Disks, CD-ROMs, DVDs See and See See for information on the hardware of a computer.

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 Add register A to register B 263 608 Store the value in register C into memory location All processing in a computer program happens in the CPU. See for a nice overview of microprocessors (CPUs). Numbers must be loaded into registers (high-speed storage areas) before they can be processed by the ALU.

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 The program counter can also be changed to “Jump” to another instruction which means that the value in the program counter is changed See for information on how microprocessors work.

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 See for a comparison of processor speeds for several common processors. One megahertz represents one million cycles per second. One gigahertz represents 1 billion cycles per second.

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. See

25 Types of Memory Registers Cache
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) Temporary storage loses the values stored in it after the power is turned off. A megabyte is more than a million bytes. A gigabyte is more than a billion bytes. See for more information on how memory works.

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 * * * 22 = = 5 1010 is 0 * * * * 23 = = 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 See for information about ASCII. Most computers store text in ASCII. ASCII uses 7 or 8 bits to store characters. See for information on EBCDIC. EBCDIC is used on IBM computers. See for information on Unicode which is a 16 bit format that can represent Greek, Chinese and Japanese as well as English characters.

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? Show the students how to do this using binary numbers. Each finger represents a power of two.

29 Binary Numbers We usually work with decimal numbers with digits from 0 to 9 and powers of 10 7313 = (7 * * * * 1) Or (7 * * * * 100) The binary number system uses digits 0 and 1 and powers of 2 0101 = (0 * * * * 1) Or (0 * * * *20) = 5 See for more information on number systems and binary numbers.

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 and = 1 1 + 1 = 0 with a carry of 1 into the next column to the left

31 It’s Hard to Work in Binary
Convert to Octal Base 8 For example: A is 65 in decimal and 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: ? 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) <a href= Java Site </a> A byte of memory might contain the values “off on off off off off off on” which we assign the binary numbers of “ ” which is the encoding for the letter “a” in ASCII which could be part of a link to Sun’s Java web site. Each layer of encoding is handled by software. a off on off off off off off on

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 Introduction to Java, and jGRASP
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 The pane that shows the code (on the right) is called the definitions pane. The pane that allows you to type Java expressions and see the result is called the interactions pane.

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 The compiler interprets 3.0 as a double (floating point number) and 3 and 4 as integers. The result of an operation is in the same type as the values being operated on so the result of 3 / 4 is 0 since and integer can’t represent 0.75. See for arithmetic operators in the Java tutorial

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 To open jGRASP double click on the jGRASP icon. You will see a small window which will show while it loads. It can take a few minutes to load.

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 Floating point means that the number is stored in two parts. The digits are stored (without the period) and the location of the period is stored as a power of 10.

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, , etc Floating point numbers (numbers with fractional parts) are represented by The types: double or float , 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 The primitive types in Java are not objects. Java left some simple data types as primitives to speed computation.

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 A floating-point type (float or double) stores both the digits and the exponent. See for more information on floating-point numbers.

47 Sizes of Primitive Types
8 bits byte 8 bits 8 bits short int 8 bits 8 bits 8 bits 8 bits 8 bits 8 bits 8 bits 8 bits 8 bits 8 bits 8 bits 8 bits long float 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 char

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 Stored as binary numbers in scientific notation is 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 – significant digits accuracy The mantissa is stored in positive binary. The exponent is stored as positive binary but with the addition of the stored value to -126 for float and for a double. So an exponent value of 0 for a float is actually an exponent of -126. See for more information.

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 The symbol ‘=‘ does not test for equal in Java. It is used as an assignment statement. See for the Java tutorial section on relational operators.

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’ Characters are represented as numbers with letters that we think of as coming first in alphabetizing schemes having lower numbers than the later letters.

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 * 2

53 Math Operator Order Exercise – Dr. Java
Try * 4 + 5 Add parentheses to make it clear what is happening first How do you change it so that happens first? How do you change it so that it multiplies the result of and the result of 4 + 5? In jGRASP expressions entered into the interactions pane are evaluated and the result is displayed on the next line. For more information on jGRASP see

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 !!”); }


Download ppt "Introduction to Programming with Java"

Similar presentations


Ads by Google