Presentation is loading. Please wait.

Presentation is loading. Please wait.

2/18: Assignment Operators About Average2.java –while loop use –explicit casting –twoDigits object Assignment Operators Increment & Decrement Operators.

Similar presentations


Presentation on theme: "2/18: Assignment Operators About Average2.java –while loop use –explicit casting –twoDigits object Assignment Operators Increment & Decrement Operators."— Presentation transcript:

1 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

2 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:");

3 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 ); }

4 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 ); }

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

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

7 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 http://java.sun.com/products/jdk/1.2/docs/api/java /text/DecimalFormat.html

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

9 Assignment Operators Assignment EXTranslation Operator +=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;

10 Increment & Decrement Operators Even more conveniently, we can use increment & decrement operators for adding & subtracting 1 from a variable. Operator EXTranslation ++x++;x = x + 1; ++x;x = x + 1; --y--;y = y – 1; --y;y = y – 1;

11 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

12 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

13 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 ( x used before incrementing ) But it is now 7 ( x used after incrementing )

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


Download ppt "2/18: Assignment Operators About Average2.java –while loop use –explicit casting –twoDigits object Assignment Operators Increment & Decrement Operators."

Similar presentations


Ads by Google