Created
September 22, 2015 07:11
-
-
Save AnnaBoro/1df4471abfac3f1baadf to your computer and use it in GitHub Desktop.
Lesson 3 Homework 1-5
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 Homework1 { | |
public static void main(String[] args) { | |
double[] num = {1.0, 4.0, 9.0, 1.0}; | |
System.out.println(findElement(num, 1.0)); | |
System.out.println(findElement(num, 4.0)); | |
System.out.println(findElement(num, 22.0)); | |
} | |
static int findElement(double[] numbers, double el) { | |
for (int i = 0; i < numbers.length; i ++) { | |
if (numbers[i] == el) { | |
return i; | |
} | |
} | |
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 Homework2 { | |
public static void main(String[] args) { | |
int[] arr = {0, 1, 2}; | |
int[] arr2 = new int[2]; | |
System.out.println("arr " + Arrays.toString(arr) + " arr2 " + Arrays.toString(arr2)); | |
copyArray(arr, 0, arr2, 0, 2); | |
System.out.println("arr " + Arrays.toString(arr) + " arr2 " + Arrays.toString(arr2)); | |
} | |
static void copyArray(int[] src, int srcPos, int[] dest, int destPos, int length) { | |
for (int i = srcPos, j = destPos; i < length; i++, j++) { | |
dest[j] = src[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
package lesson3; | |
public class Homework3 { | |
public static void main(String[] args) { | |
System.out.println(inverse("Happy Birthday!")); | |
System.out.println(inverse("Happy Birthday")); | |
} | |
static String inverse(String str) { | |
char[] charStr = str.toCharArray(); | |
char[] charStr2 = new char[charStr.length]; | |
for (int i = 0, j = charStr.length - 1; i < charStr.length; i++, j--) { | |
charStr2[j] = charStr[i]; | |
} | |
return new String(charStr2); | |
} |
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 Homework4 { | |
public static void main(String[] args) { | |
System.out.println(factorial(3)); | |
System.out.println(factorial(5)); | |
System.out.println(factorial(1)); | |
} | |
static long factorial(int i) { | |
if (i <= 0) { | |
return 1; | |
} | |
long res = 1; | |
for (int j = 1; j <= i; j++) { | |
res = res * j; | |
} | |
return res; | |
} | |
} |
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 Homework5 { | |
public static void main(String[] args) { | |
String[][] arr1 = {{"oleg"}, {"Inna", "Smith", "25"}, {"Irina", "Koval"}}; | |
String[][] arr2 = {{}, {}}; | |
String[][] arr3 = {{}, {"Boris", "Govor", "66"}, {}}; | |
String[][] arr4 = {{"oleg"}, {"anton"}, null, {"Anna", "25"}}; | |
printArray(arr1); | |
printArray(arr2); | |
printArray(arr3); | |
printArray(arr4); | |
} | |
static void printArray(String[][] data) { | |
String result = ""; | |
if (data != null) { | |
for (int i = 0; i < data.length; i++) { | |
System.out.print("{"); | |
if (data[i] != null) { | |
for (int j = 0; j < data[i].length; j++) { | |
if (data[i][j] != null) { | |
result = data[i][j] ; | |
System.out.print(" " + result + " "); | |
} | |
else System.out.print(""); | |
} | |
} | |
System.out.print("}"); | |
} | |
System.out.println(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment