AP Computer Science A. Return values

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

Building Java Programs

Building Java Programs

Building Java Programs

AP Computer Science. Return values, Math, and double. Copyright 2010 by Pearson Education

Building Java Programs

Building Java Programs

Lecture 6: While Loops and the Math Class

Building Java Programs

CS 112 Introduction to Programming

CS 112 Introduction to Programming

Building Java Programs Chapter 3

CSc 110, Spring Lecture 11: return values and math

Redundant recipes. Building Java Programs Chapter 3. Parameterized recipe. Redundant figures

CSc 110, Autumn Lecture 10: return values and math

Topic 12 more if/else, cumulative algorithms, printf

AP CS Java Syntax Summary: (version 3)

Building Java Programs

12. Numbers. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

CS110: PROGRAMMING LANGUAGE I

CSc 110, Spring Lecture 7: Graphics, return values and math

CSE 373. Data Types and Manipulation; Arrays. slides created by Marty Stepp

To define methods, invoke methods, and pass arguments to a method ( ). To develop reusable code that is modular, easy-toread, easy-to-debug,

The Math Class. Using various math class methods. Formatting the values.

AP CS Java Syntax Summary: (version 4)

Chapter 5 Methods. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

CIS133J. Working with Numbers in Java

Introduction to Computer Science Unit 2. Notes

Parameters. Repetitive figures. A solution? Parameterization. Declaring parameterized methods. Generalizing methods. Readings: 3.1

JAVASCRIPT BASICS. JavaScript Math Functions. The Math functions helps you to perform mathematical tasks

Chapter 5 Methods / Functions

AP Computer Science. if/else, return values. Copyright 2010 by Pearson Education

Introduction to Computer Science Unit 2. Notes

Introduction to Computer Science and Object-Oriented Programming

AP CS Unit 3: Control Structures Notes

A Balanced Introduction to Computer Science, 3/E David Reed, Creighton University 2011 Pearson Prentice Hall ISBN

A Balanced Introduction to Computer Science, 3/E

Chapter 6 Methods. Dr. Hikmat Jaber

Key Concept: all programs can be broken down to a combination of one of the six instructions Assignment Statements can create variables to represent

Chapter 5 Methods. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Mathematical Functions, Characters, and Strings. CSE 114, Computer Science 1 Stony Brook University

