Warm-Up: Monday, March 24 List as many commands in Java as you can remember (at least 10)

Slides:



Advertisements
Similar presentations
Escape Sequences \n newline \t tab \b backspace \r carriage return
Advertisements

2.1 Program Construction In Java
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.
Return values.
Neal Stublen C# Data Types Built-in Types  Integer Types byte, sbyte, short, ushort, int, uint, long, ulong  Floating Point Types.
Warm-up: Tuesday, March 11  In programming, what is the difference between an object and a class?
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Additional control structures. The if-else statement The if-else statement chooses which of two statements to execute The if-else statement has the form:
Introduction to Computers and Programming Lecture 7:
CMT Programming Software Applications
1 Chapter 3 Arithmetic Expressions. 2 Chapter 3 Topics l Overview of Java Data Types l Numeric Data Types l Declarations for Numeric Expressions l Simple.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Week 2 - Friday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
Warm-Up: April 21 Write a loop (type of your choosing) that prints every 3 rd number between 10 and 50.
Week #2 Java Programming. Enable Line Numbering in Eclipse Open Eclipse, then go to: Window -> Preferences -> General -> Editors -> Text Editors -> Check.
Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer.
EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapters 3-4: Using Objects.
* What kind of loop would I use to complete the following: A. Output all of the prime numbers that are less than 100,000 B. Output the Fibonacci sequence.
Week 3 - Wednesday.  What did we talk about last time?  Math methods  Lab 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.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
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.
Week 2 - Wednesday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
STANDARD FUNCTIONS Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 4 Mathematical Functions, Characters,
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
CHAPTER 4 GC 101 Data types. DATA TYPES  For all data, assign a name (identifier) and a data type  Data type tells compiler:  How much memory to allocate.
Primitive Data Types. Identifiers What word does it sound like?
Lesson 6 Selection Structures. Example program //This will convert a numeric grade into a letter grade import TerminalIO.KeyboardReader; public class.
CSE 1341 Honors Note Set 05 Professor Mark Fontenot Southern Methodist University.
FUNDAMENTALS 2 CHAPTER 2. OPERATORS  Operators are special symbols used for:  mathematical functions  assignment statements  logical comparisons 
CS111 Computer Programming Department of Computer Science Wellesley College Classic Recursion Examples Plus Class Methods and Applications Tuesday, October.
Lecture 9: Bits and Pieces, Part 2 Tami Meredith.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
CSC Programming for Science Lecture 10: Boolean Expressions & More If ­ Else Statements.
CSE 110 Review Session Hans Hovanitz, Kate Kincade, and Ian Nall.
CSCI 1226 FALL 2015 MIDTERM #1 REVIEWS.  Types of computers:  Personal computers  Embedded systems  Servers  Hardware:  I/O devices: mice, keyboards,
1 Introduction to Programming with Python: overview.
USING & CREATING FUNCTIONS. MODULAR PROGRAMMING  Why Modular Programming?  Improves Readability & Understandability  Improve Maintainability  Allows.
1 nd Semester Module2 C# Basic Concept Thanawin Rakthanmanon Computer Engineering Department Kasetsart University, Bangkok.
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
CS 201 California State University, Los Angeles.  Various Mathematical Functions  Characters  Strings.
Chapter 4 Mathematical Functions, Characters, and Strings 1.
Week 3 - Monday.  What did we talk about last time?  Using Scanner to get input  Basic math operations  Lab 2.
Today Variable declaration Mathematical Operators Input and Output Lab
Chapter 4 Mathematical Functions, Characters, and Strings
Week 3 - Wednesday CS 121.
Fundamental of Java Programming Basics of Java Programming
Java Programming: From Problem Analysis to Program Design, 4e
TK1114 Computer Programming
Week 3: Basic Operations
Operators and Expressions
CS 177 Week 3 Recitation Slides
Chapter 2: Basic Elements of Java
Functions October 23, 2017.
Java Tokens & Data types
Building Java Programs
Other types of variables
Primitive Types and Expressions
Unit 3: Variables in Java
Ch 5 : Mathematical Functions, Characters, and Strings
Presentation transcript:

Warm-Up: Monday, March 24 List as many commands in Java as you can remember (at least 10)

Review PAP Computer Science Cycle 5

Output System.out.print( ) System.out.println( ) Escape sequences \n newline \t tab \\ backslash \’ single quote \” double quote

7 Mathematical Operators Addition (+) Subtraction (-) Multiplication (*) Division (/) Modulus (%) Increment (++) Decrement (--)

Declaring/Initializing a Variable Example 1 int x; x = 5; Example 2 int x = 5;

Java Programming: Guided Learning with Early Objects 6 Input (Read) Statement The Scanner class puts data into variables from the standard input device static Scanner console = new Scanner(System.in); Next input is: Integer: console.nextInt() Double: console.nextDouble() String: console.next() or console.nextLine()

Math Methods abs(x) – absolute value abs(-96.5)  95.6 ceil(x) – ceiling operator ceil(54.13)  55 floor(x) – floor operator floor(54.13)  54 exp(x) – returns e x exp(3)  e 3 

Math Methods log(x) – natural logarithm (base e) of x log(2)  ln(2)  log10(x) – base-10 logarithm of x log10(100)  log 10 (100)  2.0 max(x,y) – maximum of two numbers max(15, 25)  25 min(x,y) – minimum of two numbers min(3, 4)  3

