Lecture 5.2 Primitive Expression Patterns. © 2006 Pearson Addison-Wesley. All rights reserved 5.2.2 A pattern is a general template that can be applied.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs.
Advertisements

Computer Programming w/ Eng. Applications
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Types, Variables and Operators Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2013.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Friday, December 08, 2006 “Experience is something you don't get until just after you need it.” - Olivier.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos:
Mathematical Operators: working with floating point numbers and more operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this.
Review for Midterm 2 Nested loops & Math class methods & User defined methods.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
1 Data types, operations, and expressions Continued l Overview l Assignment statement l Increment and Decrement operators l Short hand operators l The.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
1 for Loops Computer Science is a science of abstraction - creating the right model for a problem and devising the appropriate mechanizable techniques.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
© 2006 Pearson Addison-Wesley. All rights reserved Reference Types A variable of reference type refers to (is bound to) an object Data of reference.
Variable Declaration  It is possible to declare multiple variables of the same data type on the same line.  Ex. double hours, rate, total;  Variables.
1 Number Types  Every value in Java is either: 1.a reference to an object or 2.one of the eight primitive types  eight primitive types: a.four integer.
Do … while ( continue_cond ) Syntax: do { stuff you want to happen in the loop } while (continue_condition);
Copyright 2009 by Pearson Education Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos: Ch.
Java Methods. Topics  Declaring fields vs. local variables  Primitive data types  Strings  Compound Assignment  Conversions from one value to another.
Ch4 Data Types Every value has a type associated with it. The computer needs to know how much memory to allocate. Every value is either a primitive type.
Chapter 4: Loops and Files
PROCESSING Numeric Types. Objectives Be able to … Explain the difference between a literal, a variable and a constant Declare numeric variables and constants.
New Tools And Workshop Mod & For Loops. Modulo Calculates the remainder (remember long division?) % Examples: 7 % 3 10 % 2 2 % 3 evaluates to 1 evaluates.
CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead.
Mathematical Calculations in Java Mrs. G. Chapman.
Introduction to Computer Programming Math Random Dice.
CSC 107 – Programming For Science. The Week’s Goal.
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.
Mixing integer and floating point numbers in an arithmetic operation.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
Mathematical Calculations in Java Mrs. C. Furman.
Lecture 5.2 Primitive Expression Patterns. © 2006 Pearson Addison-Wesley. All rights reserved A pattern is a general template that can be applied.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos:
Building Java Programs Chapter 2 Primitive Data and Definite Loops Copyright (c) Pearson All rights reserved.
Variables Symbol representing a place to store information
Introduction to Computers and Programming Lecture 7:
Operators.
1 CSC 221: Computer Programming I Fall 2005 simple conditionals and expressions  if statements, if-else  increment/decrement, arithmetic assignments.
CMSC 202 Java Primer 1. July 24, 2007 Copyright © 2008 Pearson Addison-Wesley 2 A Sample Java Application.
ICS102 Lecture 1 : Expressions and Assignment King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING For Loop.
Lecture 8.5 Animating with EventTimer. © 2006 Pearson Addison-Wesley. All rights reserved A Crash Course in the Use of Timed Events What kinds of.
Chapter INTRODUCTION Data Types and Arithmetic Calculations.
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.
What will each of the following lines print? System.out.println("number" ); number645 System.out.println("number" + (6 + 4)+ 5); number105 System.out.println(6.
CSC Programming for Science Lecture 5: Actual Programming.
Chapter 3 Structured Program Development in C Part II C How to Program, 8/e, GE © 2016 Pearson Education, Ltd. All rights reserved.1.
Swap Pattern Example Code Pattern: Swap preAssertion
Expression ISYS 350.
Lecture 3 Java Operators.
Assignment and Arithmetic expressions
Primitive Data, Variables, Loops (Maybe)
Expression ISYS 350.
Arithmetic Operator Operation Example + addition x + y
Expression ISYS 350.
Expression ISYS 350.
Building Java Programs
Conversion Check your class notes and given examples at class.
Expressions and Assignment
Building Java Programs
Expression ISYS 350.
Building Java Programs
Expression ISYS 350.
Building Java Programs
Building Java Programs
Building Java Programs
Presentation transcript:

Lecture 5.2 Primitive Expression Patterns

© 2006 Pearson Addison-Wesley. All rights reserved A pattern is a general template that can be applied to solve specific problems. The task of computer programming requires knowledge of patterns. Example Code Pattern: Using a variable This pattern explains the order of events that is required to utilize a variable. 1) Declare the variable (name & type). 2) Initialize the variable. 3) Use the variable. 4) Dispose of the variable. (optional)

© 2006 Pearson Addison-Wesley. All rights reserved Example Code Pattern: Swap This pattern is a code segment that interchanges the binding (for reference data) or value (for primitive data) of two variables. SomeType tmp; tmp = var1 ; var1 = var2 ; var2 = tmp; preAssertion var1 and var2 are any two variables of the same type, call this type someType postAssertion var1 == and var2 == denotes the value/binding of varX before the pattern executes.

© 2006 Pearson Addison-Wesley. All rights reserved Problem A program to place two circles. The LEFT button should make one of the two circles grow, and the RIGHT button should select which is to grow. (Each RIGHT click should select the alternate circle.) public void leftAction() { growingCircle.setSize((int)(1.2 * growingCircle.getWidth()), (int)(1.2 * growingCircle.getHeight()) ); growingCircle.repaint(); } private Oval growingCircle, fixedCircle ; /** post growingCircle == * and fixedCircle == */ public void rightAction() { Oval tmpCircle; tmpCircle = growingCircle; growingCircle = fixedCircle; fixedCircle = tmpCircle; }

© 2006 Pearson Addison-Wesley. All rights reserved Sometimes a pattern can be as simple as a single expression.. Accurate Integer Division Expression Pattern Integer division (quotient) leads to a loss of accuracy. For maximum accuracy when dividing intExpr1 by intExpr2 the following double expression can be used. (double)( intExpr1 ) / intExpr2 ; Conversion to int Expression Pattern This pattern assigns the truncated value of doubleExpr to intVar. intVar = (int)( doubleExpr ); This pattern assigns the rounds value of intExpr to doubleVar. intVar = (int)( doubleExpr ); intVar = (int)Math.round( doubleExpr ); OR

© 2006 Pearson Addison-Wesley. All rights reserved Frequently, variables need to be increased/decreased by some fixed value. Increment by n Expression Pattern This pattern increases the value of intVar by the value n intVar = intVar + n ; Example public void leftAction() { xPosition = xPosition + 10; dot.setLocation( xPosition, dot.getY() ); } public void leftAction() { xPosition = xPosition + 10; dot.setLocation( xPosition, dot.getY() ); } /** post xPosition == + 10 * and dot.getX() == xPosition */ /** post xPosition == + 10 * and dot.getX() == xPosition */

© 2006 Pearson Addison-Wesley. All rights reserved Increment by 1 Expression Pattern This pattern increases the value of intVar, by the value 1 intVar ++; Example diameterTot = diameterTot + dot.getWidth(); System.out.println( “Total of all diameters is ” + diameterTot ); Running Total Update Expression Pattern This pattern updates a running total, called intTotal, by the value n Decrement by 1 Expression Pattern This pattern decreases the value of intVar, by the value 1 intVar --; intTotal = intTotal + n ; Add the diameter of dot to the total of all dot diameters.

© 2006 Pearson Addison-Wesley. All rights reserved General Counting Expression Pattern This pattern uses the increment pattern to count one more occurrence of some situation. intCounter ++; public void leftAction() { leftClickCount++; } public void leftAction() { leftClickCount++; } Example Count the number of times LEFT is clicked. Modulus Counting Expression Pattern This pattern counts from 0 to n then back to zero 0, 1, 2,..., n, 0, 1, 2,..., n, 0,... intCounter = ( intCounter + 1) % ( n + 1) ; public void leftAction() { leftCounter = (leftCounter + 1) % 4; } public void leftAction() { leftCounter = (leftCounter + 1) % 4; } Example Count the number of times that LEFT modulo 4. (i.e., 0, 1, 2, 3, 0, 1, 2, 3,...)

© 2006 Pearson Addison-Wesley. All rights reserved Random int Expression Pattern This results in an integer randomly selected from the range from minInt through maxInt. (int)( minInt + Math.random() * ( maxInt - minInt + 1) ) dieRoll = (int)(1 + Math.random() * 6); Examples Generate a random roll of one die Select a dot diameter from the range of 10 through 100. int diameter = (int)(10 + Math.random() * 91); dot.setSize( diameter, diameter ); Random double Expression Pattern This results in an double randomly selected from the range from minDouble through maxDouble. minDouble + Math.random() * ( maxInt - minInt ) System.out.println(“I pick “ + (10 + Math.random() * 15) ); Examples Pick a number from 10.0 to 25.0