Chapter 5 Methods. Lecture notes for computer programming 1 Faculty of Engineering and Information Technology Prepared by: Iyad Albayouk

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Chapter 5 Methods. Modifier returnvaluetype methodname(list of parameters) { // method body; }

Calculations, Formatting and Conversions

Computer Programming I - Unit 2 Lecture 1 of 13

Mathematical Functions, Characters, and Strings. CSE 114, Computer Science 1 Stony Brook University

Returns & if/else. Parameters and Objects

AP CS Java Syntax Summary: (version 5)

Chapter 4 Mathematical Functions, Characters, and Strings

Variables, Types, Operations on Numbers

Advanced Object Concepts

Welcome to the Primitives and Expressions Lab!

What did we talk about last time? Examples switch statements

Lesson 5: Introduction to the Java Basics: Java Arithmetic THEORY. Arithmetic Operators

Variable Manipulator Driver. Installation and Usage Guide. Revision: 1.0 Date: Monday, July 10, 2017 Authors: Alan Chow

4. Java Project Design, Input Methods

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

Lecture 9: Arrays. Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp. Copyright (c) Pearson All rights reserved.

Important Java terminology

DATA TYPES AND EXPRESSIONS


Coding in JavaScript functions

CT 229 Java Syntax Continued

Module 4: Characters, Strings, and Mathematical Functions

Functions, Randomness and Libraries

Methods CSC 121 Fall 2014 Howard Rosenthal

Activity 4: Methods. Content Learning Objectives. Process Skill Goals

More types, Methods, Conditionals. ARCS Lab.

static int min(int a, int b) Returns the smaller of two int values. static double pow(double a,

M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014

Chapter 5 Methods. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Java Tutorial. Saarland University. Ashkan Taslimi. Tutorial 3 September 6, 2011

Expressions. Eric Roberts Handout #3 CSCI 121 January 30, 2019 Expressions. Grace Murray Hopper. Arithmetic Expressions.

Data Structure and Programming Languages

Java Classes: Math, Integer A C S L E C T U R E 8

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

First Programs. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

First Programs. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

Building Java Programs

AP Programming - Chapter 3 Lecture. An Introduction

M e t h o d s a n d P a r a m e t e r s

Visual Programming. Lecture 2: More types, Methods, Conditionals

1.1 Your First Program

CISC 110 Week 3. Expressions, Statements, Programming Style, and Test Review

CISC-124. This week we continued to look at some aspects of Java and how they relate to building reliable software.

Object Oriented Methods : Deeper Look Lecture Three

6.092 Introduction to Software Engineering in Java January (IAP) 2009

Unit Overview. Concepts & Understandings. Learning Targets

AP Computer Science Unit 1. Programs

The Math Class (Outsource: Math Class Supplement) Random Numbers. Lab 06 Math Class

Downloaded from Chapter 2. Functions

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

Built-in data types. logical AND logical OR logical NOT &&! public static void main(string [] args)

! Widely available. ! Widely used. ! Variety of automatic checks for mistakes in programs. ! Embraces full set of modern abstractions. Caveat.

Problem 7.4: Racetrack

Data Structure and Programming Languages

Language Fundamental of VB.NET Part 1. Heng Sovannarith

First Programs. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

Expressions in JavaScript. Jerry Cain CS 106AJ October 2, 2017

Transcription:

AP Computer Science A Return values

Distance between points Write a method that given x and y coordinates for two points prints the distance between them Pseudocode?

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

Calling Math methods Math.methodName(parameters) Examples: double squareroot = Math.sqrt(121.0); System.out.println(squareRoot); // 11.0 int absolutevalue = Math.abs(-50); System.out.println(absoluteValue); // 50 System.out.println(Math.min(3, 7) + 2); // 5 The Math methods do not print to the console. Each method produces ("returns") a numeric result. The results are used as expressions (printed, stored, etc.). 4

Java libraries Java comes with lots of goodies Math Graphics Color Learn more from the Application Programming Interface Linked from the desktop http://java.sun.com/javase/6/docs/api 5

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. -42 Math.abs(-42) main 42 2.71 3 Math.round(2.71) 6

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

Returning a value public static type name(parameters) { statements;... return expression; Example: // Returns the slope of the line between the given points. public static double slope(int x1, int y1, int x2, int y2) { double dy = y2 - y1; double dx = x2 - x1; return dy / dx; 8

Methods that return Given x and y values for two points, return the distance between them Given two sides of a triangle, return the length of the hypotenuse Convert Fahrenheit to Celsius (5/9* F - 32)

Common error: Not storing Many students incorrectly think that a return statement sends a variable's name back to the calling method. public static void main(string[] args) { slope(0, 0, 6, 3); System.out.println("The slope is " + result); // ERROR: // result not defined public static double slope(int x1, int x2, int y1, int y2) { double dy = y2 - y1; double dx = x2 - x1; double result = dy / dx; return result; 10

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 = slope(0, 0, 6, 3); System.out.println("The slope is " + s); public static double slope(int x1, int x2, int y1, int y2) { double dy = y2 - y1; double dx = x2 - x1; double result = dy / dx; return result; 11

Common error: Returning early Statements after a return statement can t be reached public static void main(string[] args) { double s = slope(0, 0, 6, 3); System.out.println("The slope is " + s); public static double slope(int x1, int x2, int y1, int y2) { double dy = y2 - y1; double dx = x2 - x1; double result = dy / dx; return result; System.out.println(result); // ERROR: unreachable 12

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 13

Type casting type cast: A conversion from one type to another. 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 14

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 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; 15