Java Midterm Cheat Sheet PDF

Title Java Midterm Cheat Sheet
Course Introduction to Programming Using Java
Institution Johns Hopkins University
Pages 2
File Size 58.5 KB
File Type PDF
Total Downloads 69
Total Views 170

Summary

Java Midterm Cheat Sheet for Exam 1. Professor Selinski...


Description

● Java compiling process=Compilation -> bytecode -> JIT (just in time) execution // “Write once, run anywhere” ● Code Design Process = Analyzing problem -> design phase -> code -> testing and debugging -> maintenance ● Primitives = Int, float, double, char, boolean, etc ○ Data type for storing info, do not have any special methods associated with them compared to objects like string (what you see is what you get) ● Integer Division = Double res = (5*5) / 2 = 12.0 ● Type casting =Implicit Typecasting: changing type of var without using explicit syntax to do so ○ Double d = 3 // d becomes 3.0 ○ Int c = ‘a’ // c gets ascii value for a ○ Explicit type casting ■ Int c = (int) 3.5 ■ String -> primitive ● Int x = integer.parseInt(“5”) ● Math class ○ Double x = Math.cell(double) // returns nearest high integer ○ Double x = math.floor(double) // returns nearest low integer ○ Int x - math.round(float)// returns integer ● Shorthand Operators ○ x += 1 == x = x +1 ● Printing to screen ○ System.out.println == next line character at the end, future words will be on new line ○ System.out.printf == allows you to format a string in conjunction with the use of variables ■ System.out.printf(“%4.3”, 3.45678) -> 3.457 ● Concatenation = + operator used to concatenate strings in java ● Java libraries (Scanner) = import.java.util.Scanner; to import extra tools ○ Scanner keyboard = new Scanner(System.in); ■ For scanning a string, instead of system.in you put in the name of string or variable ■ Scanner kb = new Scanner(“hello my name is”); ■ System.out.println(kb.next()) -> hello ○ .next( ) -> takes next token of input as string “Hello my name is” = kb.next ( ) will return “Hello” ○ .nextLine(), = Takes entire line of input until newline character and moves cursor to next Line ○ .nextInt() = Takes next token of input as integer //1 2 3 4 it will return 1, if not integer return “error” ○ .nextDouble() =1.0 2.0 3.0 it will return 1.0 or error ○ NEXT CHAR IS NOT A THING ● Booleans = useful primitives that help in many cases, such as flow control Ex: boolean valid = false; ● String = Not a primitive // Substring(start_index, end_index) // (inclusive, exclusive) ○ charAt(index) =Returns character at a certain index

○ length() =Returns length of string in terms of characters ○ .equals(string) // Cannot use == for strings ○ .equalsIgnoreCase(string) =Compares strings regardless of capitalization ○ Escape characters =Need to use special syntax to place certain characters in string ● Ex: String s = “hello my name is \”Adam\”” // Quote is escaped using \” ○ If (condition) // Else if (another_condition) // Else = Do this if all else fails no matter what ○ Loops // While loop = Condition can be any boolean expression ○ For loop = Helpful for scanning strings, arrays, with an index but also can be used for any purpose ○ Do while = Useful for games, always executes at least once regardless of condition 1) initialization of variable 2) set terminating condition 3) stuff to be done in loop 4) mechanism to increment variable Checkstyle = Java doc: /** @param argument names description // @return what the function returns // */ Magic number =Final int name = 5; declared in main // Indentation level, multiples of 3 ● Operator Precedence = Array access [] -> () method invocation -> ! (not operator) -> typecasting -> traditional PEMDAS math -> equality == -> and/or operators import java.util.Scanner; Top-down = outside in public class GPA { Start with high level version of program, leave big chunks undefined /** Main method, start of execution. work thru the big chunks, breaking into smaller and smaller chunks @param args not used Bottom-up = inside out public static void main(String[] args) { Start with smallest chunk that solves a required task, gradually build it up into Scanner in = new Scanner(System.in); bigger and bigger chunks to solve the whole problem Pseudo-code: algorithms in English-like notation 3) High-Level Specialty: SQL, R Scripting: Python, Javascript, Ruby Object-oriented: C++, Java, C# Structured: FORTRAN, C, MATLAB 2) assembly: short-hand code for manipulating the hardware; Java JVM bytecode 1) Binary (1s and 0s), machine, executable Identifiers: Used for names of: classes, methods, variables ! Contain: letters, $, _, digits but not as first character ASCII: z = 122, a = 97, Z = 90, A = 65 ! means "not" !true => false, !false => true true && true => true / true && false => false false && / true => false / false && false => false || means "or" / true || true => true / true || false => true / false || true => true / false || false => false False and / true or...


Similar Free PDFs