CIS133J Working with Numbers in Java Contents: Using variables with integral numbers Using variables with floating point numbers How to declare integral variables How to declare floating point variables Saving and retrieving values using the variables Doing calculations Common calculations Trigonometric functions Numbers in Java fall into two categories: integral floating point Integral numbers are those that do not have a decimal point. They can hold whole number values only such as 100, -250, 4567 Floating point number have a decimal point and can hold fractional numbers such as 44.55, 3.14159, -56.44 Note that either type can store positive or negative numbers. Using variables with integral numbers The choice of variable types here are: byte short int long The only difference is the size of the variable. Normally, use int or long.
Using variables with floating point numbers The choice of variable type here are: float double The difference between these is the size of the variable and the precision. How to declare integral variables I will only show how to use int and long. To declare an int variable, use the int keyword and choose a name for the variable: int age; To declare a long variable, use the long keyword and choose a variable name: long distancetoplanet; How to declare floating point variables To declare a float variable, use the float keyword and name the variable. float taxrate; To declare a double variable, use the double keyword and name the variable: double budget; Saving and retrieving values using the variables To store a value into one of the variables, use an assignment statement: age = 45; budget = 245678.99; Numeric values that you use in your code, such as 45 above are called literals. A literal is a constant value.
A literal with a decimal point is assumed to be a double. For instance: float taxrate; taxrate = 55.66; will cause a compiler error because 55.66 is assumed to be a double and taxrate was declared as a float. To fix this problem, use the f suffix after any literal you want to be treated as a float. taxrate = 55.66f; Now 55.66 is considered a float and Java is happy. Warning! If you declare a variable of one type and try to store data of a different type in it, you will get a type mismatch error. Doing calculations Performing calculations is straightforward as long as all the numbers are of the same type. For example, double budget; double adjustment; double totalbudget; budget = 123456.88; adjustment = 545.66; totalbudget = budget + adjustment; Every variable and literal is the same type. There is trouble when you try to mix types in a calculation. For example, double budget; double adjustment; int totalbudget; budget = 123456.88; adjustment = 545.66; totalbudget = budget + adjustment;
Java doesn t like this because you are attempting to store budget + adjustment which is a double, into an int variable. These are called mixed calculations. Warning! If you try to store data of the wrong type into a variable, you will get a type mismatch error. The solution? The easiest is solution is: don t mix your types. Java will perform implicit casting in a mixed calculation. Here are the rules, performed in this order: 1. if either value is double, the other is converted to double 2. if either value is float, the other is converted to float 3. if either is long, the other is converted to long For example; public static void main(string[] args) double budget; float adjustment; double totalbudget; } budget = 123456.88; adjustment = 545.66f; totalbudget = budget + adjustment; Since budget is a double, and adjustment is a float, rule 2 applies and adjustment will be promoted to a double, in this calculation only. You can also force java to change the type of a variable using explicit casting, but that is dangerous if you don t know all the implications. We won t discuss that here. (Better to leave a rattlesnake alone.) I ll repeat: The easiest is solution is: don t mix your types. Common calculations Most of these are obvious and need no explanation. Addition
int x = 5; int y = 7; int total; total = x + y; Subtraction int x = 5; int y = 7; int result; result = x - y; Multiplication int x = 5; int y = 7; int result; result = x * y; Division int x = 5; int y = 7; int result; result = x / y; By the way, * and / are done before + and - in a calculation. For example, x + y * z y * z is done first, then add x to that result You can use ( ) to override this order, as in: (x + y) * z Now, x is added to y first, then the multiplication is performed. Use parentheses where needed to do the calculation correctly. Modulus Division (Integer Division) We all know that 12/2 = 6 with no remainder while 12/5 = 2 with a remainder of 2 The modulus operator (%) does a division and gives the remainder as a whole number. For example;
int x = 14; int y = 5; int result; result = x % y; result will contain 4 (because 14 % 5 = 2 with a remainder of 4) Two ways to add or subtract 1 from a variable You can do it the standard way: x = x + 1; or use the increment operator (++) x++; The same for the decrement (--) operator: or x = x - 1; x--; Find the maximum of two numbers You can do it with an if statement: int x = 6; int y = 8; if (x < y) or use the max method in the Math class: public static void main(string[] args) double budget = 44.55; double adjustment = 666.66; } double result = Math.max(budget, adjustment); System.out.println (result); Find the minimum of two numbers Use the min method in the Math class public static void main(string[] args) double budget = 44.55; double adjustment = 666.66;
} double result = Math.min(budget, adjustment); System.out.println (result); Alternatively, you could use a simple if statement. How to round a number Use the round method in the Math class public static void main(string[ ] args) double value = 66.53; } double roundedvalue = Math.round(value); System.out.println (roundedvalue); How to find the square root of a number Use the sqrt method in the Math class public static void main(string[] args) double value = 66.53; } double x = Math.sqrt(value); System.out.println (x); How to square a number Just multiply it by itself int x = 5; int square = x * x; Trigonometric functions These are all in the Math class. They aren t discussed here. Self test questions 1. What is a literal? Give an example. 2. What is the decrement operator? 3. Which is done first, addition or multiplication? 4. Java treats a literal such as 3.1435 as which data type?
5. Is it safe to use explicit casting? 6. How could you round a number?