Computer Science 1 02 APrimitive Types Expressions 1122020

  • Slides: 33
Download presentation
Computer Science 1 02 A-Primitive Types & Expressions 11/2/2020 CS 1 s - 02

Computer Science 1 02 A-Primitive Types & Expressions 11/2/2020 CS 1 s - 02 A-Primitive Types & Expressions (v 2. 00) 1

Number Bases • The most commonly used bases for numbers in computer science are:

Number Bases • The most commonly used bases for numbers in computer science are: – Binary (base 2) Alphabet: 0 1 Example: 10012, 01102, 12 – Octal (base 8) Alphabet: 0 1 2 4 5 6 7 Example: 78, 08, 4608 – Decimal (base 10): Alphabet: 0 1 2 3 4 5 6 7 8 9 Example: 10010, 200410 – Hexadecimal (base 16): Alphabet: 0 1 2 3 4 5 6 7 8 9 A B C D E F Example: 916, 016, ACDC 16, DEADBEEF 16 11/2/2020 CS 1 s - 02 A-Primitive Types & Expressions (v 2. 00) 2

Converting Number Bases to Decimal • Consider the number 1810: 1 8 = 1

Converting Number Bases to Decimal • Consider the number 1810: 1 8 = 1 * 101 + 8 * 100 = 1810 101 100 • Consider the number 100102: 1 0 24 23 0 22 1 21 0 = 1 * 24 + 1 * 21 = 1810 20 • Consider the number 1216: 1 2 = 1 * 161 + 2 * 160 = 1810 161 160 11/2/2020 CS 1 s - 02 A-Primitive Types & Expressions (v 2. 00) 3

Converting Decimal Numbers to Binary • Consider the number 2710: 20=1, 21=2, 22=4, 23=8,

Converting Decimal Numbers to Binary • Consider the number 2710: 20=1, 21=2, 22=4, 23=8, 24=16, 25=32 • Continually subtract the largest possible amount and place a 1 in the appropriate bit field 25 = (0) too large 24 = (1) ok, new value is 27 -16=11 23 = (1) ok, new value is 11 -8=3 22 = (0) too large 21 = (1) ok, new value is 3 -2=1 20 = (1) ok, new value is 1 -1=0 • 2710 11/2/2020 = 110112 CS 1 s - 02 A-Primitive Types & Expressions (v 2. 00) 4

Memory • Memory is the storage medium for a program while executing – Memory

Memory • Memory is the storage medium for a program while executing – Memory is organized as a contiguous array of space – All data and instructions are stored in binary form • i. e. the value 1210 = 11002 0 1 0 1 0 1 0 1 1 1 0 0 0 1 0 1 0 1 0 1 11/2/2020 CS 1 s - 02 A-Primitive Types & Expressions (v 2. 00) 5

Memory Units bit 0 1 0 1 byte 0 1 0 1 0 1

Memory Units bit 0 1 0 1 byte 0 1 0 1 0 1 0 1 0 1 0 1 halfword 0 1 0 1 word • • long 0 1 0 1 0 1 0 1 0 1 0 1 0 1 byte = 8 bits halfword = 16 bits / 2 bytes word = 32 bits / 4 bytes / 2 halfwords long = 64 bits / 8 bytes / 4 halfwords / 2 words 11/2/2020 CS 1 s - 02 A-Primitive Types & Expressions (v 2. 00) 6

Memory • To organize data in memory, three pieces of information are needed –

Memory • To organize data in memory, three pieces of information are needed – The address (starting location) of the data in memory – The value of the data to be stored/loaded – The size (length) of the data in memory 11/2/2020 Bit address in hexadecimal 0 x 10000000 0 1 0 1 0 x 10000008 0 1 0 1 0 x 10000010 0 1 0 1 0 x 10000018 0 1 1 1 0 0 0 x 10000020 0 1 0 1 0 x 10000028 0 1 0 1 0 x 10000030 0 1 0 1 0 x 10000038 0 1 0 1 CS 1 s - 02 A-Primitive Types & Expressions (v 2. 00) 7

Variables • In Java, variables are used to model the memory cells • All

