Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recenzja #1

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
13 changes: 13 additions & 0 deletions arrays/Arrays.jsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
void s(long time){
try{
Thread.sleep(time);
} catch (InterruptedException e){
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

serr przynajmniej mogłoby tu być. Jest coś takiego jak spurious wakeup, co może wybudzić uśpiony wątek spoza aplikacji zatem nawet jak Ty nie planujesz go budzić lub mu przerywać, to się może zdarzyć.

}
System.out.println("An array is a container object that holds a fixed number of values of a single type.");
System.out.println("The length of an array is established when the array is created. ");
System.out.println("Each item in an array is called an element, and each element is accessed by its numerical index.");
System.out.println();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Zamiast takich rzeczy może chociaż \n w poprzedniej linii?

s(2000);
System.out.println("It's time for questions.");
System.out.println("Write /open ../arrays/Q1.jsh");
19 changes: 19 additions & 0 deletions arrays/E1.jsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
if(answer == 2){
System.out.println("You're right!");
correct++;
} else{
System.out.println("Too bad :(");
}
System.out.println("When we create new array, it is fill by default values, so for logical boolean it will be false.\n" +
"If condition is true, so program will print num variable.");
s(4000);
System.out.println("You have " + correct + " correct answers on 8 possible");
System.out.println("If you aren't satisfied you know what to do");
if(correct == 8){
System.out.println("I can't believe you are so smart..");
}
System.out.println();
System.out.println("And now very basic problem. You have an array and want to reverse it. Return array with a new order.");
System.out.println("For example, [1, 2, 3] -> [3, 2, 1]");
System.out.println("Type your method in JShell and named it reverse");
System.out.println("When you finish, write /open ../arrays/E2.jsh");
37 changes: 37 additions & 0 deletions arrays/E2.jsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
System.out.println("Your method:");
/list reverse
s(5000);
System.out.println();
System.out.println("Now we will test it for this calls:");
System.out.println("\treverse([1, 2, 3]) -> [3, 2, 1]");
System.out.println("\treverse([1, 2, 3, 1, 2, 3]) -> [3, 2, 1, 3, 2, 1]");
System.out.println("\treverse([0, 0, 0, 0]) -> [0, 0, 0, 0]");
System.out.println("\treverse([]) -> []");
s(4000);
System.out.println();
if (!Arrays.equals(reverse(new int[]{1, 2, 3}),new int[]{3, 2, 1})) {
System.out.println("First test didn't work.");
} else if (!Arrays.equals(reverse(new int[]{1, 2, 3, 1, 2, 3}),new int[]{3, 2, 1, 3, 2, 1})) {
System.out.println("Second test didn't work.");
} else if (!Arrays.equals(reverse(new int[]{0, 0, 0, 0}), new int[]{0, 0, 0, 0})) {
System.out.println("Third test didn't work.");
} else if (!Arrays.equals(reverse(new int[]{}),new int[]{})) {
System.out.println("Fourth test didn't work.");
} else {
System.out.println("Wow. You passed all tests. Congratulations such a feat!");
}

public int[] rightReverse(int[] nums) {
int[] temp = new int[nums.length];
//two conditions in loop to iterate over two arrays
for(int i = nums.length - 1, j = 0; i >= 0 & j < nums.length; i--, j++){
temp[i] = nums[j];
}
return temp;
}


System.out.println("Something wrong with your solution?");
System.out.println("When you type /list rightReverse you can see how to do this exercise");

System.out.println("Then write /open ../arrays/E3.jsh");
7 changes: 7 additions & 0 deletions arrays/E3.jsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
System.out.println();
System.out.println("Next task for you");
System.out.println("Write method which takes array of int and returns array consist of max value from given array.\n" +
"For example: [3, 4, 3, 12, 7, 9] -> [12, 12, 12, 12, 12, 12]");
System.out.println("Name your method maxValues");
System.out.println("Remember that you can use /list methodName command to see what you wrote");
System.out.println("When you finish, write /open ../arrays/E4.jsh");
38 changes: 38 additions & 0 deletions arrays/E4.jsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
System.out.println("Your method looks like that:");
/list maxValues
Thread.sleep(5000);
System.out.println();
System.out.println("Now we will test it for this calls:");
System.out.println("\tmaxValues([1, 2, 3]) -> [3, 3, 3]");
System.out.println("\tmaxValues([2, 16, 6, 18, 8, 10]) -> [18, 18, 18, 18, 18, 18]");
System.out.println("\tmaxValues([5, 5, 5]) -> [5, 5, 5]");
System.out.println("\tmaxValues([3, 0, -5, 2]) -> [3, 3, 3, 3]");
System.out.println("\tmaxValues([-3, -5, -2]) -> [-2, -2, -2]");
s(4000);
System.out.println();
if (!Arrays.equals(maxValues(new int[]{1, 2, 3}),new int[]{3, 3, 3})){
System.out.println("First test didn't work.");
} else if (!Arrays.equals(maxValues(new int[]{2, 16, 6, 18, 8, 10}),new int[]{18, 18, 18, 18, 18, 18})){
System.out.println("Second test didn't work.");
} else if (!Arrays.equals(maxValues(new int[]{5, 5, 5}),new int[]{5, 5, 5})) {
System.out.println("Third test didn't work.");
} else if (!Arrays.equals(maxValues(new int[]{3, 0, -5, 2}),new int[]{3, 3, 3, 3})) {
System.out.println("Fourth test didn't work.");
} else if (!Arrays.equals(maxValues(new int[]{-3, -5, -2}),new int[]{-2, -2, -2})) {
System.out.println("Fourth test didn't work.");
} else {
System.out.println("Wow. You passed all tests. Congratulations such a feat!");
}

public int[] rightMaxValues(int[] nums) {
//finds max value at the end of array
Arrays.sort(nums);
//fill array with founded value
Arrays.fill(nums, nums[nums.length - 1]);
return nums;
}


System.out.println("When you type /list rightMaxValues you can see how to do this exercise");

System.out.println("Then write /open ../arrays/E5.jsh");
7 changes: 7 additions & 0 deletions arrays/E5.jsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
System.out.println();
System.out.println("Next task");
System.out.println("You have to connect two arrays in one.\n" +
"For example: [1, 2, 3], [5, 6, 7] -> [1, 2, 3, 5, 6, 7]");
System.out.println("Return new array.");
System.out.println("Named your method joinArrays");
System.out.println("When you finish, write /open ../arrays/E6.jsh");
47 changes: 47 additions & 0 deletions arrays/E6.jsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
System.out.println("Your method:");
/list joinArrays
Thread.sleep(5000);
System.out.println();
System.out.println("And we will test it for this calls:");
System.out.println("\tjoinArrays([1, 2, 3], [5, 6, 7]) -> [1, 2, 3, 5, 6, 7]");
System.out.println("\tjoinArrays([1, 2, 3], []) -> [1, 2, 3]");
System.out.println("\tjoinArrays([], [5, 6, 7]) -> [5, 6, 7]");
System.out.println("\tjoinArrays([1, 2], [5, 6, 7]) -> [1, 2, 5, 6, 7]");
System.out.println("\tjoinArrays([1, 2, 3], [5, 6]) -> [1, 2, 3, 5, 6]");
s(4000);
System.out.println();
if (!Arrays.equals(joinArrays(new int[]{1, 2, 3}, new int[]{5, 6, 7}), new int[]{1,2,3,5,6,7})) {
System.out.println("First test didn't work.");
} else if (!Arrays.equals(joinArrays(new int[]{1, 2, 3}, new int[]{}), new int[]{1,2,3})) {
System.out.println("Second test didn't work.");
} else if (!Arrays.equals(joinArrays(new int[]{}, new int[]{5, 6, 7}), new int[]{5,6,7})) {
System.out.println("Third test didn't work.");
} else if (!Arrays.equals(joinArrays(new int[]{1, 2}, new int[]{5, 6, 7}), new int[]{1,2,5,6,7})) {
System.out.println("Fourth test didn't work.");
} else if (!Arrays.equals(joinArrays(new int[]{1, 2, 3}, new int[]{5, 6}), new int[]{1,2,3,5,6})) {
System.out.println("Fifth test didn't work. Most importatnt!");
} else {
System.out.println("Wow. You passed all tests. Congratulations such a feat!");
}

public int[] rightJoinArrays(int[] a, int[] b) {
//checks if one of arrays is empty
if(a.length == 0){
return b;
}
if(b.length == 0){
return a;
}
//create an array to contaions two given
int[] arr = new int[a.length + b.length];
//copy first array at the begining
System.arraycopy(a, 0, arr, 0, a.length);
//copy second array at place where first was finished
System.arraycopy(b, 0, arr, a.length, b.length);
return arr;
}


System.out.println("When you type /list rightJoinArrays you can see how to do this exercise");

System.out.println("Then write /open ../arrays/E7.jsh");
7 changes: 7 additions & 0 deletions arrays/E7.jsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
System.out.println();
System.out.println("The last one task");
System.out.println("Yoir methods takes two parameters, first is an array, and second is a number, which tells how many times move array elements.\n" +
"For example: [1, 2, 3, 4], 2 -> [3, 4, 1, 2]");
System.out.println("Remember that order can returns to initial state or go through the array several times.");
System.out.println("Named your method move");
System.out.println("When you finish, write /open ../arrays/Finish.jsh");
57 changes: 57 additions & 0 deletions arrays/Finish.jsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
System.out.println("Your method:");
/list move
Thread.sleep(5000);
System.out.println();
System.out.println("And we will test it for this calls:");
System.out.println("\tmove([1, 2, 3, 4, 5], 1) -> [5, 1, 2, 3, 4]");
System.out.println("\tmove([1, 2, 3, 4, 5], 0) -> [1, 2, 3, 4, 5]");
System.out.println("\tmove([1, 2, 3, 4, 5], 3) -> [3, 4, 5, 1, 2]");
System.out.println("\tmove([1, 2, 3, 4, 5], 5) -> [1, 2, 3, 4, 5]");
System.out.println("\tmove([1, 2, 3, 4, 5], 7) -> [4, 5, 1, 2, 3]");
s(4000);
System.out.println();

if (!Arrays.equals(move(new int[]{1, 2, 3, 4, 5}, 1), new int[]{5,1,2,3,4})) {
System.out.println("First test didn't work.");
} else if (!Arrays.equals(move(new int[]{1, 2, 3, 4, 5}, 1), new int[]{5,1,2,3,4})) {
System.out.println("Second test didn't work.");
} else if (!Arrays.equals(move(new int[]{1, 2, 3, 4, 5}, 0), new int[]{1,2,3,4,5})) {
System.out.println("Third test didn't work.");
} else if (!Arrays.equals(move(new int[]{1, 2, 3, 4, 5}, 3), new int[]{3,4,5,1,2})) {
System.out.println("Fourth test didn't work.");
} else if (!Arrays.equals(move(new int[]{1, 2, 3, 4, 5}, 5), new int[]{1,2,3,4,5})) {
System.out.println("Fifth test didn't work. Most importatnt!");
} else if (!Arrays.equals(move(new int[]{1, 2, 3, 4, 5}, 7), new int[]{4,5,1,2,3})) {
System.out.println("Sixth test didn't work. Most importatnt!");
}else {
System.out.println("Wow. You passed all tests. Congratulations such a feat!");
}

public int[] rightMove(int[] arr, int times){
//checks if array can stay without changes
if (times%arr.length == 0 || times == 0){
return arr;
}
//checks if number of moves can be repeated
if (times > arr.length){
times = times%arr.length;
}
int[] moved = new int[arr.length];
for (int i = 0; i < arr.length; i++){
//for first part of array
if (i < arr.length - times){
//puts elements moved by number of times
moved[i + times] = arr[i];
} else{
moved[i - (arr.length - times)] = arr[i];
}
}
return moved;
}


System.out.println("When you type /list rightMove you can see how to do this exercise");

System.out.println();
System.out.println("That's all for now. Try to do more exercises for example on page https://codingbat.com/java");
System.out.println("Read also documentation and oracle tutorials.");
11 changes: 11 additions & 0 deletions arrays/Q1.jsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
int answer = 0;
int correct = 0;
System.out.println("Which array creations are proper?\na: int[] a = new int[5];\nb: int[5] b = new int[];\nc: int[] c = {1,2,3};\n" +
"d: int d[] = new int[5];\ne: int[] e[] = new int[5];");
System.out.println("1. all");
System.out.println("2. a, c, d");
System.out.println("3. a, b, c");
System.out.println("Please, declare your answer as answer number.");
System.out.println("And /open ../arrays/Q2.jsh");


22 changes: 22 additions & 0 deletions arrays/Q2.jsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
void declare(){
System.out.println("Remember to declare answer");
}
if(answer == 2){
System.out.println("You're right!");
correct++;
} else{
System.out.println("Too bad :(");
}
System.out.println("Possible creations for one dimensional array:\nint[] a = new int[5];\nint b[] = new int[5];\nint[] c = {1,2,3};\n" +
"int[] d = new int[]{1,2,3};\nThe size of the array is not part of its type (which is why the brackets are empty).");
s(4000);
System.out.println();
System.out.println("Next question");
System.out.println("How can't we declare multidimensional array?");
System.out.println("1. int[][] arr = new int[3][];");
System.out.println("2. int[][] arr = new int[3][3];");
System.out.println("3. int[][] arr = new int[][];");
declare();
System.out.println("Write /open ../arrays/Q3.jsh");


20 changes: 20 additions & 0 deletions arrays/Q3.jsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

if(answer == 3){
System.out.println("You're right!");
correct++;
} else{
System.out.println("Too bad :(");
}
System.out.println("Multidimensional array is an array of arrays. Each element contains an array.\n" +
"We must provide at least first size of array. Second size can be add dynamically.");
s(4000);
System.out.println();
System.out.println("Next question");
System.out.println("What will be the size of int[][] arr = new int[3][4];?\nThe result of arr.length");
System.out.println("1. 12");
System.out.println("2. 3");
System.out.println("3. 7");
declare();
System.out.println("Write /open ../arrays/Q4.jsh");


20 changes: 20 additions & 0 deletions arrays/Q4.jsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

if(answer == 2){
System.out.println("You're right!");
correct++;
} else{
System.out.println("Too bad :(");
}
System.out.println("Length is a final field which contains number of elements in array.\n" +
"In multidimensional array each element is an array, but its sizes don't add up.");
s(4000);
System.out.println();
System.out.println("Next question");
System.out.println("What is class and superClass of int intArray[] = new int[3];");
System.out.println("1. class is [I and superClass is java.lang.Object");
System.out.println("2. class is java.lang.Object and superClass doesn't exist");
System.out.println("3. arrays aren't a class");
declare();
System.out.println("Write /open ../arrays/Q5.jsh");


23 changes: 23 additions & 0 deletions arrays/Q5.jsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

if(answer == 1){
System.out.println("You're right!");
correct++;
} else{
System.out.println("Too bad :(");
}
System.out.println("All arrays are an object. Result of arr.getClass() is 'class [I' for int typ,\n" +
"'class [B' for byte type, 'class [S' for short type, 'class [Ljava.lang.String' for String.\n" +
"And the result of arr.getClass().getSuperclass() is 'class java.lang.Object'. \n" +
"Because of that we can call all of Object methods on array.");
s(4000);
System.out.println();
System.out.println("Next question");
System.out.println("What will be the result of arrays comparison:\n" +
"(new int[]{1, 2, 3}).equals(new int[]{1, 2, 3})");
System.out.println("1. true");
System.out.println("2. false");
System.out.println("3. compilation error");
declare();
System.out.println("Write /open ../arrays/Q6.jsh");


21 changes: 21 additions & 0 deletions arrays/Q6.jsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

if(answer == 2){
System.out.println("You're right!");
correct++;
} else{
System.out.println("Too bad :(");
}
System.out.println("Comparison of arrays is \"shallow\", so we don't compare elements of array.\n" +
"To do that we must use Arrays.equals() method.");
s(4000);
System.out.println();
System.out.println("Next question");
System.out.println("What will be the result of such operation:\n" +
"\tint[] max = new int[3_000_000_000];");
System.out.println("1. runtime error");
System.out.println("2. array of size 3_000_000_000 conteining elements equal 0");
System.out.println("3. compilation error");
declare();
System.out.println("Write /open ../arrays/Q7.jsh");


21 changes: 21 additions & 0 deletions arrays/Q7.jsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

if(answer == 3){
System.out.println("You're right!");
correct++;
} else{
System.out.println("Too bad :(");
}
System.out.println("Compilator will tell that \"Integer number too large\", because range of int was exceeded.\n" +
"If you don't know why, go back to primitives questions.");
s(4000);
System.out.println();
System.out.println("Next question");
System.out.println("What will be the result of such operation:\n" +
"\tbyte[] max = new byte[150];");
System.out.println("1. runtime error");
System.out.println("2. array of size 150 conteining elements equal 0");
System.out.println("3. compilation error");
declare();
System.out.println("Write /open ../arrays/Q8.jsh");


Loading