9/6: variable types, arithmetic operators, comparison operators addition.java in depth variable...

23
9/6: Variable Types, Arithmetic Operators, Comparison Operators • Addition.java in depth • Variable types & data types • Input from user: how to get it • Arithmetic operators

Upload: dale-shepherd

Post on 03-Jan-2016

245 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: 9/6: Variable Types, Arithmetic Operators, Comparison Operators Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic

9/6: Variable Types, Arithmetic Operators, Comparison Operators

• Addition.java in depth

• Variable types & data types

• Input from user: how to get it

• Arithmetic operators

Page 2: 9/6: Variable Types, Arithmetic Operators, Comparison Operators Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic

Addition.javaimport javax.swing.JOptionPane;

public class Addition { public static void main ( String args[] ) { String firstNumber, secondNumber; int number1, number2, sum;

firstNumber = JOptionPane.showInputDialog ( "Enter a number" ); secondNumber = JOptionPane.showInputDialog ( "And another" );

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

sum = number1 + number2;

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

System.exit ( 0 ); }}

import statementclassheadermethodheader

declaring variables: Strings& ints

Page 3: 9/6: Variable Types, Arithmetic Operators, Comparison Operators Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic

Variables & Data Types• String – a series of characters.

– EX: Ann , 1450 , var30 , g , YES– to declare a String variable, put the variable type

String before the name of the variable. String firstNumber ;

– to declare more than one String variable at the same time, separate the variable names with commas.

String firstNumber , secondNumber ;

• A declaration is a statement – must end with a semicolon.

Page 4: 9/6: Variable Types, Arithmetic Operators, Comparison Operators Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic

Variables & Data Types

• int – an integer-type number.– EX: 45 , -1001 , 3 , 58692– to declare an int variable, put the variable type int

before the name of the variable.int number1 ;

– to declare more than one int variable at the same time, separate the variable names with commas.

int number1 , number2 ;

– other number formats: float , double , long , short

Page 5: 9/6: Variable Types, Arithmetic Operators, Comparison Operators Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic

Addition.javaimport javax.swing.JOptionPane;

public class Addition { public static void main ( String args[] ) { String firstNumber, secondNumber; int number1, number2, sum;

firstNumber = JOptionPane.showInputDialog ( "Enter a number" );

secondNumber = JOptionPane.showInputDialog ( "And another" );

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

sum = number1 + number2;

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

System.exit ( 0 ); }}

initializing firstNumber &secondNumber

Page 6: 9/6: Variable Types, Arithmetic Operators, Comparison Operators Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic

Inputs: How we did it.

• We initialized (gave an initial value to) firstNumber & secondNumber by the lines

firstNumber = JOptionPane.showInputDialog ( "Enter a number" );secondNumber = JOptionPane.showInputDialog ( "And another" );

• JOptionPane.showInputDialog panes accept String type inputs. Even if it looks like a number, Java sees it as a String.

Page 7: 9/6: Variable Types, Arithmetic Operators, Comparison Operators Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic

Addition.javaimport javax.swing.JOptionPane;

public class Addition { public static void main ( String args[] ) { String firstNumber, secondNumber; int number1, number2, sum;

firstNumber = JOptionPane.showInputDialog ( "Enter a number" );

secondNumber = JOptionPane.showInputDialog ( "And another" );

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

sum = number1 + number2;

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

System.exit ( 0 ); }}

initializing number1 &number2

Page 8: 9/6: Variable Types, Arithmetic Operators, Comparison Operators Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic

Inputs: How we did it.

• We then initialized (gave an initial value to) number1 & number2 by the lines

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

• These lines convert the String values of firstNumber and secondNumber into int values and store them as number1 and number2.

Page 9: 9/6: Variable Types, Arithmetic Operators, Comparison Operators Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic

Addition.javaimport javax.swing.JOptionPane;

public class Addition { public static void main ( String args[] ) { String firstNumber, secondNumber; int number1, number2, sum;

firstNumber = JOptionPane.showInputDialog ( "Enter a number" );

secondNumber = JOptionPane.showInputDialog ( "And another" );

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

sum = number1 + number2;

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

System.exit ( 0 ); }}

initializing sum

Page 10: 9/6: Variable Types, Arithmetic Operators, Comparison Operators Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic

Arithmetic Operators

Page 11: 9/6: Variable Types, Arithmetic Operators, Comparison Operators Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic

Order of Operation• Just like algebra

• inside parentheses first

• multiplication, division, & modulus next

• addition & subtraction last

• left to right

• EX: 2 * 4 + 3 % 2 - (4 / 2 + 5 ) = ?

Page 12: 9/6: Variable Types, Arithmetic Operators, Comparison Operators Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic

Order of Operation Example

• 2 * 4 + 3 % 2 - (4 / 2 + 5 ) = ?

