Darrell Bethea May 25, 2011

Similar documents
COMP Introduction to Programming Primitive Types, Strings and Console I/O

COMP Primitive and Class Types. Yi Hong May 14, 2015

COMP Introduction to Programming Midterm Review

Darrell Bethea May 10, MTWRF 9:45-11:15 AM Sitterson 011

Program Fundamentals

Programming with Java

Course Outline. Introduction to java

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Full file at

St. Edmund Preparatory High School Brooklyn, NY

Basics of Java Programming

CMPT 125: Lecture 3 Data and Expressions

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Section 2.2 Your First Program in Java: Printing a Line of Text

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Computational Applications in Nuclear Astrophysics using Java Java course Lecture 2

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Full file at

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

Introduction to Java & Fundamental Data Types

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

Lecture Set 2: Starting Java

Lecture Set 2: Starting Java

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

Introduction to Java Applications

Basic Computation. Chapter 2

Computational Expression

Object-Oriented Programming

Chapter 1. Introduction to Computers and Programming. M hiwa ahmad aziz

Fundamentals of Programming CS-110. Lecture 2

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

Controls Structure for Repetition

Hello World. n Variables store information. n You can think of them like boxes. n They hold values. n The value of a variable is its current contents

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

Getting started with Java

CEN 414 Java Programming

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

Datatypes, Variables, and Operations

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

AP Computer Science Unit 1. Programs

AYBUKE BUYUKCAYLI KORAY OZUYAR MUSTAFA SOYLU. Week 21/02/ /02/2007 Lecture Notes: ASCII

PROGRAMMING FUNDAMENTALS

Computer Components. Software{ User Programs. Operating System. Hardware

CS110: PROGRAMMING LANGUAGE I

Introduction to Java Applications; Input/Output and Operators

Introduction to Programming EC-105. Lecture 2

COMP String and Console I/O. Yi Hong May 18, 2015

Flow of Control Branching 2. Cheng, Wei COMP May 19, Title

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

COMP-202: Foundations of Programming. Lecture 4: Flow Control Loops Sandeep Manjanna, Summer 2015

CSc 2010 Principles of Computer Science, Fall 2013 Practice Problems for Midterm 3* * 3 17 % 9-20 % (26 / 7) "2"

Chapter 2: Programming Concepts

COMP Flow of Control: Branching 2. Yi Hong May 19, 2015

Module 3 SELECTION STRUCTURES 2/15/19 CSE 1321 MODULE 3 1

Chapter 2 Elementary Programming

Computer Components. Software{ User Programs. Operating System. Hardware

Exam 2. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 40 Obtained Marks:

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

Chapter 2 Part 2 Edited by JJ Shepherd, James O Reilly

Computer Programming : C++

Program Elements -- Introduction

McGill University School of Computer Science COMP-202A Introduction to Computing 1

Chapter 2. Elementary Programming

ECE 122 Engineering Problem Solving with Java

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Chapter 2: Data and Expressions

COMP 202 Java in one week

CS 106 Introduction to Computer Science I

Last Time. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

Basic Computation. Chapter 2

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

CS11 Java. Fall Lecture 1

First Java Program - Output to the Screen

CSE 20. SAMPLE FINAL Version A Time: 180 minutes. The following precedence table is provided for your use:

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

Array. Prepared By - Rifat Shahriyar

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Chapter 2 ELEMENTARY PROGRAMMING

Handout 4 Conditionals. Boolean Expressions.

Fundamental of Programming (C)

Introduction to C# Applications

Loops. CSE 114, Computer Science 1 Stony Brook University

Chapter 2: Data and Expressions

Java for Python Programmers. Comparison of Python and Java Constructs Reading: L&C, App B

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. A Guide to this Instructor s Manual:

2.8. Decision Making: Equality and Relational Operators

Lecture 3 Tao Wang 1

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

COMP-202: Foundations of Programming. Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015

Introduction to Java Unit 1. Using BlueJ to Write Programs

Lesson 02 Data Types and Statements. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

CS111: PROGRAMMING LANGUAGE II

Elementary Programming

Fundamentals of Programming

Transcription:

Darrell Bethea May 25, 2011

Yesterdays slides updated Midterm on tomorrow in SN014 Closed books, no notes, no computer Program 3 due Tuesday 2

3

A whirlwind tour of almost everything we have covered so far These slides are essentially extracted from earlier lectures 4

Hardware - physical machine CPU, Memory Software - programs that give instructions to the computer Windows XP, Games, Eclipse 6

CPU the brain of your computer Memory stores data for the computer How much the brain can remember Main memory Auxiliary memory 7

Measured in bytes 1 byte = 8 bits Bit is either 0 or 1 Language of the computer is in bits 8

Your Program High-level language (human readable) Compiler Machine Language (Bits) Low-level language (computer readable) 9

Algorithm a set of instructions for solving a problem Pseudocode combination of code and English used to express an algorithm before writing algorithm into code 10

Used to store data in a program The data currently in a variable is its value Name of variable is an identifier Can change value throughout program Choose variable names that are meaningful! 11

Declare a variable int number; Assign a value to the variable number = 37; Change the value of the variable number = 513; 12

Reserved words with predefined meanings You cannot name your variables keywords if, else, return, new 13

What kind of value the variable can hold Two kinds of types. Primitive type - indecomposable values Names begin with lowercase letters int, double, char, float, byte, boolean, some others Class type - objects with both data and methods Names by convention begin with uppercase letter Scanner, String, Student 14

Change a variable s value Syntax: variable = expression; Example: sleepneeded = 8; sleepdesired = sleepneeded * 2; 15

You can only put small things into bigger things byte->short->int->long->float->double myshort myint; mybyte mylong; myfloat = mybyte; mylong = myint; 20

You can ask Java to change the type of values which would violate the compatibility rule. myfloat = mydouble; mybyte = myint; myshort = myfloat; myfloat = (float)mydouble; mybyte = (byte)myint; myshort = (short)myfloat; 21

Unary operators +, -, ++, --,! Binary arithmetic operators *, /, %, +, - rate*rate + delta 1/(time + 3*mass) (a - 7)/(t + 9*v) 18

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

Expressions inside parentheses evaluated first (cost + tax) * discount cost + (tax * discount) Highest precedence First: the unary operators: +, -, ++, --,! Second: the binary arithmetic operators: *, /, % Third: the binary arithmetic operators: +, - Lowest precedence 20

Syntax error grammatical mistake in your program int n3 = n1 + n2, // Need a ;, not a, Run-time error an error that is detected during program execution int n3 = n1 / n2; // But n2 == 0 Logic error a mistake in a program caused by the underlying algorithm int n3 = n1 - n2; // But we meant to sum.

A 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 22

String animal = aardvark ; System.out.println(animal); aardvark 23

String animal = aardvark ; String sentence; sentence = My favorite animal is the + animal; My favorite animal is the aardvark 24

mystring.length(); mystring.equals( a string ); mystring.tolowercase(); mystring.trim(); Many others 25

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

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

\ Double quote \ʼ Single quote \\ Backslash \n New line \r Carriage return \t Tab 28

Scanner kb = new Scanner(System.in); int num = kb.nextint(); 29

// this is a comment /* This is also a comment */ 30

An expression that is either true or false Examples: It is sunny today (true) 10 is larger than 5 (true) Today is Saturday (false) 31

import java.util.*; Yes Print: big number Prompt user for integer Is input greater than 10? No Print: small number public class FlowChart { public static void main(string[] args) { System.out.println("Give me an integer:"); Scanner keyboard = new Scanner(System.in); int inputint = keyboard.nextint(); } } if (inputint > 10) { System.out.println("big number"); } else { System.out.println("small number"); } 32

== Equal to!= Not equal to > Greater than >= Greater than or equal to < Less than <= Less than or equal to Example expressions:! variable <= 6 myint > 5 5 == 3 33

Can be either true or false boolean sunny = true; boolean cloudy = false; if (sunny cloudy) { // walk to school } 34

AND if ((temperature > 50) && (temperature < 75)) { // walk to school } OR if (sunny cloudy) { // walk to school } 35

!true is false!false is true Example: walk to school if it is NOT cloudy if (!cloudy) { // walk to school } 36

Case labels Default case: all other values switch(year) { Controlling expression case 1: System.out.println( freshman ); break; case 2: System.out.println( sophomore ); break; case 3: System.out.println( junior ); break; Break statements case 4: System.out.println( senior ); break; case 5: System.out.println( super senior ); break; default: System.out.println( unknown ); break;

Loop: part of a program that repeats Start Body: statements being repeated Enough sandwiches? Iteration: each repetition of body Make sandwich No Yes Stopping condition Distribute sandwiches 38

while Safest choice Not always most elegant Loop iterates 0 or more times do-while Loop iterates AT LEAST once for Similar to while, but often more convenient syntax Most useful when you have a known # of iterations you need to do 39

int n = 1; while (n <= 10) { System.out.println(n); n = n + 1; } 40

int n = 1; do { System.out.println(n); n = n + 1; } while (n <= 10); Don t forget the semicolon! 41

int n; for (n = 1; n <= 10; n++) { System.out.println(n); } 42

int n; for (n = 1; n <= 10; n = 0) { System.out.println(n); } 43

for (int item = 1; item <= 5; item++) { System.out.print( Enter cost of item # + item + : $ ); amount = keyboard.nextdouble(); total = total + amount; if (total >= 100) { System.out.println( You spent all your money. ); break; } System.out.println( Your total so far is $ + total); } System.out.println( You spent $ + total); 44

Output instructions to the user Initialize variables Prompt user for input Read a number into variable next sum = sum + next; Prompt user for input Read a number into variable next sum = sum + next; Prompt user for input Read a number into variable next sum = sum + next;... Output the sum Repeated statements become your loop body Statements that are only done once are not part of your loop body 45

Variables used in your loop need to be initialized (set to a value) before the loop next Read a number into variable next We read a new value for next before using it during each iteration of the loop so we do not need to initialize it sum sum = sum + next; sum is on the right side of an assignment statement. sum MUST have a valid value before the loop starts. 46

Count-controlled loops If you know the number of loop iterations for (count = 0; count < iterations; count++) User-controlled loops Ask-before-iterating Sentinel value 47

Outer loop Inner loop for (int stdlinea = 1; stdlinea <= 3; stdlinea++) { for (int stdlineb = 4; stdlineb <= 6; stdlineb++) { System.out.println( Student + stdlinea + shakes Student + stdlineb + s hand. ); } } 48