CS 100Lecture 21 CS100J: Lecture 2 n Previous Lecture –Programming Concepts n problem, algorithm, program, computer, input, output, sequential execution,

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

Today’s lecture Review of Chapter 1 Go over homework exercises for chapter 1.
Introduction to C Programming
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
CS 100Lecture 61 CS100J Lecture 6 n Previous Lecture –Programming Concepts n Programming by stepwise refinement –a pattern –sequential refinement –case.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
CMT Programming Software Applications
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
CS 100Lecture 41 CS100J Lecture 4 n Previous Lecture –Programming Concepts n iteration n programming patterns (templates) –Java Constructs n while-statements.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Introduction to C Programming
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
CHAPTER 4: CONTROL STRUCTURES - SEQUENCING 10/14/2014 PROBLEM SOLVING & ALGORITHM (DCT 1123)
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Chapter 4: Control Structures II
Chapter 5: Control Structures II
CS 106 Introduction to Computer Science I 01 / 26 / 2007 Instructor: Michael Eckmann.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Boolean expressions, part 1: Compare operators. Compare operators Compare operators compare 2 numerical values and return a Boolean (logical) value A.
A Sample Program public class Hello { public static void main(String[] args) { System.out.println( " Hello, world! " ); }
CS 100Lecture 111 CS100J Lecture 11 n Previous Lecture –Scope of names and the lifetime of variables n blocks and local variables n methods and parameters.
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.
CS 100Lecture 11 Introduction to Programming n What is an algorithm? n Input and output n Sequential execution n Conditional execution Reading: Chapter.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
CompSci 230 S Programming Techniques
Building Java Programs
2.5 Another Java Application: Adding Integers
Chapter 5: Control Structures II
Primitive Data, Variables, Loops (Maybe)
Java Programming: From Problem Analysis to Program Design, 4e
SELECTION STATEMENTS (1)
Introduction to C++ Programming
Chapter 2: Basic Elements of Java
Building Java Programs
CS100J Lecture 11 Previous Lecture This Lecture
Building Java Programs Chapter 2
CS100J Lecture 7 Previous Lecture This Lecture Java Constructs
CS100J Lecture 3 Previous Lecture This Lecture Programming Concepts
Building Java Programs
Building Java Programs
Chapter 2 Programming Basics.
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Unit 3: Variables in Java
Building Java Programs Chapter 2
Building Java Programs
Building Java Programs
Presentation transcript:

CS 100Lecture 21 CS100J: Lecture 2 n Previous Lecture –Programming Concepts n problem, algorithm, program, computer, input, output, sequential execution, conditional execution –Reading: Lewis & Loftus, or Savitch: Chapter 1 n This Lecture –Programming Concepts n variables and declarations, assignment, expressions –Java Constructs n assignment statement n expressions n conditional statement n output statement n comments n boilerplate n block statement –Reading: n Lewis & Loftus, Chapter 2 and Sections , or n Savitch, Chapter 2 and Section 3.1

CS 100Lecture 22 Algorithm / Program A Problem statement An integer is given as input data. If it is even, output “even”, otherwise output “odd”. Program in English 1. Get the integer and call it num 2. Divide num by 2 and call the remainder rem 3. If rem is 0, output “even”, otherwise output “odd” Program Segment in Java num = in.readInt(); num = in.readInt(); rem = num % 2; rem = num % 2; if ( rem == 0 ) if ( rem == 0 ) System.out.println("even"); System.out.println("even"); else else System.out.println("odd"); System.out.println("odd");

CS 100Lecture 23 Initial Observations n A “segment” is a part of a program n Java is like stylized English n Sequence of imperatives, known as statements n Statements are not numbered n Layout is essential for readability, but irrelevant for meaning, e.g., this code segment has the same effect: num=in.readInt();rem=num%2;if(rem==0) System.out.println("even");else System.out.println("odd"); n English version: –names num and rem Java version: Java version: –memory locations num and rem n Memory locations are known as variables

CS 100Lecture 24 Variables and Assignment n variable A place that can hold a value Graphically: name name Italics designate placeholders, e.g., name stands for some name Italics designate placeholders, e.g., name stands for some name n assignment The act of storing a value in a variable. num n destructive update Assigning a value to a variable destroys the previous contents of the variable 0 value 0 rem remnum

CS 100Lecture 25 Assignment Statement n Template: n Meaning: store the value of the expression into the variable n Read “=“ as “is assigned the value”, or “becomes equal to”, or “gets the value” n An expression is a formula (as in algebra) n Examples num = in.readInt(); rem = num % 2; postage = 33; average = sum / count; count = count + 1; variable = expression ;

CS 100Lecture 26 Expression Evaluation n Variables: Current value in the variable n Example –Suppose variable num contains 7 –Then in evaluating the expression num % 2 the value of num –Then in evaluating the expression num % 2 the value of num is 7 n Constants: Examples: 2, 33, 1 Examples: 2, 33, 1 n Operations: As in algebra n Examples + addition - subtraction * multiplication / division % remainder n Functions: As in mathematics n Examples sin(x) max(x, y) in.readInt() n Parentheses: For grouping

CS 100Lecture 27 Conditional Statement n Template: Meaning: Execute statement 1 if expression has value true, otherwise execute statement 2 Meaning: Execute statement 1 if expression has value true, otherwise execute statement 2 n Logical operations: == equality != inequality < less than <= less than or equal to > greater than >= greater than or equal to “ else statement ” can be omitted, meaning “otherwise do nothing” “ else statement ” can be omitted, meaning “otherwise do nothing” if ( expression ) statement 1 statement 1else statement 2 statement 2

CS 100Lecture 28 Output Statement n Template: n Meaning: Output the value of expression After output, println advances to the next line, print stays on same line After output, println advances to the next line, print stays on same line n Examples: System.out.println( ”even” ); System.out.println( ”even” ); System.out.println( num + ” is even”); System.out.println( num + ” is even”); In the second example, num is converted to a textual representation, which is then concatenated to the 8 letters “ is even”, and output In the second example, num is converted to a textual representation, which is then concatenated to the 8 letters “ is even”, and output System.out.println( expression ); System.out.print( expression );

CS 100Lecture 29 Comments n Template: n Meaning: Ignored by Java, but essential for human understanding n Example: // Set num to be the input integer. num = in.readInt(); num = in.readInt(); // Set rem to be the remainder of num/2. rem = num % 2; rem = num % 2; if ( rem == 0 ) System.out.println("even"); System.out.println("even");else System.out.println("odd"); System.out.println("odd"); // any-text-to-end-of-line /* any-text */

CS 100Lecture 210 Program A in Java /* Input an integer and output "even" if it is even, otherwise output "odd. */ is even, otherwise output "odd. */ import java.io.*; public class OddEven { public static void main(String args[]) { int num; // Input integer. int num; // Input integer. int rem; // Remainder of num / 2. int rem; // Remainder of num / 2. // Initialize Text object in to read // Initialize Text object in to read // from standard input. // from standard input. TokenReader in = new TokenReader(System.in); TokenReader in = new TokenReader(System.in);}} // Set num to be the input integer. num = in.readInt(); num = in.readInt(); // Set rem to be the remainder of num/2. rem = num % 2; if ( rem == 0 ) System.out.println("even"); System.out.println("even");else System.out.println("odd"); System.out.println("odd");

CS 100Lecture 211 Outermost Structure n Template: n Meaning: execute declarations, then execute statements, then stop. n The comment should specify exactly what task the program performs Declare each variable in declarations Declare each variable in declarations n Don’t try to understand the rest now /* comment */ import java.io.*; public class name { public static void main(String args[]) public static void main(String args[]){ declarations declarations // Initialize Text object in to read // Initialize Text object in to read // from standard input. // from standard input. TokenReader in = new TokenReader(System.in); TokenReader in = new TokenReader(System.in); statements statements }}

CS 100Lecture 212 Declarations n Template: Meaning: create a list of named variables that can contain int values Meaning: create a list of named variables that can contain int values Use meaningful variable names that suggests the variable’s purpose Use meaningful variable names that suggests the variable’s purpose Names in list-of-variables are separated with commas. Names in list-of-variables are separated with commas. Other types of variables besides int Other types of variables besides int Examples: –float for numbers in scientific notation –boolean for logical values true and false –etc. int list-of-variables ; // comment

CS 100Lecture 213 Block Statement n Motivation: Some syntactic contexts permit only a single statement, e.g., if ( expression ) statement 1 statement 1 else else statement 2 statement 2 To do several things at statement, you need a way to make a sequence of statements act as one statement To do several things at statement, you need a way to make a sequence of statements act as one statement n Template: i.e., a block statement is a list of statements in curly braces. i.e., a block statement is a list of statements in curly braces. n Meaning: execute each statement in the list-of-statements in sequence { list-of-statements }

CS 100Lecture 214 Block Statement, cont. n Example: if ( expression ) { list-of-statements list-of-statements } else else { list-of-statements list-of-statements }

CS 100Lecture 215 Things to Do n Buy a floppy disk if you will use public computers n Program 1: do it n Sections: pick one and attend first meeting n Reading: –Lewis & Loftus, Chapter 2 and Sections , or –Savitch, Chapter 2 and Section 3.1