Assignment Operators - AD Book Enterprises

Download Report

Transcript Assignment Operators - AD Book Enterprises

2/18: Assignment Operators
• About Average2.java
– while loop use
– explicit casting
– twoDigits object
• Assignment Operators
• Increment & Decrement Operators
– pre-increment vs. post-increment operators
• Program of the Day
About Average2.java: pt. 1
//Average2.java - sentinel-controlled repetition
import javax.swing.JOptionPane;
import java.text.DecimalFormat;
public class Average2 {
public static void main ( String args[] )
{
int gradeCounter,
//number of grades entered
gradeValue,
//grade value
total;
//sum of grades
double average;
//average of all grades
String input;
//grade typed by user
//Processing phase - prompt for input, read grade from user
input=JOptionPane.showInputDialog(“Enter grade, -1 to quit:");
About Average2.java: pt. 2
//convert input into int variable 'gradeValue'
gradeValue = Integer.parseInt ( input );
while ( gradeValue != -1 ) {
total = total + gradeValue;
//add gradeValue to total
gradeCounter = gradeCounter + 1; //add 1 to gradeCounter
//prompt for input again and read grade from user
input = JOptionPane.showInputDialog (
"Enter integer grade or -1 to quit: " );
gradeValue = Integer.parseInt ( input );
}
About Average2.java: pt. 3
//Termination phase
DecimalFormat twoDigits = new DecimalFormat ( "0.00" );
if ( gradeCounter != 0 ) {
average = (double) total / gradeCounter ;
JOptionPane.showMessageDialog ( null ,
"Class average is " + twoDigits.format ( average ) ,
"Class Average" , JOptionPane.INFORMATION_MESSAGE );
}
else
JOptionPane.showMessageDialog ( null,"No grades entered.",
"Class Average" , JOptionPane.INFORMATION_MESSAGE );
System.exit ( 0 );
}
}
Explicit casting
• Converting a variable into a more exact type
EX: int to double
EX: average = ( double ) total / gradeCounter;
• the int type variable total is temporarily
converted (cast) to a double type before being
used in the equation. The variable total
REMAINS as an int type, but the double version
is used in this equation.
twoDigits
• twoDigits is an object of class DecimalFormat.
• Variables are declared and initialized, but objects
are instantiated (created as an instance of a class).
• DecimalFormat twoDigits = new
DecimalFormat ( “0.00” )
• Objects are instantiated by specifying
the class,
naming the object,
using the keyword new, and stating the variation
of the class that the new object should be.
twoDigits
• We use twoDigits to format the value of average.
• JOptionPane.showMessageDialog ( null ,
“Class average is “ +
twoDigits.format ( average ) ,
“Class Average” ,
JOptionPane.INFORMATION_MESSAGE );
• To learn more about options for number
formatting, look at
http://java.sun.com/products/jdk/1.2/docs/api/java
/text/DecimalFormat.html
Assignment Operators
• Instead of using a statement like
x=x+1;
we can use a shorthand for this assignment:
x += 1;
• Why bother? For the speed of the overall program
and fewer keystrokes to type.
Assignment Operators
• Assignment
EX
Translation
+=
x += 2;
x = x + 2;
-=
y -= 1;
y = y – 1;
*=
z *= 3;
z = z * 3;
/=
a /= 2;
a = a / 2;
%=
b %= 3;
b = b % 3;
Operator
Increment & Decrement Operators
• Even more conveniently, we can use increment &
decrement operators for adding & subtracting 1
from a variable.
Operator
EX
Translation
++
x++;
x = x + 1;
++x;
x = x + 1;
-y--;
y = y – 1;
--y;
y = y – 1;
Increment & Decrement Operators
• More conveniently, we can use these expressions
inside other expressions: for example,
int x = 5;
System.out.println ( “The value is “ + x++ );
System.out.println ( “The value is now “ + x );
would result in the output in the MSDOS screen:
The value is 5
The value is now 6
Preincrement vs. Postincrement
• The preincrement ( ++x ) differs from the postincrement
( x++ ) by when the incrementing takes place.
• The preincrement ( ++x) increments the variable before,
while the postincrement ( x++ ) increments the variable
after it is used in the rest of the statement. Therefore:
int x = 5;
System.out.println ( “The value is “ + x++ );
System.out.println ( “But it is now “ + ++x );
would result in the output in the MSDOS screen:
The value is 5
But it is now 7
Preincrement vs. Postincrement
int x = 5; ( At end of statement, x = 5 )
System.out.println ( “The value is “ + x++ );
( EOS: x = 6 )
System.out.println ( “But it is now “ + ++x );
( EOS: x = 7 )
would result in the output in the MSDOS screen:
The value is 5
But it is now 7
( x used before incrementing )
( x used after incrementing )
Program of the Day
• pg. 180: Increment.java
– note how the variable’s value changes vs. how the
variable’s value is printed out.
– After it works, make the necessary modifications to
the program to have the same results appear in two
successive Message Dialog boxes instead of the
MSDOS window.