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

Similar documents
CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

ECE 122 Engineering Problem Solving with Java

Data Conversion & Scanner Class

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Programming with Java

Chapter. Let's explore some other fundamental programming concepts

CMPT 125: Lecture 3 Data and Expressions

More Programming Constructs -- Introduction

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

Chapter 2: Data and Expressions

Chapter 2: Data and Expressions

COMP Primitive and Class Types. Yi Hong May 14, 2015

Java Foundations: Introduction to Program Design & Data Structures, 4e John Lewis, Peter DePasquale, Joseph Chase Test Bank: Chapter 2

CS313D: ADVANCED PROGRAMMING LANGUAGE

JAVA OPERATORS GENERAL

Operators in java Operator operands.

A variable is a name for a location in memory A variable must be declared

2: Basics of Java Programming

Lecture Set 4: More About Methods and More About Operators

CS111: PROGRAMMING LANGUAGE II

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Program Fundamentals

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

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

Lecture Set 4: More About Methods and More About Operators

Prof. Navrati Saxena TA: Rochak Sachan

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

Chapter 2. Elementary Programming

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Chapter 2: Data and Expressions

Operators and Expressions

1.3b Type Conversion

Declaration and Memory

Chapter 2 Elementary Programming

1.00 Lecture 4. Promotion

SAMPLE QUESTIONS FOR DIPLOMA IN INFORMATION TECHNOLOGY; YEAR 1

Chapter 2: Using Data

Chapter 3: Operators, Expressions and Type Conversion

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

CHAPTER 3 Expressions, Functions, Output

Operators & Expressions

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Java enum, casts, and others (Select portions of Chapters 4 & 5)

Computational Expression

Lecture 3. More About C

Visual C# Instructor s Manual Table of Contents

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

A Java program contains at least one class definition.

3. Java - Language Constructs I

Full file at

Arithmetic Operators. Portability: Printing Numbers

CSC 1214: Object-Oriented Programming

Chapter 2 ELEMENTARY PROGRAMMING

Programming in C++ 5. Integral data types

Chapter 7. Expressions and Assignment Statements ISBN

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

Primitive Data Types: Intro

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 25, Name: KEY A

CSC 1051 Data Structures and Algorithms I

CMPT 125: Lecture 4 Conditionals and Loops

CIS133J. Working with Numbers in Java

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 7, Name:

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

CEN 414 Java Programming

CS110: PROGRAMMING LANGUAGE I

Learning objectives: Objects and Primitive Data. Introduction to Objects. A Predefined Object. The print versus the println Methods

Basic Operations jgrasp debugger Writing Programs & Checkstyle

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

Expressions & Assignment Statements

Introduction to Java Chapters 1 and 2 The Java Language Section 1.1 Data & Expressions Sections

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

Darrell Bethea May 25, 2011

COMP 202 Java in one week

CSC Algorithms and Data Structures I. Midterm Examination February 25, Name:

Loops and Expression Types

Expressions and Casting

COMP 202. Java in one week

Chapter 2 Primitive Data Types and Operations. Objectives

Chapter 3. Selections

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Lesson 02 Data Types and Statements. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Data Representation and Applets

Chapter 4: Basic C Operators

Getting started with Java

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 24, Name: KEY 1

Data Representation and Applets

Lesson 02 Data Types and Statements. MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Objectives of CS 230. Java portability. Why ADTs? 8/18/14

Types and Expressions. Chapter 3

CSC 1051 Data Structures and Algorithms I

Algorithms and Conditionals

Mr. Monroe s Guide to Mastering Java Syntax

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

Values and Variables 1 / 30

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

CSC 1051 Data Structures and Algorithms I

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Chapter 1 Introduction to java

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 26, Name: Key

Transcription:

AYBUKE BUYUKCAYLI KORAY OZUYAR MUSTAFA SOYLU Week 21/02/2007-23/02/2007 Lecture Notes: ASCII 7 bits = 128 characters 8 bits = 256characters Unicode = 16 bits Char Boolean boolean frag; flag = true; flag = false; These are the same İnt a, x; boolean positive; positive = (x>0); if (positive) a = x; else a = -x; a = x int a, x; if ( x>0) a = x; else a = -x; positive false true x -5 3 1

Example java program Draw a flowchart to count the number of even and odd numbers. Stop if input no. <= 0. (Assume input no. >=0) n= input no. noeven= number of even numbers noodd= number of odd numbers continue= means that input is ok. and continue with counting even and odd numbers.(boolean value) Here is the flowchart: 2

