Logout

# Miscellaneous SL Java Things

***Note that all these are fine here on their own, because each one makes a good peek back, and review to various things already covered.***

This will serve as a catch-all for any of the things that have "slipped through the (notes) cracks" as we have gone through the standard level programming curriculum. Most of these would have been mentioned a couple of times in class, but I'm not sure each of them ever made it to notes, or if they did, they bear repeating.

Modulus Division

The first thing to note is that integer division in Java cannot deal with fractional amounts directly. Integer arithmetic can only yield integer anwers, due to the limits of memory allocated to the int. (You may recall that 32 bits represent integers, thus giving a range of around -2,000,000,000 to around 2,000,000,000. There are no extra bits allocated to representing fractional amounts.)

So straight-forward integer division is therefore only capable of yeilding the whole number portion of the division. In Java, for example, 7/3 yelids 2, and 19/5 yeilds 3. But there is a Java operator that can come up with, and store the remainder of an integer division. This operator is called modulo, and used the symbol %. So 7%3 yelids 1, since 7 divided by 3 is 2, with a remeainder of 1. And 19%5 yelids 4, since 19 divided by 5 is 3, remainder 4. This kind of remainder division, which uses the modulo operator is called modulus division.

One of the most common uses of modulus division in programming is to yeild a 50:50 chance of something executing by modulus dividing an integer by 2, which will yeild 0 for all even numbers and 1 for all odd numbers. Certainly there are other ways to get a 50:50 split, but sometimes this works well. Here's an example:

```....
if(studentAge % 2 == 0){//which will be true for all even ages       System.out.println("You are in group 1.");
}else{//which will therefore be all odd ages        System.out.println("You are in group 2.");}...
```

The ! Operator

In boolean Java statements the explanation mark ! means not.

So in the expression
if(a && !b
)
the if block will be entered when the condition becomes completely to true, which is when a is true and b is false.

And in the expression
if(!isHighSchoolStudent)
the if block will be entered when the boolean isHighSchoolStudent is false.

Order of Operations

The order of operations of arthmetic and algebraic expressions in Java is similar to what you have experienced in mathematics. Multiplication and division take precidence over addition and subtraction, and expressions within parentheses alway get evaluated first.

In terms of the assignment operators =, +=, -=, *=, and /=, they are executed only after the expressions to their right are evaluated. Take for example the following:

int x = 3;
x+= 4 * 2;

The 4 * 2 is evaluated first and then added to 3, so the result of these two lines is 11, not 14.

And for boolean expressions, we'll do a lot more later, but for now the big thing to note is that && takes precedence over ||. (Remembering that && means and and || means or.) Here's an example:

if(a || b && c || d && e){

the b && c and the d && e are evaluated before the ors.

```33 protected static void compoundConditionsMethod(){//This is called the "header" of the method.
34                                                    //This is the beginning of the "body" of the method.
35
36         //The signature of this method is that it is void of return values and takes in no parameters.
37
38         boolean progressReportGood = false;
39         boolean amFeelingLazy = false;
40         boolean forgotBooks = false;
41
42         boolean isSunny = false;
43
44
45         boolean willDoHomework = false;
46
47
49         try{
50             System.out.println("Hi, Welcome to our To Do Homework, or Not To Do Homework App.");
51             System.out.println("Was your progress report good? true/false");
53             System.out.println("Are you feeling lazy? true/false");
55             System.out.println("Have you forgot your books? true/false");
57             System.out.println("Is it sunny out right now? true/false");
59
60
61             if(!isSunny || (!progressReportGood && !amFeelingLazy && !forgotBooks)){
62                 System.out.println("Great. You will do your homework then.");
63
64             }
65             else{
66                 System.out.println("So it looks like you won't be doing your homework this weekend... :("));
67             }
68
69
70         }catch(Exception e){
71             System.out.println("Error in compoundConditionMethod.");
72         }
73
74
75     }//This is the end of the "body" of the method.```

-------------------------------------------------------------------------------------------------------------------

And the following points are not necessarily part of the IB curriculum, but are good to be aware of:

Brackets, Braces, and Parentheses

"Brackets": [ ]

"Braces": { }
(They are sometimes called "curly braces")

"Parentheses": ( )

Key

The word "key" in programming usually refers to a unique value which is being searched for. So, often, in search algorithms, the variable taken in as a parameter which will be searched for is called key.

"In-line" Conditional Statements

An in-line conditional statemtent is a where there is only one line to be executed if the condition evaluates to true, so it is not necessary to continue the statement on a new line after the if part. For example:

```if(isColdOutside) putOnAHat();
```

The two other ways of doing this would be:

```if(isColdOutside)        putOnAHat();
```

and

```if(isColdOutside) {
putOnAHat();
}
```

All three are correct, though it's probably best at this stage to always put braces.

BUT note that braces are required when there is more than one line to your conditional block. For example:

```if(pragueIsTropical)        wearShorts();        putOnYourSwimSuit();System.out.printline("I love Prague.")
;```

Assumming that pragueIsTropical is false, the wearShorts() method will be passed over, but not the putOnYourSwimSuit() method. The indentation is nice, but indentation alone does not control blocks; only the braces do for multiline blocks. If the intention was for both of those methods to be called if pragueIsTropical was true, then braces needed to be put around the multiline block, as follows:

```if(pragueIsTropical) {        wearShorts();        putOnYourSwimSuit();
}System.out.printline("I love Prague.")
```

Signatures of Methods

The signature of a method is the specific nature of its header line (the header line being the first line of the method). So the signature has four parts:

• its accessability, either private, protected, or public
• its return type, which can be any type such as int, String, or int[], and is void if there is no return type
• its name, which we choose
• it parameter list, both the kinds and the order; and there may be no parameters

Optionally the static modifier may need to be added if it is a method being called directly from the static main method.

Here are a few of examples of method headers with varying kinds of signatures:

public int[] bubbleSort(int[] arr)

protected void anotherMethod(int x, String y, boolean z)

public static void main(String [] args)

So, for example, we would say the header of the first above methods is public int[] bubbleSort(int[] arr).

And we would say the signature of the first one is: a public method named bubbleSort, which takes in one parameter - an int array, and returns an int array.

(The diagram from the section above helps clarify this.)