Presentation is loading. Please wait.

Presentation is loading. Please wait.

1/28: Inputs, Variable Types, etc. Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic operators.

Similar presentations


Presentation on theme: "1/28: Inputs, Variable Types, etc. Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic operators."— Presentation transcript:

1 1/28: Inputs, Variable Types, etc. Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic operators

2 Addition.java //Fig. 2.9: Addition.java //An addition program //Java extension packages import javax.swing.JOptionPane; //import class JOptionPane public class Addition { //main method begins execution of Java application public static void main ( String args[] ) { String firstNumber; // first String entered by user String secondNumber;// second String entered by user int number1;// first number to add int number 2;// second number to add int sum;// sum of number1 & number2 //read in first number from user as a String firstNumber = JOptionPane.showInputDialog ( "Enter first integer" ); import statement class header method header declaring variables: Strings & ints

3 Addition.java //read in second number from user as a String secondNumber = JOptionPane.showInputDialog ( “Enter second integer" ); //convert numbers from type String to type int number1 = Integer.parseInt ( firstNumber ); number2 = Integer.parseInt ( secondNumber ); //add the numbers, assign the value to sum sum = number1 + number2; //display the results JOptionPane.showMessageDialog ( null, “The sum is " + sum, "Results", JOptionPane.PLAIN_MESSAGE ); System.exit ( 0 ); //terminate the application if no errors } //end method main } //end class Addition

4 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.

5 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

6 Addition.java //Fig. 2.9: Addition.java //An addition program //Java extension packages import javax.swing.JOptionPane; //import class JOptionPane public class Addition { //main method begins execution of Java application public static void main ( String args[] ) { String firstNumber; // first String entered by user String secondNumber;// second String entered by user int number1;// first number to add int number 2;// second number to add int sum;// sum of number1 & number2 //read in first number from user as a String firstNumber = JOptionPane.showInputDialog ( "Enter first integer" ); initializing firstNumber

7 Addition.java //read in second number from user as a String secondNumber = JOptionPane.showInputDialog ( “Enter second integer" ); //convert numbers from type String to type int number1 = Integer.parseInt ( firstNumber ); number2 = Integer.parseInt ( secondNumber ); //add the numbers, assign the value to sum sum = number1 + number2; //display the results JOptionPane.showMessageDialog ( null, “The sum is " + sum, "Results", JOptionPane.PLAIN_MESSAGE ); System.exit ( 0 ); //terminate the application if no errors } //end method main } //end class Addition initializing secondNumber

8 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.

9 Addition.java //read in second number from user as a String secondNumber = JOptionPane.showInputDialog ( “Enter second integer" ); //convert numbers from type String to type int number1 = Integer.parseInt ( firstNumber ); number2 = Integer.parseInt ( secondNumber ); //add the numbers, assign the value to sum sum = number1 + number2; //display the results JOptionPane.showMessageDialog ( null, “The sum is " + sum, "Results", JOptionPane.PLAIN_MESSAGE ); System.exit ( 0 ); //terminate the application if no errors } //end method main } //end class Addition initializing number1 & number2

10 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.

11 Addition.java //read in second number from user as a String secondNumber = JOptionPane.showInputDialog ( “Enter second integer" ); //convert numbers from type String to type int number1 = Integer.parseInt ( firstNumber ); number2 = Integer.parseInt ( secondNumber ); //add the numbers, assign the value to sum sum = number1 + number2; //display the results JOptionPane.showMessageDialog ( null, “The sum is " + sum, "Results", JOptionPane.PLAIN_MESSAGE ); System.exit ( 0 ); //terminate the application if no errors } //end method main } //end class Addition initializing sum

12 Arithmetic Operators

13 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 ) = ?

14 Order of Operation Example 2 * 4 + 3 % 2 - (4 / 2 + 5 ) = ? 2 * 4 + 3 % 2 - ( 2 + 5 ) = ? 2 * 4 + 3 % 2 - ( 7 ) = ? 8 + 3 % 2 - ( 7 ) = ? 8 + 1 - ( 7 ) = ? 8 + 1 - 7 = 2

15 Program of the Day: pg. 81 Pg. 81: Comparison.java –Pay attention to the comparison operators ( =, etc.) Next time: –Comparison.java in depth –the if structure –comparison operators –assigning new values to an old variable.


Download ppt "1/28: Inputs, Variable Types, etc. Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic operators."

Similar presentations


Ads by Google