Variables • In Java, variables are used to model the memory cells • All variables have the following key attributes: – – Name Type Value Address program memory 0 x 10000000 0 0 0 0 int my. Variable = 12; 0 0 0 0 0 1 1 0 0 11/2/2020 CS 1 s - 02 A-Primitive Types & Expressions (v 2. 00) 8

Java Primitive Types Type Values Default Size Range byte signed integers 0 8 bits

Java Primitive Types Type Values Default Size Range byte signed integers 0 8 bits -128 to 127 short signed integers 0 16 bits -32768 to 32767 int signed integers 0 32 bits -2, 147, 483, 648 to 2, 147, 483, 647 long signed integers 0 64 bits -9223372036854775808 to 9223372036854775807 floating point 0. 0 32 bits +/-3. 4 E+38 (6 to 7 significant digits) double floating point 0. 0 64 bits +/-1. 7 E+308, (14 to 15 significant digits) char 16 bit Unicode u 0000 16 bits u 0000 to u. FFFF boolean true false 1 bit used in 32 bit integer NA 11/2/2020 CS 1 s - 02 A-Primitive Types & Expressions (v 2. 00) 9

Declaring Variables • Variable names are case sensitive – i. e. abc is different

Declaring Variables • Variable names are case sensitive – i. e. abc is different than a. BC • Variables cannot be named the same as reserved words – i. e. illegal variable names: private, class, if • Variables cannot begin with a digit – i. e. 1 foo is illegal • Variables may contain upper or lower case letters, digits, underscores or the dollar sign: my. Var 1 My. Var 2 my_var 1 my$var 11/2/2020 CS 1 s - 02 A-Primitive Types & Expressions (v 2. 00) 10

Variable Declaration • Variables are created with a declaration statement using the following syntax:

Variable Declaration • Variables are created with a declaration statement using the following syntax: type name; • For example: int char float boolean age; middle. Initial; pay. Rate; done; • All variables are initialized with a default value based on type 11/2/2020 CS 1 s - 02 A-Primitive Types & Expressions (v 2. 00) 11

Variable Initialization • A variable can be initialized during a declaration statement using assignment

Variable Initialization • A variable can be initialized during a declaration statement using assignment type name = value; • For example: int char float boolean 11/2/2020 age middle. Initial pay. Rate done = = 14; ‘C’; 12. 65; true; CS 1 s - 02 A-Primitive Types & Expressions (v 2. 00) 12

Variable Initialization • A variable may be initialized after a declaration statement using assignment

Variable Initialization • A variable may be initialized after a declaration statement using assignment name = value; • For example: int age; age = 14; // age = 0 // age = 14 char middle. Initial; middle. Initial = ‘C’; // middle. Initial = u 0000 // middle. Initial = ‘C’ boolean done; done = true; // done = false // done = true 11/2/2020 CS 1 s - 02 A-Primitive Types & Expressions (v 2. 00) 13

Initializations • Some initializations are known prior to run time – i. e. Initialize

Initializations • Some initializations are known prior to run time – i. e. Initialize a counter variable to 0 before counting int count = 0; // sum up the numbers and store in count • Other initializations may not be known until the program is already running – i. e. How old is the person running the program? int age = ? ? ? ; – In this situation, the program will get the data from the user via • Standard input (i. e. a prompt) • File 11/2/2020 CS 1 s - 02 A-Primitive Types & Expressions (v 2. 00) 14

