comp 110 strings, console i/o

29
COMP 110 COMP 110 Strings, Console I/O Strings, Console I/O Luv Kohli September 3, 2008 MWF 2-2:50 pm Sitterson 014 1

Upload: eyad

Post on 07-Jan-2016

41 views

Category:

Documents


0 download

DESCRIPTION

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?. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: COMP 110 Strings, Console I/O

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

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

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

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

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

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

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

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

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

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

Parentheses and PrecedenceParentheses and Precedencetotal = cost + tax * discount;

Same as:

total = cost + (tax * discount);

Page 12: COMP 110 Strings, Console I/O

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

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

StringStringString animal = “aardvark”;

System.out.println(animal);

aardvark

14

Page 15: COMP 110 Strings, Console I/O

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

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

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

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

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

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

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

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

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

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

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

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

Documentation and StyleDocumentation and StyleMeaningful namesIndentingDocumentation (comments)Named Constants

27

Page 28: COMP 110 Strings, Console I/O

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

FridayFridayRecitation (bring charged laptop and

textbook)

Lab 1 due

Lab 2 will be assigned

Programming help for Program 1

29