Review for Midterm 2 Nested loops & Math class methods & User defined methods.

Slides:



Advertisements
Similar presentations
Procedural programming in Java
Advertisements

A Java API Package java.security  The Java Security Package contains classes and interfaces that are required by many Java programs.  This package is.
PAC Introduction to Methods Professor: Evan Korth New York University.
Introduction to Computers and Programming Lecture 11: 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.
Introduction to Computers and Programming Introduction to Methods in Java.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Chapter 4 - Control Structures: Part 1 Outline 4.4Control Structures 4.5The if Selection Structure 4.6The if/else Selection Structure 4.7The while Repetition.
 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.
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
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.
Introduction to Computers and Programming Lecture 16: Arrays (cont) Professor: Evan Korth New York University.
Mathematical Operators: working with floating point numbers and more operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
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.
 2007 Pearson Education, Inc. All rights reserved C Functions.
 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.
1 Introduction to Computers and Programming Quick Review What is a Function? A module of code that performs a specific job.
Introduction to Computers and Programming Lecture 11: Introduction to Methods Professor: Evan Korth New York University.
 2007 Pearson Education, Inc. All rights reserved C Functions.
Introduction to Computers and Programming Lecture 13: User defined methods Instructor: Evan Korth New York University.
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. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
 2003 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Arrays Introduction to Computers and Programming in.
Computer Science I Arrays Professor: Evan Korth New York University.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
 2003 Prentice Hall, Inc. All rights reserved Introduction Modules –Small pieces of a problem e.g., divide and conquer –Facilitate design, implementation,
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - JavaScript: Functions Outline 10.1 Introduction 10.2 Program Modules in JavaScript 10.3.
1 Lecture 3 Part 1 Functions with math and randomness.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "methods" in Java Purpose Reuse code Modularize the program This.
10/25: Methods & templates Return to concepts: methods Math class methods 1 st Program of the day About DrawLine.java modifications Method definitions.
© 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.
1 Introduction Modules  Most computer programs solve much larger problem than the examples in last sessions.  The problem is more manageable and easy.
 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.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions 5.5Function Definitions 5.6Function Prototypes.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Control Structures: Part 1.
Procedural programming in Java Methods, parameters and return values.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
CSCI/CMPE 4341 Topic: Programming in Python Review: Exam I Xiang Lian The University of Texas – Pan American Edinburg, TX 78539
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 - Functions.
 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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline Introduction Arrays Declaring Arrays Examples Using Arrays.
1/28: Inputs, Variable Types, etc. Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic operators.
3/25: Scope Rules, More Methods about RollDie.java & modifications Scope rules More methods Program of the day.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions Outline 5.1Introduction 5.2Program Modules.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
11/2: 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. 5.2Program Modules in C Functions –Modules in C –Programs combine user-defined functions with library functions.
Review for Nested loops & Math class methods & User defined methods.
 2003 Prentice Hall, Inc. All rights reserved. 1 Will not cover 4.14, Thinking About Objects: Identifying Class Attributes Chapter 4 - Control Structures.
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.
Introduction Modules Small pieces of a problem ▴ e.g., divide and conquer Facilitate design, implementation, operation and maintenance of large programs.
Methods Chapter 6.
Object Oriented Systems Lecture 03 Method
Introduction to Methods
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 6 - Functions Outline 5.1 Introduction
6 Functions.
Presentation transcript:

Review for Midterm 2 Nested loops & Math class methods & User defined methods

 2000 Prentice Hall, Inc. All rights reserved. 2 Format of the exam You will have the entire class period to complete the exam 5short answer (total of 15 points) 1what does this do(20 points) 1find the errors(15 points) 2write a program(total of 50 points)

