JAVA ARRAY CHEAT SHEET, Summaries of Computer science

ARRAY AND RANDOM FUNCTION CHEAT SHEET

Typology: Summaries

2019/2020

Uploaded on 11/13/2021

unknown user
unknown user 🇨🇳

1 document

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
JAVA ESCAPE SEQUENCE (some)
public class tutorial {
public static void main(String[] args) {
// ESCAPE SEQUENCE
// \n
System.out.println("Creates a new line\n");
// \t
System.out.println("\tInserts a tab");
// \'
System.out.println("Insert \'single\' quotation mark");
// \"
System.out.println("Insert \"double\" quotation mark");
// \\
System.out.println("Inserts a \\ or backslash");
}
}
pf3
pf4
pf5
pf8

Partial preview of the text

Download JAVA ARRAY CHEAT SHEET and more Summaries Computer science in PDF only on Docsity!

JAVA ESCAPE SEQUENCE (some) public class tutorial { public static void main(String[] args) { // ESCAPE SEQUENCE // \n System.out.println("Creates a new line\n"); // \t System.out.println("\tInserts a tab"); // ' System.out.println("Insert 'single' quotation mark"); // " System.out.println("Insert "double" quotation mark"); // \ System.out.println("Inserts a \ or backslash"); }

JAVA PRINTF

public class tutorial { public static void main(String[] args) { // PRINTF // %n specifier System.out.printf("This should inser an enter after this %n Did it work?\n"); // integer int age = 18 ; System.out.printf("Your age is %d%n", age); // char char symbol = '@'; System.out.printf("The symbol you chose is %c%n", symbol); // float double float p = (float) 3.14; double e = 2.71828; System.out.printf("The pi is %f, and the value of e is %f%n", p, e); // boolean boolean online = true; System.out.printf("Is itchy online? %b%n", online); // String String name = "Itchy"; System.out.printf("Your name is %s%n", name); }

JAVA ARRAY

public class tutorial { public static void main(String[] args) { // JAVA ARRAY // one dimension array int[] array1 = { 1 , 2 , 3 }; // declaring the size of the array and using index to assign a value int[] array2 = new int[ 3 ]; array2[ 0 ] = 1 ; array2[ 1 ] = 2 ; array2[ 2 ] = 3 ; // string array // editing array on runtime String[] names = {"Johnny","Mike","Harold"}; names[ 2 ] = "Itchy"; System.out.println(names[ 2 ]); }

JAVA 2D ARRAY

public class tutorial { public static void main(String[] args) { // JAVA 2D ARRAY // int int[][] ages = { { 11 , 14 , 16 }, { 24 , 26 , 27 }, { 31 , 35 , 36 } }; System.out.println(ages[ 1 ][ 0 ]); // String // editing 2d array during runtime String[][] names = new String[ 2 ][ 2 ]; names[ 0 ][ 0 ] = "John"; names[ 0 ][ 1 ] = "Mike"; names[ 1 ][ 0 ] = "Mica"; names[ 1 ][ 1 ] = "Charlie"; // change of value names[ 1 ][ 1 ] = "Rodney"; System.out.println(names[ 1 ][ 1 ]); }

JAVA ARRAY EXERCISE [get the average of int in an array] import java.util.Scanner; public class tutorial { public static void main(String[] args) { // JAVA ARRAY [EXERCISE] // scanner object Scanner input = new Scanner(System.in); // array declaration float[] numbers = new float[ 3 ]; System.out.print("Enter the first number: "); numbers[ 0 ] = input.nextInt(); System.out.print("Enter the second number: "); numbers[ 1 ] = input.nextInt(); System.out.print("Enter the third number: "); numbers[ 2 ] = input.nextInt(); // array length float arrLen = numbers.length; // average computation float average = (numbers[ 0 ] + numbers[ 1 ] + numbers[ 2 ])/ arrLen; // final printing System.out.println("The average of your numbers is: " + average); input.close(); }

JAVA 2D ARRAY EXERCISE [IGN generator using 2d array] import java.util.Scanner; public class tutorial { public static void main(String[] args) { // JAVA 2D ARRAY [EXERCISE] // scanner Scanner input = new Scanner(System.in); // ign variables String[][] ign = { {"red","purple","blue"}, {"kind","normal","naughty"}, {"explosive","exisitng","chilling"} }; // ign decisions System.out.print("How do you like colors? [1] warm, [2] neutral, [3] cold: "); int dec1 = input.nextInt(); System.out.print("What kind of person are you? [1] kind, [2] normal, [3] naughty: "); int dec2 = input.nextInt(); System.out.print("How do you like living? [1-3] 1 being best, 3 being low: "); int dec3 = input.nextInt(); // ign building String chosen1 = ""; String chosen2 = ""; String chosen3 = ""; // chosen conditions // chosen if (dec1 == 1 ) { chosen1 = "red"; } else if (dec1 == 2 ) { chosen1 = "purple"; } else if (dec1 == 3 ) { chosen1 = "blue"; } // chosen if (dec2 == 1 ) { chosen2 = "kind"; } else if (dec2 == 2 ) { chosen2 = "normal"; } else if (dec2 == 3 ) { chosen2 = "naughty"; } // chosen if (dec3 == 1 ) { chosen3 = "explosive"; } else if (dec3 == 2 ) { chosen3 = "exisitng"; } else if (dec3 == 3 ) { chosen3 = "normal"; } //ign printing System.out.printf("Your IGN is: %s_%s-%s", chosen1, chosen2, chosen3); input.close(); }