15-101 Variables & Random Number Generation.  A penguin is playing arctic basketball. The penguin has a basketball and will push the basketball toward.

Slides:



Advertisements
Similar presentations
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Advertisements

PIIT Computer Science Summer Camp - Alice July 10, 2012 Brenda Parker Computer Science Department MTSU.
Chapter 2: Using Objects Part 1. To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about.
CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
Built-in Functions & Arithmetic Expressions.
Tutorial 12: Enhancing Excel with Visual Basic for Applications
Don Slater Wanda Dann
Stepwise Refinement -- A procedure calls another procedure
 2005 Pearson Education, Inc. All rights reserved Introduction.
Alice in Action with Java Chapter 7 From Alice to Java.
Alice in Action with Java
©2004 Brooks/Cole Chapter 1: Getting Started Sections Covered: 1.1Introduction to Programming 1.2Constructing a Java Program 1.3The print() and println()
Begin Java having used Alice Pepper - Some slides from Alice in Action with Java.
Java An introduction. Example 1 public class Example1 { public static void main (String [] args) { System.out.println (“This is the first example”); int.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
1 Character Strings and Variables Character Strings Variables, Initialization, and Assignment Reading for this class: L&L,
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
Variables and Functions Chapter Variables Named storage location in computer’s memory Programs may need to store data when running o Stored in.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
A First Program Using C#
Working with Numbers in Alice - Converting to integers and to strings - Rounding numbers. - Truncating Numbers Samantha Huerta under the direction of Professor.
ICS312 Set 4 Program Structure. Outline for a SMALL Model Program Note the quiz at the next lecture will be to reproduce this slide.MODEL SMALL.586 ;
Programming Logic Program Design. Objectives Steps in program development Algorithms and Pseudocode Data Activity: Alice program.
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
by Chris Brown under Prof. Susan Rodger Duke University June 2012
Alice in Action with Java Chapter 7 From Alice to Java.
INTRODUCTION TO ALGORITHMS PROGRAMMING. Objectives Give a definition of the term algorithm Describe the various parts of the pseudocode algorithm or algorithm.
Alice 2.0 Introductory Concepts and Techniques Project 1 Exploring Alice and Object-Oriented Programming.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Input, Output, and Processing
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Exam scores by range 3-1.
CHAPTER 4: CONTROL STRUCTURES - SEQUENCING 10/14/2014 PROBLEM SOLVING & ALGORITHM (DCT 1123)
FIRST JAVA PROGRAM. JAVA PROGRAMS Every program may consist of 1 or more classes. Syntax of a class: Each class can contain 1 or more methods. public.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Programming with Visual C++: Concepts and Projects Chapter 2B: Reading, Processing and Displaying Data (Tutorial)
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs -
Creating and Calling Procedures Alice 3 Beta. A few choices to be made Right window, upper left hand corner start by clicking on Class “MyScene” button.
© 2004 Pearson Addison-Wesley. All rights reserved ComS 207: Programming I Instructor: Alexander Stoytchev
Chapter 4 Introduction to Classes, Objects, Methods and strings
Introduction to Visual Basic Programming. Introduction Simple Program: Printing a Line of Text Another Simple Program: Adding Integers Memory Concepts.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming Third Edition.
Don Slater Wanda Dann Copyright 2012 Wanda Dann, Don Slater All rights reserved.
Variables and Functions Chapter Variables Named storage location in computer’s memory Programs may need to store data when running Types of data.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
Copyright 2008 Wanda Dann, Steve Cooper, Don Slater Alice Workshop Variables & Conditions.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
1 Data and Expressions Chapter 2 In PowerPoint, click on the speaker icon then the “play” button to hear audio narration.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming Third Edition.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Programming in Alice IT-IDT-9 Design, develop, test and implement programs using visual programming. 9.1 Utilize drag and drop software to develop programs.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
Review A program is… a set of instructions that tell a computer what to do. Programs can also be called… software. Hardware refers to… the physical components.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Unit 2 Programming in Alice & Java 1. Day 1: Learning to Program  Objective: Understand the need for step-by-step logic; Basic working of a computer.
Some of the utilities associated with the development of programs. These program development tools allow users to write and construct programs that the.
Beginning Animation. Storyboard An Invitation to Dinner 1.Marlin and Dory (two small fish) are swimming and talking. 2.A shark (Bruce) swims up behind.
Introduction to Programming
Programming what is C++
Introduction to Programming
Console Output, Variables, Literals, and Introduction to Type
Chapter 3: Variables, Functions, Math, and Strings
Chapter 3: Variables, Functions, Math, and Strings
Instructor: Alexander Stoytchev
Presentation transcript:

Variables & Random Number Generation

 A penguin is playing arctic basketball. The penguin has a basketball and will push the basketball toward a hole in the ice. Each push causes the basketball to move forward a random distance.  If the penguin gets the basketball in the water, the penguin scores two points. Recall: Problem

Problem Solving step 2  Design an algorithm and set up scene Do in order penguin swagger penguin push basketball a random distance If basketball is in the water, score 2 points

 Implementation – translate algorithm into code (note that the basketball is now the target) Problem Solving step 3 Do in order penguin swagger penguin push basketball a random distance If basketball is in the water, score 2 points This italicized item will be modified / added in future presentations Need to modify

 At runtime, a program is allocated a stack of memory (bytes) in which to store its data. – Several data items may be stored in the stack Runtime stack Data items

 A variable is a name for a location in memory where an item of data is stored  Each data item in the stack is given a unique name (an identifier) Example: in diagram at right, done is a name for location 4  We say a variable “represents a specific value”  The value stored in a variable can and will change when a program runs  Thus -> variable Variable done

 The data item stored at a specific location has a specific data type, which describes the characteristics of the stored information  Range of values  Legal operations  Data types used in Alice 3

Declaring a variable  Drag the local tile into the code editor

 A variable is declared by specifying – Type – Name – Initial value Declaring a variable Double randDistance = 0.0; AliceJava Assignment: Stores a value in the variable's memory space

At the time a variable is declared, it’s data type determines how many bytes of memory the variable is allocated. Example: Byte Allocation randDistance has 8 bytes of memory where a real number may be stored

 The previously written pushObject procedure uses the distance parameter to move the target object forward 2.75 meters  but we want the target (basketball) to move forward a random distance...so we need to send a random number to the distance parameter. Value sent to parameter

 In MyScene’s run method, Add a local variable, randDistance, that will represent a random distance.  Replace default initialization value (0.0) with a random value in the range of 0.5 to 5.0 (Why?) Random distance for the ball

 replace 0.0 with one of the Random number functions Random functions

 A function designed to generate a random sequence of numbers or symbols that lack any pattern, i.e. appear random - Wikipedia Random number functions  The next value in the random sequence will be in the range 0.0 <= value < 1.0  The next value in the random sequence will be in the range first ??? <= value < second ??? Which function is better for our purposes?

Build the random number expression  Build the random expression from the cascading menus.  Resulting statement:

Use the randDistance variable  We have generated a random value in the range 0.5 >= value < 5.0, and stored it in our local variable, randDistance  We now need to use this variable as the argument to the pushObject method for the distance parameter  Resulting statement: click select

Problem Solving Step 4: Testing Because we are using random numbers, we need to run this code many times to convince ourselves that successive runs yield random results. Alice provides visual feedback Another common technique for testing is to display a value as a textual string

Problem Solving Step 4: Testing Because we are using random numbers, we need to run this code many times to convince ourselves that successive runs yield random results. Alice provides visual feedback Another common technique for testing is to display a value as a textual string

 We could use say or think to display the value in Alice.  Example, using say

Displaying a value: Alice The think and say procedural methods expect a String argument To create a String, we can use concatenation – Concatenate means: to append one String to another String – Symbol for concatenation is '+' Example: “hello” + “ world” → “hello world”

Example: Alice Drag in say statement tile -> Select “hello” to begin Replace “hello” Select concatenate symbol tile Select operands

Custom String One of the operands is a custom string, so Alice displays a dialog box where a string can be entered Resulting statement

Display a value: Java The run method in Java: public void run() { penguin.swagger(); Double randDistance = RandomUtilities.nextDoubleInRange(0.5, 5.0); penguin.pushObject(ball, randDistance); penguin.say("Distance moved: " + randDistance); } Note the quote marks around the custom string

System output: Java Java defines a System output (by default, to the console box on the monitor screen) public void run() { penguin.swagger(); Double randDistance = RandomUtilities.nextDoubleInRange(0.5, 5.0); penguin.pushObject(ball, randDistance); penguin.say("Distance moved: " + randDistance); System.out.println("Distance moved: " + randDistance); } println – prints on the current line and then moves the cursor to the next line to prepare for next line of output