Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.

Slides:



Advertisements
Similar presentations
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Advertisements

1 Lecture Today’s Topics Classes –Attribute (variables) –Behaviors (methods) Using methods –How to write methods –How to use methods Scope –Private.
Introduction to Computers and Programming Lecture 4: Mathematical Operators 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.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 3: Parameters, Return, and Interactive Programs with Scanner.
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
Primitive Data Types There are exactly eight primitive data types in Java four of them represent integers: byte (class Byte), short (class Short), int.
Lecture 6. What is the difference in pictures ???
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
Lecture 5. Review (Attributes and Methods) Class will contain –a set of attributes and methods public class Turtle { ///// Field (Attributes) ///// …
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Expressions, Data Conversion, and Input
Copyright © 2003 Pearson Education, Inc. Slide 2-1 Problem Solving with Java™ Second Edition Elliot Koffman and Ursula Wolz Copyright © 2003 Pearson Education,
Variable Declaration  It is possible to declare multiple variables of the same data type on the same line.  Ex. double hours, rate, total;  Variables.
CSC Programming I Lecture 5 August 30, 2002.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Variables, Arithmetic, etc.)
First Programs CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Math Class Part of the Java.lang package. This package is from object so you do not have to import the lang package. Static: Math Class is static.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
Chapter 2 topics Concept # on Java Subset Required for AP Exam print and println10. Testing of output is restricted to System.out.print and System.out.println.
Mathematical Calculations in Java Mrs. C. Furman.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Programs That Calculate. Arithmetic Expressions +addition -subtruction *multiplication /division  Order of Operations BEDMAS (brackets, exponentiation,
The Math Class Methods Utilizing the Important Math Operations of Java!
Arithmetic Expressions Addition (+) Subtraction (-) Multiplication (*) Division (/) –Integer –Real Number Mod Operator (%) Same as regular Depends on the.
1 10/3/05CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
1 Chapter 2: Java Fundamentals cont’d Spring Lory Al Moakar.
Doing math In java.
1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
Dr. Sajib Datta Jan 21,  Declare a variable ◦ int height; [note that no value is still assigned]  Assign a variable a value ◦ height =
Copyright 2011 by Pearson Education Building Java Programs Chapter 3 Lecture 3-2: Return values, Math, and double reading: 3.2,
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
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.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
CompSci 230 S Programming Techniques
Lecture 3 Java Operators.
Building Java Programs
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
OPERATORS (1) CSC 111.
Escape Sequences What if we wanted to print the quote character?
Lecture 3 Expressions Richard Gesick.
Building Java Programs
Arithmetic Expressions & Data Conversions
Expressions and Assignment
Data Types and Expressions
Building Java Programs
Building Java Programs
Building Java Programs
Chapter 2: Java Fundamentals cont’d
Building Java Programs
Data Types and Expressions
Building Java Programs
Arithmetic Expressions & Data Conversions
Data Types and Expressions
Presentation transcript:

Lecture 7

Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles e,g,.

Review (Loops) Repetition statements allow us to run a statement or a block of statements multiple times Often we call them as loops for while do

Example of for Loop public class Turtle { public void drawSquare( int len ) { for(int i=0; i < 4; i++) { forward( len ); turnRight(); } Repeat these statements 4 times Count starts from 0 Add one for each repeat 4 is not included

Example of for Loop public class Turtle { public void printNumber( int num ) { for(int i=0; i < 10; i++) { System.out.pritnln( num ); } You can specify the printed number when you use this method

Exercise 1 in lecture 6 public class Test { public static void main(String[] args) { World w = new World(); Turtle t = new Turtle(w); for(int k=0; k < 10; k++) { t.drawSquare(100); t.turn(20); } public class Turtle { public void drawSquare(int len) { for(int k=0; k < 4; k++) { forward(len); turnRight(); }

Exercise 1 in lecture 6 (Another way) public class Test { public static void main(String[] args) { World w = new World(); Turtle t = new Turtle(w); t.drawPicture(); } public class Turtle { public void drawPicture() { for(int k=0; k < 10; k++) { drawSquare(100); turn(20); } public void drawSquare(int len) { for(int k=0; k < 4; k++) { forward(len); turnRight(); }

Today’s topic More on Arithmetic Expression - Expression - Operators -Precedence -Math class

Data type –Integer: { …, -1, 0, 1, … } e.g.int total; int number = 5; –Double: { …, -1, …, -0.5, …, 0, …, 0.5, …, 1, … } e.g.double average; double radius = 2.4;

Expressions An expression is a combination of one or more operators and operands Addition+ Subtraction- Multiplication* Division/ Remainder% / * 5 operatorsoperands

Assignment The result of expression is assigned to a variable by using = symbol x = 5 + 3; int x; Variable declaration Assignment Note: Somewhat different from math Variable x contains a value of 8

Assignment (integer) If both operands are integers, the result is an integer int result; result = 5 + 7; int a = 5; int b = 7; int c; c = a + b; result = ? c = ?

Assignment (double) If both operands are double (floating point), the result is a double double result; result = ; double a = 5.7; double b = 2.5; double c; c = a + b; result = ? c = ?

Division and Remainder If both operands to the division operator ( / ) are integers, the result is an integer (the fractional part is discarded) The remainder operator (%) returns the remainder after division 14 / 3 equals 8 / 12 equals % 3 equals 8 % 12 equals 2 8

Operator Precedence Operators can be combined into complex expressions result = 3 * 5.1 / 2 – 10.1; Operators have a precedence which determines the order in which they are evaluated

Operator Precedence Multiplication, division, and remainder are evaluated prior to addition and subtraction result = * 1; Arithmetic operators with the same precedence are evaluated from left to right, but parentheses can be used to force the evaluation order result = 2 * (3 + 5) * 1;

Examples What is the order of evaluation in the following expressions? a + b + c + d + e 1432 a + b * c - d / e 3241 a / (b + c) - d % e 2341 a / (b * (c + (d - e))) 4123

You should be careful for this difference 10 2 * 5 = * 5 = 1 10 / (2 * 5) 10 / 2 * 5 Math Java

Assignment Revisited The assignment operator has a lower precedence than the arithmetic operators First the expression on the right hand side of the = operator is evaluated Then the result is stored in the variable on the left hand side answer = sum / * number; 1432

Assignment Revisited The right and left hand sides of an assignment statement can contain the same variable First, one is added to the original value of count Then the result is stored back into count (overwriting the original value) count = count + 1;

Methods of Math class // return absolute value of num int abs(int num) // return value of square root and power double sqrt(double num) double pow(double num, double power)

int abs(int num) This method calculates the absolute of the entered number How to use? It takes one integer | -10 | The result will be an integer Class name Method dot Math.abs( -10 );

Example int negative = -10; int value = Math.abs( negative ); System.out.println( “The result is “ + value); The result is 10 | -10 |

double sqrt(double num) This method calculates the square root of the entered number How to use Math.sqrt( 6.25 ); It takes one double value or variableThe result will be a double class name Method Dot 6.25

Example double area = 2.25; double value2 = Math.sqrt( area ); System.out.println( “value2 is “ + value2); value2 is

double pow(double num, double power) This method calculates the power of the entered number How to use Math.pow( 2.0, 3.0 ); It takes two double values or variablesThe result will be a double Object name Method Dot 2323

Example int three = 3; double square = Math.pow( three, 2 ); double cube = Math.pow( three, 3.0 ); System.out.println( “3 square is “ + square); System.out.println( “3 cube is “ + cube); 3 square is cube is 27.0 pow method takes two variables 3 2

Exercise!!!

Exercise 1 (Test.java) public class Test { public static void main(String args[]) { int num1; double num2; num1 = 10 * 5 – 7 / 3; System.out.println( num1 ); num2 = 2.5 * ( ); System.out.println( num2 ); }

Exercise 1 (Test.java) public class Test { public static void main(String args[]) { int num1; double num2; num1 = 10 * 5 – 7 / 3; System.out.println( “num1 is “ + num1 ); num2 = 2.5 * ( ); System.out.println( “num2 is “ + num2 ); } You can print out whatever you want by using double quotation marks

Exercise 2 Calculate the average score of final exams e.g. English90 Math86 Science92 History78 Spanish83 Average ?

Exercise 2 (sample codes) public class Test { public static void main(String args[]) { int num = 5; double average; average = ( ) / num; System.out.println( average ); } Is this correct ? double? = int / int

Exercise 2 (sample codes) public class Test { public static void main(String args[]) { int num = 5; double average, total; total = ; average = total / num; System.out.println( “Average is “ + average ); } Is this correct ?

Exercise 3 Let’s calculate the following equations (2.3) 2 + (3.5) 3 How to write in Java program?

Exercise 3 (sample codes) public class Test { public static void main(String args[]) { int num1; double num2; num2 = Math.pow( 2.3, 2 ) + Math.pow( 3.5, 3 ); System.out.println( “The result is “ + num2 ); }

Exercise 4 Let’s solve the following quadratic equations 3X 2 = 15  X 2 = 15 / 3  X = 5 How to write in Java program?

Exercise 4 (sample codes) public class Test { public static void main(String args[]) { int num1; double num2; num2 = Math.sqrt( 5 ); System.out.println( “3*X^2 = 15“); System.out.println( “X is “ + num2 ); }

X Y (4,7) (-6, -2) Challenge Let’s compute the distance between two points P1 (4, 7) P2 (-6, -2)

P1 (X 1, Y 1 ) P2 (X 2, Y 2 ) distance = (X 1 – X 2 ) 2 + (Y 1 – Y 2 ) X Y (4,7) (-6, -2)

So, how to compute the distance in Java ??? The result will be double number, let’s store the result into double variable (it should be declared before) Then print out the distance Try to report in details like… e.g. The distance between (-1,3) and (2, -1) is 5.0 distance = (X 1 – X 2 ) 2 + (Y 1 – Y 2 ) 2 dist = Math.sqrt( Math.pow(X1-X2,2) + Math.pow(Y1-Y2,2) )

public class Test { public static void main(String[] args) { // declare variables and // assign values into variables representing each point P1 and P2 int x1 = 4; int y1 = 7; int x2 = -6; int y2 = -2; double dist; // caluclate the distance and print out the result dist = Math.sqrt( Math.pow(x1-x2, 2) + Math.pow(y1-y2, 2) ); System.out.println(“The distance between P1(“ + x1 + “,” + y1 +”)” + “ and P2(“ + x2 + “,” + y2 + “) is “ + dist ); } } Sample codes