Return values.

Slides:



Advertisements
Similar presentations
Escape Sequences \n newline \t tab \b backspace \r carriage return
Advertisements

Arithmetic Calculations
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
Methods Java 5.1 A quick overview of methods
Building Java Programs
1 MATH METHODS THAT RETURN VALUES. 2 JAVA'S MATH CLASS.
Computer Programming w/ Eng. Applications
Pemrograman Dasar - Data Types1 OPERATOR. Pemrograman Dasar - Data Types2 Arithmetic operator  + - * /  / operator denotes integer division if both.
Copyright 2011 by Pearson Education Building Java Programs Chapter 3 Lecture 3-2: Return values, Math, and double reading: 3.2,
1 Parameters. 2 Repetitive figures Consider the task of drawing the following figures: ************* ******* *********************************** **********
1 Fundamental Data types Overview l Primitive Data Types l Variable declaration l Arithmetical Operations l Expressions l Assignment statement l Increment.
Strings, if/else, return, user input
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 3: Parameters, Return, and Interactive Programs with Scanner.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-2: Return; double ; System.out.printf reading: 3.2, 3.5, 4.4 videos: Ch.
CS180 Recitation 3. Lecture: Overflow byte b; b = 127; b += 1; System.out.println("b is" + b); b is -128 byte b; b = 128; //will not compile! b went out.
CS 106 Introduction to Computer Science I 02 / 24 / 2010 Instructor: Michael Eckmann.
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 Building Java Programs Chapter 3 Lecture 3-2: Return; double ; System.out.printf reading: 3.2, 3.5, 4.4 videos: Ch.
Topic 10 return values, Math methods Based on slides bu Marty Stepp and Stuart Reges from " Thinking like a computer.
Microsoft® Small Basic The Math Object Estimated time to complete this lesson: 1 hour.
INTRODUCTION TO PYTHON PART 1 CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
Methods Material from Chapters 5 & 6. Terminology  Method, function, procedure, subroutine  all mean approximately the same thing »functions may return.
CSC1015F – Chapter 3, Computing with Numbers Michelle Kuttel
Computer Science 111 Fundamentals of Programming I Basic Program Elements.
CSC Programming I Lecture 5 August 30, 2002.
Week 2 - Friday.  What did we talk about last time?  Using Scanner to get input  Basic math operations.
Copyright 2011 by Pearson Education Building Java Programs Chapter 3 Lecture 7: Return values, Math, and double reading: 3.2,
1 Math Expressions and Operators. 2 Some C++ Operators Precedence OperatorDescription Higher ( )Function call +Positive - Negative *Multiplication / Division.
Building Java Programs Chapter 3 Parameters and Objects Copyright (c) Pearson All rights reserved.
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.
29 January 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
CS 112 Introduction to Programming Animations; Methods with return Yang (Richard) Yang Computer Science Department Yale University 208A Watson, Phone:
If/else, return, user input, strings
CS305j Introduction to Computing Parameters and Methods 1 Topic 8 Parameters and Methods "We're flooding people with information. We need to feed it through.
Copyright 2011 by Pearson Education Building Java Programs Chapter 3 Lecture 3-2: Return values, Math, and double reading: 3.2,
Chapter INTRODUCTION Data Types and Arithmetic Calculations.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 3: Parameters, Return, and Interactive Programs with Scanner.
CSE 110: Programming Language I Afroza Sultana UB 1230.
Lecture 8: Return values and math
Week 3 - Wednesday CS 121.
Lecture 3: Method Parameters
Lecture 6: While Loops and the Math Class
Lecture 10: return values and math
CSCI 161 – Introduction to Programming I William Killian
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Math Methods that return values
Building Java Programs
Building Java Programs
Building Java Programs
Introduction to Programming
Topic 10 return values, Math methods
Building Java Programs
Lecture 7: Graphics, return values and math
Java's Math class Method name Description Math.abs(value)
Building Java Programs
4TC00 Model-based systems engineering 2.2 Types and values
Topic 10 return values, Math methods
Lecture 6: While Loops and the Math Class
Lecture 3: Method Parameters
Lecture 9:The While Loop + Math methods AP Computer Science Principles
Building Java Programs
Building Java Programs
SSEA Computer Science: Track A
CSE 142 Lecture Notes Global Constants, Parameters, Return Values
Building Java Programs
4.3 Arithmetic Operators and Mathematical Functions
Lecture 11: return values and math
Presentation transcript:

Return values

