1 / 29

COMP 110 Strings, Console I/O

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?.

eyad
Télécharger la présentation

COMP 110 Strings, Console I/O

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. COMP 110Strings, Console I/O Luv Kohli September 3, 2008 MWF 2-2:50 pm Sitterson 014 1

  2. Announcements • Lab 1 due Friday, 2pm • Program 1 due next Wednesday, 2pm • Please follow the assignment submission instructions! 2

  3. Questions? • What is the point of pseudocode? Why not just start writing code and edit it as necessary? 3

  4. Today in COMP 110 • Type casting, arithmetic operators, operator precedence • Errors • Strings • Console I/O

  5. 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 ;

  6. Assignment Compatibilities • byte->short->int->long->float->double • myShort  myInt; • myByte  myLong; • myFloat  myByte; • myLong  myInt; 6

  7. 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

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

  9. 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

  10. 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

  11. Parentheses and Precedence total = cost + tax * discount; Same as: total = cost + (tax * discount);

  12. 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

  13. 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

  14. String String animal = “aardvark”; System.out.println(animal); aardvark 14

  15. String Concatenation String animal = “aardvark”; String sentence; sentence = “My favorite animal is the ” + animal; My favorite animal is the aardvark 15

  16. 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

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

  18. 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

  19. String Indices String output = myString.substring(1, 8); 19

  20. String Indices String output = myString.substring(1, 8); 20

  21. How do you put quotes in a string? System.out.println(“How do I put \“quotes\” in my string?”);

  22. But what about backslashes? System.out.println(“How do I put a \\ in my string?”);

  23. Escape Characters 23

  24. 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

  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 (4th edition) or p. 86 (5th edition) • Make sure to read Gotcha on p. 95 (4th edition) or p. 89 (5th edition) 26

  27. Documentation and Style • Meaningful names • Indenting • Documentation (comments) • Named Constants 27

  28. Named constants • publicstaticfinalTypeVariable = Constant; • Named in ALL_CAPS • publicclass NamedConstant{ • publicstaticfinaldouble PI = 3.14159;publicstaticvoid main(String[] args) • { • …

  29. Friday • Recitation (bring charged laptop and textbook) • Lab 1 due • Lab 2 will be assigned • Programming help for Program 1 29

More Related