Math Methods pow(x,y) – returns x y pow(2,3)  2 3  8 round(x) – rounds x to the nearest whole # round(34.4)  34 round(34.7)  35 sqrt(x) – square root of a number sqrt(121)  √121  11

String Methods String.length( ) – returns the length of the string (how many letters long it is) String word = “dog”; word.length( )  3 String.toLowerCase( ) – changes all capital letters to lowercase String word = “HAHA”; word.toLowerCase( )  “haha”; String.toUpperCase( ) – changes all lowercase letters to capital letters String word = “kitty”; word.toUpperCase( )  “KITTY”

More String Methods String.charAt(x) – returns the letter at position x in the String String word = “happy”; word.charAt(2)  ‘p’; String.replace(x,y) – replaces each instance of character ‘x’ with character ‘y’ String word = “happy”; word.replace(‘p’,’ r’)  “harry”

indexOf( ) indexOf(char) – returns the index, or list location, of the first instance of letter char String word = “happy”; word.indexOf(‘h’)  0 indexOf(str) – returns the index, or list location, of the first instance of a String str String name = “Alexander”; name.indexOf(“and”)  4 Note: Both of these commands will return -1 if the character or String is not found

String.substring( ) String.substring(x, y) – Converts the characters between list location x of the String (inclusive) and list location y of the String (exclusive) into a new String String word = “Spring Break”; word.substring(0,6)  “Spring” word.substring(7, 12)  “Break” word.substring(3, 9)  “ing Br”

TODAY IF YOU ARE MISSING WORK…USE THIS TIME TO COMPLETE SOME OF YOUR MISSING WORK IF YOU ARE NOT MISSING WORK codingbat.com/java Complete codes within any section (though String-1 will likely be the easiest) 1 ML:A pass per SECTION STAR completed

Warm-Up: Tuesday, March 25 Give an example of a program that would require you to use an IF statement EXAMPLE: Output the phrase “Be sure to wear a jacket” if the temperature is less than 55°

IF Statements PAP Computer Science Cycle 5

IF Statements IF statements are used to add branching structure to your codes They are used to make decisions

Branching Structures ? Task A Task B YES NO

IF Statement: Structure if (condition) { //statements } STATEMENTS ARE ONLY EXECUTED IF THE CONDITION IS TRUE

Conditions Conditions evaluate a state or compare two or more numbers While the code is running If x is greater than y If the remainder is equal to 0 While x + y is less than 7

Conditional Operators Less than (<) Greater than (>) Equal to (==) Less than or equal to (<=) Greater than or equal to (>=) Not equal to (!=)

English to Code x is greater than y x > y x plus y is less than 7 x + y < 7 x is an even number x % 2 == 0 the number does not equal our target number != target

Using Conditions with IF Statements If the temperature is below 55, output that the user needs a jacket if (temp < 55) { System.out.println(“Bring a jacket!”); }

Warm-up: Thursday, Mar 27 Write an IF statement for the following: Given two numbers num1 and num2, output the sum of the numbers ONLY IF the sum is less than 10.

Warm-up: Thursday, Mar 27 Write an IF statement for the following: Given two numbers num1 and num2, output the sum of the numbers ONLY IF the sum is less than 10. double sum = a+b; if (sum < 10) { System.out.println(sum); }

Review: IF Statements Used to add branching structure to codes Make decisions Answer questions if (condition) { //statements; }

Review: Conditional Operators Less than (<) Greater than (>) Equal to (==) Less than or equal to (<=) Greater than or equal to (>=) Not equal to (!=)

IF-ELSE Statements Used for situations with two possible outcomes Noted by the keyword “otherwise” Examples If it’s sunny, wear shorts; otherwise, wear jeans If it’s a weekday, set the alarm for 7:00, otherwise, turn off the alarm

IF-ELSE Statement: Structure if (condition) { //statements; } else { //statements; } *NOTE: There is NO condition after else

Example: IF-ELSE if (weather==“sunny”) { System.out.println(“Wear shorts”); } else { System.out.println(“Wear jeans”); }

EVEN MORE BRANCHING! Sometimes, you need EVEN MORE than 2 outcomes For these situations, you use IF – ELSE-IF – ELSE statements Examples If you like dogs, buy a dog, but if you like cats, but a cat; otherwise, get a different pet. If your number is divisible by 5, output 1, but if your number is divisible by 7, output 2; otherwise, output 0.

IF – ELSE-IF – ELSE Structure if (condition1) { //statements; } else if (condition2) { //statements; } else { //statements; }

Example IF – ELSE-IF – ELSE if (num % 5 == 0) { System.out.println(1); } else if (num % 7 == 0) { System.out.println(2); } else { System.out.println(0); }

ELSE-IF’s You can have as many ELSE-IF’s in an IF statement as you want, but you can only have ONE IF and ONE ELSE

OKAY if ( …) { } else if (…) { } else if (…) { } else { }

NOT OKAY if ( …) { } else if (…) { } else { } else { }

OKAY…but it doesn’t mean the same thing… if ( …) { } if (…) { } else if (…) { } else { }

OKAY…but it doesn’t mean the same thing… if ( …) { } if (…) { } else if (…) { } else { }

Warm-Up: Friday, Mar 28 Write an IF statement for the following situation: Given two numbers, a and b, output the sum; unless the sum is greater than 20, then output the number 20.

Warm-Up: Thursday, Mar 26 Given two numbers, a and b, output the sum; unless the sum is greater than 20, then output the number 20. sum = a+b; if (sum > 20) { System.out.println(20); } else { System.out.println(sum); }