Course PJL. Arithmetic Operations

Similar documents
Copyright 1999 by Deitel & Associates, Inc. All Rights Reserved.

Chapter 2: Using Data

More about JOptionPane Dialog Boxes

Full file at

Introduction to Java. Nihar Ranjan Roy.

19. GUI Basics. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

Part 1: Introduction. Course Contents. Goals. Books. Difference between conventional and objectoriented

Chapter 2: Using Data

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

Introduction to C# Applications

Chapter 2 ELEMENTARY PROGRAMMING

2.5 Another Application: Adding Integers

Chapter 02: Using Data

CS 106 Introduction to Computer Science I

C: How to Program. Week /Mar/05

CS 106 Introduction to Computer Science I

Object-Oriented Programming in Java

Chapter 2 - Introduction to C Programming

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.

Control Structures (Deitel chapter 4,5)

Handout 3 cs180 - Programming Fundamentals Fall 17 Page 1 of 6. Handout 3. Strings and String Class. Input/Output with JOptionPane.

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. A Guide to this Instructor s Manual:

Chapter 2 Primitive Data Types and Operations

א א א E2-1F Welcome to Java Programming!

In this lab, you will be given the implementation of the classes GeometricObject, Circle, and Rectangle, as shown in the following UML class diagram.

2.8. Decision Making: Equality and Relational Operators

JOptionPane Dialogs. javax.swing.joptionpane is a class for creating dialog boxes. Has both static methods and instance methods for dialogs.

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

Programming for Engineers Introduction to C

Chapter 3 - Introduction to Java Applets

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

TTh 9.25 AM AM Strain 322

Arithmetic Operators. Binary Arithmetic Operators. Arithmetic Operators. A Closer Look at the / Operator. A Closer Look at the % Operator

CS313D: ADVANCED PROGRAMMING LANGUAGE

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Lecture 3 Tao Wang 1

MODULE 02: BASIC COMPUTATION IN JAVA

IT 374 C# and Applications/ IT695 C# Data Structures

Key Concept: all programs can be broken down to a combination of one of the six instructions Assignment Statements can create variables to represent

CIS 110: Introduction to Computer Programming

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Introduction to Java Applications; Input/Output and Operators

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

Course Outline. Introduction to java

Chapter 2 Elementary Programming

JAVA Programming Concepts

Full file at

Lesson 3: Accepting User Input and Using Different Methods for Output

Introduction to Java Applications

CST141 Thinking in Objects Page 1

Chapter 2: Java Fundamentals

8/23/2014. Chapter Topics. Chapter Topics (2) Parts of a Java Program. Parts of a Java Program. Analyzing The Example. Chapter 2: Java Fundamentals

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

Programming: Java. Chapter Objectives. Control Structures. Chapter 4: Control Structures I. Program Design Including Data Structures

Chapter 2 Primitive Data Types and Operations

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

Introduction to Classes and Objects Pearson Education, Inc. All rights reserved.

Chapter 1 & 2 Introduction to C Language

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Basic Operations jgrasp debugger Writing Programs & Checkstyle

Numerical Data. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

On a 64-bit CPU. Size/Range vary by CPU model and Word size.

Unit 3. Operators. School of Science and Technology INTRODUCTION

Introduction to Scientific Programming Using Java

Introduction to Classes and Objects Pearson Education, Inc. All rights reserved.

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

Introduction to Programming

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

4 WORKING WITH DATA TYPES AND OPERATIONS

Information Science 1

CT 229 Fundamentals of Java Syntax

Introduction to Java Applications

Information Science 1

COMP 110 Introduction to Programming. What did we discuss?

1.8 Intro to Variables, Algebraic Expressions, and Equations

Visual C# Instructor s Manual Table of Contents

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

Building Java Programs

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

Topic 4 Expressions and variables

Introduction to Programming Using Java (98-388)

Chapter 2 Primitive Data Types and Operations. Objectives

Program Elements -- Introduction

XQ: An XML Query Language Language Reference Manual

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

