7/31: Math.random, more methods About DrawLine.java modifications –allow user input –draw a curve Method definitions Math.random()

Slides:



Advertisements
Similar presentations
Modular Programming With Functions
Advertisements

Lab 8 User Defined Function.
1 Programmer-Defined Functions Functions allow program modularization Variables declared in function are local variables Only known inside function in.
Copyright 2011 by Pearson Education Building Java Programs Chapter 3 Lecture 3-2: Return values, Math, and double reading: 3.2,
1 Lecture 4: Chapter 6 - Methods Outline Introduction Program Modules in Java Math -Class Methods Method Declarations Java API Packages Random-Number Generation.
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,
 2003 Prentice Hall, Inc. All rights reserved. 7.1 Introduction Arrays –Data structures which reference one or more value –All items must have same data.
Introduction to Computers and Programming Lecture 12: Math.random() Professor: Evan Korth New York University.
Math class methods & User defined methods Introduction to Computers and Programming in JAVA: V
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
Math class methods & User defined methods Math class methods Math.sqrt(4.0) Math.random() java.lang is the library/package that provides Math class methods.
CS 106 Introduction to Computer Science I 02 / 22 / 2008 Instructor: Michael Eckmann.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Creating Arrays 7.4 Examples Using.
CS 106 Introduction to Computer Science I 10 / 09 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 24 / 2010 Instructor: Michael Eckmann.
Review for Midterm 2 Nested loops & Math class methods & User defined methods.
 2003 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Arrays Introduction to Computers and Programming in.
Computers and Scientific Thinking David Reed, Creighton University Functions and Randomness 1.
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,
JOptionPane class. Dialog Boxes A dialog box is a small graphical window that displays a message to the user or requests input. A variety of dialog boxes.
1 Lecture 3 Part 1 Functions with math and randomness.
10/25: Methods & templates Return to concepts: methods Math class methods 1 st Program of the day About DrawLine.java modifications Method definitions.
Variable Declaration  It is possible to declare multiple variables of the same data type on the same line.  Ex. double hours, rate, total;  Variables.
11/9: Recursion, Method Overloading About Scoping.java Recursion Method overloading.
MATH AND RANDOM CLASSES.  The need for random numbers occurs frequently when writing software.  The Random class, which is part of the java.util class,
* * 0 Chapter 6 Java Methods. * * 0 Method Syntax [access specifier][qualifier] return type method name(argument list) Access specifier public – method.
CSC Programming I Lecture 5 August 30, 2002.
GCOC – A.P. Computer Science A. College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose,
Copyright 2011 by Pearson Education Building Java Programs Chapter 3 Lecture 7: Return values, Math, and double reading: 3.2,
 2005 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
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.
CMSC 1041 Functions II Functions that return a value.
Today’s Lecture Predefined Functions. Introduction to Functions  Reuse Issue  Building Blocks of Programs  Two types of functions  Predefined  Programmer.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
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.
Lecture 5 Methods. Sometimes we want to perform the same sequence of operations multiple times in a program. While loops allow us to do this, they are.
 2003 Prentice Hall, Inc. All rights reserved. Outline 1 fig02_07.cpp (1 of 2) 1 // Fig. 2.7: fig02_07.cpp 2 // Class average program with counter-controlled.
3/25: Scope Rules, More Methods about RollDie.java & modifications Scope rules More methods Program of the day.
 In this chapter you ‘’ll learn: ◦ To construct programs modularly from functions ◦ To use common math library functions ◦ The mechanism for passing.
