Recall: Accessing Location Info private Location firstPoint; public void onMousePress( Location pressPt ) { new Text("Pressed", pressPt, canvas ); firstPoint.

Slides:



Advertisements
Similar presentations
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
Advertisements

Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
ObjectDraw and Objects Early Chris Nevison Barbara Wells.
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
Classes with multiple methods Part 1. Review of classes & objects Early on, we learned that objects are the basic working units in object-oriented programs.
A Sample Program A rudimentary solar simulation: We want a click of the mouse in the window to make the ‘sun’ rise.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
1  Ex: Declare a variable to store user’s age: int age; Prompt the user to enter his/her age: printf (“ How old are you? “); Read / scan the entered value.
Laboratory Study October, The very first example, traditional "Hello World!" program: public class first { public static void main (String[ ]
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.
© 2006 Lawrenceville Press Slide 1 Chapter 3 Visual Basic Interface.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Rational Expressions.
R7 Rational Expressions. Rational Expressions An expression that can be written in the form P/Q, where P and Q are polynomials and Q is not equal to zero.
Week 5 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
Operations with Positive Fractions
Primitive Data Type Can write value as a literal Ex: int a=1732 Can perform operations using operator symbols Ex: x+1.
Mathematical Calculations in Java Mrs. G. Chapman.
NA2204.1jcmt CSE 1320 Intermediate Programming C Program Basics Structure of a program and a function type name (parameters) { /* declarations */ statement;
Performing Calculations—1 of 2 In addition to using queries to retrieve, update, sort, and filter data in a database, you can use a query to perform calculations.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
1-2 Order of Operations and Evaluating Expressions.
Chapter 2 Objects and Mutator Methods. A Sample Program A rudimentary solar simulation: We want a click of the mouse in the window to make the ‘sun’ rise.
A Class of Our Own We’ve used many classes: –Location –FilledRect –Color –Text –And More! –But what if we want to make our own class?
Chapter 4 Conditional Statements. A Programmer's Lament I really hate this damned machine; I wish that they would sell it It never does quite what I want.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Arithmetic, Class Variables and Class Methods Week 11
Arithmetic Expressions Addition (+) Subtraction (-) Multiplication (*) Division (/) –Integer –Real Number Mod Operator (%) Same as regular Depends on the.
Graphics Tools and Parameters Chris Nevison Barbara Wells.
CompSci Primitive Types  Primitive Types (base types)  Built-in data types; native to most hardware  Note: not objects (will use mostly first.
Doing math In java.
Java Working with Numbers (Java: An Eventful Approach, Ch 3), Slides Credit: Bruce, Danyluk and Murtagh CS 120 Lecture October 2012.
Java Primitive Types, Operators, and Strings (Java: An Eventful Approach, Ch 5), Slides Credit: Bruce, Danyluk and Murtagh CSCI 120 Lecture October.
Creating and Using Class Methods. Definition Class Object.
1 CSC 221: Computer Programming I Fall 2005 simple conditionals and expressions  if statements, if-else  increment/decrement, arithmetic assignments.
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
Java Objects (Java: An Eventful Approach, Ch 2), Slides Credit: Bruce, Danyluk and Murtagh CSCI 120 Lecture October 2012.
Dr. Sajib Datta Jan 21,  Declare a variable ◦ int height; [note that no value is still assigned]  Assign a variable a value ◦ height =
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
Notes 2.1 Order of Operations PEMDAS If an expression has only numbers and symbols, it is a numerical expression. If it has a variable it is a variable.
Inside Class Methods Chapter 4. 4 What are variables? Variables store values within methods and may change value as the method processes data.
Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
What will each of the following lines print? System.out.println("number" ); number645 System.out.println("number" + (6 + 4)+ 5); number105 System.out.println(6.
Monomials Chapter 5.1. Vocabulary Monomial: an expression that is a number, a variable, or the product of a number and one or more variables. – Can not.
Chapter Sections 1.1 – Study Skills for Success in Mathematics
Chapter 4 Assignment Statement
Visual Basic Variables
Lecture 2: Data Types, Variables, Operators, and Expressions
1 Introduction to Algebra: Integers.
Arithmetic operations & assignment statement
Multiple variables can be created in one declaration
Chapter 3 Assignment Statement
STANDARDS: UNIT FOUR MGSE6.EE.1. Write and evaluate numerical expressions involving whole-number exponents. Essential Question: How do you write Prime.
Linear Equations Doctor Shildneck.
ALGEBRA. ALGEBRA VARIABLES AND EXPRESSIONS Algebra – Uses symbols to represent quantities that are unknown or that vary. You can represent mathematical.
One step equation with Multiplication and Division
Two Step Equation.
Equation with variables on both sides
One step equation with Addition and Subtraction
Chapter 2 Programming Basics.
Two step equation Operations
Two step equation Brackets
In this class, we will cover:
Multiply and divide Expressions
Chapter 2: Java Fundamentals cont’d
Do Now Simplify. 1. 5(7) – (18 – 11)  (40 – 35) (12 – 4)
Like Terms.
Presentation transcript:

Recall: Accessing Location Info private Location firstPoint; public void onMousePress( Location pressPt ) { new Text("Pressed", pressPt, canvas ); firstPoint = press; } public void onMouseRelease ( Location releasePt) { new Text("Released", releasePt, canvas ); new Line( firstPoint, releasePt, canvas ); }

Being Unpredictable Formal parameters: allow us to refer to information that is unknown when the program is written Are there other ways we access and use “unknown” information?

The Highs and Lows What if we want to move an object only vertically by dragging the mouse? Pong: public void onMouseDrag( Location mousePos ) { paddle.moveTo( 10, mousePos.getY() ); }

Accessor Methods Methods that return information about an object Examples: mousePos.getY(); canvas.getWidth();

Expressions vs Statements Statements: “Top of the morning to you.” paddle.moveTo( 10, mousePos.getY() ); Perform a useful action Expressions: “the morning” mousePos.getY() Used in statements

Fun with Numbers initialPosition.getX() and -5 are both numbers Can add, subtract, divide, multiply numbers initialPosition.getX() initialPosition.getX() initialPosition.getX() / 5 initialPosition.getY() * 3

Order of Arithmetic Operations 2 + (initialPosition.getX() – initialPosition.getY()) * 5 Precedence Rules: Perform operations within parentheses first Divisions and multiplications before additions and subtractions Operations of equal precedence performed left to right

Instance Variables We can give names to numeric values as we can name objects private int brightness; //shade of gray brightness = 50;

Using Variables First declare: private int brightness; private Color purple; Then initialize brightness = 50; purple = new Color( 255, 0, 255 ); We can also reassign values to variables brightness = 34; Unless…

Using Constants private static final int BRIGHTNESS = 255; The keyword final indicates we cannot reassign a new value to brightness The Java convention for naming constants is all caps

Displaying Numeric Information We can combine numbers and text for display Text countDisplay; int programNum = 3; countDisplay = new Text("This is program #" + programNum, …);

System.out.println Two ways to display textual information Text class System.out.println(…); System.out.println("Number " + 5 ); prints Number 5 to the Java Console

Playing Dice with the Universe How can we simulate a roll of dice? Through random numbers!

Random Numbers RandomIntGenerator die; die = new RandomIntGenerator( 1, 6 ); //display a random integer between 1 and 6 System.out.println( die.nextValue() );

public class RollAnotherOne extends WindowController { private static final int NUM_SIDES = 6; private RandomIntGenerator die = new RandomIntGenerator ( 1, NUM_SIDES ); public void begin() { new Text("Click to make me roll the dice ", TEXT_X, PROMPT_Y, canvas ); } public void onMouseClick (Location point ){ roll1 = die.nextValue(); roll2 = die.nextValue(); System.out.println("You rolled a " + roll1 + "and a " + roll2 + " for a total of " + ( roll1 + roll2 ) ); }

Review Numbers Expressions and statements Variables and Constants System.out.println(…) Generating random numbers