Building Java Programs

Slides:



Advertisements
Similar presentations
Building Java Programs
Advertisements

1 MATH METHODS THAT RETURN VALUES. 2 JAVA'S MATH CLASS.
Return values.
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.
©2004 Brooks/Cole Chapter 3 Assignment. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Assignment In the last chapter we learned how to declare.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
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.
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
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.
What is a variable?  A variable holds data in memory so the program may use that data, or store results.  Variables have a data type. int, boolean, char,
1 Intro to Computer Science I Chapter 2 Fundamental Data Types Java scripting with BeanShell.
Topic 12 more if/else, cumulative algorithms, printf Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
CSE1222: Lecture 4The Ohio State University1. Mathematical Functions (1)  The math library file cmath Yes, this is a file with definitions for common.
CSC Programming I Lecture 5 August 30, 2002.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 4 part 3 GEORGE KOUTSOGIANNAKIS.
Java Program Components Java Keywords - Java has special keywords that have meaning in Java. - You have already seen a fair amount of keywords. Examples.
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,
Building Java Programs Chapter 3 Parameters and Objects Copyright (c) Pearson All rights reserved.
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.
Lesson 6 Selection Structures. Example program //This will convert a numeric grade into a letter grade import TerminalIO.KeyboardReader; public class.
The Math Class Methods Utilizing the Important Math Operations of Java!
Numeric Data Types There are six numeric data types: byte, short, int, long, float, and double. Sample variable declarations: int i, j, k; float numberOne,
Math Class Mrs. C. Furman September 2, Math frequently used methods NameUse floor()rounds down ceil()rounds up pow(x,y)returns x to the power of.
Creating and Using Class Methods. Definition Class Object.
CSE 190 M Flash Sessions Session 2 Alex Miller, Spring 2011.
CS 112 Introduction to Programming Animations; Methods with return Yang (Richard) Yang Computer Science Department Yale University 208A Watson, Phone:
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,
VERTICAL ONE DIMENSIONAL MOTION.  Relate the motion of a freely falling body to motion with constant acceleration.  Calculate displacement, velocity,
Unit 3.
Building Java Programs Chapter 3
Lecture 8: Return values and math
Building Java Programs Chapter 3
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
BIL 104E Introduction to Scientific and Engineering Computing
Building Java Programs
Methods Chapter 6.
Chapter 4 Mathematical Functions, Characters, and Strings
Building Java Programs
Math Methods that return values
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs Chapter 3
Topic 10 return values, Math methods
Lecture 7: Graphics, return values and math
Java's Math class Method name Description Math.abs(value)
Topic 12 more if/else, cumulative algorithms, printf
Building Java Programs
Topic 10 return values, Math methods
Lecture 9: Arrays Building Java Programs: A Back to Basics Approach
Lecture 6: While Loops and the Math Class
Lecture 9:The While Loop + Math methods AP Computer Science Principles
Building Java Programs
CSE 142 Lecture Notes Global Constants, Parameters, Return Values
Building Java Programs
Chapter 2: Java Fundamentals cont’d
Bouncing Ball Physics – Velocity / Acceleration / Displacement
4.3 Arithmetic Operators and Mathematical Functions
Lecture 11: return values and math
Presentation transcript:

Building Java Programs Chapter 3 Lecture 7: Return values, Math, and double reading: 3.2, 2.1 - 2.2

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...

Return return: To send out a value as the result of a method. Return values send information out from a method to its caller. A call to the method can be used as part of an expression. (Compare to parameters which send values into a method) main Math.abs(-42) -42 Math.round(2.71) 2.71 42 3

Returning a value When Java reaches a return statement: public static type name(parameters) { statements; ... return expression; } When Java reaches a return statement: it evaluates the expression it substitutes the return value in place of the call it goes back to the caller and continues after the method call

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; // 3.8 int result2 = (int) result; // 3 int x = (int) Math.pow(10, 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; // 1.0 double y = 1 + (double) 1 / 2; // 1.5 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;

Exercise In physics, the displacement of a moving body represents its change in position over time while accelerating. Given initial velocity v0 in m/s, acceleration a in m/s2, and elapsed time t in s, the displacement of the body is: Displacement = v0 t + ½ a t 2 Write a method displacement that accepts v0, a, and t and computes and returns the change in position. example: displacement(3.0, 4.0, 5.0) returns 65.0

Exercise If you drop two balls, which will hit the ground first? Ball 1: height of 600m, initial velocity = 25 m/sec downward Ball 2: height of 500m, initial velocity = 15 m/sec downward Write a program that determines how long each ball takes to hit the ground (and draws each ball falling). Total time is based on the force of gravity on each ball. Acceleration due to gravity ≅ 9.81 m/s2, downward Displacement = v0 t + ½ a t 2