1 / 33

Computer Science 1 02A-Primitive Types & Expressions

Computer Science 1 02A-Primitive Types & Expressions. Number Bases. The most commonly used bases for numbers in computer science are: Binary (base 2) Alphabet : 0 1 Example: 1001 2 , 0110 2 , 0 2 , 1 2 Octal (base 8) Alphabet : 0 1 2 4 5 6 7 Example: 7 8 , 0 8 , 460 8

nmeyers
Download Presentation

Computer Science 1 02A-Primitive Types & Expressions

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. Computer Science 1 02A-Primitive Types & Expressions CS1s - 02A-Primitive Types & Expressions (v2.00)

  2. Number Bases • The most commonly used bases for numbers in computer science are: • Binary (base 2) Alphabet: 0 1 Example: 10012, 01102, 02, 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, 010, 200410 • Hexadecimal (base 16): Alphabet: 0 1 2 3 4 5 6 7 8 9 A B C D E F Example: 916, 016, ACDC16, DEADBEEF16 CS1s - 02A-Primitive Types & Expressions (v2.00)

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

  4. 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 = 110112 CS1s - 02A-Primitive Types & Expressions (v2.00)

  5. 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 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 1 1 0 0 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 CS1s - 02A-Primitive Types & Expressions (v2.00)

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

  7. 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 Bit address in hexadecimal 0 1 0 1 0 1 0 1 0x10000000 0 1 0 1 0 1 0 1 0x10000008 0 1 0 1 0 1 0 1 0x10000010 0x10000018 0 1 0 1 1 1 0 0 0 1 0 1 0 1 0 1 0x10000020 0 1 0 1 0 1 0 1 0x10000028 0 1 0 1 0 1 0 1 0x10000030 0 1 0 1 0 1 0 1 0x10000038 CS1s - 02A-Primitive Types & Expressions (v2.00)

  8. Variables • In Java, variables are used to model the memory cells • All variables have the following key attributes: • Name • Type • Value • Address intmyVariable = 12; program memory 0x10000000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 CS1s - 02A-Primitive Types & Expressions (v2.00)

  9. Java Primitive Types CS1s - 02A-Primitive Types & Expressions (v2.00)

  10. Declaring Variables • Variable names are case sensitive • i.e. abc is different than aBC • 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. 1foo is illegal • Variables may contain upper or lower case letters, digits, underscores or the dollar sign: myVar1 MyVar2 my_var1 my$var CS1s - 02A-Primitive Types & Expressions (v2.00)

  11. Variable Declaration • Variables are created with a declaration statement using the following syntax: type name; • For example: int age; char middleInitial; float payRate; boolean done; • All variables are initialized with a default value based on type CS1s - 02A-Primitive Types & Expressions (v2.00)

  12. Variable Initialization • A variable can be initialized during a declaration statement using assignment type name = value; • For example: int age = 14; char middleInitial = ‘C’; float payRate = 12.65; boolean done = true; CS1s - 02A-Primitive Types & Expressions (v2.00)

  13. Variable Initialization • A variable may be initialized after a declaration statement using assignment name = value; • For example: int age; // age = 0 age = 14; // age = 14 char middleInitial; // middleInitial = \u0000 middleInitial = ‘C’; // middleInitial = ‘C’ boolean done; // done = false done = true; // done = true CS1s - 02A-Primitive Types & Expressions (v2.00)

  14. 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 CS1s - 02A-Primitive Types & Expressions (v2.00)

  15. 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 myInt • A float named myFloat • A boolean named myBool with an initial value of true • After declaring, print out the value for myBool using: System.out.println(“myBool = “ + myBool); • Compile and run using: % javac Variables.java % java Variables • What happens if you try to print out the un-initialized variables? CS1s - 02A-Primitive Types & Expressions (v2.00)

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

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

  18. Arithmetic Expressions • Which of the following expressions are valid? • x + y = z; • distance = rate X time; • percent = 10 %; • temp = temp + 1; • temp++; • x = y = z; • x = (1 + (2 + 3); CS1s - 02A-Primitive Types & Expressions (v2.00)

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

  20. 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 SimpleAge { public static void main (String args[]) { Scanner sc = new Scanner(System.in); System.out.print( "What is your age? " ); int age = sc.nextInt(); System.out.println("You're " + age + " years old."); } } • The class name is Scanner,and the method called within the class is nextInt • Nothing is passed to the method: () • An int is returned from the method CS1s - 02A-Primitive Types & Expressions (v2.00)

  21. Average.java • Navigate to the Scanner page: • CS1S->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.33333333 CS1s - 02A-Primitive Types & Expressions (v2.00)

  22. 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 or xor not 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 1 1 0 1 1 1 0 1 0 0 1 0 1 1 0 1 1 1 1 1 1 1 1 1 0 CS1s - 02A-Primitive Types & Expressions (v2.00)

  23. Boolean Operators • What are the results of the following boolean expressions? • true == true • true && false • true && true • true || false • false || false • true ^ true // bitwise exclusive OR • false ^ true • !true CS1s - 02A-Primitive Types & Expressions (v2.00)

  24. 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 < 2 // true 1 <= 1 // true 2 == 3 // false 1 != 2 // true 1 > 2 // false 2 >= 1 // true CS1s - 02A-Primitive Types & Expressions (v2.00)

  25. 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 CS1s - 02A-Primitive Types & Expressions (v2.00)

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

  27. 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? CS1s - 02A-Primitive Types & Expressions (v2.00)

  28. 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 CS1s - 02A-Primitive Types & Expressions (v2.00)

  29. 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; CS1s - 02A-Primitive Types & Expressions (v2.00)

  30. 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? CS1s - 02A-Primitive Types & Expressions (v2.00)

  31. 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: • CS1S->Java API->Math CS1s - 02A-Primitive Types & Expressions (v2.00)

  32. SalesTax.java • In class activity: • Review the source code for SalesTax.java and answer the questions that follow. • http://www.cs.rit.edu/~cs1s/week2/SalesTax.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 CS1s - 02A-Primitive Types & Expressions (v2.00)

  33. Revision History • Revision History • v1.00, 9/13/2004 1:22 PM, sps Initial revision. -- v2.00, 9/10/2005, chr CS1s - 02A-Primitive Types & Expressions (v2.00)

More Related