OWEN COMPUTER SCIENCE LESSONS

Slides:



Advertisements
Similar presentations
Numbers Treasure Hunt Following each question, click on the answer. If correct, the next page will load with a graphic first – these can be used to check.
Advertisements

1 A B C
Chapter 4 Loops Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
5.1 Rules for Exponents Review of Bases and Exponents Zero Exponents
Variations of the Turing Machine
Adders Used to perform addition, subtraction, multiplication, and division (sometimes) Half-adder adds rightmost (least significant) bit Full-adder.
AP STUDY SESSION 2.
1
& dding ubtracting ractions.
Copyright © 2003 Pearson Education, Inc. Slide 1 Computer Systems Organization & Architecture Chapters 8-12 John D. Carpinelli.
Copyright © 2011, Elsevier Inc. All rights reserved. Chapter 6 Author: Julia Richards and R. Scott Hawley.
Prepared by: Workforce Enterprise Services For: The Illinois Department of Commerce and Economic Opportunity Bureau of Workforce Development ENTRY OF EMPLOYER.
Local Customization Chapter 2. Local Customization 2-2 Objectives Customization Considerations Types of Data Elements Location for Locally Defined Data.
Create an Application Title 1Y - Youth Chapter 5.
Process a Customer Chapter 2. Process a Customer 2-2 Objectives Understand what defines a Customer Learn how to check for an existing Customer Learn how.
CALENDAR.
Programming Language Concepts
© Vinny Cahill 1 Writing a Program in Java. © Vinny Cahill 2 The Hello World Program l Want to write a program to print a message on the screen.
The 5S numbers game..
1.
Order of Operations Lesson
Columbus State Community College
Break Time Remaining 10:00.
Factoring Quadratics — ax² + bx + c Topic
Turing Machines.
Lesson 8 – Passing Parameters By John B. Owen All rights reserved ©2011.
Table 12.1: Cash Flows to a Cash and Carry Trading Strategy.
PP Test Review Sections 6-1 to 6-6
Digital Lessons on Factoring
MM4A6c: Apply the law of sines and the law of cosines.
Briana B. Morrison Adapted from William Collins
LIAL HORNSBY SCHNEIDER
Outline Minimum Spanning Tree Maximal Flow Algorithm LP formulation 1.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 24.1 Test-Driving the Ticket Information Application.
Exarte Bezoek aan de Mediacampus Bachelor in de grafische en digitale media April 2014.
Copyright © 2012, Elsevier Inc. All rights Reserved. 1 Chapter 7 Modeling Structure with Blocks.
1 RA III - Regional Training Seminar on CLIMAT&CLIMAT TEMP Reporting Buenos Aires, Argentina, 25 – 27 October 2006 Status of observing programmes in RA.
Chapter 1: Expressions, Equations, & Inequalities
Ch. 1: Number Relationships
Adding Up In Chunks.
MaK_Full ahead loaded 1 Alarm Page Directory (F11)
1 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt Synthetic.
CSci 1130 Intro to Programming in Java
Slide R - 1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Prentice Hall Active Learning Lecture Slides For use with Classroom Response.
Subtraction: Adding UP
1 Non Deterministic Automata. 2 Alphabet = Nondeterministic Finite Accepter (NFA)
1 hi at no doifpi me be go we of at be do go hi if me no of pi we Inorder Traversal Inorder traversal. n Visit the left subtree. n Visit the node. n Visit.
Chapter Three Arithmetic Expressions and Assignment Statements
1 Let’s Recapitulate. 2 Regular Languages DFAs NFAs Regular Expressions Regular Grammars.
Types of selection structures
Speak Up for Safety Dr. Susan Strauss Harassment & Bullying Consultant November 9, 2012.
Essential Cell Biology
Chapter 2 JAVA FUNDAMENTALS
Converting a Fraction to %
Exponents and Radicals
1 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 1 Operators and Expressions.
Clock will move after 1 minute
PSSA Preparation.
Chapter 11 Creating Framed Layouts Principles of Web Design, 4 th Edition.
Lial/Hungerford/Holcomb/Mullins: Mathematics with Applications 11e Finite Mathematics with Applications 11e Copyright ©2015 Pearson Education, Inc. All.
Physics for Scientists & Engineers, 3rd Edition
Copyright Tim Morris/St Stephen's School
9. Two Functions of Two Random Variables
A Data Warehouse Mining Tool Stephen Turner Chris Frala
1 Non Deterministic Automata. 2 Alphabet = Nondeterministic Finite Accepter (NFA)
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 3 Loops.
Multiple variables can be created in one declaration
Presentation transcript:

OWEN COMPUTER SCIENCE LESSONS Lesson 2B – Operations, Characters, Mixing Data Types, Order of Operations By John B. Owen All rights reserved ©2011

Table of Contents Objectives Operators Character primitive, ASCII system Mixing data types Lesson Summary / Labs Acknowledgement of use Lesson 2B - Operations, Characters, Mixing Data Types, Order of Operations

Objectives The student will understand how Java operations work with the various data types The student will have a more in- depth understanding of the char data type Lesson 2B - Operations, Characters, Mixing Data Types, Order of Operations

Objectives The student will also understand how to mix variable types and how the JAVA order of operations works. The JAVA order of operations, PMMDAS is introduced and explored Lesson 2B - Operations, Characters, Mixing Data Types, Order of Operations

BINARY OPERATORS A few of the standard binary operators in JAVA are: = assignment == “is equal to” != “is not equal to” + addition, String concatenation - subtraction * multiplication / division % modulus Lesson 2B - Operations, Characters, Mixing Data Types, Order of Operations

Binary vs Unary They are called “binary” operators because they perform their function on two items, one on each side. There are some operators that are called “unary” because they only operate on one item, like the negative sign, ‘-’, or the NOT symbol, ‘!’ Lesson 2B - Operations, Characters, Mixing Data Types, Order of Operations

Overloaded operators Notice that the ‘+’ operator performs two different functions: Numerical addition String concatenation This is common in programming, called “overloading” the operator, which is a good thing. It means the ‘+’ operator can handle several situations. Lesson 2B - Operations, Characters, Mixing Data Types, Order of Operations

‘=‘ , ‘==‘, and ‘!=‘ The ‘=‘ operator is used to give values to variables and constants. The ‘==‘ operator is used to check for equality. It is important to keep clear the difference between the two. The ‘!=‘ checks to see if two values are NOT equal to each other. Lesson 2B - Operations, Characters, Mixing Data Types, Order of Operations

‘=‘ vs ‘==‘ The most common mistake among novice programmers is to use the ‘=‘ to check for equality…fortunately the compiler will usually catch this for you and not allow it. Lesson 2B - Operations, Characters, Mixing Data Types, Order of Operations

Sample program with operators In the program shown below, you see several examples of operator use. Study the whole program carefully and compare it to the output . Lesson 2B - Operations, Characters, Mixing Data Types, Order of Operations

Sample program with operators Lines 5-10 use the ‘=‘ operator to assign values to the variables num1 through num6. num1 and num2 are each assigned an integer. num3 is assigned the sum of two integers using the ‘+’ operator. The values 7 and 9 are first added together in a temporary memory location, and then the resulting value is placed into the num3 memory location. num1 3 num2 5 num3 16 num4 24 num5 8 num6 -12 Lesson 2B - Operations, Characters, Mixing Data Types, Order of Operations

Sample program with operators The variable num4 is assigned the product (using the ‘*’ operator) of a previously assigned variable, num1, and a literal integer value 8. num5 “gets” (is assigned the value of) the difference (using the subtraction operator) of two variables, num4 and num3. num6 gets the difference between a literal value 12 and the variable num4. num1 3 num2 5 num3 16 num4 24 num5 8 num6 -12 Lesson 2B - Operations, Characters, Mixing Data Types, Order of Operations

Assignment – right to left operation It is very important to understand that an assignment statement is a right to left operation. This simply means that the right side of an assignment statement is always taken care of first by the compiler. Lesson 2B - Operations, Characters, Mixing Data Types, Order of Operations

Assignment – right to left operation If an operation on the right side of the assignment operator is a multi-term expression and needs to be calculated first, it is handled in a temporary location, and then the resulting value is placed into the memory location of the variable. In line 7 below, the values 7 and 9 are first added together, then assigned to num3. num3 16 temp 7+9 Lesson 2B - Operations, Characters, Mixing Data Types, Order of Operations

Assignment – NEVER left to right Finally, it is equally important to realize that an assignment operation is NEVER a left to right action: 7 + 9 = num3 is NOT allowed, as you can see in line 7 of the program below!!!! The compiler will throw another “fit” and report an error statement! The left side of an assignment statement must ALWAYS be a variable or constant. Lesson 2B - Operations, Characters, Mixing Data Types, Order of Operations

Two different output statements Examine closely the first two output statements. They both do the same thing: output three variables with spaces separating each, but they work differently. One is a println statement, and the other a printf. You should be familiar with how the printf statement works from Lesson 1. num1 3 num2 5 num3 16 num4 24 num5 8 num6 -12 Lesson 2B - Operations, Characters, Mixing Data Types, Order of Operations

String concatenation num1 3 num2 5 num3 16 num4 24 num5 8 num6 -12 Notice that the println statement uses “+” operators, but the values are not numerically added. Instead a process called “concatenation” occurs, the joining or connecting of two strings together into a longer string. Here’s how it works… num1 3 num2 5 num3 16 num4 24 num5 8 num6 -12 Lesson 2B - Operations, Characters, Mixing Data Types, Order of Operations

‘+’ operator process num1 3 num2 5 num3 16 num4 24 num5 8 num6 -12 When the ‘+’ operator is about to do its job, it first must determine what two things are involved. In line 7, it sees two integers, therefore it numerically adds the two values. However, in the println statement, the first ‘+’ operator “sees” an integer variable on one side, and a string on the other. num1 3 num2 5 num3 16 num4 24 num5 8 num6 -12 Lesson 2B - Operations, Characters, Mixing Data Types, Order of Operations

‘+’ operator process num1 3 num2 5 num3 16 num4 24 num5 8 num6 -12 When either or both items in a ‘+’ operation are a String, concatenation is performed automatically, creating a new String, in this case, “3 “. The next operator sees the String “3 “ (the result from the first operation) and the variable num2, and creates another String, “3 5“. num1 3 num2 5 num3 16 num4 24 num5 8 num6 -12 Lesson 2B - Operations, Characters, Mixing Data Types, Order of Operations

‘+’ operator process num1 3 num2 5 num3 16 num4 24 num5 8 num6 -12 “3 5 “ and “3 5 16“ complete the four part concatenation process, resulting in one long String, which is then output by the println method. In summary, the println statement waits until the concatenation operations are completed, and then outputs the final result. It DOES NOT output each term one at a time. num1 3 num2 5 num3 16 num4 24 num5 8 num6 -12 Lesson 2B - Operations, Characters, Mixing Data Types, Order of Operations

‘==‘ and ‘!=‘ operators num1 3 num2 5 num3 16 num4 24 num5 8 num6 -12 The ‘==‘ operator compares the values on either side of it, and reports true if they match, false if they don’t. The ‘!=‘ does the opposite…reports true if they don’t match, false if they do. Pretty simple, huh? Word of warning (for now)…only use these operators on primitives, never objects. More on this later. num1 3 num2 5 num3 16 num4 24 num5 8 num6 -12 Lesson 2B - Operations, Characters, Mixing Data Types, Order of Operations

“char” primitive data type Now let’s examine the “char” data type in more detail. As discussed earlier, it belongs to the integer data type family, requires 16 bits to store, and represents characters from the ASCII and Unicode universal system of characters. Characters are simply integers in disguise! Lesson 2B - Operations, Characters, Mixing Data Types, Order of Operations

ASCII vs Unicode ASCII (American Standard Code for Information Interchange) is a system of symbols used in the English language. As technology spread worldwide, the Unicode system developed from the ASCII system, spanning a much broader spectrum of language systems. Lesson 2B - Operations, Characters, Mixing Data Types, Order of Operations

‘A’ ‘a’ ‘0’ The three most important characters you must learn and memorize are ‘A’, ‘a’, and ‘0’(zero). Each one has an integer value associated with it that is a part of the ASCII system. Lesson 2B - Operations, Characters, Mixing Data Types, Order of Operations

