1 / 13

7/6: Inputs, Variable Types, etc.

7/6: Inputs, Variable Types, etc. Addition.java in depth Variable Types & data types Input from user: how to get it Arithmetic operators. import statement. class header. method header. declaring variables: Strings & ints. Addition.java. import javax.swing.JOptionPane;

camden
Download Presentation

7/6: Inputs, Variable Types, etc.

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. import statement classheader method header declaring variables: Strings& ints Addition.java import 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 ); } }

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

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

  5. initializing firstNumber &secondNumber Addition.java import 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 ); } }

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

  7. initializing number1 &number2 Addition.java import 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 ); } }

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

  9. initializing sum Addition.java import 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 ); } }

  10. Arithmetic Operators

  11. 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 ) = ?

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

  13. Program of the Day: pg. 58 • Pg. 58: 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.

More Related