Do the Math and Other Things - Java Bootcamp: Learn the Basics of Java Programming in 2 Weeks (2014)

Java Bootcamp: Learn the Basics of Java Programming in 2 Weeks (2014)

Chapter 5. Do the Math and Other Things

It’s time to do the Math, the Java way. You can add, subtract, multiply, divide, and get the remainder. It is not difficult to understand, and you will surely appreciate the numerical operators once you started coding your own program. If you still haven’t memorized the signs of the different numerical operators in Java, then you can always go back to Chapter 4 to see the table for numerical operators.

Trying your Hands on Java Addition

You can create a program like this:

1

public class AdditionProgram {

2

public static void main(String []args) {

3

System.out.println(5 + 9);

4

}

5

}

You can also make use of variables like this:

1

public class AdditionProgram {

2

public static void main(String []args) {

3

4

int a = 5;

5

int b = 6;

6

System.out.println(a + b);

7

8

}

9

}

Trying your Hands on Java Subtraction and other Mathematical Operations

Turn the above example as your guide in creating a simple Java program for subtraction, multiplication, division, and remainder (also called modulo). You can try practicing on your own until you get the hang of it. The more you practice, the more you find it less challenging. You will find it more comfortable to work with Java when that happens.

The Comparison Operators

Comparison operators are also called relational operators. They compare data types, and always give you a boolean value, which is either true or false. If you are still not familiar with the table, then you can go back and look at it when you start working with relational operators.

A comparison operator goes between two data, known as operands. If you will print this statement:

System.out.println(9<7);

You will get false for an answer. A Java program recognizes that it should give a boolen answer when presented with a statement that bears a relational operator.

The equal to (==) and not equal to (!=) operators are also known an equality operators. In Java programming, the programmer can use the equality operators to test the equality of the data. The programmer can test the equality across int, boolean, or char data types. Take the example below:

char sampleChar = ‘L’;

int sampleNum = 5;

System.out.println(sampleChar==sampleNum);

The above example will print ‘false’ onscreen because the value in variable sampleChar is not the same as the value that the variable sampleNum holds.

Some Exercises to Try

Now you can test how much you have learned with this simple programming exercise. Don’t worry; you will see the correct codes later if you find it a bit challenging. If you see an error message, read the message and try to figure out the mistake that you made. Sometimes, a simply typo error can jeopardize the whole program, unless you spot it immediately. Make sure that you are using your upper cases and lower cases correctly.

1. Declare ‘this is my program’ as your class; make sure to write it correctly when coding your program.

2. Write a single line comment, anything will do.

3. Assign the value ‘false’ to the boolean variable isAnything.

4. Assign the value ‘789’ to the int variable isTooMuch.

5. Assign the value of isTooMuch multiplied by 3 to int variable isMuchMuch.

6. Print the value isAnything, isTooMuch, and isMuchMuch.

Try coding the given sample first, on your own, before looking at the answer. You can also try practicing more.

If you are finished, you can look at the answer below. Did you get the same?

1

public class ThisIsMyProgram {

2

// this is my single line comment

3

4

public static void main(String []args) {

5

6

boolean isAnything = false;

7

int isTooMuch = 789;

8

int isMuchMuch = isTooMuch * 3;

9

System.out.println(isAnything);

10

System.out.println(isTooMuch);

11

System.out.println(isMuchMuch);

12

13

}

14

}

Now that you know about the Java basics, let’s take it a notch higher. You need to be ready for an intro to control flow.