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

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Modular Programming With Functions
1 Programmer-Defined Functions Functions allow program modularization Variables declared in function are local variables Only known inside function in.
PAC Introduction to Methods Professor: Evan Korth New York University.
1 Lecture 4: Chapter 6 - Methods Outline Introduction Program Modules in Java Math -Class Methods Method Declarations Java API Packages Random-Number Generation.
Introduction to working with Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
 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.
Chapter 5 - Control Structures - Part 2 Outline 5.1Introduction 5.2Essentials of Counter-Controlled Repetition 5.3The for Repetition Structure 5.4Examples.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions.
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
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
Introduction to Computers and Programming Lecture 16: Arrays (cont) Professor: Evan Korth New York University.
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.
 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.
Introduction to Computers and Programming Lecture 11: Introduction to Methods Professor: Evan Korth New York University.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
Chapter 2 - Introduction to Java Applications
Review for Midterm 2 Nested loops & Math class methods & User defined methods.
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
 2003 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Arrays Introduction to Computers and Programming in.
C Lecture Notes Functions (Cont...). C Lecture Notes 5.8Calling Functions: Call by Value and Call by Reference Used when invoking functions Call by value.
Computer Science I Arrays Professor: Evan Korth New York University.
1 Topic 04 Methods Programming II/A CMC2522 / CIM2561 Bavy Li.
1 JavaScript/Jscript 4 Functions. 2 Introduction Programs that solve real-world programs –More complex than programs from previous chapters Best way to.
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.
10/25: Methods & templates Return to concepts: methods Math class methods 1 st Program of the day About DrawLine.java modifications Method definitions.
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
11/9: Recursion, Method Overloading About Scoping.java Recursion Method overloading.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 - Methods.
11/15: Ch. 7: Arrays What is an array? Declaring & allocating arrays Sorting & searching arrays.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
 2005 Pearson Education, Inc. All rights reserved. 1 Methods Called functions or procedures in other languages Modularize programs by separating its tasks.
 2005 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
9/6: Variable Types, Arithmetic Operators, Comparison Operators Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A Simple Program: Printing a.
Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions 5.5Function Definitions 5.6Function Prototypes.
Chapter 6 - Methods Outline 6.1Introduction 6.3 Math Class Methods 6.4Methods 6.5Method Definitions 6.6Java API Packages 6.7Random Number Generation 6.8Example:
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.
9/20: The while Repetition Structure last time’s program repetition structures: what they are the while repetition structure homework due on Thursday program.
 2001 Deitel & Associates, Inc. All rights reserved. 1 Outline 16.1Introduction 16.2Program Modules in JavaScript 16.3Programmer-Defined Functions 16.4Function.
 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.
1/28: Inputs, Variable Types, etc. Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic operators.
Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski.
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.
Java Fundamentals Part Integer Division Division can be tricky. In a Java program, what is the value of 1/2? You might think the answer is.
2/18: Assignment Operators About Average2.java –while loop use –explicit casting –twoDigits object Assignment Operators Increment & Decrement Operators.
7/31: Math.random, more methods About DrawLine.java modifications –allow user input –draw a curve Method definitions Math.random()
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 - Methods Outline 6.1Introduction 6.2Program Modules in Java 6.3 Math Class Methods 6.4Methods.
Review for Nested loops & Math class methods & User defined methods.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 25 - Beyond C & C++: Operators, Methods, and.
 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.
8/2: Recursion About Scoping.java Recursion Program of the Day.
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.
“Form Ever Follows Function” Louis Henri Sullivan
2.5 Another Java Application: Adding Integers
Introduction to Methods
Chapter 5 - Functions Outline 5.1 Introduction
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Chapter 6 - Functions Outline 5.1 Introduction
Constructor Laboratory /12/4.
CHAPTER 6 GENERAL-PURPOSE METHODS
Chapter 3: Introduction to Objects and Input/Output
Cs212: Data Structures Computer Science Department Lecture 2: Arrays.
JOptionPane class.
Chapter 2: Java Fundamentals cont’d
Presentation transcript:

11/2: 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() * );

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

About RollDie.java – pt. 1 //RollDie.java //Roll a six-sided die 6000 times import javax.swing.*; public class RollDie { public static void main ( String args[] ) { int freq1 = 0, freq2 = 0, freq3 = 0, freq4 = 0, freq5 = 0, freq6 = 0 ; int face;

About RollDie.java – pt. 2 for ( int roll = 1; roll <= 6000 ; roll++ ) { face = 1 + (int) ( Math.random() * 6 ); switch ( face ) { case 1: ++freq1; break; case 2: ++freq2; break; case 3: ++freq3; break; case 4: ++freq4; break; case 5: ++freq5; break; case 6: ++freq6; break; }

About RollDie.java – pt. 3 JTextArea outputArea = new JTextArea ( 7, 10 ); outputArea.setText ( "Face\tFrequency" + "\n1 \t" + freq1 + "\n2 \t" + freq2 + "\n3 \t" + freq3 + "\n4 \t" + freq4 + "\n5 \t" + freq5 + "\n6 \t" + freq6 ); JOptionPane.showMessageDialog ( null, outputArea, "Rolling a die 6000 times", JOptionPane.INFORMATION_MESSAGE); System.exit (0); } }

Modifying RollDie.java we need to replace the 6000 in the for loop with a user-defined value, so we add in int rolls; rolls = Integer.parseInt ( JOptionPane.showInputDialog ( “How many rolls?” ) ); and replace the 6000 with rolls. for ( int roll = 1; roll <= rolls ; roll++ ) { face = 1 + (int) ( Math.random() * 6 );

Automatic Variables: Duration Identifiers are used for variable names. Identifiers have a specific duration (or lifetime): –How long the identifier exists in memory (RAM). –Identifiers representing local variables exist ONLY while the program is inside that block. –A block is a set of compound statements in a program that contains declarations.

Automatic Variables: Duration Identifiers have a specific duration (or lifetime): –They are automatically created in memory upon entering the block and automatically destroyed in (deleted from) the computer’s memory when that block is exited. –They are said to have automatic duration. –They are called automatic duration variables, or simply automatic variables.

Instance Variables & Initialization Instance (class-wide) variables are initialized by default: –primitive data types to zero (0). –boolean types to false. –references (names given to objects) to null. Automatic (block-local) variables must be initialized manually. –you must initialize them or get a compiler error.

Scope Rules Identifiers have a specific scope: –where the identifier can be referenced in the program. –Class scope: accessible throughout the body of the class (between the class {}’s) EX: instance variables, methods –Block scope: accessible inside their block. EX: local variables, method parameters

Hidden Instance Variables Instance variables can be “hidden” if a local variable has the same name. While the program is in the local variable’s block, the instance variable is “hidden”. Scoping.java gives a taste of this. (pg. 192)

2 nd Program of the day Scoping.java After getting it to run correctly, analyze why the Container c is there.