32
Mathematical Operators 2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in JAVA: V22.0002

Mathematical Operators 2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in

  • View
    221

  • Download
    3

Embed Size (px)

Citation preview

Page 1: Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in

Mathematical Operators

2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.

Introduction to Computers and Programming in JAVA: V22.0002

Page 2: Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in

Basic Mathematical Operators

Page 3: Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in

Basic Mathematical Operators

Each of the operators in the table are binary operators. A binary operator acts on two operands

Operation Operator Algebraic Expression

Java Expression

Addition + x + 7 x + 7

Subtraction - x - 7 x - 7

Multiplication * 7x x * 7

Division / x / 7 x / 7

Modulus % x mod 7 x % 7

Page 4: Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in

4

2000 Prentice Hall, Inc. Modified for use with this course. All rights reserved.

Lets look at a program toCalculate the area of a circle

// Program calculates area of a circle (uses double data types)

public class circle_area { public static void main(String[] args) {

double radius, area; // declare variables doubleradius = 3.00; // assign radius of the circle

area = radius * radius * 3.14159 ;

System.out.println("The area of the circle of radius " + radius + " is " + area);

System.exit(0);

}}

Page 5: Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in

5

2000 Prentice Hall, Inc. Modified for use with this course. All rights reserved.

Same program with defining a constant final datatype constant_name value;

public class circle_area_pi { public static void main(String[] args) {

final double PI = 3.14159; // declare variables double radius, area;

// assign radius of the circle radius = 3.00;

area = radius * radius * PI ; System.out.println("The area of the circle of radius " + radius + " is " + area); System.exit(0); }}

Page 6: Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in

Integer Division - Solution

• To understand the solution, you need to remember your 3rd Grade Math (really.)

• 7/4 = 1 (Integer Division)• 7%4 = 3 (Modulus Division)

4 74

3

1

The answer: 1 remainder 3

Page 7: Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in

Example: Integer and Modulus Division

/* Integer and Modulus Division */

public class mod_division

{

public static void main(String args[])

{

int x = 5, y =10;

System.out.println ("5/10: " + x/y);

System.out.println ("5%10: " + x%y);

} // end method main

} // end class mod_division

5/10: 05%10: 5

• No matter what, your answers must be integers.

5/10 = 0

5%10 = 5

5100

0

5

Page 8: Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in

Odd / Even Numbers

• Modulus division can also be used to determine whether a number is odd or even.

• Just divide by 2. – If the remainder (modulus) is 0, the number is even.

• Examples:– 10 % 2 = 0. Hence 10 is even.

– 11 % 2 = 1. Hence 11 is odd.

Common Programming Error: Dividing by zero is normally undefined on computer systems generally results in a fatal error.

Page 9: Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in

To find out if a number evenly divides by another number

• Modulus division can also be used to determine whether a number divides evenly into another number or not.

• If the remainder (modulus) is 0, the number evenly divide.

• Examples:– 10 % 2 = 0. Hence 10 is evenly divide into 2 .– 11 % 2 = 1. Hence 11 is does not divide evenly into 1.– 30 % 5 = 0. Hence 30 evenly divide into 5.– 100 % 8 = 5. ( 100 / 8 = 12.5 )

» Hence 100 does not evenly divide into 8.

Page 10: Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in

Do you see a pattern?

1234 / 1000 = ?

1234 % 1000 =?

234 / 100= ?

234 % 100= ?

34 / 10= ?

34 % 10=?

Page 11: Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in

What is the pattern?1234 / 1000 = 1

( lost three right digits and ended up with the left (first) digit)1234 % 1000 = 234

(lost the left most digit and ended up with the remaining 3 digits)234 / 100= 2

( lost the 2 right digits and ended up with the left (first) digit)

234 % 100= 34 (lost the left most digit and ended up with the remaining 2 digits)

34 / 10= 3 ( lost right digit and ended up with the left (first) digit)