Basics and arrays Operators:  Arithmetic: +, -, *, /, %  Relational:, >=, ==, !=  Logical: &&, ||, ! Primitive data types Byte(8), short(16), int(32),
Dialog Boxes.
10/25: Methods & templates Return to concepts: methods Math class methods Program of the day.
11/2: Math.random, more methods About DrawLine.java modifications –allow user input –draw a curve Method definitions Math.random()
The Math class Java provides certain math functions for us. The Math class contains methods and constants that can be very useful. The Math class is like.
The Math Class Methods Utilizing the Important Math Operations of Java!
Function prototype A function must be declared before it can be referenced. One way to declare a function is to insert a function prototype before the.
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.
Review for Nested loops & Math class methods & User defined methods.
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Let’s not leave anything to chance. How that process to generate random numbers takes places requires some complicated statistics that are outside the.
Creating and Using Class Methods. Definition Class Object.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 25 - Beyond C & C++: Operators, Methods, and.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "___________________" in Java Purpose –Reuse code –Modularize.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 – Methods Part I.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 - Methods Outline 6.1 Introduction 6.2 Program Modules in Java 6.3 Math -Class Methods 6.4.
Special Methods in Java. Mathematical methods The Math library is extensive, has many methods that you can call to aid you in your programming. Math.pow(double.
Lecture 02 Dr. Eng. Ibrahim El-Nahry Methods. 2 Learning Objectives Class Definition includes both methods and data properties Method Definition and Declaration.
Arrays. What is an array? An array is a collection of data types. For example, what if I wanted to 10 different integers? int num1; int num2; int num3;
AP Java 10/1/2015. public class Rolling { public static void main( String [] args) public static void main( String [] args) { int roll; int roll; for.
L131 Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) math library functions Reading Sections.
Loops Review. How to generate random numbers Math.random() will return a random decimal value in the form of a double. double num1 = Math.random(); num1.
© 2016 Pearson Education, Ltd. All rights reserved.
AP Java 10/4/2016.
The Math class The java.lang.Math class contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square.
METHODS, CLASSES, AND OBJECTS A FIRST LOOK
Arrays.
Unit 7 - Short-Circuit Evaluation - De Morgan’s Laws - The switch statement - Type-casting in Java - Using Math.random()
Applet 2019/4/23.
Random Numbers while loop
Presentation transcript:

7/31: Math.random, more methods About DrawLine.java modifications –allow user input –draw a curve Method definitions Math.random()

Allow User Input (1 of 2) Add in an init method for two input dialog boxes. double coeff;//note that I declared these double yInt; //two variables as global. public void init () { cText = JOptionPane.showInputDialog (“Coefficient, please.”); yText = JOptionPane.showInputDialog (“Y-intercept, please.”); coeff = Double.parseDouble ( cText ); yInt = Double.parseDouble ( yText ); }

Allow User Input (2 of 2) Use coeff & yInt in the drawDot method int drawDot ( int a ) { int b; //must cast to int below b = (int) (coeff * a + yInt) ; return b; }

Draw a Curve Change the formula in the drawDot method from: int drawDot ( int a ) { int b; b = 2 * a + 10 ; return b; } to: int drawDot ( int a ) { int b; b = 2 * ( a * a ) + 10 ; return b; }

Method Definitions in SquareInt.java (simple version below), we see a method called square: public void paint ( Graphics g ) { g.drawString(square(x) + “ “,25,25); } public int square ( int y ) { return y * y ; }

Method Definitions public void paint ( Graphics g ) { g.drawString(square(x) + “ “,25,25); } public int square ( int y ) { return y * y ; } the method square is called in this line. Because it is in parentheses, x is the value to use as the input.

Method Definitions public void paint ( Graphics g ) { g.drawString(square(x) + “ “,25,25); } public int square ( int y ) { return y * y ; } x fits the requirement that it must be an integer.

Method Definitions public void paint ( Graphics g ) { g.drawString(square(x) + “ “,25,25); } public int square ( int y ) { return y * y ; } using x as the input, the method calculates a return value.

Method Definitions public void paint ( Graphics g ) { g.drawString(square(x) + “ “,25,25); } public int square ( int y ) { return y * y ; } the returned value is what replaces the square(x). This value is of type int. x * x

Example: Maximum.java (p. 212) public void init () { (stuff left out…) double max = maximum ( num1, num2, num3 ); outputArea.setText ( “Max is “ + max ); } public double maximum ( double x, double y, double z ) { return Math.max ( x, Math.max ( y, z ) ); }

Important: Coercion of Arguments Java will automatically promote variables to larger types when necessary; EX: int to double. In Math.pow, the requested types of input are double. However, Java will create a double version of an int variable to use in that method automatically. go to Java class index for Math. pow Math. pow read p carefully

Math.random() generates a random double number in the range 0.0 <= x < 1.0 To utilize this function, we use scaling & shifting. scaling: multiplying the function by a number to get the range we want. shifting: adding a number to the function to get the beginning point of the range that we want.

Math.random() EX: Math.random () shift it by 1: Math.random() + 1 makes 1 to <2 scale it by 6: Math.random()* 6 makes 0 to <5 to simulate rolling a die, we want 6 possibilities (or a range of 6), from 1 to 6. They have to be integers. (int) ( Math.random() * );

Program of the day pg. 220 RollDie.java Modify the program to allow the user to input how many times to roll the die.