1 / 55

Introduction to Programming with Java

Introduction to Programming with Java. AP Computer Science ASFA. 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)

kenaz
Download Presentation

Introduction to Programming with Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Programmingwith 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 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 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. 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 • 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 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

  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 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 Add register A to register B 263 Store the value in register C into memory location 320843202 608

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

  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 * 103 + 3 * 102 + 1 * 101 + 3 * 100) • The binary number system uses digits 0 and 1 and powers of 2 0101 = (0 * 8 + 1 * 4 + 0 * 2 + 1 * 1) Or (0 * 23 + 1 * 22 + 0 * 21 + 1 *20) = 5

  30. 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 Binary Addition

  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 (for example) <a href=http://java.sun.com>Sun’s Java Site </a> a 0100 0001 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 DrJavapart 3

  37. Part 3 Introduction to Java, and DrJava

  38. 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)

  39. Dr. Java • We will use Dr. Java 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 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

  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 DrJava without casting • Try it in DrJava 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. 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 8 bits 8 bits 8 bits 8 bits 8 bits 8 bits 8 bits 8 bits 8 bits 8 bits 8 bits Sizes of Primitive Types byte short int long float double 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 • 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

  50. 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 Comparison (Relational) Operators

More Related