CS 106 Introduction to Computer Science I

Similar documents
CS 106 Introduction to Computer Science I

CS 106 Introduction to Computer Science I

CS 106 Introduction to Computer Science I

Information Science 1

Chapter 3 Structured Program Development in C Part II

Information Science 1

SOFTWARE DEVELOPMENT 1. Operators 2018W A. Ferscha (Institute of Pervasive Computing, JKU Linz)

In Fig. 3.5 and Fig. 3.7, we include some completely blank lines in the pseudocode for readability. programs into their various phases.

ECE 122 Engineering Problem Solving with Java

Fundamentals of Programming Session 7

Slide 1 Side Effects Duration: 00:00:53 Advance mode: Auto

Data Conversion & Scanner Class

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

CS 106 Introduction to Computer Science I

CS313D: ADVANCED PROGRAMMING LANGUAGE

Declaration and Memory

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

cs1114 REVIEW of details test closed laptop period

CS 106 Introduction to Computer Science I

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

Primitive Types. Four integer types: Two floating-point types: One character type: One boolean type: byte short int (most common) long

Chapter 2 Elementary Programming

Chapter 2. Elementary Programming

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

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

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

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

More Programming Constructs -- Introduction

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

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

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

Chapter 2: Data and Expressions

What we will do today Explain and look at examples of. Programs that examine data. Data types. Topic 4. variables. expressions. assignment statements

Advanced Computer Programming

Chapter 2: Data and Expressions

Chapter 12 Variables and Operators

CSC 1214: Object-Oriented Programming

Visual C# Instructor s Manual Table of Contents

Computational Expression

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 14: OCT. 25TH INSTRUCTOR: JIAYIN WANG

CS171:Introduction to Computer Science II

Chapter 12 Variables and Operators

ISA 563 : Fundamentals of Systems Programming

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

Chapter 12 Variables and Operators

SECTION II: LANGUAGE BASICS

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

JAVA OPERATORS GENERAL

CMPT 125: Lecture 3 Data and Expressions

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

JAVA REVIEW cs2420 Introduction to Algorithms and Data Structures Spring 2015

1.3b Type Conversion

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

CS 206 Introduction to Computer Science II

CS 106 Introduction to Computer Science I

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

Chapter 7. Expressions and Assignment Statements ISBN

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Chapter 2: Data and Expressions

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

MODULE 02: BASIC COMPUTATION IN JAVA

Operators & Expressions

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Chapter 7. Expressions and Assignment Statements

Java enum, casts, and others (Select portions of Chapters 4 & 5)

Full file at

Motivations. Chapter 2: Elementary Programming 8/24/18. Introducing Programming with an Example. Trace a Program Execution. Trace a Program Execution

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

SSEA Computer Science: Track A. Dr. Cynthia Lee Lecturer in Computer Science Stanford

Chapter 3: Operators, Expressions and Type Conversion

DATA TYPES AND EXPRESSIONS

Chapter 6 Primitive types

Expressions & Assignment Statements

LECTURE 3 C++ Basics Part 2

1.00 Lecture 4. Promotion

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

CS 115 Lecture 8. Selection: the if statement. Neil Moore

Chapter 7. Additional Control Structures

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Expression and Operator

Topic 4 Expressions and variables

Chapter 2: Using Data

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

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

Chapter 2 Elementary Programming. Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.

Operators in java Operator operands.

Lecture Set 4: More About Methods and More About Operators

CS 106 Introduction to Computer Science I

Data Types and Variables in C language

Slide 1 CS 170 Java Programming 1 Expressions Duration: 00:00:41 Advance mode: Auto

Decisions, Decisions. Testing, testing C H A P T E R 7

Informatics Ingeniería en Electrónica y Automática Industrial

Classwork 7: Craps. N. Duong & R. Rodriguez, Java Crash Course January 6, 2015

Chapter 7. Expressions and Assignment Statements (updated edition 11) ISBN

COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal

Operators. Lecture 3 COP 3014 Spring January 16, 2018

CS 206 Introduction to Computer Science II

Java Basic Programming Constructs

Transcription:

CS 106 Introduction to Computer Science I 01 / 31 / 2014 Instructor: Michael Eckmann

Today s Topics Comments and/or Questions? Pseudocode Programming exercise to determine projected homeruns How Java determines type

Review Method call Parameter What a method returns How to capture what a method returns Packages / classes / methods

Pseudocode pseudocode is an informal use of English to describe what a program is to do and in what order pseudocode is not an actual computer programming language it is used prior to writing actual code to help the programmer in the planning stages of a program

Example Application Exercise write a program to compute the number of projected home runs a baseball player will hit for the season based on how many homers he s hit so far. Output should look like: player s name is projected to hit number home runs in 162 games. Any ideas?

Pseudocode for our example get player s name from the user get the number of homeruns so far get the number of games played so far compute the number of projected homeruns for this player based on a season of 162 games by using the following calculation projected homers homers so far --------------------- = ------------------------- 162 games played so far

Pseudocode for our example (continued) from this equation, projected homers homers so far --------------------- = ------------------------- 162 games played so far we can multiply both sides of the equation by 162 and get projected homers = homers so far * 162 / games played so far

Pseudocode for our example (continued) Print out the following with actual values for player s name and number player s name is projected to hit number home runs in 162 games. Pseudocode could be more fleshed out than what we have done here --- use as much or as little detail in pseudocode as you prefer.

Pseudocode for our example (continued) Now we can write the program based on our pseudocode.

How Java determines type Each operator in a complex expression is evaluated one at a time. The order they are done is according to the precedence rules we learned last time. What Java does (and we need to do as well if we are to understand how it works) is it evaluates one operator at a time and uses information about the type of operand(s) being worked on to determine the type of the result. example expressions: 14 * some_int // assume that the type of some_int is int 3.14159 * diameter // assume that the type of diameter is double Java figures out the resulting types according to the types of the operands. This is easy if the operands are the same type.

How Java determines type If the operands are of different types, Java promotes/converts one of the operands to the type of the other thereby temporarily making that value the same type and makes that the resulting type as well. This promotion is sometimes called type coercion. We saw a kind of type coercion when we concatenate Strings with non-strings. Java temporarily converts the non-string to a String and concatenates them. In expressions among operands of different types, Java does the following. It temporarily converts the smaller type into the larger type, then does the operation and the result is the larger type. The order of the primitive types from smaller to larger is: byte, short, int, long, float, double this list excludes char and boolean

How Java determines types Examples 45 + 34.5 // an int is being added to a double 19 / 9 // an int is being divided by an int

How Java determines types Examples: int a = 5, b = 6, c; double d1 = 4.3, d2 = 88.4, d3; d3 = d1 * a; /* here a's value (not the a variable) is temporarily converted to be used as a double (just for this calculation) and then the multiplication is done and the result is a double. This result is then assigned to the variable d3 */ //problem if try to assign a double to an int, e.g. c = d2 * b; // what will happen? Try it and see.

another promotion example E.g. int count = 3; double sum=6.7, result; result = sum / count; /* count's value is promoted to be used as a double automatically so the division can take place */

Assignment conversion The name given to Java s type conversion that is done when a value of one type is assigned to a variable of another type. This is allowed as long as we are assigning a smaller type to a larger type. What does that mean? E.g. int count = 3; float result; result = count; /* count is converted to be used as a float automatically and its value is assigned to result 3.0 */

Casting conversion The name given to the programmer s ability to force type conversion. E.g. int number_of_students, number_of_faculty; double students_per_faculty; students_per_faculty = (double) number_of_students / number_of_faculty; /* force number_of_students to be used as a double and then result of division is double also contains use of promotion --- where is promotion done? */

Another example. Casting conversion int homers_so_far, games_so_far; double projected_homers; // assume the ints get values from somewhere then... projected_homers = (double)162 * homers_so_far / games_so_far; // I'll explain exactly what is going on here with the types

Increment & decrement (sec. 2.2) ++ and - - note: there must be no space btwn the minuses or the plusses ++ adds 1 to the variable - - subtracts 1 from the variable int count = 55; count++; // same as count = count + 1;

Increment & decrement (sec. 2.2) count++; // adds 1 to count ++count; // also adds 1 to count count--; // subtracts 1 from count --count; // also subtracts 1 from count Which side the ++ or - - is on only matters when it's used within an expression.

Increment & decrement (sec. 2.2) Note: ++ before a variable or expression is a preincrement, ++ after a variable or expression is postincrement, -- before a variable or expression is a predecrement, -- after a variable or expression is a postdecrement.

Increment & decrement (sec. 2.2) Which side the ++ or -- is on only matters when it's used within an expression. e.g. total = count++; acts differently than total = ++count;

Increment & decrement (sec. 2.2) total = count++; /* the above line assigns the value of count to total and then adds 1 to count (hence the name post-increment) */ total = ++count; /* the above line adds 1 to the value of count and then assigns this new value of count to total (pre-increment) */ // so what's the difference?

Increment & decrement (sec. 2.2) Also similar behavior in conditions of if's e.g. if (index++ >= 5) // execute something if true else // execute something if false /* the above first compares index and 5 to determine if index is >=5, then 1 is added to index, then depending on result of the compare, the if or else line will execute */

Increment & decrement (sec. 2.2) Also similar behavior in conditions of if's e.g. if (++index >= 5) // execute something if true else // execute something if false /* the above first adds 1 to index and then compares this new value of index to 5 to determine if index is >=5, then depending on result of the compare, the if or else line will execute */

More assignment operators besides = +=, -=, *=, /=, %= Here's what they mean. Suppose we have: int a=7; a = a + 6; // is equivalent to: a += 6; These assignment operators are used only if the LHS (left hand side of the assignment) is also the first variable on the RHS.

More assignment operators besides = +=, -=, *=, /=, %= More examples: degrees -= 5; /* subtract 5 from degrees and store this new value in degrees. */ halve_me /= 2; /* divides the value in halve_me by 2 and stores this new value in itself */ fine *= 3; // value in fine is tripled