Math class methods Math.sqrt(4.0) Math.random() java.lang is the library/package that provides Math class methods such as –Math.random() to generate random numbers Java.lang is accessed by all java programs by default. –YOU do not have to include it at the beginning of the program as you do with javax package.

 2000 Prentice Hall, Inc. All rights reserved. 4 Random-Number Generation Often we want our programs to generate random numbers. –games of chance –testing without user interaction Java random-number generators –Math.random() Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. –What if we want to generate random integers?

 2000 Prentice Hall, Inc. All rights reserved. 5 Random-Number Generation –Math.random() Produces double from 0.0 to 1.0 (excluding 1) ( int ) ( Math.random() * 6 ) Produces integers from 0 – 5 –1 + ( int ) ( Math.random() * 6 ) Produces integers from 1 – 6 Scaling Shifting

 2000 Prentice Hall, Inc. All rights reserved. Outline 6 1 // Fig. 6.7: RandomIntegers.java 2 // Shifted, scaled random integers. 3 import javax.swing.JOptionPane; 4 5 public class RandomIntegers { 6 7 public static void main( String args[] ) 8 { 9 int value; 10 String output = ""; // loop 20 times 13 for ( int counter = 1; counter <= 20; counter++ ) 14 { 15 // pick random integer between 1 and 6 16 value = 1 + ( int ) ( Math.random() * 6 ); output += value + " "; // append value to output // if counter divisible by 5, append newline to String output 21 if ( counter % 5 == 0 ) 22 output += "\n"; 23 } // end for Produce integers in range 1-6 Math.random returns double s. We cast the double as an int

 2000 Prentice Hall, Inc. All rights reserved. Outline 7 RandomIntegers.java 26 JOptionPane.showMessageDialog( null, output, 27 "20 Random Numbers from 1 to 6", 28 JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); // terminate application } // end main } // end class RandomIntegers

 2000 Prentice Hall, Inc. All rights reserved. Outline 8 1 // Fig. 6.8: RollDie.java 2 // Roll a six-sided die 6000 times. 3 import javax.swing.*; 4 5 public class RollDie { 6 7 public static void main( String args[] ) 8 { 9 int frequency1 = 0, frequency2 = 0, frequency3 = 0, 10 frequency4 = 0, frequency5 = 0, frequency6 = 0, face; 11 String output; 12 // summarize results 13 for ( int roll = 1; roll <= 6000; roll++ ) { 14 face = 1 + ( int ) ( Math.random() * 6 ); // determine roll value and increment appropriate counter 17 switch ( face ) { case 1: 20 ++frequency1; 21 break; case 2: 24 ++frequency2; 25 break; case 3: 28 ++frequency3; 29 break; 30 Produce integers in range 1-6 Increment appropriate frequency counter, depending on randomly generated number

 2000 Prentice Hall, Inc. All rights reserved. Outline 9 31 case 4: 32 ++frequency4; 33 break; case 5: 36 ++frequency5; 37 break; case 6: 40 ++frequency6; 41 break; } // end switch } // end for Output = "Face\tFrequency" + "\n1\t" + frequency "\n2\t" + frequency2 + "\n3\t" + frequency "\n4\t" + frequency4 + "\n5\t" + frequency "\n6 \t" + frequency6 ; JOptionPane.showMessageDialog( null, output, 55 "Rolling a Die 6000 Times", JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); // terminate application } // end main } // end class RollDie

 2000 Prentice Hall, Inc. All rights reserved. 10

Benefits of Methods Experience has shown that the best way to develop and maintain large programs is to build it from smaller components or modules. In Java, one such module is the method. Each module is generally simpler and more manageable than the entire program. This concept is known as Divide and Conquer, also with Abstraction in the mix.

 2000 Prentice Hall, Inc. All rights reserved. 12 User defined Method returning a value General format of method declaration: modifiers return-value-type method-name ( parameter1, …, parameterN ) { declarations and statements } Method can return one value: return expression ; Or, it can returns nothing using keyword void in the header. Method header Method body Scope of local variables

Return Value Types You can only return one value from a method. Returning void –void : means nothing –A method that returns void therefore returns nothing. –Hence, there is no need for the optional return statement. But using one can force early exit from the method. –Example: public static void printIntro (int n);

Parameter Data Types Unlike return values, you can pass as many parameters as you like. To pass more than one parameter, you need to separate the parameters with commas. public static int maximum (int x, int y) { /*body*/ }

Warning Unlike declaring variables, you must specifically state the type for multiple variables –For example takeInTwoFloats( float x, y ) is incorrect –Instead you must write takeInTwoFloats(float x, float y)

No parameters You can also have a method that accepts no parameters. In such case, you would just have an empty parameter list. E.g. public static int rollDie () public static void printIntro ()

 2000 Prentice Hall, Inc. All rights reserved Argument Promotion Coercion of arguments –Forcing arguments to appropriate type to pass to method e.g., System.out.println( Math.sqrt( 4 ) ); –Evaluates Math.sqrt( 4 ) –Then evaluates System.out.println() Promotion rules –Specify how to convert types without data loss

 2000 Prentice Hall, Inc. All rights reserved. 18

review What is a method? What information can you learn about a method from its header? What does it mean to invoke a method? What is call by value? What is scope of local variables within a method? Why don’t we have to import the Math class? What is abstraction in computer science?