COMP 110 Primitive Types, Strings, and Console I/O Tabitha Peck M.S. January 23, 2008 MWF 3-3:50 pm Philips 367 1
Announcements Lab 1 Due Friday Before Class Follow submission instructions.jar tutorial 2
Questions? 3
Today in COMP 110 Primitive Types and Expressions Strings Console I/O 4
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
Variable Declarations Syntax: Type Variable_1, Variable_2, …; Examples: int count, score, myInt; char letter; double totalCost, ratio; 6
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
Keywords Reserved words with predefined meanings You cannot name your variables keywords Appendix 1 if, else, return, new 8
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
Variables Change int changingVar = 0; changingVar = 5; changingVar = changingVar + 4; 10 identifier type 5 9
Primitive Types Integer (byte, short, int, long) 0, -3, 5, 43 Floating-point number (double, float) 0.5, , -4.3 Characters (char) A, r, %, T Boolean (boolean) true, false 11
Assignment Statements something = otherSomethings; answerToLife = 42; theUniverse = (answerToLife*4) + everything; 12 Variable Expression Assignment operator
Specialized Assignment Operators mario += 5; // is the same as mario = mario + 5; luigi++; // is the same as luigi = luigi + 1; 13
Assignment Compatibilities You can only put small things into bigger things 14
Assignment Compatibilities byte->short->int->long->float->double myShort myInt; myByte myLong; myFloat mybyte; myLong myInt; 15
Type Casting byte->short->int->long->float->double myFloat = (float)myDouble; myByte = (byte)myInt; myShort = (byte)myFloat; 16
Arithmetic Operators Unary operators (more info later) +, -, ++, --, ! Binary arithmetic operators *, /, %, +, - rate*rate + delta 1/(time + 3*mass) (a - 7)/(t + 9*v) 17
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
String String animal = “Mad Max”; System.out.println(animal); Mad Max 19
String Concatenation String animal = “Mad Max”; String sentence; Sentence = “My dog’s name is ” + animal; My dog’s name is Mad Max 20
String (Class Type) Class types have methods String myString = “COMP110”; int len = myString.length(); Object Method 7 21
Strings Methods (pp ) myString.length(); myString.equals(“a string”); myString.toLowerCase(); myString.trim(); You will see these in Lab on Friday 22
String Indices UNCisGreat String output = myString.substring(1, 7); 23
Escape Characters System.out.println(“How do I put \“quotes\” in my string?”); \”Double quote \’Single quote \\Backslash \nNew line \rCarriage return \tTab 24
I/O (Input/Output) System.out.print(“this is a string”); System.out.println(“this is a string”); What is the difference? 25
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
Documentation and Style Meaningful names Indenting Documentation Defined Constants 27
Named constants public static final Type Variable = Constant; Named in ALL_CAPS public class NamedConstant { public static final double PI = ; public static void main(String[] args) { 28
Friday Recitation (bring charged laptop) Bring your book!!!!! Lab 2 Programming help for Program 1 Monday - In class exercise Bring your book 29