0% found this document useful (0 votes)
17 views

Cheatsheet

hh

Uploaded by

Asif Mahmud
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
17 views

Cheatsheet

hh

Uploaded by

Asif Mahmud
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 2
Input Scanner in = new Scanner(System. in); 7/ Canalso use new Scanner(new File(*input..txt’ int _n = in.nextIntO; double x = in.nextDouble(); String word = in.next(i String line = in.nextLineQ; while (in.hasNextDouble()) { double x = in.nextDouble() ; Process x y ‘Satoat Doo vt advan eo line. Systen.out.print("Enter a value: Use + te concatenate values. System. out.printIn("Volume: " + volume); Field width ——_Preision ——_ / System.out.printf("%-10s %10d %10.2f", name, qty, price); iN Lefi-jutifed string Integer Floating-point number try (PrintWriter out = new PrintWriter("output. txt")) { Mite fe out ate prinverinelifprimcf maths ‘The output is closed at the ond of. the try-with-rosourees statement, Arrays Element Ele it Length peo tie JOM iain aa: ne(] numbers = new int (51: int(] squares = (0, 1, 4, 9, 16}; int{10 magicSquare = 3, 2, 13}, 10, 11, 8}, 6, 7, 12h, 15, 14, 1 for Cint i = 0; { numbers[i] = i * i; } 4 < nunbers.length; i++) for (int element : nunbers) Process element } System.out.printIn¢Arrays. toString nunbers)); 17 Prints (0, 1, 4, 9, 16] Array Lists Use wrapper type, Integer, Double, Iuitially emply tt., for primitive types. Y toma ArrayList nanes = new ArrayList; ‘Add elements to the end names. add("Ann""); ames. add("Cindy"); // [Ann, Cindy], names.size() is now 2 names.add(1, "Bob"); // (Ann, Bob, Cindy] rnanes.renove(2); // (Ann, Bob] nanes.set(1, "BiT1"); // (Ann, 8111] String name = nanes.get(0); // Gets “Ann Systen.out.printIn(nanes); // Prints (Ann, 8111) ked Lists, Sets, and Iterators LinkedList names = new LinkedList<>(; nrames.add("Bob"); // Adds at end ListIterator iter = nanes.listIterator(; iter.add("Ann"); // Adds before current position String name = iter.next(); // Returns “Ann” iter.removeQ; // Removes SeteString> nanes = new HashSete>Q; nanes,addC"Ann"); // Adds to set if not present nanes.renove("Bob"); // Removes if present Iterator iter = nanes.iterator(); while Citer.hasNext) { Process iter.next() } Maps ia type Nile type Map scores = new Hashlap<>0 scores.put("Bob", 10); Returns nut if hey wot present Integer score = Scores .get("Bob") ; for (String key : scores. keySet()) { Process key and scores.get(key) } Variable and Constant Declarations Tye Name Initial value wy / ‘int cansPerPack final double CAN_VOLUME = 0.335; Method Declaration Parameter Modifiers and name / oN a / public static double cubeVolune(double sideLength) { double volume = sideLength * sideLength * sideLength; return volume; } Exits method and roluns result, Return type / Mathematical Operations Math.pow(x, y) Math. sart(x) Nath. 10g10¢x) Nath. abs (x) Nath. sin) Math. cos (x) Math. tan(x) Raising toa power x? Square root [x Decimal log log;o(x) Absolute value || } se cosine, tangent of x (¢ in radians) Selected Operators and Their Precedence (See Appendix B for the complete list.) Array element access Increment, decrement, Boolean not ication, division, remainder Addition, subtraction Comparisons Equal, not equal Boolean and Boolean or = Assignment Loop Statements Condition 7 while (balance < TARGET) { , years+s Executed balance = balance * (1 + rate / 100); [ condition is true } Initialization Condition Update / Toad for Cint i = 0; i < 10; i++) t ‘System.out .printin(i);, } Sot to now clomen String Operations String s = "Hel10"; int n= s.lengthO; // char ch = s.charAt(l); // 'e' String t = s.substring(1, 4); // "el" String u = s.toUpperCase(); // "HELLO" if (u-equals("HELLO")) ... // Use equals, not for (int i = 0; i < s.lengthO; i++) { char ch = s.charAt(i); Process ch , Con nal Statement if floor >= 13) > Condition { actualFloor = floor + Executed when condition is true b sl IF Wa >=) > Secend condition (optional) actual Floor = Floor: Cs Executed when System.out.printIn("Floor negative"); | ll condi } false (op Class Declaration public class BankAccount { private double balance; Instance variables private int transactions public BankAccount (double initiaiBalancey { Constructor balance = initialBalance; transactions = 1; } public void deposit(double anount) { Method balance = balance + amount; transactions++; } + Lop body executed e at least onee System.out.print("Enter a positive integer input = in.nextIntO; } while Cinput <= 0); yo collection for (double value : values) x bee for each element sun = sum + value; }

You might also like