# Java Programming 3: Shorthand operators and String and Math classes We introduced some operators before, now we will show their shorthand form. It may seem a bit harder in the beginning, but you will adapt them soon and then you will use them as often as you can. If you have a variable named `v` and want to add its value by 9, you would use the expression `v = v + 9;` Just like that, you can use the **shorthand operator** `+=`. In this case, the expression would be `v += 9;`. The table below shows all the shorthand operators and an example of their usage: Operator | Example | Equivalent -------- | -------- | ----------- `+=` | `v += 9` | `v = v + 9` `-=` | `v -= 9` | `v = v – 9` `*=` | `v *= 9` | `v = v * 9` `/=` | `v /= 9` | `v = v / 9` `%=` | `v %= 9` | `v = v % 9` This example shows how to use them in a program: ```java public class ShorthandOperators { public static void main(String[] args) { int value = 10; //show the initial value System.out.println("value=" + value); //add 12 to the value value += 12; System.out.println("value=" + value); //subtract 5 from the value value -= 5; System.out.println("value=" + value); //make value 10 times bigger value *= 10; System.out.println("value=" + value); //make value 17 times smaller value /= 17; System.out.println("value=" + value); } } ``` There are also two operators you need to learn, the `++` operator and the `--` operator. The first one is the increment operator; the second one is the decrement operator. The increment operator adds 1 to the value where it is used, while the decrement operator subtracts 1. They may be used as `v++` and `v--`, or as `++v` and `--v`. Where is the difference? This program shows it: ```java public class IncrementDifferences { public static void main(String[] args) { int a = 10, b; b = a++; System.out.println("a=" + a + " b=" + b); a = 10; //reset the value of a b = ++a; System.out.println("a=" + a + " b=" + b); } } ``` The output is: ```bash a=11 b=10 a=11 b=11 ``` When we use `b = a++;` Java firstly makes `b` equal to `a`, and then adds 1 to the value of `a`. When we use `b = ++a;` Java firstly adds 1 to the value of `a`, and then makes `b` equal to `a`. This is not crucial information, but it may be useful. The same thing happens when we use the decrement operator, but in this time, it subtracts instead of adding. ## Using the String and Math classes Classes are building blocks of Java. One class we have been using since the beginning of Java programming is the String class. We have been passing String objects as arguments of the main function and the `System.out.println()` function. Now you will learn how to use `String` in our program. Strings are nothing more than arrays of characters; that is, words, sentences, text, etc. We learned that the + operator is used to concatenate strings. This program shows how to define and concatenate strings: ```java public class LearningStrings { public static void main(String[] args) { String s1 = "Java "; //defining a string String s2 = "Programming "; String s3 = "Language"; String s4 = s1 + s2 + s3; //concatenate 3 strings into a fourth System.out.println(s4); s4 += " is awesome!"; //concatenate using shorthand System.out.println(s4); } } ``` We've learned how to read strings from the user, using the `next()` and `nextLine()` functions of the `Scanner` class. The `Math` class is a special kind of class. You cannot create objects of this class, but you can use its methods by calling them as `Math.method(args...)` and its constants as `Math.CONSTANT`. See the program below: ```java public class MathExample { public static void main(String[] args) { double sin90; sin90 = Math.sin(Math.PI / 2); System.out.println(sin90); } } ``` In this program we use the `Math.sin()` method and the `Math.PI` constant defined in the `Math` class. Here is a list of some methods in the Math class: Method | What it does ---------------------- | ---------------------------------------- `Math.sin(arg)` | Returns the sine of `arg` (in radian) `Math.cos(arg)` | Returns the cosine of `arg` (in radian) `Math.tan(arg)` | Returns the tangent of `arg` (in radian) `Math.sqrt(arg)` | Returns the square root of `arg` `Math.pow(arg1, arg2)` | Returns `arg1` in power of `arg2` `Math.random()` | Returns a random number between 0 and 1 The `Math.random()` is a method that returns a random number between 0 and 1, but with a little work we can obtain more values. Consider the following program: ```java public class RandomNumbers { public static void main(String[] args) { double rnd1 = Math.random(); System.out.println("This is a random number between 0 and 1: " + rnd1); int rnd2 = (int) (Math.random() * 100); System.out.println("This is a random integer between 0 and 100:" + rnd2); int rnd3 = (int) (Math.random() * 100) + 20; System.out.println("This is a random integer between 20 and 120:" + rnd3); } } ``` When I run this the first time, it creates this output: ```bash This is a random number between 0 and 1: 0.2139571054316357 This is a random integer between 0 and 100: 56 This is a random integer between 20 and 120: 98 ``` The second time it creates this output: ```bash This is a random number between 0 and 1: 0.3273192730084672 This is a random integer between 0 and 100: 63 This is a random integer between 20 and 120: 24 ``` As you can see, there is a different number generated every time we run the program. Let explain this line of code: `int rnd2 = (int) (Math.random() * 100);` You can see there is a `(int)` added before the `(Math.random() * 100)`. This is called a **casting operator**. The `Math.random()` method returns a `double`, not an `int`. By casting, we get the value of `Math.random() * 100` and force it to be an integer value, which is assigned to the `rnd2`. We will now make a simple power calculator using the `Math.power()` function. We will make a program that asks us for two numbers, and then adds them, subtracts them, multiplies them. ```java import java.util.Scanner; public class SimpleMath { public static void main(String[] args) { Scanner stdIn = new Scanner(System.in); int num1, num2; System.out.print("Enter two numbers divided by space: "); num1 = stdIn.nextInt(); num2 = stdIn.nextInt(); System.out.println(num1 + "+" + num2 + "=" + (num1 + num2) ); System.out.println(num1 + "-" + num2 + "=" + (num1 - num2) ); System.out.println(num1 + "*" + num2 + "=" + (num1 * num2) ); System.out.println(num1 + "^" + num2 + "=" + Math.pow(num1, num2)); } } ``` One sample run is shown below: ```bash Enter two numbers divided by space: 5 7 5+7=12 5-7=-2 5*7=35 5^7=78125.0 ``` This other program is a simple lottery. We choose 5 numbers between 1 and 53, and another Magic number between 1 and 42. Now, this is a relatively long and repetitive code for this kind of task. Later, you will use **loops**, a control statement that makes you use the same part of the program as long as you want. ```java public class Lottery { public static void main(String[] args) { int number, magicNumber; //we choose the numbers number = (int) (Math.random() * 53) + 1; //1st System.out.print(number+" "); number = (int) (Math.random() * 53) + 1; //2nd System.out.print(number+" "); number = (int) (Math.random() * 53) + 1; //3rd System.out.print(number+" "); number = (int) (Math.random() * 53) + 1; //4th System.out.print(number+" "); number = (int) (Math.random() * 53) + 1; //5th System.out.print(number+" "); System.out.println(); //an empty line //we choose the Magic number magicNumber = (int) (Math.random() * 42) + 1; System.out.println("Magic number: " + magicNumber); } } ``` One output is: ```bash 25 3 39 22 14 Magic number: 16 ``` Another one is: ```bash 24 23 19 3 43 Magic number: 24 ``` There are plenty of combinations and the `Math.random()` function doesn't return the same result twice.