1 / 14

10/3: Assignment Operators

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

ezhno
Download Presentation

10/3: Assignment Operators

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

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

  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

  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 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;

  10. 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;

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

  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 5But it is now 7

  13. 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 )

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

More Related