Pseudo code: (Here is the pseudo code type of the previous program that counts odd and even numbers) int noeven, noodd; boolean continue, even; noeven=0; noodd=0; read n continue=(n>0) while (continue) { even = (n%2 = = 0) if (even) noeven++ else noodd++ { end if read n continue=(n>0) print noeven, noodd stop 3

Example of Interactive Java Program import java.util.scanner; public class CountEvenOdd { public static void main( String [] args) { int noeven = 0,n; int noodd = 0; boolean continue,even; Scanner scan = new Scanner(System.in); // We make a new object. System.out.print( Enter an integer,to stop you may enter 0 or a negative number: ) ; n = Scan.nextInt( ); cont = (n > 0); while(continue) { even = (n % 2 = = 0); if(even) noeven + +; // (noeven = noeven + 1) else noodd + +; System.out.print( Enter next integer: ); n = Scan.nextInt( ); count = ( n > 0 ); } //end of while System.out.println( noofeven: + noeven + noofodd + noodd); } //end of main method } //end of CountEvenOdd (end of class) 4

OPERATOR PRECEDENCE Operand ( is the variable involved in the expression) operand a = b ; Assign operator binary operator a = b + c; = a + a = -b ; unary operator b c highest precedence Precedence Operator Associates 1 + unary R to L - 2 * / L to R % 3 + } Addition - } Substraction L to R + } String concatenation lowest precedence 4 - } Assignment R to L 5

Example: x = a + (b - c) / d; = * + a / - d b c Example: a = (( b + ( c* d )) (( e / f ) % x )); a = b + c * d e / f % x ; = a - + % b * / * c d e f 6

17 % 4 1 sign is the sign of the numerator - 20 % 3-2 20 % -3 2 10 % -5 0 3 % 8 3 10 / 4 2 10 / 4,0 2,5 ( integer / double) 10,0 / 4 2,5 ( doble / integer) If one of the number is a floating number, the result should be a floating number. INCREMENT AND DECREMENT OPERATORS count ++; (post increment) count--;(post decrement) ++ count; (pre increment) Here are some examples to help you to understand clearly: 1) count = 15; count total 15 0 total = count++; 16 15 Don t forget that we first assign and then increment the value!!! 7

2) count = 15; count total total = 0; 15 0 16 16 total = ++ count; Assignment operators: total = total+1; total + = 5; (these are the same) result = result* (n1 + n2); result * = (n1+n2); Data conversion: In java, conversions can occur in three ways: a) assignment conversion b) promotion c) casting 1) Widening Byte a = 5; Short b = 10; b = a; a = b; ( a < b) widening compile time error 8 (because we can not try assign a big value into a short value)

2) Promotion float f1, f2; int i1; f1 = f1 / i1; will be converted to float during the operation and use like that 3) Casting float Money = 18,53; money dollar int dolar ; dolar = (int) money 18,53 18 type casting ( it changes the type of the variable ) Java program 1.) Application 2.) Applet 9

Java Cordinate System * We have two important declerations: 1.) import.javax.swing.japplet; 2.) import.java.awt.*; public class Einstein extends JApplet { public void paint(graphics page) { Page.drawRect(50,50,40,40); //square Page.drawRect(60,80,225,30); Page.drawOval(75,65,20,20); } } 10

Questions 1) In which case the postfix and prefix forms of the operators (such as total++, ++total) have same results and in which case they don t have the same functions? (Explain the cases by giving specific examples for each one!) 2) What are the main three techniques of data conversions in Java and what is the purpose of this process? 3) What is the main reason of the equivalence in the result; although two different usages of assignment operators? 4) When we are writing an applet,what declerations should we write in the beginning? 5) In public void main (String [ ] args),why do we use void,what does it mean? 6) How many bits does Unicode have? 7) What is the order of operator precedence? When the java associates it R to L or L to R? 8) When we use the remainder % what is the role of it? Can it changes the sign of the numerator every time? 9) In data conversion can we try to assign a big value in to short value? or opposite of it? 10) What is the type casting in java and give an example? 11

Answer to the questions 1) Prefix and postfix operators have same functions when they aren t written as a statement by itself. For instance; total++; ++total; both operators have same function. However; int sum; int total = 5; sum = total++;(this operator assigns the value of total to 6 and then sum is assigned to 6) sum = ++total;(this operator assigns both the total s and sum s value to 6) 2) The three main data conversions in Java are; assignment conversion, promotion and casting. Through widening and narrowing conversions data can be passed without losing data. 3) All of the assignment operators evaluate the expression on the right hand side first, then use the result as right operand of other operation. division/= num1-num2; is equal to division= division/(num1-num2); 4) import.javax.swing.japplet; import.java.awt.*; 5) We use void because it means that it doesn t return anyhing. 6) Unicode has 16 bits. 12

7) highest precedence Precedence Operator Associates 1 + unary R to L - 4 * / L to R % 5 + } Addition - } Substraction L to R + } String concatenation lowest precedence 4 - } Assignment R to L 8) Remainder in java gives remainder of to two dividing number. Remainder can not changes the sign of the numerator. I changes, If and only if the sign is the numerator is negative. Example: - 33 % 5-3 33 % -5 5 9) In data conversion we can assign short value to big value, however, we cannot assign big value to small value. If we try to assign big value to small value the program gives compile time error. 10) Type casting changes the type of the variable by specifying the type in parentheses. It is placed in front of the value to be converted. Example: float Money = 18,53; money dollar int dolar ; dolar = (int) money 18,53 18 13