Research Group. 2: More types, Methods, Conditionals

Similar documents
More types, Methods, Conditionals. ARCS Lab.

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

Data Structure and Programming Languages

Data Structure and Programming Languages

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

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

Lecture Set 4: More About Methods and More About 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.

JAVA OPERATORS GENERAL

CIS133J. Working with Numbers in Java

Introduction to Computer Science Unit 2. Notes

Lecture Set 4: More About Methods and More About Operators

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

Zheng-Liang Lu Java Programming 45 / 79

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

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

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

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

Building Java Programs

Building Java Programs

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

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

CS 106 Introduction to Computer Science I

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

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

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

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

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

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

Object-Oriented Programming

egrapher Language Reference Manual

In this chapter, you will:

CS313D: ADVANCED PROGRAMMING LANGUAGE

Object Oriented Software Design

Algorithms and Programming I. Lecture#12 Spring 2015

FRAC: Language Reference Manual

Program Fundamentals

Recap: Assignment as an Operator CS 112 Introduction to Programming

Operators. Java operators are classified into three categories:

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

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

COMP1730/COMP6730 Programming for Scientists. Data: Values, types and expressions.

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

Selenium Class 9 - Java Operators

Lecture 3 Operators MIT AITI

CSC Web Programming. Introduction to JavaScript

Important Java terminology

Prof. Navrati Saxena TA: Rochak Sachan

CS 112 Introduction to Programming

Basics of Java Programming

Chapter 4: Control Structures I

Chapter 2: Using Data

Chapter 3: Operators, Expressions and Type Conversion

DATA TYPES AND EXPRESSIONS

Review Chapter 6 in Bravaco. Short Answers 1. This type of method does not return a value. a. null b. void c. empty d. anonymous

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

Programming with Java

1.1 Your First Program

CSI33 Data Structures

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

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

Chapter 02: Using Data

Simple Java Reference

Introduction to Java Applications

IEEE Floating-Point Representation 1

A.P. Computer Science A Summer Packet

Chapter 2: Using Data

Building Java Programs

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

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

AP CS Unit 3: Control Structures Notes

AP COMPUTER SCIENCE A

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

Lecture 2: Variables and Operators. AITI Nigeria Summer 2012 University of Lagos.

Java Basic Programming Constructs

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

Pace University. Fundamental Concepts of CS121 1

Accelerating Information Technology Innovation

CS111: PROGRAMMING LANGUAGE II

Course Outline. Introduction to java

Introduction to Programming Using Java (98-388)

CS 231 Data Structures and Algorithms, Fall 2016

MODULE 02: BASIC COMPUTATION IN JAVA

Lecture 5: Methods CS2301

COMP-202: Foundations of Programming. Lecture 6: Conditionals Jackie Cheung, Winter 2016

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

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

Introduction to Computer Science Unit 2. Notes

Lecture #6-7 Methods

Full file at

Full file at

Elementary Programming

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

Building Java Programs Chapter 2

DM503 Programming B. Peter Schneider-Kamp.

CHAPTER 7 OBJECTS AND CLASSES

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

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

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

Transcription:

Research Group 2: More types, Methods, Conditionals

Outline Lecture 1 Review More types Methods Conditionals 2

Types Kinds of values that can be stored and manipulated. boolean: Truth value (true or false). int: Integer (0, 1, -47). double: Real number (3.14, 1.0, -2.1). String: Text ( hello, example ). 3

Variables Named location that stores a value Example: String a = a ; String b = letter b ; a = letter a ; String c = a + and + b;

Operators Symbols that perform simple computations Assignment: = Addition: + Subtraction: - Multiplication: * Division: /

Exercise 1 class GravityCalculator { public static void main(string[] args) { double gravity = -9.81; double initialvelocity = 0.0; double fallingtime = 10.0; double initialposition = 0.0; double finalposition =.5 * gravity * fallingtime * fallingtime; finalposition = finalposition + initialvelocity * fallingtime; finalposition = finalposition + initialposition; System.out.println("An object's position after " + fallingtime + " seconds is " + finalposition + m.");

Exercise 1 double finalposition =.5 * gravity * fallingtime * finalposition = finalposition + initialvelocity double finalposition =.5 * gravity * fallingtime * finalposition = finalposition + initialvelocity finalposition += initialposition; fallingtime; * fallingtime; finalposition = finalposition + initialposition; OR fallingtime; * fallingtime;

Questions from last lecture?

Outline Lecture 1 Review More types Methods Conditionals

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

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

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

What is a casting? Conversion by casting Taking an Object of one particular type and turning it into another Object type. 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.666... double d = 5.25; int i = (int) d; int d = 5; double i = d; // d = 5 (Explicit) DOWNCAST // i = 5.0 (Implicit) UPCAST

Outline Lecture 1 Review More types Methods Conditionals

Java Methods A collection of statements that are grouped together to perform an operation. System.out.println()à Methods - The system actually executes several statements in order to display a message on the console. The only required elements of a method declaration are the method's return type, name, a pair of parentheses, (), and a body between braces, {.

Methods Parts class Main { public static void main(string[] arguments) { System.out.println( Hello World );

Methods Method declarations have six components: Modifiers. The return type (or void). The method name. The parameter list in parenthesis. An exception list. The method body, enclosed between braces. class Main { public static void main(string[] arguments) { System.out.println( Hello World ); 17

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

Methods 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");

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

Parameters 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);

What s wrong here? Parameters class Square { 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? Parameters class Square { public static void printsquare(double x) { System.out.println(x*x); public static void main(string[] arguments) { printsquare(5);

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

Multiple Parameters 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);

Return Values public static TYPE NAME() { STATEMENTS return EXPRESSION; void means no returned value

Return Values class Square3 { public static void printsquare(double x) { System.out.println(x*x); public static void main(string[] arguments) { printsquare(5); 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));

Variable Scope Variables live in the block ({) where they are defined (scope) Scope starts where the variable is declared and ends whith the block where it was declared (the variable lives within the block) Method parameters are like defining a new variable in the method

Variable Scope class SquareChange { public static void printsquare(int 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);

Variables Scope 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);

Methods: Building Blocks Methods as the way of encapsulating functionality 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 Black box operations In Computer Science, this is called abstraction

Mathematical Functions Encapsulated functionality that we can use without having to master inner details public class Main { public static void main(string[] arguments) { int x = 90; Math.sin(x); Math.cos(Math.PI / 2); Math.pow(2, 3); System.out.println(...);

Outline Lecture 1 Review More types Methods Conditionals

if statement if (CONDITION) { STATEMENTS /* statements performed when the boolean expression results true */

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

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: = )

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

else if (CONDITION) { STATEMENTS else { STATEMENTS /* performed when CONDITION is not true */

else 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);

else if if (CONDITION) { STATEMENTS else if (CONDITION) { STATEMENTS else if (CONDITION) { STATEMENTS else { STATEMENTS

else 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);

Questions?