This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static int totalEven(int[] a) { | |
int result = 0; | |
for(int i=0; i < a.length; i++) { | |
if(a[i] % 2 == 0) { //curent number IS even | |
result+=a[i]; //add it to the total | |
} | |
} | |
return result; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class NeedForSpeed { | |
public static void main(String[] args) { | |
double distance = 34; | |
double time = 21; | |
double speed = distance/time; | |
System.out.println(speed); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int result = -3; | |
for(int i=1; i <= 7; i++) { | |
if(i%2 == 1) { //is i an odd number? | |
result = result + i; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int a = 5, b = 10; | |
int result = 0; | |
if(a == b) { //check for equality | |
result = a; | |
} | |
else { | |
b = b - a; | |
if(a == b) { | |
result = b; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int a = 5; | |
int b = 10; | |
int result; | |
if(a < b && b % a == 0 && a % 2 == 0) { //b should be divisible by a and a should be even | |
result = a; | |
} | |
else { | |
result = b; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int a = 4, b = 12, c = 3; | |
boolean d = false; | |
int result = 4; | |
if(a > b || b % c == 0) { | |
if(d == true) { | |
result = result + 1; | |
} | |
else { | |
result = result - 1; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int a = 4, b = 12, c = 3; | |
boolean d = false; | |
int result = 4; | |
if(a < b || b % c == 1) { | |
//a < b: false, b%c==0: true, false || true: true | |
if(d == true) { //false == true: false | |
result = result + 1; | |
} | |
else { //else block executes | |
result = result - 1; //result becomes 4 - 1 = 3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Client { | |
public static int foo(int a) { | |
return a*a; | |
} | |
public static void main(String[] args) { | |
int result = foo(foo(3)); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int a = 10, b = 20; | |
int temp = a; | |
a = b; | |
b = temp; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
change the values of fib1 and fib2 such | |
that fib1, fib2 occur in fibonacci sequence. | |
the number of "petals" you'll notice | |
is fib1+fib2 | |
*/ | |
float fib1 = 8.0; | |
float fib2 = 13.0; |