More types, Methods, Conditionals. ARCS Lab.

Similar documents
Visual Programming. Lecture 2: More types, Methods, Conditionals

Research Group. 2: More types, Methods, Conditionals

Data Structure and Programming Languages

6.092 Introduction to Software Engineering in Java January (IAP) 2009

Data Structure and Programming Languages

Java Tutorial. Saarland University. Ashkan Taslimi. Tutorial 3 September 6, 2011

JAVA OPERATORS GENERAL

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

bitwise inclusive OR Logical logical AND && logical OR Ternary ternary? : Assignment assignment = += -= *= /= %= &= ^= = <<= >>= >>>=

Lecture Set 4: More About Methods and More About Operators

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.

1.1 Your First Program

Selenium Class 9 - Java Operators

Lesson 5: Introduction to the Java Basics: Java Arithmetic THEORY. Arithmetic Operators

1 class Lecture2 { 2 3 "Elementray Programming" / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch.

Introduction to Computer Science Unit 2. Notes

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

CIS133J. Working with Numbers in Java

when you call the method, you do not have to know exactly what those instructions are, or even how the object is organized internally

1.1 Your First Program

Introduction to Computer Science Unit 2. Notes

CS110: PROGRAMMING LANGUAGE I

Prof. Navrati Saxena TA: Rochak Sachan

Java Basic Programming Constructs

Object-Oriented Programming

Zheng-Liang Lu Java Programming 45 / 79

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods

Lecture 6: While Loops and the Math Class

Lecture #6-7 Methods

Data Types. 1 You cannot change the type of the variable after declaration. Zheng-Liang Lu Java Programming 52 / 87

Variables, Types, Operations on Numbers

! Widely available. ! Widely used. ! Variety of automatic checks for mistakes in programs. ! Embraces full set of modern abstractions. Caveat.

Review. Primitive Data Types & Variables. String Mathematical operators: + - * / % Comparison: < > <= >= == int, long float, double boolean char

Lecture Set 4: More About Methods and More About Operators

COMP-202: Foundations of Programming. Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

if (x == 0); System.out.println( x=0 ); if (x = 0) System.out.println( x=0 );

Chapter 4: Control Structures I

AP CS Unit 3: Control Structures Notes

Selected Questions from by Nageshwara Rao

AP COMPUTER SCIENCE A

Programming Basics. Digital Urban Visualization. People as Flows. ia

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

First Programs. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

Computer Science II (20082) Week 1: Review and Inheritance

Building Java Programs

DATA TYPES AND EXPRESSIONS

Java Memory Management

More Java Basics. class Vector { Object[] myarray;... //insert x in the array void insert(object x) {...} Then we can use Vector to hold any objects.

1. Short circuit AND (&&) = if first condition is false then the second is never evaluated

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

Building Java Programs

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

DM503 Programming B. Peter Schneider-Kamp.

Chapter 4: Conditionals and Recursion

CS111: PROGRAMMING LANGUAGE II

In this chapter, you will:

Lecture 2: Operations and Data Types

Built-in data types. logical AND logical OR logical NOT &&! public static void main(string [] args)

CS 106 Introduction to Computer Science I

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

CS 101 Spring 2007 Midterm 2 Name: ID:

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods

CT 229 Java Syntax Continued

Chapter 5: Methods. by Tony Gaddis. Starting Out with Java: From Control Structures through Objects. Fourth Edition

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

Lecture 2: Intro to Java

CSC 1214: Object-Oriented Programming

University of Palestine. Mid Exam Total Grade: 100

Built-in data types. public static void main(string [] args) logical AND logical OR logical NOT &&! Fundamentals of Computer Science

CS 231 Data Structures and Algorithms, Fall 2016

Exercises Software Development I. 05 Conversions and Promotions; Lifetime, Scope, Shadowing. November 5th, 2014

Simple Java Reference

First Programs. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

Operators Questions

Building Java Programs. Chapter 2: Primitive Data and Definite Loops

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

1.1 Your First Program

CS 177 Spring 2010 Exam I

Controls Structure for Repetition

MODULE 02: BASIC COMPUTATION IN JAVA

1.1 Your First Program

4. Java Project Design, Input Methods

COMP 110/L Lecture 13. Kyle Dewey

CS 101 Fall 2006 Midterm 3 Name: ID:

CS 170 Exam 1. Version: B Fall Name (as on OPUS):

CT 229 Methods Continued

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

Some Sample AP Computer Science A Questions - Solutions

Program Fundamentals

1.1 Your First Program

Java Simple Data Types

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

PRIMITIVE VARIABLES. CS302 Introduction to Programming University of Wisconsin Madison Lecture 3. By Matthew Bernstein

Calculations, Formatting and Conversions

Introduction to Java Applications

CS 170 Exam 1. Version: C Fall Name (as on OPUS):

A Foundation for Programming

Transcription:

More types, Methods, Conditionals ARCS Lab.

Division Division ( / ) operates differently on integers and on doubles! Example double a = 5.0/2.0; 0; // a = 2.5 int b = 4/2; // b = 2 int c = 5/2; // c = 2 double d = 5/2; // d = 2.0 2

Order of Operations Precedence like math, left to right Right hand side of = evaluated first Parenthesis increase precedence Example double x = 3 / 2 + 1; // x = 2.0 double y = 3 / (2 + 1); // x = 1.0 3

Mismatched Types Java verifies that types always match Example String five = 5; // ERROR!! 4

Conversion by casting int a = 2; // a = 2 double a = 2; // a = 2.0 (Implicit) int a = 18.7; // ERROR int a = (int)18.7; // a = 18 double a = 2/3; // a = 0.0 double a = (double)2/3; // a = 0.6666 5

Methods public static void main (String[] arguments) System.out.println( Hello World ); 6

Adding Methods public static void NAME () STATEMENTS To call a method : NAME(); 7

class NewLine public static void newline() System.out.println(""); public static void threelines() newline(); newline(); newline(); public static void main(string[] arguments) System.out.println("Line 1"); threelines(); System.out.println("Line 2"); 8

class NewLine public static void newline() System.out.println(""); public static void threelines() newline(); newline(); newline(); public static void main(string[] arguments) System.out.println("Line 1"); threelines(); System.out.println("Line 2"); 9

class NewLine public static void newline() System.out.println(""); public static void threelines() newline(); newline(); newline(); public static void main(string[] arguments) System.out.println("Line 1"); threelines(); System.out.println("Line 2"); 10

class NewLine public static void newline() System.out.println(""); public static void threelines() newline(); newline(); newline(); public static void main(string[] arguments) System.out.println("Line 1"); threelines(); System.out.println("Line 2"); 11

Parameters public static void NAME (TYPE NAME) STATEMENTS To call : NAME(EXPRESSION); 12

class Square public static void printsquare(int x) System.out.println(x*x); public static void main(string[] arguments) int value = 2; printsquare(value); printsquare(3); printsquare(value*2); 13

class Square2 public static void printsquare(int x) System.out.println(x*x); public static void main(string[] arguments) printsquare( hello ); printsquare(5.5); What s wrong here? 14

class Square3 public static void printsquare(double x) System.out.println(x*x); public static void main(string[] arguments) printsquare(5); What s wrong here? 15

Multiple Parameters [ ] NAME (TYPE NAME, TYPE NAME) STATEMENTS To call : NAME(arg1, arg2); 16

class Multiply public static void times (double a, double b) System.out.println(a * b); public static void main(string[] arguments) times (2, 2); times (3, 4); 17

Return Values public static TYPE NAME () STATEMENTS return EXPRESSION; void means no type 18

class Square3 public static void printsquare(double x) System.out.println(x*x); public static void main(string[] arguments) printsquare(5); 19

class Square4 public static double square(double x) return x*x; public static void main(string[] arguments) System.out.println(square(5)); System.out.println(square(2)); 20

Variable Scope Variables live in the block ( ) where they are defined (scope) Method parameters are like defining a new variable in the method 21

class SquareChange public static ti void printsquare(int t x) System.out.println("printSquare x = " + x); x = x * x; System.out.println("printSquare x = " + x); public static void main(string[] arguments) int x = 5; System.out.println("main x = " + x); printsquare(x); System.out.println("main x = " + x); 22

class Scope public static void main(string[] arguments) int x = 5; if (x == 5) int x = 6; int y = 72; System.out.println("x = " + x + " y = " + y); System.out.println("x = " + x + " y = " + y); 23

Methods: Building Blocks Big programs are built out of small methods Methods can be individually developed, tested and reused User of method does not need to know how it works In Computer Science, this is called abstraction 24

Mathematical Functions Math.sin(x) Math.cos(Math.PI / 2) Math.pow(2, 3) Math.log(Math.log(x + y)) 25

if statement if (CONDITION) O ) STATEMENTS 26

public static void test(int x) if (x > 5) System.out.println(x + " is > 5"); public static void main(string[] arguments) test(6); t(6) test(5); test(4); 27

Comparison operators x > y: x is greater than y x < y: x is less than y x >= y: x is greater than or equal to x x <= y: x is less than or equal to y x == y: x equals y equality: == assignment: = 28

Boolean operators && : logical AND : logical OR if (x > 6) if (x > 6 && x < 9) if (x < 9) 29

else if (CONDITION) O ) STATEMENTS else STATEMENTS 30

public static void test(int x) if (x > 5) System.out.println(x + " is > 5"); else System.out.println(x + " is not > 5"); public static void main(string[] arguments) test(6); test(5); test(4); 31

else if if (CONDITION)( ) STATEMENTS else if (CONDITION) STATEMENTS else if (CONDITION) STATEMENTS else STATEMENTS 32

public static void test(int x) if (x > 5) System.out.println(x + " is > 5"); else if (x == 5) System.out.println(x + " equals 5"); else System.out.println(x + " is < 5"); public static void main(string[] arguments) test(6); test(5); test(4); 33

Conversion by method int to String String five = 5; // ERROR String five = Integer.toString(5); String five = + 5; String to int int foo = 18 ; // ERROR int foo = Integer.parseInt( 18 ); 34

Comparison operators Do NOT call == on doubles! EVER. double a = Math.cos (Math.PI / 2); double b = 0.0; a = 6.123233995736766E-17 a == b will return FALSE! 35

Q&A 36