Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

hardware: cpu, ram, secondary storage devices, input/output devices; central processing unit - chip that executes

program commands; ram - primary storage for programs/data in active use; ssd - long term storage, input - keyboard,
touchscreen, microphone, scanner; output - printer, speaker
software: programs/data; program - series of instructions; operating system - controls all machine activities, provides user
interface to computer, manages resources such as cpu and memory; application program - generic term for any other kind
of software

In java, program is made up of 1+ classes, class contains 1+ methods, methods contain programming statements; always
contains method called main.
class { public static void main(String[] args) { } } identifiers: names in program, can only have letters, digits, _, $, cannot
start w/ number, no spaces
Escape sequences: System.out.println(“ \”quote\” “); \b - backspace, \\ - backslash

Scanner: import java.util.Scanner; (before class) Scanner scan = new Scanner(System.in); x = scan.nextLine();
nextInt() next(); (for string) nextDouble();

String is in java.lang, ln adds line after printing; use + to add different strings together, also if print statement takes up 2
lines
out.print(“ “ + 24 + 45); //2445 out.print(24+45) //69 String s1 = “ab “ s2 = “bc” s1.length(); //3, includes space and
any punctuation
s3 = s1.concat (s2); //ab bc s4 = s3.replace(‘c’, ‘a’); //ab ba s5 = s4.toUpperCase(); //AB BA
s6 = s5.substring(3); //BA
s1.equals(s1Copy)) to see if they have same characters, do not use ==

Variables: = is assignment operator, assigns value on right to left int temp, num1 = 111, num2 = 222; temp = num1; num 1
= num2; num2 = temp;
use final for constants (final double), constant name in all caps, constant value cannot be changed

Prim data types: integers (byte short int long) floating point numbers (float, double) characters (char) and boolean
byte: 8 bits, -128, 127 short: 16 bits, -32,768, 32,767 int: 32 bits, 2 bil, long: 64 bits, 9*10^18 float: 32 bits, 3.4*10^38 w/ 7
sig digits
double: 64 bits, 1.7*10^308 w/15 sig digits char: stores single char, use ‘ ‘

Arithmetic expr: double x 3.0/2; // 1.5 double x 3/2; //11’s: x%10 10’s: (x/10)%10 x=10 y = x++; //y = 10, x = 11
z = ++x; //z = 12, x = 12

Multiple assign: int x, y, z, w; x = y = z = w = 15; Combined assign: +=, -=, *=, /=, %=, x+=5; //x = x+5

DecimalFormat: import java.text.DecimalFormat; DecimalFormat fmt1 = new DecimalFormat(“$0.00”); double num1 =


3.125; System.out.println(fmt1.format(num1)); //$3.13 num2 = 1.4, fmt1.format(num2)); //$1.40
fmt2 = new DecimalFormat(“$0.##”); fmt2.format(num2)); //$1.4 for commas (“$##,###.00”);
if you want to use percentages (“0.00%”); make sure decimal is equivalent (0.10 = 10%)

Math class: part of java.lang Math.abs(x); //absolute value Math.exp(x); //e^x Math.log(x) //base-e log
Math.log10(x); //base-10 log Math.round(x); //rounding Math.sqrt(x); //square root Math.pow(x, z); //x^z
BE VERY CAREFUL WITH STATEMENTS

Random class: import java.util.Random; Random rand = new Random();


x = rand.nextInt(); //ranges over all possible int values (no decimals) rand.nextInt(num); //from 0 to (num-1)
nextFloat(); //[0,1)
nextFloat()*y; //[0,y) nextInt(6)+1; //1 to 6 nextInt(10)-5; //-5 to 4

== != < > >= <= DIFFERENCE BETWEEN EQUALITY (==) AND ASSIGNMENT (=) OPERATORS

Operator precedence rules: Arithmetic > relational > logical


(), sign change, * / %, + -, < > >= <=, == !=, !, && ||, = += -= *= /= %=

if...else: even numbers: (x%2 = 0) flag: boolean passed = true; if (passed) //if true if (!passed) //if false
use if else for mutually exclusive statements, otherwise multiple ifs (if (temp > 100) { (if temp > 120))
if (score >= 0 && score =< 100), if (score < 0 || score > 100)

switch: switch (Integral_Expression) { case 1: (tab) statement; break; case 2: (tab) statement; break;
default: (tab) statement; break; }
Integral_Expression must be integral data type (char, int, boolean)
WATCH WHERE THE BREAK IS (system continues to compute until it reaches break)

You might also like