140 likes | 307 Views
10/3: Assignment Operators. About Average2.java while loop use explicit casting twoDigits object Assignment Operators Increment & Decrement Operators pre-increment vs. post-increment operators. About Average2.java: pt. 1. //Fig. 4.9: Average2.java - sentinel-controlled repetition
E N D
10/3: Assignment Operators • About Average2.java • while loop use • explicit casting • twoDigits object • Assignment Operators • Increment & Decrement Operators • pre-increment vs. post-increment operators
About Average2.java: pt. 1 //Fig. 4.9: 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 typeEX: int to doubleEX: 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 EXTranslationOperator += 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;
Increment & Decrement Operators • Even more conveniently, we can use increment & decrement operators for adding & subtracting 1 from a variable.OperatorEXTranslation++ 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: EX 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 5The 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 5But it is now 7
Preincrement vs. Postincrement int x = 5; ( EOS: 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 ( x used before incrementing )But it is now 7 ( x used after incrementing )
Program of the Day • pg. 142: Increment.java • note how the variable’s value changes vs. how the variable’s value is printed out. • make the necessary modifications to the program to have the same results appear in two Message Dialog boxes instead of the MSDOS window.