Last active
August 29, 2015 14:16
-
-
Save MarcusVoelker/4f2160e2225aa779dcc8 to your computer and use it in GitHub Desktop.
Some chunks of Java code
This file contains 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
import java.util.Random; | |
public class Main | |
{ | |
private static void doThing(int[] param, int x, int y) | |
{ | |
if (x >= y) | |
return; | |
int i = x; | |
int j = y-1; | |
int p = param[y]; | |
while(i < j) | |
{ | |
while (param[i] >= p && i < y) | |
i++; | |
while (param[j] <= p && j > x) | |
j--; | |
if (i < j) | |
{ | |
int t = param[i]; | |
param[i] = param[j]; | |
param[j] = t; | |
} | |
} | |
if (param[i] < p) | |
{ | |
int t = param[i]; | |
param[i] = param[y]; | |
param[y] = t; | |
} | |
doThing(param,x,i-1); | |
doThing(param,i+1,y); | |
} | |
public static void main(String[] args) | |
{ | |
Random rng = new Random(); //Random number generator | |
int[] xs = new int[100]; | |
for (int i = 0; i < 100; ++i) | |
xs[i] = rng.nextInt(100); //nextInt(x) generates random number between 0 and x-1 (inclusive) | |
doThing(xs,0,99); | |
System.out.print("["); | |
for (int i = 0; i < 100; ++i) | |
{ | |
System.out.print(xs[i]); | |
if (i < 99) | |
System.out.print(", "); | |
} | |
System.out.print("]"); | |
} | |
} |
This file contains 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 Main | |
{ | |
private static int foo(int x) | |
{ | |
if (x != 1 || x != 2) | |
x = 3; | |
return x; | |
} | |
public static void main(String[] args) | |
{ | |
for (int i = 0; i < 3; ++i) | |
System.out.println(foo(i)); | |
} | |
} |
This file contains 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 Main | |
{ | |
private static int foo(int x) | |
{ | |
if (x > x == x < x) | |
x = -x; | |
return x; | |
} | |
public static void Main(String[] args) | |
{ | |
for (int i = 0; i < 3; ++i) | |
System.out.println(foo(i)); | |
} | |
} |
This file contains 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
class A | |
{ | |
public void foo(float f) | |
{ | |
System.out.println("A"); | |
} | |
} | |
class B extends A | |
{ | |
public void foo(int i) | |
{ | |
System.out.println("B"); | |
} | |
} | |
class C extends B | |
{ | |
public void foo(int i) | |
{ | |
System.out.println("C"); | |
} | |
} | |
public class Main | |
{ | |
public static void main(String[] args) | |
{ | |
A a = new A(); | |
a.foo(1); | |
B b = new B(); | |
b.foo(1); | |
a = b; | |
a.foo(1); | |
b.foo(1); | |
b = new C(); | |
a.foo(1); | |
b.foo(1); | |
} | |
} |
This file contains 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
class A | |
{ | |
public void foo(int f) | |
{ | |
System.out.println("A"); | |
} | |
} | |
class B extends A | |
{ | |
public void foo(float i) | |
{ | |
System.out.println("B"); | |
} | |
} | |
class C extends B | |
{ | |
public void foo(int i) | |
{ | |
System.out.println("C"); | |
} | |
} | |
public class Main | |
{ | |
public static void main(String[] args) | |
{ | |
A a = new A(); | |
a.foo(1); | |
B b = new B(); | |
b.foo(1); | |
a = b; | |
a.foo(1); | |
b.foo(1); | |
b = new C(); | |
a.foo(1); | |
b.foo(1); | |
} | |
} |
This file contains 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
class A | |
{ | |
public void foo() | |
{ | |
System.out.println("A"); | |
} | |
} | |
class B extends A | |
{ | |
public void foo() | |
{ | |
System.out.println("B"); | |
} | |
} | |
class C extends B | |
{ | |
public void foo() | |
{ | |
System.out.println("C"); | |
} | |
} | |
public class Main | |
{ | |
public static void main(String[] args) | |
{ | |
A a = new A(); | |
a.foo(); | |
B b = new B(); | |
b.foo(); | |
a = b; | |
a.foo(); | |
b.foo(); | |
b = new C(); | |
a.foo(); | |
b.foo(); | |
} | |
} |
This file contains 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 Main | |
{ | |
public static void main(String[] args) | |
{ | |
boolean c = true; | |
int i = 0; | |
while(c) | |
{ | |
if (i > 5) | |
break; | |
while (i < 3) | |
{ | |
++i; | |
System.out.println(i); | |
} | |
System.out.println(i); | |
} | |
System.out.println("Over."); | |
} | |
} |
This file contains 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 Main | |
{ | |
private static int func(int x) | |
{ | |
if (x < 1) | |
return 1; | |
return x * f(x-1); | |
} | |
public static void main(String[] args) | |
{ | |
for (int i = -1; i < 5; ++i) | |
System.out.println(func(i)); | |
} | |
} |
This file contains 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 Main | |
{ | |
private static boolean funcA(int x) | |
{ | |
if (x == 0) | |
return true; | |
else | |
return funcB(x - 1); | |
} | |
private static boolean funcB(int x) | |
{ | |
if (x == 0) | |
return false; | |
else | |
return funcA(x - 1); | |
} | |
public static void main(String[] args) | |
{ | |
System.out.println(funcA(2) ? "true" : "false"); | |
System.out.println(funcA(0) ? "true" : "false"); | |
System.out.println(funcA(1) ? "true" : "false"); | |
System.out.println(funcA(5) ? "true" : "false"); | |
System.out.println(funcA(128) ? "true" : "false"); | |
System.out.println(funcA(2015) ? "true" : "false"); | |
} | |
} |
This file contains 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 Main | |
{ | |
private static int a(int x, int y) | |
{ | |
if (x == 0) | |
return y+1; | |
if (y == 0) | |
return a(x-1,1); | |
return a(x-1,a(x,y-1)) | |
} | |
public static void main(String[] args) | |
{ | |
System.out.println(a(2,2)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment