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
Computer Programming w/ Eng. Applications
Advertisements

Types, Variables and Operators Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2013.
Copyright 2011 by Pearson Education Building Java Programs Chapter 3 Lecture 3-2: Return values, Math, and double reading: 3.2,
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 4 Mathematical Functions, Characters,
Methods Liang, Chapter 4. What is a method? A method is a way of running an ‘encapsulated’ series of commands. System.out.println(“ Whazzup ”); JOptionPane.showMessageDialog(null,
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Math class methods & User defined methods Introduction to Computers and Programming in JAVA: V
Introduction to Java Programming, 4E Y. Daniel Liang.
Review for Midterm 2 Nested loops & Math class methods & User defined methods.
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
1 Chapter 5 Methods. 2 Introducing Methods A method is a collection of statements that are grouped together to perform an operation.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
1 Topic 04 Methods Programming II/A CMC2522 / CIM2561 Bavy Li.
1 Data types, operations, and expressions Continued l Overview l Assignment statement l Increment and Decrement operators l Short hand operators l The.
Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.
Java Basics (continued)
© 2006 Pearson Addison-Wesley. All rights reserved Reference Types A variable of reference type refers to (is bound to) an object Data of reference.
1 Intro to Computer Science I Chapter 2 Fundamental Data Types Java scripting with BeanShell.
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.
INF120 Basics in JAVA Programming AUBG, COS dept, Spring 2014 Lecture 08 Title: Methods, Part 2 Reference: MalikFarrell, chap 1, Liang, Ch 5.
Java Methods. Topics  Declaring fields vs. local variables  Primitive data types  Strings  Compound Assignment  Conversions from one value to another.
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
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
Lecture 5.2 Primitive Expression Patterns. © 2006 Pearson Addison-Wesley. All rights reserved A pattern is a general template that can be applied.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
1 Introducing Methods A method is a collection of statements that are grouped together to perform an operation.
Chapter 5: Methods 1. Objectives To declare methods, invoke methods, and pass arguments to a method (§ ). To use method overloading and know ambiguous.
Chapter 4 Methods F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters F Pass by Value F Overloading Methods F Method Abstraction.
Copyright 2011 by Pearson Education Building Java Programs Chapter 3 Lecture 7: Return values, Math, and double reading: 3.2,
Week 5 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 4 Mathematical Functions, Characters,
PROCESSING Numeric Types. Objectives Be able to … Explain the difference between a literal, a variable and a constant Declare numeric variables and constants.
Math With Java The Math Class. First, A Quick Review of Math Operators in Java Primitive Data type in Java that represent numbers: Primitive Data type.
Mathematical Calculations in Java Mrs. G. Chapman.
Math Class Part of the Java.lang package. This package is from object so you do not have to import the lang package. Static: Math Class is static.
Introduction to Computer Programming Math Random Dice.
1 Chapter 6 Methods. 2 Objectives F To declare methods, invoke methods, and pass arguments to a method. F To use method overloading and know ambiguous.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Lecture 15.1 Static Methods and Variables Static Methods In Java it is possible to declare methods and variables to belong to a class rather than.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 4 Methods Chapter.
Mathematical Calculations in Java Mrs. C. Furman.
Programs That Calculate. Arithmetic Expressions +addition -subtruction *multiplication /division  Order of Operations BEDMAS (brackets, exponentiation,
The Math Class Methods Utilizing the Important Math Operations of Java!
1 Java Library Lecture 9 by Dr. Norazah Yusof. 2 Java Library Java has pre-defined classes that consist of the basic language classes in Java (organized.
1 Chapter 6 Methods. 2 Objectives F To declare methods, invoke methods, and pass arguments to a method. F To use method overloading and know ambiguous.
Chapter 4: Methods Method Structure Method Structure Declaring Methods Declaring Methods Calling Methods Calling Methods Passing Parameters Passing Parameters.
CMSC 202 Java Primer 1. July 24, 2007 Copyright © 2008 Pearson Addison-Wesley 2 A Sample Java Application.
Copyright 2011 by Pearson Education Building Java Programs Chapter 3 Lecture 3-2: Return values, Math, and double reading: 3.2,
Lecture 5: java.lang.Math, java.lang.String and Characters Michael Hsu CSULA.
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.
CSC Programming for Science Lecture 5: Actual Programming.
Chapter 4 Mathematical Functions, Characters, and Strings 1.
Chapter 4 Mathematical Functions, Characters, and Strings
Swap Pattern Example Code Pattern: Swap preAssertion
Lecture 6: While Loops and the Math Class
Chapter 4 Mathematical Functions, Characters, and Strings
Chapter 3 Methods.
Arithmetic Operator Operation Example + addition x + y
Chapter 5 – Part 2 Methods Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Chapter 4: Mathematical Functions, Characters, and Strings
The Math class The java.lang.Math class contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square.
Chapter 5 Methods.
Chapter 5 Methods Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Chapter 4 Methods Introducing Methods Declaring Methods
Ch 5 : Mathematical Functions, Characters, and Strings
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. 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. 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. OR

© 2006 Pearson Addison-Wesley. All rights reserved doubleVar = Math.pow(6, 3); doubleVar = Math.sqrt(10); intVar = Math.abs(-3); doubleVar = Math.cos( ); doubleVar = Math.sin( Math.toRadians(30) ); Math «query» int abs(int ) double abs(double) double cos(double) double sin(double) double tan(double) double pow(double, double) double random() long round(double) double sqrt(double) double toDegrees(double) double toRadians(double) double final PI

© 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 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() { } public void leftAction() { } 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) ) Examples Generate a random roll of one die Select a dot diameter from the range of 10 through 100. 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() * ( maxDouble - minDouble ) System.out.println(“I pick “ + (10 + Math.random() * 15) ); Examples Pick a number from 10.0 to 25.0