‘A’ ‘a’ ‘0’ Here they are. Memorize them! ‘A’ = 65 ‘a’ = 97 ‘0’ = 48 Once you know these, you can easily figure out any other letter or digit. For example, ‘B’ would be 66, ‘b’ = 98, and ‘1’ = 49, and so on. Lesson 2B - Operations, Characters, Mixing Data Types, Order of Operations

charmap Each computer has an application that shows the ASCII/Unicode system. Type charmap at the Run prompt of your computer, and you will get this window: Lesson 2B - Operations, Characters, Mixing Data Types, Order of Operations

charmap As you hover over a letter with the mouse, the hexadecimal value of that character will appear. Lesson 2B - Operations, Characters, Mixing Data Types, Order of Operations

charmap The “hex” value of the capital letter ‘A’ is 0041, which converts to 65 in base 10. Lesson 2B - Operations, Characters, Mixing Data Types, Order of Operations

charmap Another fact you need to learn is that the difference between ‘A’ and ‘a’ is 32. This is true for every letter of the alphabet, and is no accident! It was designed that way for a reason. Lesson 2B - Operations, Characters, Mixing Data Types, Order of Operations

Why a difference of 32? In the significant binary representation of the character ‘A’, 1000001 (the value 64+1, or 65), the sixth bit (from the right) is a zero. For the character ‘a’, 1100001 (the value 64+32+1, or 97), the sixth bit is a one. The value of the sixth position is 32, causing the difference. Lesson 2B - Operations, Characters, Mixing Data Types, Order of Operations

Why a difference of 32? Internally, it is very easy to uppercase or lowercase a letter…just “flick the 6 bit”, that is, change it from 1 to zero, or zero to 1. Bit String Flicking is whole field of study in itself, and will be covered in a lesson we learn discuss later on. We’ll also discuss later on in more detail the binary number system and how it works. If you want to know now, check out this lesson on Computer Number Systems. Lesson 2B - Operations, Characters, Mixing Data Types, Order of Operations

MIXING DATA TYPES Now its time for another brief case study on mixing, or combining different data types. Check out this program, and its output. Lesson 2B - Operations, Characters, Mixing Data Types, Order of Operations

MIXING DATA TYPES: int + ? The integer variable num1 has been added to four different variables: an integer (num2), a double (dub1), a char (char1), and a String (str1). Lesson 2B - Operations, Characters, Mixing Data Types, Order of Operations

MIXING DATA TYPES: int + ? Notice the results! The first one is easy to see…46 is indeed the sum of 3 and 43. Lesson 2B - Operations, Characters, Mixing Data Types, Order of Operations

MIXING DATA TYPES: int + ? The second one mixes an int with a double, and the result is a double. It is always the case that the “mixture” of an int and a double results in a double value. Lesson 2B - Operations, Characters, Mixing Data Types, Order of Operations

MIXING DATA TYPES: int + ? The third output mixes an int with a char, and the result is an int! Since the int value of ‘A’ is 65, 3 + 65 is indeed 68. It is always the case that the “mixture” of an int and a char results in an int value. Lesson 2B - Operations, Characters, Mixing Data Types, Order of Operations

MIXING DATA TYPES: int + ? The fourth output mixes an int with a String, and the result is a String. As we discussed earlier, when a String is involved with the ‘+’ operator with any data type, concatenation occurs, and the result is always a String. Lesson 2B - Operations, Characters, Mixing Data Types, Order of Operations

MIXING DATA TYPES: double + ? Here are the results when using a double. The first three results are doubles, and the last is another String. Lesson 2B - Operations, Characters, Mixing Data Types, Order of Operations

MIXING DATA TYPES: char + ? Here are the results when using a char. Results: int double String Lesson 2B - Operations, Characters, Mixing Data Types, Order of Operations

MIXING DATA TYPES: char + ? Generally speaking, when a char is added, it behaves like an int Exception: concatenation with a String Lesson 2B - Operations, Characters, Mixing Data Types, Order of Operations

MIXING DATA TYPES: String + ? Predictably, every result in this example is a String since a String is involved in each expression. Lesson 2B - Operations, Characters, Mixing Data Types, Order of Operations

