SlideServe
Menu
Préférences
© SlideServe · v2026

10/3: Assignment Operators

ezhno avatar
14 diapositives · 326 Views · Aug 28, 2014

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

10/3: Assignment Operators - slide 1
01 / 1410/3: Assignment Operators
1 / 17

Télécharger la présentation

10/3: Assignment Operators
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  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.