Expressions and Operators

 Introduction.  Without really thinking about it we have started forming what are called mathematical expressions.  This is done in ways that are so similar to what is done in other computer languages there is almost no need mention them.  The same will largely be true of the operators that are used to build these mathematical expressions. The following table contains a list of the basic arithmetic operators. Even within this range of types of data one needs to consider issues about the precision of the representation within the computer.  Things that are true or false require nowhere near as much memory to store as the numbers associated with space travel. But this motivates the classification of data into what are usually called data types. In Java there are some standard data types listed in the following tables. we also present some information about the ranges of representation and the amount of memory that is used. First we list what are called the primitive data types.  Then we will present some derived data types that are worked with in their own classes.

 Fundamental Arithmetic Operators Operator Meaning Example + addition 3 + 4 = 7 - subtraction 3 - 4 = -1 * multiplication 3 * 4 = 12 / division 12/5 = 2.4 (depending on data type) % modulus (remainder operator) 12 % 5 = 2

 We will develop a little program to demonstrate a couple of these operators.A Program for Expressions.  The following Java applet just requests that the user input two different numbers.  It then parses them as floats and casts them to integers.  Then it computes their quotient and modulus and displays the result.  You need to call the file ExpressionsOperators.java.

 /* applet to investigate the division and modulus operators for integers and floats */ import javax.swing.JOptionPane; import javax.swing.JTextArea; import javax.swing.JApplet; public class ExpressionsOperators extends JApplet { public void init() { String firstUserEntry, secondUserEntry; int a, b, intQuotient, intModulus; float floatA = 0; float floatB = 1; float floatQuotient, floatModulus; JTextArea outputArea = new JTextArea(); // Prompt user for input firstUserEntry = JOptionPane.showInputDialog("Please enter a number:"); // convert these string values to integers try { floatA = Float.parseFloat(firstUserEntry); } catch (NumberFormatException nfeFloat) { JOptionPane.showMessageDialog( null, "The first entry needs to be a number", "Not a Float", JOptionPane.INFORMATION_MESSAGE); System.exit(0); } // Prompt user for second input secondUserEntry = JOptionPane.showInputDialog("Please enter another number:"); try { floatB = Float.parseFloat(secondUserEntry); } catch (NumberFormatException nfeFloat) { JOptionPane.showMessageDialog( null, "The second entry needs to be a number", "Not a Float", JOptionPane.INFORMATION_MESSAGE); System.exit(0); } // casting the floats to integers a = (int) floatA; b = (int) floatB; // computing the integer solution intQuotient = a/b; intModulus = a % b; // computing the float solutions floatQuotient = floatA/floatB; floatModulus = floatA % floatB; // displaying the solution outputArea.setText("Data Type\ta\tb\ta/b\ta%b\n\n"); // displaying the integer solution outputArea.append("Integer\t" + a+ "\t" + b + "\t" + intQuotient + "\t" + intModulus + "\n\n"); // displaying the floating point solution outputArea.append("Float\t" + floatA+ "\t" + floatB + "\t" + floatQuotient + "\t" + floatModulus); // displaying the textarea within an MessageDialog JOptionPane.showMessageDialog( null, outputArea, "Solutions", JOptionPane.INFORMATION_MESSAGE); } // end method init } // end class ExpressionsOperator

 The following section will describe the code. As usual the first section of code imports the classes from external packages.  In this instance they all come from swing. JOptionPane JTextArea JApplet declares the overall class declares the init( ) method that automatically executes in applets declares and initializes variables the next section of code attempts to get the first number from the user it contains the effort to parse it into a float within a try block the catch block is set up to trap for non-numeric inputs give the user a message about the error stably exit the program the next section of code attempts to get the second number from the user it contains the effort to parse it into a float within a try block the catch block is set up to trap for non-numeric inputs give the user a message about the error stably exit the program the next section of code does the manipulation of the numbers the floats are cast to integers for integer arithmetic the integer quotient and modulus are computed the float quotient and modulus are computed the output is put to the JTextArea first a header row of text is set using tab and newline control characters then the user's inputs cast to integers and the quotient and modulus are appended to the JTextArea then the user's inputs used as floats and the quotient and modulus are appended to the JTextArea then this JTextArea is displayed within a JOptionPane showMessageDialog box. You will also need to create the following ExpressionsOperators.html.



 After compiling and executing you should try the following inputs. 56.7  and  12.4 77  and  13 The output for the first case is displayed below.  Notice how the integers are cast and the integer division gives a rather surprising result.  Computers are programmed to deal with integers in this way.

 Obviously, we can develop far more complicated mathematical expressions using parentheses.  One that is probably more beastly than you have ever seen is contained in the following image.

 That complicated capital sigma is actually a summation.  But, even mathematical expressions of this degree of complexity can be computed if the coder is adequately systematic. Now we will describe some other operators and operations that can be used to develop mathematical expressions.  We will make use of these in future web pages. Assignments and Increments/Decrements.  You may have already encountered how an equal sign = is really an assignment operator in computing rather than an equal sign.  Thus statements like x = 23.4 * ( x + 11.2 ) actually make sense.  Whatever,  x currently is you should add 11.2 to it and then multiply this sum by 23.4 to get the "new value" of x.In Java, there are several other ways that certain fairly standard assignment operations can be performed.  The following table contains four of them.

 Expression Meaning x += a x = x + a x -= a x = x - a x *= a x = x * a x /= a x = x / a

 I have a strong tendency to not use these shorthand references because they can make code just that little bit more obscure, but you are free to use them.It is also possible to increment and decrement a variable by one in a shorthand notation as shown in the  following table.

 Expression Meaning x ++ x = x + 1 x -- x = x - 1

 This notation is much more common and I will use it. Comparisons.  Everyone that has reached college has seen things like > and < in order to make comparisons.  It is not very much different in Java.  The following  table contains the basic comparison operators for Java.

 Comparison Operators Operator Meaning Example < less than 4 < 7.6 <= less than or equal 3 <= 11 > greater than 12.34 > 12.33 >= greater than or equal 3.14159 >= 3.14159 == equal "fox" == "fox" != not equal "fox" != "Fox"

 Logical Operators.  While there are more logical operators than we will discuss, particularly because we aren't going to get into bit level operations, we will present two that you will need throughout the rest of this semester and beyond. The operators are in the following table.  We will give a few examples after the table.

 Operator Meaning && logical AND | | logical OR

 Let's say you want something to be able to satisfy more than one comparison condition.  Maybe you want your significant other to be more than 60" tall and less than 70" tall.  The condition you might use to search through a database would look something like the following.( height > 60 ) && ( height < 70 ) If you don't care if they are exactly at the limits then the relevant expression would be ( height >=  60 ) && ( height <=  70 ) Or maybe you don't care what color their hair is.  This might be developed as the following. ( hairColor == "Blonde")  | |  ( hairColor == "Brown")  | |  ( hairColor == "Black")  | |  ( hairColor == "Red") Well, we could go on and on with this.  But the next few webpages will focus on code that makes use of mathematical expressions and comparisons in order to make decisions.