comp 110 strings, console i/o

Post on 07-Jan-2016

43 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

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

COMP 110COMP 110Strings, Console I/OStrings, Console I/O

Luv KohliSeptember 3, 2008

MWF 2-2:50 pmSitterson 014

1

AnnouncementsAnnouncementsLab 1 due Friday, 2pm

Program 1 due next Wednesday, 2pm

Please follow the assignment submission instructions!

2

Questions?Questions?What is the point of pseudocode? Why

not just start writing code and edit it as necessary?

3

Today in COMP 110Today in COMP 110Type casting, arithmetic operators,

operator precedence

Errors

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

Assignment CompatibilitiesAssignment Compatibilitiesbyte->short->int->long->float->double

◦myShort myInt;◦myByte myLong;

◦myFloat myByte;◦myLong myInt;

6

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

Arithmetic OperatorsArithmetic OperatorsUnary operators (more info later)◦+, -, ++, --, !

Binary arithmetic operators◦*, /, %, +, -

rate*rate + delta 1/(time + 3*mass) (a - 7)/(t + 9*v)

8

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

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

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

Same as:

total = cost + (tax * discount);

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

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

StringStringString animal = “aardvark”;

System.out.println(animal);

aardvark

14

String ConcatenationString ConcatenationString animal = “aardvark”;

String sentence;

sentence = “My favorite animal is the ” + animal;

My favorite animal is the aardvark

15

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

String (Class type)String (Class type)

Class types have methods

String myString = “COMP110”;

int len = myString.length();

Object

7

17

Method

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

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);

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);

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?”);

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

Escape CharactersEscape Characters

\” Double quote

\’ Single quote

\\ Backslash

\n New line

\r Carriage return

\t Tab

23

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

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

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

Documentation and StyleDocumentation and StyleMeaningful namesIndentingDocumentation (comments)Named Constants

27

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) {

FridayFridayRecitation (bring charged laptop and

textbook)

Lab 1 due

Lab 2 will be assigned

Programming help for Program 1

29

top related