by Pearson Education, Inc. All Rights Reserved. 2

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Language Reference Manual

COSC 243. Data Representation 3. Lecture 3 - Data Representation 3 1. COSC 243 (Computer Architecture)

Chapter 2 Elementary Programming

Computer Programming CS F111

CS 106 Introduction to Computer Science I

Computational Expression

JAVA OPERATORS GENERAL

Chapter 2: Data and Expressions

Ex: If you use a program to record sales, you will want to remember data:

Transcription:

Outline Oman College of Management and Technology Course 503200 PJL Handout 5 Arithmetic Operations CS/MIS Department

1 // Fig. 2.9: Addition.java 2 // Addition program that displays the sum of two numbers. 3 4 // Java packages 5 import javax.swing.joptionpane; // program uses JOptionPane 6 7 public class Addition { 8 9 // main method begins execution Declare of Java variables: application name and type. 10 public static void main( String args[] ) 11 { 12 String firstnumber; // first string entered by user 13 String secondnumber; // second string entered by user 14 15 int number1; // first number to add 16 int number2; // second Input number first integer to add as a String, assign 17 int sum; // sum to of firstnumber. number1 and number2 18 19 // read in first number from user as a String 20 firstnumber = JOptionPane.showInputDialog( "Enter first integer" ); 21 22 // read in second number from user as a String 23 secondnumber = 24 JOptionPane.showInputDialog( "Enter second integer" ); 25 26 // convert numbers from type String Add, to type place int result in sum. 27 number1 = Integer.parseInt( firstnumber ); 28 number2 = Integer.parseInt( secondnumber ); 29 30 // add numbers 31 sum = number1 + number2; 32 Outline Addition.java 1. import Convert strings to integers. 2. class Addition 2.1 Declare variables (name and type) 3. showinputdialog 4. parseint 5. Add numbers, put result in sum

33 // display result 34 JOptionPane.showMessageDialog( null, "The sum is " + sum, 35 "Results", JOptionPane.PLAIN_MESSAGE ); 36 37 System.exit( 0 ); // terminate application with window 38 39 } // end method main 40 41 } // end class Addition Outline Program output

Another Java Application: Adding Integers 5 import javax.swing.joptionpane; // program uses JOptionPane Location of JOptionPane for use in the program 7 public class Addition { Begins public class Addition Recall that file name must be Addition.java Lines 10-11: main 12 String firstnumber; // first string entered by user 13 String secondnumber; // second string entered by user Declaration firstnumber and secondnumber are variables

Variables Another Java Application: Adding Integers 12 String firstnumber; // first string entered by user 13 String secondnumber; // second string entered by user Location in memory that stores a value Declare with name and type before use firstnumber and secondnumber are of type String (package java.lang) Hold strings Variable name: any valid identifier Declarations end with semicolons ; String firstnumber, secondnumber; Can declare multiple variables of the same type at a time Use comma separated list Can add comments to describe purpose of variables

Another Java Application: Adding Integers 15 int number1; // first number to add 16 int number2; // second number to add 17 int sum; // sum of number1 and number2 Declares variables number1, number2, and sum of type int int holds integer values (whole numbers): i.e., 0, -4, 97 Types float and double can hold decimal numbers Type char can hold a single character: i.e., x, $, \n, 7 Primitive types - more in Chapter 4

Another Java Application: Adding Integers 20 firstnumber = JOptionPane.showInputDialog( "Enter first integer" ); Reads String from the user, representing the first number to be added Method JOptionPane.showInputDialog displays the following: Message called a prompt - directs user to perform an action Argument appears as prompt text If wrong type of data entered (non-integer) or click Cancel, error occurs

Another Java Application: Adding Integers 20 firstnumber = JOptionPane.showInputDialog( "Enter first integer" ); Result of call to showinputdialog given to firstnumber using assignment operator = Assignment statement = binary operator - takes two operands Expression on right evaluated and assigned to variable on left Read as: firstnumber gets value of JOptionPane.showInputDialog( "Enter first integer" )