Variables. java public class Variables { public static void main(String args[]) { // declare

Variables. java public class Variables { public static void main(String args[]) { // declare variable here } } • In class activity: – Create a file Variables. java with emacs – Use the template above and declare/initialize the following: • An integer named my. Int • A float named my. Float • A boolean named my. Bool with an initial value of true – After declaring, print out the value for my. Bool using: System. out. println(“my. Bool = “ + my. Bool); – Compile and run using: % javac Variables. java % java Variables – What happens if you try to print out the un-initialized variables? 11/2/2020 CS 1 s - 02 A-Primitive Types & Expressions (v 2. 00) 15

Arithmetic Expressions • Here are some basic mathematical operators: – – – addition: +

Arithmetic Expressions • Here are some basic mathematical operators: – – – addition: + subtraction: division: / multiplication: * modulus: % assignment: = • For example: 2 3 4 3 2 x 11/2/2020 + – / * % = 3 2 2 2 5 4 + 5 // // // 5 1 2 6 2 x = 9 CS 1 s - 02 A-Primitive Types & Expressions (v 2. 00) 16

Arithmetic Expressions • Operators have precedence – just like in math *, /, %

Arithmetic Expressions • Operators have precedence – just like in math *, /, % left to right +, left to right = right to left • For example: 2 + 3 * 6 6 / 3 – 2 * 2 2 * 3 % 10 + 2 • // 2 + (3 * 6) = 20 // (6 / 3) – (2 * 2) = -2 // ((2 * 3) % 10) + 2 = 8 To override precedence, use parentheses: () (2 + 3) * 5 11/2/2020 // 25 CS 1 s - 02 A-Primitive Types & Expressions (v 2. 00) 17

Arithmetic Expressions • Which of the following expressions are valid? 1. 2. 3. 4.

Arithmetic Expressions • Which of the following expressions are valid? 1. 2. 3. 4. 5. 6. 7. 11/2/2020 x + y = z; distance = rate X time; percent = 10 %; temp = temp + 1; temp++; x = y = z; x = (1 + (2 + 3); CS 1 s - 02 A-Primitive Types & Expressions (v 2. 00) 18

 • Discuss narrowing of types: – float->double – int->byte • Also discuss mixed

• Discuss narrowing of types: – float->double – int->byte • Also discuss mixed mode arithmetic expressions – 1/2 – 1. / 2 – (float) 1 / 2, etc… 11/2/2020 CS 1 s - 02 A-Primitive Types & Expressions (v 2. 00) 19

Scanner Class • Scanner is a Java class which supports reading of values from

Scanner Class • Scanner is a Java class which supports reading of values from text. (A number is different from a string of characters!) import java. util. Scanner; public class Simple. Age { public static void main (String args[]) { Scanner sc = new Scanner(System. in); System. out. print( "What is your age? " ); int age = sc. next. Int(); System. out. println("You're " + age + " years old. "); } } • The class name is Scanner, and the method called within the class is next. Int – Nothing is passed to the method: () – An int is returned from the method 11/2/2020 CS 1 s - 02 A-Primitive Types & Expressions (v 2. 00) 20

Average. java • Navigate to the Scanner page: – CS 1 S->Java Documentation->Scanner •

Average. java • Navigate to the Scanner page: – CS 1 S->Java Documentation->Scanner • In class activity: – – – Create a class, Average, which does the following: Prompt the user for three integers using the Scanner class Calculate the average of the three integers Print the result to standard output Verify your program works with the following sets of values: 1 2 3 1 3 6 0 0 0 -1 – 2 – 3 • Sample output: 1 3 6 Result = 3. 3333 11/2/2020 CS 1 s - 02 A-Primitive Types & Expressions (v 2. 00) 21

Boolean Operators • Booleans are binary values of either true or false • The

Boolean Operators • Booleans are binary values of either true or false • The operators used in boolean expressions and: && or: || xor: ^ not: ! • The truth tables: and 0 0 1 1 11/2/2020 0 1 or 0 0 0 1 1 0 1 xor 0 1 1 1 0 0 1 1 0 1 not 0 1 1 0 CS 1 s - 02 A-Primitive Types & Expressions (v 2. 00) 0 1 1 0 22

Boolean Operators • What are the results of the following boolean expressions? 1. 2.

Boolean Operators • What are the results of the following boolean expressions? 1. 2. 3. 4. 5. 6. 7. 8. 11/2/2020 true == true && false true && true || false true ^ true false ^ true !true // bitwise exclusive OR CS 1 s - 02 A-Primitive Types & Expressions (v 2. 00) 23

Comparison Operators • An expression with integer values can be evaluated to a boolean

Comparison Operators • An expression with integer values can be evaluated to a boolean using comparison operators: less than: < less than or equal: <= equal: == not equal: != greater than: > greater than or equal: >= • For example: 1 1 2 11/2/2020 < 2 <= 1 == 3 != 2 >= 1 // // // true false true CS 1 s - 02 A-Primitive Types & Expressions (v 2. 00) 24

Operator Precedence • The comparison operators have higher precedence than the boolean operators true

Operator Precedence • The comparison operators have higher precedence than the boolean operators true == true && 1 < 2 // (true == true) && (1 < 2) • The precedence for the operators we’ve discussed: high precedence low precedence 1 2 3 4 5 6 7 8 9 = || && ^ == < + * ! != <= - / > % >= 11/2/2020 CS 1 s - 02 A-Primitive Types & Expressions (v 2. 00) 25

Operator Precedence • Which of the following expressions are valid? For those which are

Operator Precedence • Which of the following expressions are valid? For those which are valid, what is the result? 1. 2. 3. 4. 5. 6. 7. 11/2/2020 boolean flag = 1 == 2; boolean flag = 1 != 2 < 10; boolean flag 2 = 1 == 1 && true; boolean flag = 1 < 2 < 32; 3 + 5 == 10 – 2 true || false && false || true boolean flag = true ^ false && 1 < 2 CS 1 s - 02 A-Primitive Types & Expressions (v 2. 00) 26

Temperature Conversion Program • A temperature in degrees Celsius can be converted to Farenheit

Temperature Conversion Program • A temperature in degrees Celsius can be converted to Farenheit • Multiply the degrees Celsius by 1. 8 and add 32 to get the degrees Farenheit – i. e. 10 degrees Celsius = 50 degrees Farenheit • Write a complete Java program that performs this calculation • First answer the following questions: – What problem are you trying to solve? – What are the inputs and outputs? – How will you verify whether your program is correct? 11/2/2020 CS 1 s - 02 A-Primitive Types & Expressions (v 2. 00) 27

Temperature. java • In class activity: – Create a class, Temperature, which converts a

Temperature. java • In class activity: – Create a class, Temperature, which converts a temperature in degrees Celsius to degrees Farenheit • Sample output: Celsius: 13 Farenheit = 55. 4 Celsius: 13. 2 Farenheit = 55. 76 11/2/2020 CS 1 s - 02 A-Primitive Types & Expressions (v 2. 00) 28

Constants • A constant in Java is like a variable which cannot change value

Constants • A constant in Java is like a variable which cannot change value after declaration/initialization • The variable declaration is preceded by the final keyword final double CELSIUS_MUL = 1. 8; final double CELSIUS_ADD = 32; • All capital letters (and underscores) are commonly used to indicate a constant • A modification to the Temperature program could be: double farenheit = celsius * CELSIUS_MUL + CELSIUS_ADD; 11/2/2020 CS 1 s - 02 A-Primitive Types & Expressions (v 2. 00) 29

Rectangle. java • In class activity: – Write a complete program, Rectangle. java, that

Rectangle. java • In class activity: – Write a complete program, Rectangle. java, that prompts for the length and width of a rectangle, then calculates and prints the perimeter and the area • If you don’t remember the equations, look them up on the web • Be sure to answer the following questions first: – What problem are you trying to solve? – What are the inputs and outputs? – How will you verify whether your program is correct? 11/2/2020 CS 1 s - 02 A-Primitive Types & Expressions (v 2. 00) 30

Circle. java • In class activity: – Write a complete Java program that prompts

Circle. java • In class activity: – Write a complete Java program that prompts for the diameter of a circle, then calculates and prints the perimeter and the area • Be sure to answer the following questions first: – What problem are you trying to solve? – What are the inputs and outputs? – How will you verify whether your program is correct? • Look at the methods and constants in the Math class Javadocs: – CS 1 S->Java API->Math 11/2/2020 CS 1 s - 02 A-Primitive Types & Expressions (v 2. 00) 31

Sales. Tax. java • In class activity: – Review the source code for Sales.

Sales. Tax. java • In class activity: – Review the source code for Sales. Tax. java and answer the questions that follow. • http: //www. cs. rit. edu/~cs 1 s/week 2/Sales. Tax. java • Questions: – There is one logic error, can you find it? – The is one style violation, can you find it? – What would happen to this program if the sales tax in this area changed? – Explain one way to make this program more maintainable 11/2/2020 CS 1 s - 02 A-Primitive Types & Expressions (v 2. 00) 32

Revision History • Revision History – v 1. 00, 9/13/2004 1: 22 PM, sps

Revision History • Revision History – v 1. 00, 9/13/2004 1: 22 PM, sps Initial revision. -- v 2. 00, 9/10/2005, chr 11/2/2020 CS 1 s - 02 A-Primitive Types & Expressions (v 2. 00) 33