Introduction to Programming with Java AP Computer Science ASFA.

Slides:



Advertisements
Similar presentations
The Central Processing Unit: What Goes on Inside the Computer.
Advertisements

ENGR2216 FORTRAN PROGRAMMING FOR ENGINEERS. Chapter 1 The computer CPU MEMORY INPUT/OUTPUT DEVICES DATA REPRESENTATION BINARY SYSTEM OCTAL & HEXADECIMAL.
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. COMPSCI 125 Introduction to Computer Science I.
CSCE 121, Sec 200, 507, 508 Fall 2010 Prof. Jennifer L. Welch.
Introduction to Computers and Programming. Some definitions Algorithm: –A procedure for solving a problem –A sequence of discrete steps that defines such.
COMP 14 Introduction to Programming
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. COMPSCI 125 Introduction to Computer Science I.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Chapter 2 Data Types, Declarations, and Displays
Introduction to Computers and Programming. Some definitions Algorithm: Algorithm: A procedure for solving a problem A procedure for solving a problem.
Computer Applications NCBS Stage 1. The Central Processing UnitSlide 2Computer Applications Stage 1 Course Content and Assessment Practical – 60% (2 Hrs.
Intro CS, Computers, Programming Introduction to Computer Science, Computers, and Programming Barbara Ericson Georgia Tech Aug 2009.
The Study of Computer Science Chapter 0 Intro to Computer Science CS1510, Section 2.
Aug CMSC 104, LECT-021 Machine Architecture and Number Systems Some material in this presentation is borrowed form Adrian Ilie From The UNIVERSITY.
01- Intro-Java-part1 1 Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology June 2008.
Abstraction – Number Systems and Data Representation.
Topics Introduction Hardware and Software How Computers Store Data
Georgia Institute of Technology Introduction to Programming Part 2 Barb Ericson Georgia Institute of Technology May 2006.
The Computer Systems By : Prabir Nandi Computer Instructor KV Lumding.
Technology in Focus: Under the Hood
1 TOPIC 1 INTRODUCTION TO COMPUTER SCIENCE AND PROGRAMMING Topic 1 Introduction to Computer Science and Programming Notes adapted from Introduction to.
Lecture 11: Machine Processing Intro to IT COSC1078 Introduction to Information Technology Lecture 11 Machine Processing James Harland
Lesson 2 — How Does A Computer Process Data?
The Study of Computer Science Chapter 0 Intro to Computer Science CS1510.
Georgia Institute of Technology Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology Aug 2005.
An Introduction to Computers August 12, 2008 Mrs. C. Furman.
1 Introduction to Computers Prof. Sokol Computer and Information Science Brooklyn College.
Copyright © 2003 by Prentice Hall Module 5 Central Processing Unit 1. Binary representation of data 2. The components of the CPU 3. CPU and Instruction.
Computer Systems Organization CS 1428 Foundations of Computer Science.
Computers Are Your Future Eleventh Edition Chapter 2: Inside the System Unit Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall1.
An overview of Computer System
Introduction to Computer Systems and the Java Programming Language.
Georgia Institute of Technology Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology June 2005.
Computer Organization & Assembly Language © by DR. M. Amer.
Computer Science Binary. Binary Code Remember the power supply that is inside your computer and how it sends electricity to all of the components? That.
Georgia Institute of Technology Workshop for Programming And Systems Management Teachers Chapter 1 Introduction to Computers and Programming.
1 TOPIC 1 INTRODUCTION TO COMPUTER SCIENCE AND PROGRAMMING Topic 1 Introduction to Computer Science and Programming Notes adapted from Introduction to.
Java Camp II (Jun th ) Instructor: Dr. Chengcui Zhang TAs: Sangita Pillay: Soma.
Computer Systems. Bits Computers represent information as patterns of bits A bit (binary digit) is either 0 or 1 –binary  “two states” true and false,
Georgia Institute of Technology Introduction to Programming Part 1 Barb Ericson Georgia Institute of Technology Aug 2005.
COMPUTER SYSTEM A computer system is define as combination of components designed to process data and store files. A computer system consists of four.
 A computer is an electronic device that receives data (input), processes data, stores data, and produces a result (output).  It performs only three.
Georgia Institute of Technology Introduction to Java, and DrJava part 1 Dr Usman Saeed Assistant Professor Faculty of Computing and Information Technology.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Computer Systems Salihu Ibrahim Dasuki (PhD) CSC102 INTRODUCTION TO COMPUTER SCIENCE.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
The Study of Computer Science Chapter 0
Introduction to Programming with Java
Introduction to Programming Part 1
Introduction to Programming Part 2
The Central Processing Unit
The Study of Computer Science Chapter 0
Multiple variables can be created in one declaration
The Study of Computer Science
Introduction to Programming Part 1
A451: Computer Systems and Programming
Introduction to Java, and DrJava part 1
Topics Introduction Hardware and Software How Computers Store Data
Introduction to Programming Part 2
The Study of Computer Science
Introduction to Java, and DrJava
Abstraction – Number Systems and Data Representation
Introduction to Java, and DrJava
Introduction to Java, and DrJava part 1
The Study of Computer Science Chapter 0
Presentation transcript:

Introduction to Programming with Java AP Computer Science ASFA

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

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

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

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

Now Let’s Look at the Small Steps How are we going to make this happen through a computer program?

Part 1 What is a computer? What is this programming thang? What is Java?

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

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

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

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

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

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)

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

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?

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

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

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

Part 2 How does the computer do it?

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

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

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

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

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.

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)

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

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 2 = = 5 –1010 is 0 * * * * 2 3 = = 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

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?

Binary Numbers We usually work with decimal numbers with digits from 0 to 9 and powers of = (7 * * * * 1) Or (7 * * * * 10 0 ) The binary number system uses digits 0 and 1 and powers of = (0 * * * * 1) Or (0 * * * *2 0 ) = 5

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

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

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)

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

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 off on off off off off off on a Sun’s Java Site

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

Introduction to Java, and jGRASP part 3

Part 3 Introduction to Java, and jGRASP

What is jGRASP? jGRASP is a free integrated development environment for doing Java programming –From Auburn University

jGRASP We will use jGRASP to create our Java programs But, first, we will use it to try out some mathematical operations

Math Operators in Java (+ * / - %) Addition Multiplication 3 * 4 Division 3 / 4 Subtraction 3 – 4 Negation -4 Modulo (Remainder) 10 % 2 and 11 % 2

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

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

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

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

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

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

Sizes of Primitive Types byte 8 bits short int float long 8 bits double 8 bits char 8 bits

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

Floating Point Numbers Numbers with a fractional part – Stored as binary numbers in scientific notation is 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 – significant digits accuracy

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

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’

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

Math Operator Order Exercise – Dr. Java Try * 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?

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

Hello World!! Our first Java program public class FirstProgram { public static void main( String[] args) { System.out.println(“ Hello World !!”); }