Created
September 17, 2015 07:06
-
-
Save AnnaBoro/f1f72686c4ef9343bd2b to your computer and use it in GitHub Desktop.
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
package lesson3; | |
public class Frame13 { | |
public static void main(String[] args) { | |
int[] data1 = {2, 44, 4, 44, 2, 33, 1, 77}; | |
int[] data2 = {0}; | |
int[] data3 = {0, 0, 0, 0, 0}; | |
System.out.println(oddElementSum(data1)); | |
System.out.println(oddElementSum(data2)); | |
System.out.println(oddElementSum(data3)); | |
} | |
static long oddElementSum(int[] data) { | |
long sum = 0; | |
if (data.length > 1) { | |
for (int i = 1; i < data.length; i +=2){ | |
sum = data[i] + sum; | |
} | |
return sum; | |
} | |
else return -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
package lesson3; | |
public class Frame15 { | |
public static void main(String[] args) { | |
System.out.println(average(new int[] {32, 2, 2})); | |
System.out.println(average(new int[] {0, 0, 3})); | |
System.out.println(average(new int[] {0})); | |
System.out.println(average(new int[] {})); | |
} | |
static double average(int[] data) { | |
double sum = 0; | |
if (data.length > 0) { | |
for (int dat : data) { | |
sum = dat + sum; | |
} | |
return sum / data.length; | |
} | |
else return -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
package lesson3; | |
import java.util.Arrays; | |
public class Frame16 { | |
public static void main(String[] args) { | |
int[] stack1 = {1, 2, 3, 4}; | |
int[] stack2 = {2, 22, 33, 44, 55}; | |
swap(stack1); | |
swap(stack2); | |
System.out.println(Arrays.toString(stack1)); | |
System.out.println(Arrays.toString(stack2)); | |
} | |
static void swap(int[] data) { | |
int perm; | |
if (data.length > 0) { | |
for(int i = 0; i < data.length - 1; i++) { | |
perm = data[i]; | |
data[i] = data[i+1]; | |
data[i+1] = perm; | |
} | |
} | |
} | |
} |
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
package lesson3; | |
import java.util.Arrays; | |
public class Frame17 { | |
public static void main(String[] args) { | |
int[] stack1 = {1, 2, 3, 4}; | |
int[] stack2 = {66, 22, 33, 44, 55}; | |
swap(stack1); | |
swap(stack2); | |
System.out.println(Arrays.toString(stack1)); | |
System.out.println(Arrays.toString(stack2)); | |
} | |
static void swap(int[] data) { | |
int perm; | |
if (data.length > 0) { | |
for(int i = 0; i < data.length - 1; i++) { | |
if (data[i] > data[i+1]) { | |
perm = data[i]; | |
data[i] = data[i+1]; | |
data[i+1] = perm; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment