290 likes | 416 Views
COMP 110 Strings, Console I/O. Luv Kohli September 3, 2008 MWF 2-2:50 pm Sitterson 014. 1. Announcements. Lab 1 due Friday, 2pm Program 1 due next Wednesday, 2pm Please follow the assignment submission instructions!. 2. Questions?.
E N D
COMP 110Strings, Console I/O Luv Kohli September 3, 2008 MWF 2-2:50 pm Sitterson 014 1
Announcements • Lab 1 due Friday, 2pm • Program 1 due next Wednesday, 2pm • Please follow the assignment submission instructions! 2
Questions? • What is the point of pseudocode? Why not just start writing code and edit it as necessary? 3
Today in COMP 110 • Type casting, arithmetic operators, operator precedence • Errors • Strings • Console I/O
Assignment compatibilities • Usually, we need to put values of a certain type into variables of the same type • However, in some cases, the value will automatically be converted when types are different • int age; • age = 10; • double length; • length = age ;
Assignment Compatibilities • byte->short->int->long->float->double • myShort myInt; • myByte myLong; • myFloat myByte; • myLong myInt; 6
Type Casting • You can ask the computer to change the type of values which are against the compatibility. • myFloat = myDouble; • myByte = myInt; • myShort = myFloat; • myFloat = (float)myDouble; • myByte = (byte)myInt; • myShort = (short)myFloat; 7
Arithmetic Operators • Unary operators (more info later) • +, -, ++, --, ! • Binary arithmetic operators • *, /, %, +, - • rate*rate + delta • 1/(time + 3*mass) • (a - 7)/(t + 9*v) 8
Modular Arithmetic - % • 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) • “clock arithmetic” • Minutes on a clock are mod 60 9
Parentheses and Precedence • Expressions inside parentheses evaluated first • (cost + tax) * discount • cost + (tax * discount) • Highest precedenceFirst: the unary operators: +, -, ++, --, !Second: the binary arithmetic operators: *, /, %Third: the binary arithmetic operators: +, -Lowest precedence
Parentheses and Precedence total = cost + tax * discount; Same as: total = cost + (tax * discount);
Errors • Syntax error – grammatical mistake in your program • Run-time error – an error that is detected during program execution • Logic error – a mistake in a program caused by the underlying algorithm
Strings • A string (lowercase) is a sequence of characters • “Hello world!” • “Enter a whole number from 1 to 99.” • String (capital S) is a class in Java, not a primitive type 13
String String animal = “aardvark”; System.out.println(animal); aardvark 14
String Concatenation String animal = “aardvark”; String sentence; sentence = “My favorite animal is the ” + animal; My favorite animal is the aardvark 15
String Concatenation String animal = “aardvark”; String sentence; sentence = “My favorite animal is the ” + animal + “. What is yours?”; My favorite animal is the aardvark. What is yours? 16
String (Class type) Class types have methods String myString = “COMP110”; int len = myString.length(); Object 7 Method 17
Strings Methods (pp. 80-82) • myString.length(); • myString.equals(“a string”); • myString.toLowerCase(); • myString.trim(); • You will see some of these in Lab on Friday 18
String Indices String output = myString.substring(1, 8); 19
String Indices String output = myString.substring(1, 8); 20
How do you put quotes in a string? System.out.println(“How do I put \“quotes\” in my string?”);
But what about backslashes? System.out.println(“How do I put a \\ in my string?”);
ASCII and Unicode • ASCII – American Standard Code for Information Interchange • 1-byte characters (actually 7 bits, but there are 8-bit supersets of ASCII like ISO-8859-1) • Includes characters normally used with English-language keyboard • Unicode • 2-byte characters (16 bits -> 216 = 65536 possibilities) • Superset of ASCII
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 (4th edition) or p. 86 (5th edition) • Make sure to read Gotcha on p. 95 (4th edition) or p. 89 (5th edition) 26
Documentation and Style • Meaningful names • Indenting • Documentation (comments) • Named Constants 27
Named constants • publicstaticfinalTypeVariable = Constant; • Named in ALL_CAPS • publicclass NamedConstant{ • publicstaticfinaldouble PI = 3.14159;publicstaticvoid main(String[] args) • { • …
Friday • Recitation (bring charged laptop and textbook) • Lab 1 due • Lab 2 will be assigned • Programming help for Program 1 29