• 2 * 4 + 3 % 2 - ( 2 + 5 ) = ?

• 2 * 4 + 3 % 2 - ( 2 + 5 ) = ?

• 2 * 4 + 3 % 2 - ( 7 ) = ?

• 2 * 4 + 3 % 2 - ( 7 ) = ?

• 8 + 3 % 2 - ( 7 ) = ?

• 8 + 3 % 2 - ( 7 ) = ?

• 8 + 1 - ( 7 ) = ?

• 8 + 1 - 7 = 2

Page 13: 9/6: Variable Types, Arithmetic Operators, Comparison Operators Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic

1st Program of the Day: pg. 58

• Pg. 58: Comparison.java– Pay attention to the comparison operators (<, >=, etc.)

• Second Half: – Comparison.java in depth: – the if structure– comparison operators– assigning new values to an old variable.

Page 14: 9/6: Variable Types, Arithmetic Operators, Comparison Operators Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic

Part 2: Comparison Operators

• Comparison.java

• Using the if structure

• Equality operators

• Relational operators

Page 15: 9/6: Variable Types, Arithmetic Operators, Comparison Operators Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic

Comparison.javaimport javax.swing.JOptionPane;

public class Comparison { public static void main( String args[] ) { String firstNumber, secondNumber, result; int number1, number2;

firstNumber = JOptionPane.showInputDialog( "Enter first int.:" ); secondNumber = JOptionPane.showInputDialog( "Enter 2nd int:"

);

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

result = "";

import statement

classheadermethodheader

declaring variables: Strings& intsinitializing firstNumber &secondNumber

initializing number1 &number2

initializing result

Page 16: 9/6: Variable Types, Arithmetic Operators, Comparison Operators Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic

Comparison.java (pt. 2) if ( number1 == number2 ) result = number1 + " == " + number2;

if ( number1 != number2 ) result = number1 + " != " + number2;

if ( number1 < number2 ) result = result + "\n" + number1 + " < " + number2;

if ( number1 > number2 ) result = result + "\n" + number1 + " > " + number2;

if ( number1 <= number2 ) result = result + "\n" + number1 + " <= " + number2;

if ( number1 >= number2 ) result = result + "\n" + number1 + " >= " + number2;

“If number1 is equal tonumber2, then set result to be number1 plus a double equal signplus number2.”

Page 17: 9/6: Variable Types, Arithmetic Operators, Comparison Operators Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic

The if structure

Use the if structure to have statements execute ONLY under certain conditions.

if ( condition )rest of statement ;

Note that the if ( condition ) is not a statement by itself ( just like English ).

The spacing and line structure is good practice.

Page 18: 9/6: Variable Types, Arithmetic Operators, Comparison Operators Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic

Comparison.java (pt. 2) if ( number1 == number2 ) result = number1 + " == " + number2;

if ( number1 != number2 ) result = number1 + " != " + number2;

if ( number1 < number2 ) result = result + "\n" + number1 + " < " + number2;

if ( number1 > number2 ) result = result + "\n" + number1 + " > " + number2;

if ( number1 <= number2 ) result = result + "\n" + number1 + " <= " + number2;

if ( number1 >= number2 ) result = result + "\n" + number1 + " >= " + number2;

“If number1 is less than number2, set result to be itself plus a new line escape sequence plus number1 plus a ‘less than’ sign plus number2.”

Page 19: 9/6: Variable Types, Arithmetic Operators, Comparison Operators Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic

Equality & Relational Operators

• To compare values.

• Equality operators: Equal to, not equal to

• Relational operators: Greater than, less than

Page 20: 9/6: Variable Types, Arithmetic Operators, Comparison Operators Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic

Equality Operators

Englishphrase

AlgebraicEqualityOperator

Javaequalityoperator

Example ofJava condition

x is equalto y

= == x == y

x is notequal to y

= != x != y

Page 21: 9/6: Variable Types, Arithmetic Operators, Comparison Operators Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic

Relational Operators

Englishphrase

AlgebraicRelationalOperator

Javarelationaloperator

Example ofJava condition

x is greaterthan y

> > x > y

x is lessthan y

< < x < y

x is greateror equal to

y

> >= x >= y

x is lessthan or

equal to y

< <= x <= y

Page 22: 9/6: Variable Types, Arithmetic Operators, Comparison Operators Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic

Comparison.java (pt. 3)

JOptionPane.showMessageDialog( null, result, "Comparison Results", JOptionPane.INFORMATION_MESSAGE );

System.exit( 0 ); }}

result, whatever it is after executing the if statements, is put in the main body of the MessageDialog box.

Page 23: 9/6: Variable Types, Arithmetic Operators, Comparison Operators Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic

Program of the Day

• Problem 2.17 (pg. 73): write a program

• Work in pairs

• Use Comparison.java as a help/template in creating the program