You are on page 1of 7

JAVA SHEET CHEAT

import java.util.*;

INPUT
Scanner sc = new Scanner(System.in);
int i = sc.nextInt(); //stops at whitespace
String line = sc.nextLine(); //whole line
System.out.println(“bla”); //stdout
System.err.print(“bla”); //stderr,no newline

MATH
math.sqrt()
math.pow()
math.round()
math.ceil()
math.floor()
Math.max(x,y)
Math.min(x,y)
Math.abs(x); // absolute value
Math.random(); // 0 to 1
int randomNum = (int)(Math.random() * 101); // 0 to 100

RANDOM
Random rand = new Random();
int rand_int1 = rand.nextInt(1000); // random int 0 to 999
double rand_dub1 = rand.nextDouble(1000); // random double 0 to 999

METHODS
static int name(int x, int y) {
return x + y;
}
public static void main(String args[]) {
System.out.println(add(2, 3));
}
ARRAYS
String[] people = new String[3]; // ARRAY WITHOUT CONTENT
String[] people = { "Max", "Anna" };
System.out.println(people[0]);

PRINTING AN ARRAY
for (int i = 0; i < people.length; i++) {
System.out.println(people[i]);
}
CYLCIC
package Drushim;

public class cyclic {

public static char[] Cyclic(char[] arrCyc, int move) {

char[] arrCycCopy = arrCyc.clone();

for(int i = 0; i < arrCyc.length; i++) {


int newIndex = (i+move) % arrCyc.length;
arrCyc[newIndex] = arrCycCopy[i];
}
return(arrCyc);
}

public static void printCharr(char[] arr) {


for(int i = 0; i < arr.length; i++)
System.out.println(arr[i]);
}

public static void main(String[] args) {


char[] array = {'a','b','c','d'};
printCharr(Cyclic(array, 3));

}
SORT ARRAY
package Drushim;

public class sortArray {

public static boolean megaSorter(int[] arr) {


int sort1=1;
int sort2=1;
for(int i=1; i<arr.length; i++) {
if(arr[i] > arr[i-1]) {
sort1++;
}else {
sort1=1;
}

if(sort1 > sort2){


sort2=sort1;
}
}

return sort2 > arr.length/2 ;

public static void main(String[] args) {


int[] arr = {1};
boolean arrMegaSorted = megaSorter(arr);
System.out.println(arrMegaSorted);
}

}
RANDOM D&D AT HOME
package Drushim;
import java.til.*;

public class randomOrSmth {

public static void main(String[] args) {


Random rand = new Random();
int hp = 10;
int count = 0;
for(int day=1; day <= 7 && hp > 0; day++) {
int spells = rand.nextInt(20)-10;
if(spells<0) {
System.out.println("day "+day+": negative spells: "+spells);
hp+=spells;
} else if (spells>0){
System.out.println("day "+day+": good spells: "+spells);
hp+=spells;
} else {
count++;
}
System.out.println("lives "+hp);
System.out.println(count);
if(hp>0) {
System.out.println("The Dragon has won!");
} else {
System.out.println("The Dragon has been Defeated!");
}
}
}

}
IDK
package Drushim;

import java.util.*;

public class classAvg {

public static void main(String[] args) {


Scanner scan = new Scanner(System.in);
System.out.println("How many classes?");
int classes = scan.nextInt();
int totalGrades=0;
for(int i = 0; i<classes; i++) {
System.out.println("how many students in this class?");
int students = scan.nextInt();
System.out.println("write the student's grades?");
for(int j = 0; j<students; j++) {
totalGrades += scan.nextInt();
}
System.out.println("avg grade: "+totalGrades/students);
totalGrades=0;
}
}

You might also like