This week, we learn java looping, in which I am able to say that I am a Java learner. Sadly, I was still in America when Java looping was taught by my teacher. However, I caught up soon when I got back, thanks to my CS teacher. I am going to mention following things in my blog:
- Scanner class
- While, Do-while, and For loops
- Use of Scanner class and While loop for Type-Safe input
- Guessing game
- Logical operations
- Arithmetic operations
- Typing casting
- Exercise
- Summary
Scanner
Scanner provides method for reading bytes, short, int, long, float, double, and String data types from Java console and other sources. Scanner is in the java.util package. Scanner spares input into sequence of characters called tokens. By defaults, tokens are separated by standard white space characters. In order to read this datatype, methods are needed. For example, nextDataType( ) can make the computer to return the next token and the datatype of it can be bytes, short, int, long, float, double. There are also next(), nextLine() that can make the computer to return the token in the input stream as a string.
While, Do-while, and For loops
While loop is best used when the loop would repeat an unknown number of times. The condition of while loop is in the first bracket. The loop would go on as long as the condition is true.(the condition would be checked at first)
Do-while loop is different form while loop because the condition of the loop is at the end of the loop. Therefore, no matter what the condition is, the loop would be executed for at least one time.
For loop is the third kind of loop. The condition of the loop is in the beginning of the loop. Therefore, only condition is true, the execution would go on. It is different form the While loop because the number of the loop repeated should be clear in the condition.
Type-Safe Input
Type safe input is very special because it can check whether the datatype the user input is the correct one accords with the right one. If not, the error would not happen, instead of that, it would ask for another input.
Logical operation
In the unit of logic gate, we have some idea about the logical operation, such as and, or, xor. However, there are logical operation in Java. “==” in Java means equal. “!=“means not equal. ‘!’means not obviously. And && means “and”. || means or. If && appear together with||, then && comes first, and || goes next, which is like the logic gate.
Arithmetic operations
The basic arithmetic operations contain add(+), subtract(-), multiplication(*),division(/),modulus(%),++,- -.
++, – – are short cut operators. ++ means plus one. – – means minus one.
There are also escape sequence. \t means inserting a tab in the text at this point. \b means inserting a backspace in the text at this point. \n means inserting a new line at this point. \r means inserting a carriage return in the text at this point. \f would insert a formatted in the text at this point. \’ would insert a single quote character in the text at this point. \” would insert a double quote characters in the text at this point. \\ would insert a backlash character in the text at this point.
Type casting
Implicit type casting
When performing calculations with operands of different data types, lower precision operands are promoted to higher-precision data types and then operands is performed. The Promotion is effective only for expression evaluation, not a permanent change.
Explicit casting
The intentional modulation is needed. The programmer should add data type in front of the a variable, therefore the result would be converted to such data type. For example, (double)1/2 = 0.5 the result won’t be 0 again.
Rules of promotion
- If either operand is a double, the other operand is converted to a double.
- If either operand is a float, the other operand is converted to a float.
- If either operand is a long, the other operand is converted to a long.
- If either operand is an int, the other operand is promoted to an int
- If neither operand is a double, float, long, or an int, both operands are promoted to int.
Exercise

This picture shows the program for the bubble sort. As for the name, it is about sorting due to the value of the numbers. The program is very simple, so so it saves a lot of time while sorting.

This picture shows the program for addition. The user could input two numbers, and computer would add them up and output the result. The thing is, if the use input other datatype instead of integers, the program would ask thee user to input again until user input integers.

This program shows the procedure calculating the area of the circle. It is very simple, but I learn that things like Pi need to add”final”and capitalize “pi”.

This picture shows the program for array. We learn how to create an array and add value into it.

This picture shows the introduction to Java looping. We learn how to use looping to output the value we want.

This picture shows one of the practice we did in the class. It is about finding the prime numbers within a boundary. I develop java programming skills.
Summary
In this week, I learn java looping. I feel like I can solve some problem in my real-life. For example, during the math modeling competition, I use java looping to find the abnormal value of the data set. Honestly, java is not very kind with importing the data comparing to Matlab, Mathematica, or Python. But java did run very fast, which help me a lot.