Another Java Application: Adding Integers 23 secondnumber = 24 JOptionPane.showInputDialog( "Enter second integer" ); Similar to previous statement Assigns variable secondnumber to second integer input 27 number1 = Integer.parseInt( firstnumber ); 28 number2 = Integer.parseInt( secondnumber ); Method Integer.parseInt Converts String argument into an integer (type int) Class Integer in java.lang Integer returned by Integer.parseInt is assigned to variable number1 (line 27) Remember that number1 was declared as type int Line 28 similar

Another Java Application: Adding Integers 31 sum = number1 + number2; Assignment statement Calculates sum of number1 and number2 (right hand side) Uses assignment operator = to assign result to variable sum Read as: sum gets the value of number1 + number2 number1 and number2 are operands

Another Java Application: Adding Integers 34 JOptionPane.showMessageDialog( null, "The sum is " + sum, 35 "Results", JOptionPane.PLAIN_MESSAGE ); Use showmessagedialog to display results "Thesumis"+sum Uses the operator + to "add" the string literal "The sum is" and sum Concatenation of a String and another type Results in a new string If sum contains 117, then "Thesumis"+sum results in the new string "The sum is 117" Note the space in "Thesumis" More on strings in Chapter 11

Another Java Application: Adding Integers 34 JOptionPane.showMessageDialog( null, "The sum is " + sum, 35 "Results", JOptionPane.PLAIN_MESSAGE ); Different version of showmessagedialog Requires four arguments (instead of two as before) First argument: null for now Second: string to display Third: string in title bar Fourth: type of message dialog with icon Line 35 no icon: JOptionPane.PLAIN_MESSAGE

Another Java Application: Adding Integers Message dialog type Icon Description JOptionPane.ERROR_MESSAGE JOptionPane.INFORMATION_MESSAGE JOptionPane.WARNING_MESSAGE JOptionPane.QUESTION_MESSAGE Displays a dialog that indicates an error to the user. Displays a dialog with an informational message to the user. The user can simply dismiss the dialog. Displays a dialog that warns the user of a potential problem. Displays a dialog that poses a question to the user. This dialog normally requires a response, such as clicking on a Yes or a No button. JOptionPane.PLAIN_MESSAGE no icon Displays a dialog that simply contains a message, with no icon. Fig. 2.12 JOptionPane constants for message dialogs.

Variables Memory Concepts Every variable has a name, a type, a size and a value Name corresponds to location in memory When new value is placed into a variable, replaces (and destroys) previous value Reading variables from memory does not change them

Visual Representation Memory Concepts Sum = 0; number1 = 1; number2 = 2; sum 0 Sum = number1 + number2; after execution of statement sum 3

Arithmetic Arithmetic calculations used in most programs Usage * for multiplication / for division +, - No operator for exponentiation (more in Chapter 5) Integer division truncates remainder 7 / 5 evaluates to 1 Remainder operator % returns the remainder 7 % 5 evaluates to 2

Operator precedence Arithmetic Some arithmetic operators act before others (i.e., multiplication before addition) Use parenthesis when needed Example: Find the average of three variables a, b and c Do not use: a + b + c / 3 Use: ( a + b + c ) / 3 Follows PEMDAS Parentheses, Exponents, Multiplication, Division, Addition, Subtraction

Arithmetic Operator(s) * / + - Fig. 2.17 Operation(s) Order of evaluation (precedence) Multiplication Evaluated first. If there are several of this type Division of operator, they are evaluated from left to Remainder right. Addition Subtraction Precedence of arithmetic operators. Evaluated next. If there are several of this type of operator, they are evaluated from left to right.

Decision Making: Equality and Relational Operators if control statement Simple version in this section, more detail later If a condition is true, then the body of the if statement executed 0 interpreted as false, non-zero is true Control always resumes after the if structure Conditions for if statements can be formed using equality or relational operators (next slide) if ( condition ) statement executed if condition true No semicolon needed after condition Else conditional task not performed

