Problem 1 Given n, calculate 2n

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Chapter 2: Using Data.
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
Some basic I/O.
CS 117 Spring 2002 Review for Exam 2 March 6, 2002 open book, 1 page of notes.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
General Features of Java Programming Language Variables and Data Types Operators Expressions Control Flow Statements.
Chapter 3: Data Types and Operators JavaScript - Introductory.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Sahar Mosleh California State University San MarcosPage 1 A for loop can contain multiple initialization actions separated with commas Caution must be.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
JAVA 0. HAFTA Algorithms FOURTH EDITION Robert Sedgewick and Kevin Wayne Princeton University.
Flow of Control Part 1: Selection
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
CompSci 100E 2.1 Java Basics - Expressions  Literals  A literal is a constant value also called a self-defining term  Possibilities: o Object: null,
Chapter 05 (Part III) Control Statements: Part II.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Chapter 2 Variables.
Flow of Control Chapter 3. Outline Branching Statements Java Loop Statements Programming with Loops The Type boolean.
Control Flow Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
CompSci Primitive Types  Primitive Types (base types)  Built-in data types; native to most hardware  Note: not objects (will use mostly first.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
COMP Loop Statements Yi Hong May 21, 2015.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
CompSci 100E JB1.1 Java Basics (ala Goodrich & Tamassia)  Everything is in a class  A minimal program: public class Hello { public static void main(String[]
Chapter 2 Variables.
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Decisions Chapter 4.
2.5 Another Java Application: Adding Integers
Control Statements: Part 2
Multiple variables can be created in one declaration
User input We’ve seen how to use the standard output buffer
Expressions and Control Flow in JavaScript
Java Programming: From Problem Analysis to Program Design, 4e
OPERATORS (1) CSC 111.
MSIS 655 Advanced Business Applications Programming
OPERATORS (2) CSC 111.
Introduction to C++ Programming
Chapter 8 JavaScript: Control Statements, Part 2
Chapter 2: Basic Elements of Java
Chapter 7 Additional Control Structures
Chapter 2 Variables.
CSC215 Lecture Control Flow.
Structured Program Development in C
Recap Week 2 and 3.
Expressions and Assignment
elementary programming
Chapter 2 Programming Basics.
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
In this class, we will cover:
Chapter 2 Variables.
Just Enough Java 17-May-19.
Chap 7. Advanced Control Statements in Java
ENERGY 211 / CME 211 Lecture 5 October 1, 2008.
CSC215 Lecture Control Flow.
Presentation transcript:

Problem 1 Given n, calculate 2n What if you wanted to print all from 20 to 2n? What if you wanted to return the value?

Problem 2 Given a real number c and some error tolerance epsilon, estimate t, the square root of c

Problem 3 Suppose that you have a shuffled deck of cards and you 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?

Java Basics - Expressions Literals A literal is a constant value also called a self-defining term Possibilities: Object: null, the only object literal available Boolean: true or false Integer: e.g., 127, -13, 42, or 0 create 32-bit integers For 64-bit long append L or l, e.g., 17L 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-12f Character: e.g., ’A’, ’Z’, ’w’, ’$’, ’%’ for 16 bit Unicode control: ’\n’, ’\b’, ’\f’, ’\t’, ’\r’ escape: ’\’’, ’\\’, ’\”’ Strings: e.g., ”How are things?” or ”” (null string) or null Use mostly same control and escape characters as char

Java Basics - Expressions Operators Arithmetic +, -, *, /, % (remainder or mod) Increment/Decrement e.g., k++, k-- , ++k, --k Logical (results in boolean value) <, <=, ==, !=, >=, > Used only for numbers except == and != For boolean only: !, &&, || String Concatenation ”I’m ” + 19 + ” years old and live in ” + city Assignment variable = expression variable op= expression ( shorthand for: variable = variable op expression )

Java Basics - Expressions Operator Precedence Determines order of operation See table in text For arithmetic, matches grammar school learning multiplication and division before addition and subtraction what is the value of 4.0 + 5.0 / 9.0 * 27.0 ? (what is the value for the integer version?) Parentheses override precedence rules (and don’t do harm when not needed) For equal precedence (e.g., * and /) work strictly left to right except for assignment and prefix operations which work right to left Precedence rules same as for C and C++

Java 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 is automatic (no information is lost). (double cast at end of previous example not needed) When double is on one side of an operator and int at other, int is automatically cast to a double before op is used. 5 / 9 * (68 – 32) vs. 5.0 / 9 * (68 – 32)

Java Basics - Expressions Autoboxing/Unboxing Since Java 5.0, there is automatic casting between primitive types and their related Object types (also called wrapper classes). 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();

Java Basics – Control of Flow If Statement 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 what_to_do_if_all_false

Java Basics – Control Flow Switch Statement Example switch (stars) { case 4: message = ”truly exceptional”; break; case 3: message = ”quite good”; case 2: message = ”fair”; case 1: case 0: message = ”forget it”; default: message = ”no info found”; }

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

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

Java Basics – Loops Do-While Loops Syntax initialize do { work_to_be_done update } while (boolean_exp); NOTE REQUIRED SEMICOLON!!! Example int counter = 10; do { System.out.println(counter); counter-- ; } while (counter > 0); System.out.println(”Blast Off!”);

Java Basics – Loops Which Kind of Loop Do I Use? While Loop For Loop Don’t know how often it’s going be Update can be anywhere in the loop body For Loop Know how often in advance All information controlling loop together, in front Do-While Loop Least popular Often used with data input What is the minimum number of times each of these loop? while? for? do-while?

Java Basics – Control Flow Returning from a Method Executing a return statements means you exit from the the method. Subsequent statements are ignored! void Methods Implicit return at end of body Can make it explicit Can have other return statements as logic dictates Functions (non-void Methods) Require return as last statement (with argument of correct type)

Java Basics – Control Flow Break Statement Use to exit from loop or switch One level only! With nested loops, only leave loop immediately surrounding break Continue Statement Use to go to the end of a loop, ignoring remaining statements Loop continues with next iteration (if needed) With nested loops, only got to end of loop immediately surrounding continue