Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP 110 Primitive Types, Strings, and Console I/O Tabitha Peck M.S. January 23, 2008 MWF 3-3:50 pm Philips 367 1.

Similar presentations


Presentation on theme: "COMP 110 Primitive Types, Strings, and Console I/O Tabitha Peck M.S. January 23, 2008 MWF 3-3:50 pm Philips 367 1."— Presentation transcript:

1 COMP 110 Primitive Types, Strings, and Console I/O Tabitha Peck M.S. January 23, 2008 MWF 3-3:50 pm Philips 367 1

2 Announcements Lab 1 Due Friday Before Class Follow submission instructions.jar tutorial 2

3 Questions? 3

4 Today in COMP 110 Primitive Types and Expressions Strings Console I/O 4

5 Variables Used to store data in program The data currently in a variable is its value Name of variable is an identifier Can change value throughout program Choose variable names that are helpful!!! 5

6 Variable Declarations Syntax: Type Variable_1, Variable_2, …; Examples: int count, score, myInt; char letter; double totalCost, ratio; 6

7 How to name an identifier Letters, digits(0-9), underscore (_) First character cannot be a digit Java is case sensitive Legal names pinkFloyd, the_coup, b3atles Illegal names michael.bolton, kenny-G, 1CP 7

8 Keywords Reserved words with predefined meanings You cannot name your variables keywords Appendix 1 if, else, return, new 8

9 Type What kind of value the variable can hold Class type - objects with both data and methods Name begins with uppercase letter Scanner, String Primitive type - indecomposable values Names begin with lowercase letters int, double, char, boolean See display 2.2, p 53 for full list 9

10 Variables Change int changingVar = 0; changingVar = 5; changingVar = changingVar + 4; 10 identifier type 5 9

11 Primitive Types Integer (byte, short, int, long) 0, -3, 5, 43 Floating-point number (double, float) 0.5, 12.4863, -4.3 Characters (char) A, r, %, T Boolean (boolean) true, false 11

12 Assignment Statements something = otherSomethings; answerToLife = 42; theUniverse = (answerToLife*4) + everything; 12 Variable Expression Assignment operator

13 Specialized Assignment Operators mario += 5; // is the same as mario = mario + 5; luigi++; // is the same as luigi = luigi + 1; 13

14 Assignment Compatibilities You can only put small things into bigger things   14

15 Assignment Compatibilities byte->short->int->long->float->double myShort  myInt; myByte  myLong; myFloat  mybyte; myLong  myInt; 15

16 Type Casting byte->short->int->long->float->double myFloat = (float)myDouble; myByte = (byte)myInt; myShort = (byte)myFloat; 16

17 Arithmetic Operators Unary operators (more info later) +, -, ++, --, ! Binary arithmetic operators *, /, %, +, - rate*rate + delta 1/(time + 3*mass) (a - 7)/(t + 9*v) 17

18 Modular Arithmetic - % “clock arithmetic” Minutes on a clock are mod 60 Remainder 7 % 3 = 1 (7 / 3 = 2, remainder 1) 8 % 3 = 2 (8 / 3 = 2, remainder 2) 9 % 3 = 0 (9 / 3 = 3, remainder 0) 18

19 String String animal = “Mad Max”; System.out.println(animal); Mad Max 19

20 String Concatenation String animal = “Mad Max”; String sentence; Sentence = “My dog’s name is ” + animal; My dog’s name is Mad Max 20

21 String (Class Type) Class types have methods String myString = “COMP110”; int len = myString.length(); Object Method 7 21

22 Strings Methods (pp. 80-82) myString.length(); myString.equals(“a string”); myString.toLowerCase(); myString.trim(); You will see these in Lab on Friday 22

23 String Indices UNCisGreat 01234567891011 String output = myString.substring(1, 7); 23

24 Escape Characters System.out.println(“How do I put \“quotes\” in my string?”); \”Double quote \’Single quote \\Backslash \nNew line \rCarriage return \tTab 24

25 I/O (Input/Output) System.out.print(“this is a string”); System.out.println(“this is a string”); What is the difference? 25

26 Keyboard Input Scanner Scanner_object_name = new Scanner(System.in); Scanner_object_name.nextLine(); Scanner_object_name.nextInt(); Scanner_object_name.nextDouble(); See p. 93 Make sure to read Gotcha on p. 95 26

27 Documentation and Style Meaningful names Indenting Documentation Defined Constants 27

28 Named constants public static final Type Variable = Constant; Named in ALL_CAPS public class NamedConstant { public static final double PI = 3.14159; public static void main(String[] args) { 28

29 Friday Recitation (bring charged laptop) Bring your book!!!!! Lab 2 Programming help for Program 1 Monday - In class exercise Bring your book 29


Download ppt "COMP 110 Primitive Types, Strings, and Console I/O Tabitha Peck M.S. January 23, 2008 MWF 3-3:50 pm Philips 367 1."

Similar presentations


Ads by Google