counter += 2;This is equivalent to the line
counter = counter + 2;If the increment or decrement is 1, then we may use double plus or double minus signs.
counter = counter + 1;is equivalent to
counter += 1;is equivalent to
counter++;We also looked at the casting operator. That is, if we want to take a variable which is of one type and change its type to another we use the following syntax.
(data-type) expression;For example, suppose we wish to find the quotient upon dividing a by b. We could use the following code.
quotient = (int)(a/b);When evaluating arithmetic expressions, Java uses the standard hierarchy.
a = 3 + 4*5;could also be written
a = 3 + (4*5);even though the multiplication would be performed first.
2 < a < 3Instead we need a way to combine the two logical comparisons 2 < a and a < 3.
(2 < a) && (a < b)Another logical comparison availabe to us is the logical or. The symbol for this is ||. This is the pipe symbol. So we could write the following expression.
(2 < a) || (a < b)The difference between these two expressions is that with the logical and, both smaller expressions have to be true in order for the whole expression to be true. With a logical or, only one of the expressions need to be true in order for the whole expression to be true.
| Input 1 | Input 2 | Logical and | Logical or |
| True | True | True | True |
| True | False | False | True |
| False | True | False | True |
| False | False | False | False |
A.BA represents a directory where the directory B is located. An example of a package is java.lang.
import java.io.*;This tells the compiler to find the java directory. Inside that directory find a directory called io, and then load all the class files it finds there.
public int half(int a) {
...
}
public is the access control keyword. The return type is an integer. The
name of the method is half. The formal parameter list is int a. The
combination of these is called the signature of the method.
public class DecimaltoBinary {
int original;
public DecimaltoBinary(int decimal) {
original = decimal;
}
public void convert() {
int quotient;
int remainder = 0;
int num = (int)(Math.log(original)/Math.log(2));
while (num>=1) {
quotient = (int)(original/Math.pow(2,num));
remainder = (int)(original - quotient*Math.pow(2,num));
System.out.println(quotient);
original -= quotient*Math.pow(2,num);
num--;
}
System.out.println(remainder);
}
public static void main(String[] args) {
DecimaltoBinary myConverter = new DecimaltoBinary(32);
myConverter.convert();
}
}