1-28 - AD Book Enterprises
Download
Report
Transcript 1-28 - AD Book Enterprises
1/28: Inputs, Variable Types, etc.
•
•
•
•
Addition.java in depth
Variable types & data types
Input from user: how to get it
Arithmetic operators
Addition.java
//Fig. 2.9: Addition.java
//An addition program
import
//Java extension packages
statement
import javax.swing.JOptionPane; //import class JOptionPane
class
header public class Addition {
method
header
//main method begins execution of Java application
public static void main ( String args[] )
declaring {
variables:
String firstNumber;
// first String entered by user
Strings
String secondNumber;
// second String entered by user
& ints
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" );
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 );
} //end method main
} //end class Addition
//terminate the application if no errors
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.
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
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
initializing
firstNumber
//read in first number from user as a String
firstNumber = JOptionPane.showInputDialog
( "Enter first integer" );
Addition.java
initializing
//read in second number from user as a String
secondNumber
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 );
} //end method main
} //end class Addition
//terminate the application if no errors
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.
Addition.java
//read in second number from user as a String
secondNumber = JOptionPane.showInputDialog
( “Enter second integer" );
initializing
number1 &
number2
//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 );
} //end method main
} //end class Addition
//terminate the application if no errors
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.
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
initializing
sum = number1 + number2;
sum
//display the results
JOptionPane.showMessageDialog ( null, “The sum is " + sum,
"Results", JOptionPane.PLAIN_MESSAGE );
System.exit ( 0 );
} //end method main
} //end class Addition
//terminate the application if no errors
Arithmetic Operators
Java operator Arithmetic
operator
Addition
+
Algebraic
expression
Java
expression
f+7
f+7
Subtraction
-
p–c
p–c
Multiplication
*
pv
p*v
Division
/
x/y
x/y
Modulus
%
r mod s
r%s
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 ) = ?
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
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.