Decision Making: Equality and Relational Operators Standard algebraic equality or relational operator Java equality or relational operator Example of Java condition Meaning of Java condition Equality operators = == x == y x is equal to y!= x!= y x is not equal to y Relational 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 Fig. 2.19 Equality and relational operators.

1 // Fig. 2.20: Comparison.java 2 // Compare integers using if statements, relational operators 3 // and equality operators. 4 5 // Java packages 6 import javax.swing.joptionpane; 7 8 public class Comparison { 9 10 // main method begins execution of Java application 11 public static void main( String args[] ) 12 { 13 String firstnumber; // first string entered by user 14 String secondnumber; // second string entered by user 15 String result; // a string containing the output 16 17 int number1; // first number to compare 18 int number2; // second number to compare 19 20 // read first number from user as a string 21 firstnumber = JOptionPane.showInputDialog( "Enter first integer:" ); 22 23 // read second number from user as a string 24 secondnumber = 25 JOptionPane.showInputDialog( "Enter second integer:" ); 26 27 // convert numbers from type String to type int 28 number1 = Integer.parseInt( firstnumber ); 29 number2 = Integer.parseInt( secondnumber ); 30 31 // initialize result to empty String 32 result = ""; 33 Outline Comparison.java 1. import 2. Class Comparison 2.1 main 2.2 Declarations 2.3 Input data (showinputdialo g) 2.4 parseint 2.5 Initialize result

34 if ( number1 == number2 ) 35 result = result + number1 + " == " + number2; 36 37 if ( number1!= number2 ) 38 result = result + number1 + "!= " + number2; 39 40 if ( number1 < number2 ) 41 result = result + "\n" + number1 + " < " + number2; 42 43 if ( number1 > number2 ) 44 result = result + "\n" + number1 + " > " + number2; 45 46 if ( number1 <= number2 ) 47 result = result + "\n" + number1 + " <= " + number2; 48 49 if ( number1 >= number2 ) 50 result = result + "\n" + number1 + " >= " + number2; 51 52 // Display results 53 JOptionPane.showMessageDialog( null, result, "Comparison Results", 54 JOptionPane.INFORMATION_MESSAGE ); 55 56 System.exit( 0 ); // terminate application 57 58 } // end method main 59 60 } // end class Comparison Outline Test for equality, create Comparison.java new string, assign to result. Notice use of JOptionPane.INFORMATION_MESSAGE 3. if statements 4. showmessagedialo g

Outline Program Output

Decision Making: Equality and Relational Operators Lines 1-12: Comments, import JOptionPane, begin class Comparison and main Lines 13-18: declare variables Can use comma-separated lists instead: 13 String firstnumber, 14 secondnumber, 15 result; Lines 21-30: obtain user-input numbers and parses input string into integer variables

Decision Making: Equality and Relational Operators 32 result = ""; Initialize result with empty string 34 if ( number1 == number2 ) 35 result = result + number1 + " == " + number2; if statement to test for equality using (==) If variables equal (condition true) result concatenated using + operator result = result + other strings Right side evaluated first, new string assigned to result If variables not equal, statement skipped

Decision Making: Equality and Relational Operators Lines 37-50: other if statements testing for less than, more than, etc. If number1 = 123 and number2 = 123 Line 34 evaluates true (if number1 = = number 2) Because number1 equals number2 Line 40 evaluates false (if number1 < number 2) Because number1 is not less than number2 Line 49 evaluates true (if number1 >= number2) Because number1 is greater than or equal to number2 Lines 53-54: result displayed in a dialog box using showmessagedialog

Decision Making: Equality and Relational Operators Precedence of operators All operators except for = (assignment) associates from left to right For example: x = y = z is evaluated x = (y = z) Operators Associativity Type * / % left to right multiplicative + - left to right additive < <= > >= left to right relational ==!= left to right equality = right to left assignment Fig. 2.21 Precedence and associativity of the operators discussed so far.