34 % 10= 4 (lost the left most digit and ended up with the remaining right digit)

Page 12: Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in

12

2000 Prentice Hall, Inc. Modified for use with this course. All rights reserved.

What is the out put of this program?

public class date_digits{

public static void main(String[] args){

int date, month, day, div;date = 1213; month = date/100; day = date % 100;

System.out.println("month is " +month +"\n" + "day is " + day + "\n” );

}}

Page 13: Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in

13

2000 Prentice Hall, Inc. Modified for use with this course. All rights reserved.

What is the out put of this program?

public class date_digits{

public static void main(String[] args){

int date, month, day, div;date = 1213; month = date/100; day = date % 100;System.out.println("month is " +month +"\n" + "day is " + day + "\n");

}}

month is 12

day is 13

Press any key to continue...

Page 14: Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in

Operator Precedence

Page 15: Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in

Operator Precedence

• Operator precedence represent rules for evaluating mathematical expressions.

• Every programming language has similar rules.

Operator(s) Operation(s) Order of evaluation (precedence)

() Parentheses Evaluated first. If the parentheses are nested, the expression in the innermost pair is evaluated first. If there are several pairs of parentheses “on the same level” (i.e., not nested), they are evaluated left to right.

*, /, or % Multiplication,Division, Modulus

Evaluated second. If there are several, they are evaluated left to right.

+ or - Addition Subtraction

Evaluated last. If there are several, they are evaluated left to right.

Page 16: Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in

Operator Precedence

• Hence, option #2 is always correct – (multiplication is performed first):

• Example: • Find the average of three variables a, b and c

• Do not use: a + b + c / 3

• Use: (a + b + c ) / 3

x = 7 + 3 * 6;Evaluates to x = 7 + 18 = 25

Page 17: Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in

17

2000 Prentice Hall, Inc. Modified for use with this course. All rights reserved.

Lets look at a program to allow user to input data

using an input text prompt window

Page 18: Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in

2003 Prentice Hall, Inc. All rights reserved (Modified) .

18

Getting Input from Input Dialog Boxes

String string = JOptionPane.showInputDialog(

null, “Prompt Message”,

“Dialog Title”,

JOptionPane.QUESTION_MESSAGE));

Page 19: Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in

2003 Prentice Hall, Inc. All rights reserved (Modified) .

19

Converting Strings to Integers

• The input returned from the input dialog box is a string.

• If you enter a numeric value such as 123, it returns “123”.

• To obtain the input as a number, you have to convert a string into a number.

 

• To convert a string into an int value, you can use the static parseInt method in the Integer class as follows:

 

int intValue = Integer.parseInt(intString);

 

• Where intString is a numeric string such as “123”.

Page 20: Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in

2003 Prentice Hall, Inc. All rights reserved (Modified) .

20

Converting Strings to Doubles

•To convert a string into a double value, you can use the static parseDouble method in the Double class as follows:

 

double doubleValue =Double.parseDouble(doubleString);

 

where doubleString is a numeric string such as “123.45”.

Page 21: Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in

2003 Prentice Hall, Inc.All rights reserved.

Outline21

Addition2.java

1. import

2. class Addition

2.1 Declare variables (name and type)

3. showInputDialog

4. parseInt

5. Add numbers, put result in sum

1 // Addition2.java uses JOptionPane prompt window to enter data2 // Addition program that displays the sum of two numbers.3 4 // Java packages 5 import javax.swing.JOptionPane; // program uses JOptionPane6 7 public class Addition2 {8 9 // main method begins execution of Java application10 public static void main( String args[] )11 {12 String firstNumber; // first string entered by user 13 String secondNumber; // second string entered by user14 15 int number1; // first number to add 16 int number2; // second number to add 17 int sum; // sum of number1 and number218 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 to type int27 number1 = Integer.parseInt( firstNumber ); 28 number2 = Integer.parseInt( secondNumber ); 29 30 // add numbers 31 sum = number1 + number2;32

