1/28: Inputs, Variable Types, etc. Addition.java in depth Variable types & data types Input from...
-
Upload
rhoda-mcdaniel -
Category
Documents
-
view
214 -
download
0
Transcript of 1/28: Inputs, Variable Types, etc. Addition.java in depth Variable types & data types Input from...
![Page 1: 1/28: Inputs, Variable Types, etc. Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic operators.](https://reader036.fdocuments.in/reader036/viewer/2022082819/56649f335503460f94c5091b/html5/thumbnails/1.jpg)
1/28: Inputs, Variable Types, etc.
• Addition.java in depth
• Variable types & data types
• Input from user: how to get it
• Arithmetic operators
![Page 2: 1/28: Inputs, Variable Types, etc. Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic operators.](https://reader036.fdocuments.in/reader036/viewer/2022082819/56649f335503460f94c5091b/html5/thumbnails/2.jpg)
Addition.java//Fig. 2.9: Addition.java//An addition program
//Java extension packagesimport 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
classheader
methodheader
declaring variables: Strings& ints
![Page 3: 1/28: Inputs, Variable Types, etc. Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic operators.](https://reader036.fdocuments.in/reader036/viewer/2022082819/56649f335503460f94c5091b/html5/thumbnails/3.jpg)
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
![Page 4: 1/28: Inputs, Variable Types, etc. Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic operators.](https://reader036.fdocuments.in/reader036/viewer/2022082819/56649f335503460f94c5091b/html5/thumbnails/4.jpg)
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 5: 1/28: Inputs, Variable Types, etc. Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic operators.](https://reader036.fdocuments.in/reader036/viewer/2022082819/56649f335503460f94c5091b/html5/thumbnails/5.jpg)
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 6: 1/28: Inputs, Variable Types, etc. Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic operators.](https://reader036.fdocuments.in/reader036/viewer/2022082819/56649f335503460f94c5091b/html5/thumbnails/6.jpg)
Addition.java//Fig. 2.9: Addition.java//An addition program
//Java extension packagesimport 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
![Page 7: 1/28: Inputs, Variable Types, etc. Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic operators.](https://reader036.fdocuments.in/reader036/viewer/2022082819/56649f335503460f94c5091b/html5/thumbnails/7.jpg)
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
![Page 8: 1/28: Inputs, Variable Types, etc. Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic operators.](https://reader036.fdocuments.in/reader036/viewer/2022082819/56649f335503460f94c5091b/html5/thumbnails/8.jpg)
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 9: 1/28: Inputs, Variable Types, etc. Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic operators.](https://reader036.fdocuments.in/reader036/viewer/2022082819/56649f335503460f94c5091b/html5/thumbnails/9.jpg)
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
![Page 10: 1/28: Inputs, Variable Types, etc. Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic operators.](https://reader036.fdocuments.in/reader036/viewer/2022082819/56649f335503460f94c5091b/html5/thumbnails/10.jpg)
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 11: 1/28: Inputs, Variable Types, etc. Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic operators.](https://reader036.fdocuments.in/reader036/viewer/2022082819/56649f335503460f94c5091b/html5/thumbnails/11.jpg)
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
![Page 12: 1/28: Inputs, Variable Types, etc. Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic operators.](https://reader036.fdocuments.in/reader036/viewer/2022082819/56649f335503460f94c5091b/html5/thumbnails/12.jpg)
Arithmetic Operators
![Page 13: 1/28: Inputs, Variable Types, etc. Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic operators.](https://reader036.fdocuments.in/reader036/viewer/2022082819/56649f335503460f94c5091b/html5/thumbnails/13.jpg)
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 14: 1/28: Inputs, Variable Types, etc. Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic operators.](https://reader036.fdocuments.in/reader036/viewer/2022082819/56649f335503460f94c5091b/html5/thumbnails/14.jpg)
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 15: 1/28: Inputs, Variable Types, etc. Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic operators.](https://reader036.fdocuments.in/reader036/viewer/2022082819/56649f335503460f94c5091b/html5/thumbnails/15.jpg)
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.