DOC PREVIEW
Duke CPS 100E - Lecture

This preview shows page 1 out of 4 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CompSci 100E2.1Problem 1! Given n, calculate 2n" What if you wanted to print all from 20 to 2n?" What if you wanted to return the value?CompSci 100E2.2Problem 2! Given a real number c and some error toleranceepsilon, estimate t, the square root of cCompSci 100E2.3Problem 3! Suppose that you have a shuffled deck of cards andyou turn them up face up, one by one." How many cards until you see one of each suit?" How many cards until you see one of each value?CompSci 100E2.4Java Basics - Expressions! Literals" A literal is a constant value also called a self-defining term" Possibilities:o Object: null, the only object literal availableo Boolean: true or falseo Integer: e.g., 127, -13, 42, or 0 create 32-bit integers# For 64-bit long append L or l, e.g., 17Lo Floating Point: 3.14592 or 0.0 or 2.1E16 for 64-bit doubles# For 32-bit float append F or f, e.g., 2.56F or 0.5e-12fo Character: e.g., ’A’, ’Z’, ’w’, ’$’, ’%’ for 16 bit Unicode# control: ’\n’, ’\b’, ’\f’, ’\t’, ’\r’# escape: ’\’’, ’\\’, ’\”’o Strings: e.g., ”How are things?” or ”” (null string) or null# Use mostly same control and escape characters as charCompSci 100E2.5Java Basics - Expressions! Operators" Arithmetico +, -, *, /, % (remainder or mod)" Increment/Decremento e.g., k++, k-- , ++k, --k" Logical (results in boolean value)o <, <=, ==, !=, >=, >o Used only for numbers except == and !=o For boolean only: !, &&, ||" String Concatenationo ”I’m ” + 19 + ” years old and live in ” + city" Assignmento variable = expressiono variable op= expressiono ( shorthand for: variable = variable op expression )CompSci 100E2.6Java Basics - Expressions! Operator Precedence" Determines order of operation" See table in text" For arithmetic, matches grammar school learningo multiplication and division before addition andsubtractiono what is the value of 4.0 + 5.0 / 9.0 * 27.0 ?o (what is the value for the integer version?)" Parentheses override precedence rules (and don’t do harmwhen not needed)" For equal precedence (e.g., * and /) work strictly left to rightexcept for assignment and prefix operations which work rightto left" Precedence rules same as for C and C++CompSci 100E2.7Java Basics - Expressions! Casting" Allows us to change the type of the value of an expression" (Type change must be reasonable and supported.)" Simple example:double x = 5.5, y = 2.9999;int k = (int) x;int m = (int) y;double z = (double) k; // what is in x, y, z, k, m ?! Implicit Casting" When an int expression is assigned to a double, casting isautomatic (no information is lost).o (double cast at end of previous example not needed)" When double is on one side of an operator and int at other, intis automatically cast to a double before op is used.5 / 9 * (68 – 32) vs. 5.0 / 9 * (68 – 32)CompSci 100E2.8Java Basics - Expressions! Autoboxing/Unboxing" Since Java 5.0, there is automatic casting between primitivetypes and their related Object types (also called wrapperclasses)." Simple examples: Double d = 2.9; used to require: Double d = new Double(2.9); and double x = d; used to require double x = d.doubleValue();CompSci 100E2.9Java Basics – Control of Flow! If Statement" if (boolean_exp) { what_to_do_if_true }" if (boolean_exp) { what_to_do_if_true } else { what_to_do_if_false }" if (1st_boolean_exp) { what_to_do_if_1st_true } else if (2nd_boolean_exp){ what_to_do_if_2nd_true } else { what_to_do_if_all_false }CompSci 100E2.10Java Basics – Control Flow! Switch Statement Example" switch (stars) { case 4: message = ”truly exceptional”; break; case 3: message = ”quite good”; break; case 2: message = ”fair”; break; case 1: case 0: message = ”forget it”; break; default: message = ”no info found”; break; }CompSci 100E2.11Java Basics – Loops! While Loops" Syntax initialize while (boolean_exp) { work_to_be_done update }" Example int counter = 10; while (counter > 0) { System.out.println(counter); counter--; } System.out.println(”Blast Off!”);" What is the output?" What if we exchange order of two statements in loop?CompSci 100E2.12Java Basics – Loops! For Loops" Syntax for (intialization; boolean_exp; update) { work_to_be_done }" Example for (int counter = 10; counter > 0; counter--) { System.out.println(counter); } System.out.println(”Blast Off!”);" What is the output?" When is update performed?" What is value of counter after loop?CompSci 100E2.13Java Basics – Loops! Do-While Loops" Syntax initialize do { work_to_be_done update } while (boolean_exp);o NOTE REQUIRED SEMICOLON!!!" Example int counter = 10; do { System.out.println(counter); counter-- ; } while (counter > 0); System.out.println(”Blast Off!”);CompSci 100E2.14Java Basics – Loops! Which Kind of Loop Do I Use?" While Loopo Don’t know how often it’s going beo Update can be anywhere in the loop body" For Loopo Know how often in advanceo All information controlling loop together, in front" Do-While Loopo Least popularo Often used with data input" What is the minimum number of times each of these loop?o while?o for?o do-while?CompSci 100E2.15Java Basics – Control Flow! Returning from a Method" Executing a return statements means you exit from the the method. Subsequent statements are ignored!" void Methodso Implicit return at end of body# Can make it explicito Can have other return statements as logic dictates" Functions (non-void Methods)o Require return as last statement (with argument of correcttype)o Can have other return statements as logic dictatesCompSci 100E2.16Java Basics – Control Flow! Break Statement" Use to exit from loop or switcho One level only!o With nested loops, only leave loop immediatelysurrounding break! Continue Statement" Use to go to the end of a loop, ignoring remainingstatementso Loop continues with next iteration (if needed)o One level only!o With nested loops, only got to end of loop immediatelysurrounding


View Full Document

Duke CPS 100E - Lecture

Documents in this Course
Topics

Topics

9 pages

Lecture

Lecture

3 pages

Notes

Notes

2 pages

Hashing

Hashing

19 pages

Lecture

Lecture

59 pages

Lecture

Lecture

6 pages

Lecture

Lecture

4 pages

Lecture

Lecture

20 pages

Lecture

Lecture

12 pages

Lecture

Lecture

12 pages

Lecture

Lecture

7 pages

Lecture

Lecture

8 pages

Lecture

Lecture

10 pages

Lecture

Lecture

4 pages

Notes

Notes

16 pages

Lecture

Lecture

5 pages

Lecture

Lecture

9 pages

Lecture

Lecture

4 pages

Lecture

Lecture

13 pages

Lecture

Lecture

6 pages

Lecture

Lecture

16 pages

Lecture

Lecture

5 pages

Lecture

Lecture

5 pages

Lecture

Lecture

12 pages

Lecture

Lecture

12 pages

Lecture

Lecture

10 pages

Sets

Sets

14 pages

Lecture

Lecture

9 pages

Test 1

Test 1

7 pages

Load more
Download Lecture
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?