Declare variables: name and type.

Input first integer as a String, assign to firstNumber.

Add, place result in sum.

Convert strings to integers.

Page 22: Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in

2003 Prentice Hall, Inc.All rights reserved.

Outline22

Program output

33 // display result 34 JOptionPane.showMessageDialog( null, "The sum is " + sum,35 "Results", JOptionPane.PLAIN_MESSAGE ); 36 37 System.exit( 0 ); // terminate application with window38 39 } // end method main40 41 } // end class Addition2

Page 23: Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in

2003 Prentice Hall, Inc. All rights reserved (Modified) .

23

Adding Integers

– Location of JOptionPane for use in the program

– Begins public class Addition• Recall that file name must be Addition.java

– Lines 10-11: main

– Declaration• firstNumber and secondNumber are variables

5 import javax.swing.JOptionPane; // program uses JOptionPane

7 public class Addition {

12 String firstNumber; // first string entered by user 13 String secondNumber; // second string entered by user

Page 24: Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in

2003 Prentice Hall, Inc. All rights reserved (Modified) .

24

Adding Integers

– Variables• 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 ;

– Can declare multiple variables of the same type at a time

– Use comma separated list

– Can add comments to describe purpose of variables

String firstNumber, secondNumber;

12 String firstNumber; // first string entered by user 13 String secondNumber; // second string entered by user

Page 25: Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in

2003 Prentice Hall, Inc. All rights reserved (Modified) .

25

Adding Integers

– 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

15 int number1; // first number to add 16 int number2; // second number to add 17 int sum; // sum of number1 and number2

Page 26: Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in

2003 Prentice Hall, Inc. All rights reserved (Modified) .

26

Adding Integers

– 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

20 firstNumber = JOptionPane.showInputDialog( "Enter first integer" );

Page 27: Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in

2003 Prentice Hall, Inc. All rights reserved (Modified) .

27

Adding Integers

– 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" )

20 firstNumber = JOptionPane.showInputDialog( "Enter first integer" );

Page 28: Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in

2003 Prentice Hall, Inc. All rights reserved (Modified) .

28

Adding Integers

– Similar to previous statement• Assigns variable secondNumber to second integer input

– 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

23 secondNumber = 24 JOptionPane.showInputDialog( "Enter second integer" );

27 number1 = Integer.parseInt( firstNumber ); 28 number2 = Integer.parseInt( secondNumber );

Page 29: Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in

2003 Prentice Hall, Inc. All rights reserved (Modified) .

29

Adding Integers

– 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

31 sum = number1 + number2;

Page 30: Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in

2003 Prentice Hall, Inc. All rights reserved (Modified) .

30

Adding Integers

– Use showMessageDialog to display results

– "The sum is " + 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 "The sum is " + sum results in the new string "The sum is 117"

• Note the space in "The sum is "• More on strings in Chapter 11

34 JOptionPane.showMessageDialog( null, "The sum is " + sum,35 "Results", JOptionPane.PLAIN_MESSAGE );

Page 31: Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in

2003 Prentice Hall, Inc. All rights reserved (Modified) .

31

Adding Integers

– 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

34 JOptionPane.showMessageDialog( null, "The sum is " + sum,35 "Results", JOptionPane.PLAIN_MESSAGE );

Page 32: Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in

2003 Prentice Hall, Inc. All rights reserved (Modified) .

32

Adding Integers

Message dialog type Icon Description

JOptionPane.ERROR_MESSAGE

Displays a dialog that indicates an error to the user.

JOptionPane.INFORMATION_MESSAGE

Displays a dialog with an informational message to the user. The user can simply dismiss the dialog.

JOptionPane.WARNING_MESSAGE

Displays a dialog that warns the user of a potential problem.

JOptionPane.QUESTION_MESSAGE

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.