Data Structure and Programming Languages

Similar documents
Data Structure and Programming Languages

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

More types, Methods, Conditionals. ARCS Lab.

Research Group. 2: More types, Methods, Conditionals

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

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

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

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

Lecture Set 4: More About Methods and More About Operators

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

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

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

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

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

Chapter 4: Control Structures I

Data Structure and Programming Languages

CISC-124. This week we continued to look at some aspects of Java and how they relate to building reliable software.

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

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.

H212 Introduction to Software Systems Honors

Object-Oriented Programming

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

4 WORKING WITH DATA TYPES AND OPERATIONS

Introduction to Computer Science Unit 2. Notes

Lecture Set 4: More About Methods and More About Operators

JAVA OPERATORS GENERAL

Introduction to Computer Science Unit 2. Notes

Zheng-Liang Lu Java Programming 45 / 79

CS111: PROGRAMMING LANGUAGE II

AP Computer Science Unit 1. Programs

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

Chapter 3: Operators, Expressions and Type Conversion

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

Section 2.2 Your First Program in Java: Printing a Line of Text

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

Boolean Expressions. So, for example, here are the results of several simple Boolean expressions:

Chapter 3. Selections

Fundamentals of Programming Data Types & Methods

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

CSC 1051 Data Structures and Algorithms I

Chapter 4: Conditionals and Recursion

Programming with Java

4. Java Project Design, Input Methods

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

More Things We Can Do With It! Overview. Circle Calculations. πr 2. π = More operators and expression types More statements

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

Full file at

Welcome to the Primitives and Expressions Lab!

COMP-202: Foundations of Programming. Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015

Object Oriented Programming. Java-Lecture 1

Introduction to Java Applications

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

Java Basic Programming Constructs

CIS133J. Working with Numbers in Java

Branching and Boolean Expressions

Branching and Boolean Expressions

Introduction to Computer Programming

Algorithms and Conditionals

CS 302: Introduction to Programming

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

AP Computer Science Unit 1. Writing Programs Using BlueJ

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

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

Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4

Chapter 2: Basic Elements of Java

Simple Java Reference

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

Course Outline. Introduction to java

AYBUKE BUYUKCAYLI KORAY OZUYAR MUSTAFA SOYLU. Week 21/02/ /02/2007 Lecture Notes: ASCII

1.1 Your First Program

Introduction to Programming Using Java (98-388)

CIS 1068 Program Design and Abstraction Spring2016 Midterm Exam 1. Name SOLUTION

Place your name tag here

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

Chapter 2: Data and Expressions

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

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

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

Mid Term Exam 1. Programming I (CPCS 202) Instructor: M. G. Abbas Malik Date: Sunday November 3, 2013 Total Marks: 50 Obtained Marks:

Building Java Programs

DATA TYPES AND EXPRESSIONS

Chapter 2. Elementary Programming

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

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

Full file at

PROGRAMMING FUNDAMENTALS

Program Control Flow

Program Control Flow

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

In this chapter, you will:

Introduction to Java Unit 1. Using BlueJ to Write Programs

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

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

Variables, Types, Operations on Numbers

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

AP CS Unit 3: Control Structures Notes

Selected Questions from by Nageshwara Rao

Loops and Expression Types

Outline. Why Java? (1/2) Why Java? (2/2) Java Compiler and Virtual Machine. Classes. COMP9024: Data Structures and Algorithms

AP Computer Science Unit 1. Writing Programs Using BlueJ

Transcription:

204700 Data Structure and Programming Languages Jakarin Chawachat From: http://ocw.mit.edu/courses/electrical-engineering-and-computerscience/6-092-introduction-to-programming-in-java-january-iap-2010/index.htm

2.MORE TYPES, METHODS, OPERATORS

Outline Lecture 1 Review More types Methods Conditionals

