3/25: Scope Rules, More Methods about RollDie.java & modifications Scope rules More methods Program of the day.

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

1 Storage Duration and Scope –Local and global variables Storage classes –automatic, static, external, register Todays Material.
8/7: Ch. 7: Arrays What is an array? Declaring & allocating arrays Program of the day.
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 7 - Arrays Outline 7.1Introduction 7.2Arrays 7.3Declaring and Allocating Arrays 7.4Examples Using Arrays 7.5References and Reference Parameters.
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 25 - Beyond C & C++: Operators, Methods, and Arrays in Java Outline 25.1Introduction 25.2Primitive.
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.
JTextArea formatting text areas. Using JTextArea JTextArea class is found in the javax.swing package. Creates an text area that can process the escape.
 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.
© 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.
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.
Introduction to Computers and Programming Lecture 3: Variables and Input Professor: Evan Korth New York University.
 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.
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
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.
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 1 CMT1000: Introduction to Programming Ed Currie Lecture 9: Arrays.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 5 - Control Structures - Part 2 Outline 5.1Introduction 5.2Essentials of Counter-Controlled Repetition.
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.
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.
9/6: Variable Types, Arithmetic Operators, Comparison Operators Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic.
 2007 Pearson Education, Inc. All rights reserved C Functions -Continue…-
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
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:
Logic Our programs will have to make decisions in terms of what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if.
9/20: The while Repetition Structure last time’s program repetition structures: what they are the while repetition structure homework due on Thursday program.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 - Arrays Outline 7.1Introduction 7.2Arrays 7.3Declaring and Allocating Arrays 7.4Examples Using.
1/28: Inputs, Variable Types, etc. Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic operators.
Chapter 4 Control Structures: Part I 1 3 “ There is No goto in Java ” Structured programming: the building blocks There are 3 different kinds.
 2007 Pearson Education, Inc. All rights reserved Random Number Generation  rand function – Load – Returns "random" number between
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Dale Roberts CSCI 230 Functions Scope, Parameter Passing, Storage Specifiers Department of Computer and Information Science, School of Science, IUPUI Dale.
EPSII 59:006 Spring Call-by-value example #include void increment(int); //prototype for increment function int main(void) { int a=1; printf("Value.
KIC/Computer Programming & Problem Solving 1.  Header Files  Storage Classes  Scope Rules  Recursion Outline KIC/Computer Programming & Problem Solving.
11/2: Math.random, more methods About DrawLine.java modifications –allow user input –draw a curve Method definitions Math.random()
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1 Introduction 5.2 Essentials of Counter-Controlled.
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Will not cover 4.14, Thinking About Objects: Identifying Class Attributes Chapter 4 - Control Structures.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 – Methods Part I.
8/2: Recursion About Scoping.java Recursion Program of the Day.
Java I--Copyright © Tom Hunter. Chapter 4 Control Structures: Part I.
Lecture 4: Chapter 7 - Arrays Outline Declaring and Creating Arrays Examples Using Arrays References and Reference Parameters Passing Arrays to Methods.
C Functions -Continue…-.
“Form Ever Follows Function” Louis Henri Sullivan
Introduction to Methods
Chapter 5 - Functions Outline 5.1 Introduction
Scope, Parameter Passing, Storage Specifiers
Java I.
CHAPTER 6 GENERAL-PURPOSE METHODS
Cs212: Data Structures Computer Science Department Lecture 2: Arrays.
design OO words Debug Variables Data types
Presentation transcript:

3/25: Scope Rules, More Methods about RollDie.java & modifications Scope rules More methods Program of the day

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. 276)

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