COMP 110 Strings, Console I/O
description
Transcript of COMP 110 Strings, Console I/O
![Page 1: COMP 110 Strings, Console I/O](https://reader030.fdocuments.us/reader030/viewer/2022032612/56813d84550346895da761e2/html5/thumbnails/1.jpg)
COMP 110COMP 110Strings, Console I/OStrings, Console I/O
Luv KohliSeptember 3, 2008
MWF 2-2:50 pmSitterson 014
1
![Page 2: COMP 110 Strings, Console I/O](https://reader030.fdocuments.us/reader030/viewer/2022032612/56813d84550346895da761e2/html5/thumbnails/2.jpg)
AnnouncementsAnnouncementsLab 1 due Friday, 2pm
Program 1 due next Wednesday, 2pm
Please follow the assignment submission instructions!
2
![Page 3: COMP 110 Strings, Console I/O](https://reader030.fdocuments.us/reader030/viewer/2022032612/56813d84550346895da761e2/html5/thumbnails/3.jpg)
Questions?Questions?What is the point of pseudocode? Why
not just start writing code and edit it as necessary?
3
![Page 4: COMP 110 Strings, Console I/O](https://reader030.fdocuments.us/reader030/viewer/2022032612/56813d84550346895da761e2/html5/thumbnails/4.jpg)
Today in COMP 110Today in COMP 110Type casting, arithmetic operators,
operator precedence
Errors
Strings
Console I/O
![Page 5: COMP 110 Strings, Console I/O](https://reader030.fdocuments.us/reader030/viewer/2022032612/56813d84550346895da761e2/html5/thumbnails/5.jpg)
Assignment compatibilitiesAssignment 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 ;
doubl e
fl oat
l ong
i nt
short
byte
![Page 6: COMP 110 Strings, Console I/O](https://reader030.fdocuments.us/reader030/viewer/2022032612/56813d84550346895da761e2/html5/thumbnails/6.jpg)
Assignment CompatibilitiesAssignment Compatibilitiesbyte->short->int->long->float->double
◦myShort myInt;◦myByte myLong;
◦myFloat myByte;◦myLong myInt;
6
![Page 7: COMP 110 Strings, Console I/O](https://reader030.fdocuments.us/reader030/viewer/2022032612/56813d84550346895da761e2/html5/thumbnails/7.jpg)
Type CastingType 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
![Page 8: COMP 110 Strings, Console I/O](https://reader030.fdocuments.us/reader030/viewer/2022032612/56813d84550346895da761e2/html5/thumbnails/8.jpg)
Arithmetic OperatorsArithmetic OperatorsUnary operators (more info later)◦+, -, ++, --, !
Binary arithmetic operators◦*, /, %, +, -
rate*rate + delta 1/(time + 3*mass) (a - 7)/(t + 9*v)
8
![Page 9: COMP 110 Strings, Console I/O](https://reader030.fdocuments.us/reader030/viewer/2022032612/56813d84550346895da761e2/html5/thumbnails/9.jpg)
Modular Arithmetic - %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
![Page 10: COMP 110 Strings, Console I/O](https://reader030.fdocuments.us/reader030/viewer/2022032612/56813d84550346895da761e2/html5/thumbnails/10.jpg)
Parentheses and PrecedenceParentheses and PrecedenceExpressions 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
![Page 11: COMP 110 Strings, Console I/O](https://reader030.fdocuments.us/reader030/viewer/2022032612/56813d84550346895da761e2/html5/thumbnails/11.jpg)
Parentheses and PrecedenceParentheses and Precedencetotal = cost + tax * discount;
Same as:
total = cost + (tax * discount);
![Page 12: COMP 110 Strings, Console I/O](https://reader030.fdocuments.us/reader030/viewer/2022032612/56813d84550346895da761e2/html5/thumbnails/12.jpg)
ErrorsErrorsSyntax error – grammatical mistake in
your programRun-time error – an error that is detected
during program executionLogic error – a mistake in a program
caused by the underlying algorithm
![Page 13: COMP 110 Strings, Console I/O](https://reader030.fdocuments.us/reader030/viewer/2022032612/56813d84550346895da761e2/html5/thumbnails/13.jpg)
StringsStringsA 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
![Page 14: COMP 110 Strings, Console I/O](https://reader030.fdocuments.us/reader030/viewer/2022032612/56813d84550346895da761e2/html5/thumbnails/14.jpg)
StringStringString animal = “aardvark”;
System.out.println(animal);
aardvark
14
![Page 15: COMP 110 Strings, Console I/O](https://reader030.fdocuments.us/reader030/viewer/2022032612/56813d84550346895da761e2/html5/thumbnails/15.jpg)
String ConcatenationString ConcatenationString animal = “aardvark”;
String sentence;
sentence = “My favorite animal is the ” + animal;
My favorite animal is the aardvark
15
![Page 16: COMP 110 Strings, Console I/O](https://reader030.fdocuments.us/reader030/viewer/2022032612/56813d84550346895da761e2/html5/thumbnails/16.jpg)
String ConcatenationString ConcatenationString animal = “aardvark”;
String sentence;
sentence = “My favorite animal is the ” + animal +
“. What is yours?”;
My favorite animal is the aardvark. What is yours?
16
![Page 17: COMP 110 Strings, Console I/O](https://reader030.fdocuments.us/reader030/viewer/2022032612/56813d84550346895da761e2/html5/thumbnails/17.jpg)
String (Class type)String (Class type)
Class types have methods
String myString = “COMP110”;
int len = myString.length();
Object
7
17
Method
![Page 18: COMP 110 Strings, Console I/O](https://reader030.fdocuments.us/reader030/viewer/2022032612/56813d84550346895da761e2/html5/thumbnails/18.jpg)
Strings Methods (pp. 80-82)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
![Page 19: COMP 110 Strings, Console I/O](https://reader030.fdocuments.us/reader030/viewer/2022032612/56813d84550346895da761e2/html5/thumbnails/19.jpg)
String IndicesString Indices
19
U N C i s G r e a t
0 1 2 3 4 5 6 7 8 9 10 11
String output = myString.substring(1, 8);
![Page 20: COMP 110 Strings, Console I/O](https://reader030.fdocuments.us/reader030/viewer/2022032612/56813d84550346895da761e2/html5/thumbnails/20.jpg)
String IndicesString Indices
20
U N C i s G r e a t
0 1 2 3 4 5 6 7 8 9 10 11
String output = myString.substring(1, 8);
![Page 21: COMP 110 Strings, Console I/O](https://reader030.fdocuments.us/reader030/viewer/2022032612/56813d84550346895da761e2/html5/thumbnails/21.jpg)
How do you put quotes in a string?How do you put quotes in a string?System.out.println(“How do I put \“quotes\” in my
string?”);
![Page 22: COMP 110 Strings, Console I/O](https://reader030.fdocuments.us/reader030/viewer/2022032612/56813d84550346895da761e2/html5/thumbnails/22.jpg)
But what about backslashes?But what about backslashes?System.out.println(“How do I put a \\ in my string?”);
![Page 23: COMP 110 Strings, Console I/O](https://reader030.fdocuments.us/reader030/viewer/2022032612/56813d84550346895da761e2/html5/thumbnails/23.jpg)
Escape CharactersEscape Characters
\” Double quote
\’ Single quote
\\ Backslash
\n New line
\r Carriage return
\t Tab
23
![Page 24: COMP 110 Strings, Console I/O](https://reader030.fdocuments.us/reader030/viewer/2022032612/56813d84550346895da761e2/html5/thumbnails/24.jpg)
ASCII and UnicodeASCII and UnicodeASCII – 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 keyboardUnicode◦ 2-byte characters (16 bits -> 216 = 65536 possibilities)◦ Superset of ASCII
![Page 25: COMP 110 Strings, Console I/O](https://reader030.fdocuments.us/reader030/viewer/2022032612/56813d84550346895da761e2/html5/thumbnails/25.jpg)
I/O (Input/Output)I/O (Input/Output)System.out.print(“this is a string”);
System.out.println(“this is a string”);
What is the difference?
25
![Page 26: COMP 110 Strings, Console I/O](https://reader030.fdocuments.us/reader030/viewer/2022032612/56813d84550346895da761e2/html5/thumbnails/26.jpg)
Keyboard InputKeyboard InputScanner 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
![Page 27: COMP 110 Strings, Console I/O](https://reader030.fdocuments.us/reader030/viewer/2022032612/56813d84550346895da761e2/html5/thumbnails/27.jpg)
Documentation and StyleDocumentation and StyleMeaningful namesIndentingDocumentation (comments)Named Constants
27
![Page 28: COMP 110 Strings, Console I/O](https://reader030.fdocuments.us/reader030/viewer/2022032612/56813d84550346895da761e2/html5/thumbnails/28.jpg)
Named constantsNamed constantspublic 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) {
…
![Page 29: COMP 110 Strings, Console I/O](https://reader030.fdocuments.us/reader030/viewer/2022032612/56813d84550346895da761e2/html5/thumbnails/29.jpg)
FridayFridayRecitation (bring charged laptop and
textbook)
Lab 1 due
Lab 2 will be assigned
Programming help for Program 1
29