Types Kind of values that can be stored and manipulated. boolean: Truth value (true or false). int: Integer (0, 1-50) double: Real number (3.14, 1.0, -756.015) String: Text ( Hello world., example ).

Variables Named location that stores a value of one particular type. Form: TYPE NAME; Example: String foo;

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

Assignment 1 public class C2FConterter { public static void main(string[] args) { double celcius = 50; double farenheit = 0.0; farenheit = ((celcius/5) * 9) + 32; System.out.println(celcius+ Celsius degree is equal to + farenheit + Fahrenheit degree.");

Output of Assignment 1 50.0 Celsius degree is equal to 122.0 Fahrenheit degree.

Outline Lecture 1 Review More types Methods Conditionals

Division Division ( / ) operates differently on integer and doubles! Example: double a = 5.0/2.0; // a = 2.5 int b = 4/2; // b = 2 int c = 5/2; // b = 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 x = 3/2+1; // x = 2.0; Double y = 3/(2+1); // y = 1.0;

Mismatched Types Java verifies that types always match: String five = 5; // ERROR! Exception in thread "main" java.lang.error: Unresolved compilation problem: Type mismatch: cannot convert from int to String at C2FConterter.main(C2FConterter.java:6)

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; //18 double a =2/3; // a = 0.0 double a = (double)2/3; // a = 0.6666.

Input and Output Simple Output System.out.print( Text ); Simple Input Use Scanner Import java.util.scanner; Create Scanner object Scanner s = new Scanner(System.in);

Simple Output Example float height = s.nextfloat(); int i = s.nextint(); nexttype(): Return the next token in the input stream, returned as the base type corresponding to Type

Example

Outline Lecture 1 Review More types Methods Conditionals

Methods A Java method is a collection of statements that are grouped together to perform an operation. When you call the System.out.println method, for example, the system actually executes several statements in order to display a message on the console.

Adding Methods In general, a method has the following syntax: modifier returnvaluetype methodname(list of parameters) { // Method body;

The parts of a method Modifiers: (optional) The modifier tells the compiler how to call the method. This defines the access type of the method. Return Type: The returnvaluetype is the data type of the value the method returns. (void if no return value) Method Name: This is the actual name of the method. Parameters: (optional) When a method is invoked, you pass a value to the parameter. Method Body: The method body contains a collection of statements that define what the method does.

Examples Example: no return value public static void threelines() { System.out.println("");

Examples Example: return value public static double pivalue() { return 3.14;

Examples Example: return value / parameter public static double addition(int a, int b) { int result; result = a + b; return result;

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

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 a method: NAME(EXPRESSION);

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

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?

Multiple Parameters public static void NAME(TYPE NAME, TYPE NAME){ STATEMENTS To call a method: NAME(arg1, arg2);

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 type

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){ double ans; ans = square(5); System.out.println(ans); System.out.println(square(2));

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

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

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); Duplicate local variable x

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 to it workd In Computer Science, this is called abstraction

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

Outline Lecture 1 Review More types Methods Conditionals

If statement if (CONDITION) { true condition false STATEMENTS

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 y x <= y: x is less than or equal to y x == y: x equals to y (equality: ==, assignment: =)

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

else if (CONDITION) { true condition false STATEMENTS else { STATEMENTS

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); 6 is > 5 5 is not > 5 4 is not > 5

Else if if (CONDITION) { STATEMENTS else if (CONDITION) { STATEMENTS else if (CONDITION) { STATEMENTS else STATEMENTS true condition true false condition false

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); 6 is > 5 5 equals 5 4 is < 5

Conversion be method int to String: String five = 5; //ERROR String five = Integer.toString(5); String five = + 5; //five = 5 String to int: int foo = 18 ; //ERROR! Int foo = Integer.parseInt( 18 );

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;

Assignment 2 Method to print pay based on base pay and hour worked Overtime: More than 40 hours, paid 1.5 times base pay Minimum Wage: $8.00/hour Maximum Work: 60 hours a week