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

Similar documents
AP Computer Science A. Return values

Building Java Programs

Building Java Programs

Building Java Programs

Building Java Programs

Building Java Programs

Building Java Programs

Lecture 6: While Loops and the Math Class

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

CS 112 Introduction to Programming

CS 112 Introduction to Programming

CSc 110, Spring Lecture 11: return values and math

Building Java Programs Chapter 3

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

CSc 110, Autumn Lecture 10: return values and math

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

Topic 12 more if/else, cumulative algorithms, printf

Building Java Programs

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

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

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

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

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

Returns & if/else. Parameters and Objects

AP CS Java Syntax Summary: (version 3)

CS110: PROGRAMMING LANGUAGE I

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

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

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

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.

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

AP CS Java Syntax Summary: (version 4)

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

CIS133J. Working with Numbers in Java

Introduction to Computer Science Unit 2. Notes

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

Chapter 5 Methods / Functions

Computer Programming I - Unit 2 Lecture 1 of 13

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

AP CS Unit 3: Control Structures Notes

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

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

Introduction to Computer Science Unit 2. Notes

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

Welcome to the Primitives and Expressions Lab!

A Balanced Introduction to Computer Science, 3/E

Introduction to Computer Science and Object-Oriented Programming

Chapter 6 Methods. Dr. Hikmat Jaber

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

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

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

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

More types, Methods, Conditionals. ARCS Lab.

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

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

Variables, Types, Operations on Numbers

Calculations, Formatting and Conversions

Advanced Object Concepts

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

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

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

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.

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

AP CS Java Syntax Summary: (version 5)

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

Important Java terminology

CT 229 Java Syntax Continued

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

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

Building Java Programs Chapter 2

Data Structure and Programming Languages

Chapter 4 Mathematical Functions, Characters, and Strings

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

Research Group. 2: More types, Methods, Conditionals

What did we talk about last time? Examples switch statements

Data Structure and Programming Languages


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

Building Java Programs Chapter 2. bug. Primitive Data and Definite Loops. Copyright (c) Pearson All rights reserved. Software Flaw.

Building Java Programs Chapter 2

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

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

DATA TYPES AND EXPRESSIONS

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

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

Functions, Randomness and Libraries

4. Java Project Design, Input Methods

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

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

CS111: PROGRAMMING LANGUAGE II

COMP-202: Foundations of Programming. Lecture 7: Strings and Methods Jackie Cheung, Winter 2015

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

Object Oriented Methods : Deeper Look Lecture Three

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

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

Web Technology for Industrial Engineering 5 September 2002

Getting started with Java

We have written lots of code so far It has all been inside of the main() method What about a big program? The main() method is going to get really

Transcription:

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

COSC 236 Web Site You will always find the course material at: http://www.class-notes.us From this site you can click on the COSC-236 tab to download the PowerPoint lectures, the Quiz solutions and the Laboratory assignments. 2

Lecture 05 2/10/2014 3

Review of Quiz 7 4

Review of Quiz 7 5

Review of Quiz 7 6

Review of Quiz 7 (Solution for drawline();) public static void drawline() { // Prints top and bottom line System.out.print("#"); for (int i = 1; i <= 4*SIZE; i++) { System.out.print("="); System.out.println("#"); 7

Review of Quiz 7 (Solution for bottomhalf();) public static void bottomhalf() { // Prints the contracting pattern of <> for the top half of the figure for (int line = SIZE; line >= 1; line--) { System.out.print(" "); for (int space = 1; space <= (-2 * line + 2*SIZE); space++) { System.out.print(" "); System.out.print("<>"); for (int dot = 1; dot <= (4 * line - 4); dot++) { System.out.print("."); System.out.print("<>"); for (int space = 1; space <= (-2 * line + 2*SIZE); space++) { System.out.print(" "); System.out.println(" "); 8

3.2 Methods that Return Values (pp. 151-159) Return values (pp. 151-152) So far our methods have looked like this: public static void square(int x){ System.out.println(x*x); The void means that no values are returned form the called method back to the calling method. To return the value of x-squared to the main program, we modify the method as follows: public static int square(int x) { return x*x; 9

3.2 Methods that Return Values (pp. 151-159) Return values (pp. 151-152) The complete program: public class TestReturn { public static void main (String[] args) { System.out.println("The square of 3 is " + square(3)); public static int square(int x) { return x*x; 10

Java's Math class (pp. 152-155) Method name Math.abs(value) Math.ceil(value) Math.floor(value) Description absolute value rounds up rounds down Constant Description Math.E 2.7182818... Math.PI 3.1415926... 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) Math.sqrt(value) Math.sin(value) Math.cos(value) Math.tan(value) Math.toDegrees(value) Math.toRadians(value) nearest whole number square root sine/cosine/tangent of an angle in radians convert degrees to radians and back 11

3.2 Methods that Return Values (pp. 151-159) Using Java s math class (pp. 152-155) 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.). 12

Return (pp. 155-157) 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. A call to the method can be used as part of an expression. -42 Math.abs(-42) main 42 2.71 3 Math.round(2.71) 13

3.2 Methods that Return Values (pp. 151-159) Math questions (pp. 155-157) 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)) 1.23 9 0.01 11.0 16.0 = -5.0 3.0 + 3.0 = 6.0 7.0 + 15.0 = 22.0 Math.abs(-5) = 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? Math.max(0, age); Math.min (40, age); 14

3.2 Methods that Return Values (pp. 151-159) 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 15

3.2 Methods that Return Values (pp. 151-159) 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 16

3.2 Methods that Return Values (pp. 151-159) 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; 17

3.2 Methods that Return Values (pp. 151-159) 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; slope(1, 3, 5, 11) returns 2.0 18

3.2 Methods that Return Values (pp. 151-159) Return examples // Converts degrees Fahrenheit to Celsius. public static double ftoc(double degreesf) { double degreesc = 5.0 / 9.0 * (degreesf - 32); return degreesc; // Computes triangle hypotenuse length given its side lengths. public static double hypotenuse(int a, int b) { double c = Math.sqrt(a * a + b * b); return c; You can shorten the examples by returning an expression: public static double ftoc(double degreesf) { return 5.0 / 9.0 * (degreesf - 32); 19

3.2 Methods that Return Values (pp. 151-159) Common error: Not storing (pp. 157-160) 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; 20

3.2 Methods that Return Values (pp. 151-159) 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; 21

Assignments for this week 1. Laboratory for Chapter 2 due TODAY (Monday 2/17) NOTE: Due date on handout is incorrect IMPORTANT: When you email me your laboratory Word Document, be sure it is all in one file 2. Read Section 3.2 for Monday (pp. 151-159) 3. Be sure to complete Quiz 7 before leaving class tonight This is another program to write You must demonstrate the program to me before you leave lab 22