Java Order of Operations Java expressions always follow the PMMDAS order of operations: Parentheses first (There is NO exponent operator in Java, but there is a power function called Math.pow – see Lesson 2C) Multiplication, modulus (%) , and division in order from left to right Addition and subtraction, left to right Division and modulus will be covered in a later lesson Lesson 2B - Operations, Characters, Mixing Data Types, Order of Operations

Java Order of Operations Below are two output statements demonstrating order of operations. Do the math in your head and you will see that it does indeed follow order of operations. Also notice that you can involve a String in the mix, but only in certain situations. Lesson 2B - Operations, Characters, Mixing Data Types, Order of Operations

Java Order of Operations When the third output line is uncommented and compiled, an error message is thrown by the compiler. Can you figure out why? Lesson 2B - Operations, Characters, Mixing Data Types, Order of Operations

Java Order of Operations Here is what happened… The multiplication, division, and addition operations all occurred legally, but the subtraction encountered an illegal situation…String - double Unlike addition, which is overloaded to handle Strings, the subtraction operator is not, and when the String result from the addition operation attempted to subtract the double result of the multiplication and division operations, the compiler threw the error. Lesson 2B - Operations, Characters, Mixing Data Types, Order of Operations

Java Order of Operations You might be wondering why the ‘^’ is pointing to the division symbol instead of to the subtraction symbol. Here’s why…the division operator was the last operation performed successfully by the compiler before it stopped to throw the error. Lesson 2B - Operations, Characters, Mixing Data Types, Order of Operations

Lesson Summary This lesson discussed several basic operations involved with the main Java data types. It also explored results when different data types were mixed together in operation expressions. The char data type was explored in more detail, introducing the ASCII and Unicode systems. Lesson 2B - Operations, Characters, Mixing Data Types, Order of Operations

Lab 2B-1 WAP (write a program) to output the answer to the following calculation: 3+7*6. The three values are first to be initialized as constants, NUM1, NUM2, and NUM3. The value 45 should be the result of the expression assigned to a variable called answer . NO LITERAL VALUES SHOULD ACTUALLY BE SEEN IN THE OUTPUT STATEMENT. Indent output 20 spaces. Required output is shown below.   ****.****.****.****.* 3 + 7 * 6 = 45 Lesson 2B - Operations, Characters, Mixing Data Types, Order of Operations

Lab 2B-2 WAP (write a program) to do the following: Assign each of the letters of your first name (not mine!!!) to a char variable. Use let1, let2, let3, etc. as the variable names of the characters. Output the contents of each variable, all on one line, with 2 spaces in between each letter. Below each letter, output the ASCII value of that letter. Hint: System.out.println ((int)let1 + (int)let2); Then add up all the ASCII values of the letters and store the result in an int variable, and then output that sum. Indent all lines 7 spaces. Required output is shown below, using your name, of course.  ****.*** J O H N 74 79 72 78 303 Lesson 2B - Operations, Characters, Mixing Data Types, Order of Operations

Lab 2B-3 WAP to output to 3 decimal places the answer to the following calculation: (9.2 - 8.45) + 0.29 * 27. Each value should be defined as a constant. The final result must be a calculation using the four constants, the final answer assigned to a variable. No literal values should appear in the output statement. Indent 15 spaces (with the open parenthesis in column 16), and separate all operands and operators with exactly one space. ****.****.****.* (9.200 - 8.450) + 0.290 * 27.000 = 8.580 Lesson 2B - Operations, Characters, Mixing Data Types, Order of Operations

CONGRATULATIONS! You now have a greater understanding of the operations used in JAVA, the char primitive data type, and the various results when mixing different data types, and how JAVA performs the order of operations Lesson 2C will go further with operations, show some operator shortcuts, introduce the casting technique, and introduce a few of the more commonly used Math class methods. Lesson 2B - Operations, Characters, Mixing Data Types, Order of Operations

Acknowledgement of use Please copy, paste, and customize the brief acknowledgement shown below in an email to jowen@acisd.org, indicating that you used this lesson. Feel free to include any comments you wish to offer, positive or negative. “I, (your name), from (educational institution) in (city, state, country) used Lesson 2B on (date), understand, respect and honor all of the rights claimed by the author, Mr. John Owen, and offer the following comments..._______________.” “THANK YOU!” Lesson 2B - Operations, Characters, Mixing Data Types, Order of Operations