Java's Math class Method name Description Math.abs(value) absolute value Math.ceil(value) rounds up Math.floor(value) rounds down Math.log10(value) logarithm, base 10 Math.max(value1, value2) larger of two values Math.min(value1, value2) smaller of two values Math.pow(base, exp) base to the exp power Math.random() random double between 0 and 1 Math.round(value) nearest whole number Math.sqrt(value) square root Math.sin(value) Math.cos(value) Math.tan(value) sine/cosine/tangent of an angle in radians Math.toDegrees(value) Math.toRadians(value) convert degrees to radians and back Constant Description Math.E 2.7182818... Math.PI 3.1415926...

Calling Math methods Examples: Math.methodName(parameters) Examples: double squareRoot = Math.sqrt(121.0); System.out.println(squareRoot); int absoluteValue = Math.abs(-50); System.out.println(absoluteValue); System.out.println(Math.min(3, 7) + 2); The Math methods do not print to the console. Each method produces ("returns") a numeric result.

Return return: To send out a value as the result of a method. The opposite of a parameter: Parameters send information in from the caller to the method. Return values send information out from a method to its caller. main Math.abs(-42) -42 Math.round(2.71) 2.71 42 3

Math questions Evaluate the following expressions: Math.abs(-1.23) Math.pow(3, 2) Math.pow(10, -2) Math.sqrt(121.0) - Math.sqrt(256.0) Math.round(Math.PI) + Math.round(Math.E) Math.ceil(6.022) + Math.floor(15.9994) Math.abs(Math.min(-3, -5)) Math.max and Math.min can be used to bound numbers. Consider an int variable named age. What statement would replace negative ages with 0? What statement would cap the maximum age to 40?

Quirks of real numbers Some Math methods return double or other non-int types. int x = Math.pow(10, 3); // ERROR: incompat. types Some double values print poorly (too many digits). double result = 1.0 / 3.0; System.out.println(result); // 0.3333333333333 The computer represents doubles in an imprecise way. System.out.println(0.1 + 0.2); Instead of 0.3, the output is 0.30000000000000004

Type casting type cast: A conversion from one type to another. Syntax: To promote an int into a double to get exact division from / To truncate a double from a real number to an integer Syntax: (type) expression Examples: double result = (double) 19 / 5; int result2 = (int) result; int x = (int) Math.pow(10, 3); 3.8,3,1000

More about type casting Type casting has high precedence and only casts the item immediately next to it. double x = (double) 1 + 1 / 2; // ? double y = 1 + (double) 1 / 2; // ? You can use parentheses to force evaluation order. double average = (double) (a + b + c) / 3; A conversion to double can be achieved in other ways. double average = 1.0 * (a + b + c) / 3;

Returning a value Example: public static type name(parameters) { statements; ... return expression; } Example: // Returns the slope of the line between the given points. public static double findSlope(int x1, int y1, int x2, int y2) { // What do we do here? return dy / dx; findSlope(1, 3, 5, 11) returns 2.0

Return examples // Converts degrees Fahrenheit to Celsius. public static double convertFToC(double degreesF) { double degreesC = 5.0 / 9.0 * (degreesF - 32); return degreesC; } // Computes triangle hypotenuse length given its side lengths. public static double computeHypotenuse(int a, int b) { double c = Math.sqrt(a * a + b * b); return c; You can shorten the examples by returning an expression: return 5.0 / 9.0 * (degreesF - 32);

Common error: Not storing A return statement does NOT send a variable's name back to the calling method. public static void main(String[] args) { findSlope(0, 0, 6, 3); System.out.println("The slope is " + result); // ERROR: } // result not defined public static double findSlope(int x1, int x2, int y1, int y2) { double dy = y2 - y1; double dx = x2 - x1; double result = dy / dx; return result; }

Fixing the common error Instead, returning sends the variable's value back. The returned value must be stored into a variable or used in an expression to be useful to the caller. public static void main(String[] args) { double s = findSlope(0, 0, 6, 3); System.out.println("The slope is " + s); } public static double findSlope(int x1, int x2, int y1, int y2) { double dy = y2 - y1; double dx = x2 - x1; double result = dy / dx; return result;

Exercises Write method “countQuarters” that takes an integer number of cents and returns the number of quarters represented by that many cents. Don’t count dollars, as those will be dispensed as dollar bills. countQuarters (83) returns 3 countQuarters (1256) returns 2 Write method “distance” that takes four integer coordinates (x1, y1, x2, y2) and computes the distance between them. distance (1, 2, 4, 6) returns 5.0