APCSA Overview

Ace your homework & exams now with Quizwiz!

parseInt

-parseInt() method is part of int class -converts String that only consists of #s into int value while assigning it to primitive data type int -general format: Integer.parseInt(String name) -returns int, not object from Integer class -only works if String consists solely of #s & no characters

logical complement

-represented by "!" -changes true to false and vice-versa -use {} to section off NOT operator

ceiling

-return largest whole # greater than/ equal to the input -similar to floor -does not truncate EXCEPT if input is between 0 & -1.0, which returns -0.0 -general format: Math.ceil(parameter)

equals

-since every String is an object, "==" operator checks whether reference (not value) for String objects is the same -equals() method checks whether they hold same value (text) -general format: Stringname.equals(Stringname); -== only works if 1st String directly assigned to 2nd String value

concatenation

-takes 2 Strings & forms new String by putting original 2 Strings together -general format: varOne,concat(varTwo) //concatenates varOne & varTwo **AKA varOne + varTwo = varThree; **keep in mind that if the operand before "+" is a String, everything after will be changed to a String

ToString

-takes almost any objects & converts it to a String -allows for printing of objects that cannot be printed directly -general format: public static String example1 = datatype.toString(example VarTwo);

square root

-takes/ returns double EVEN if result is a perfect square -returns rounded positive square root -general format: Math.sqrt(parameter) -if use number less than 0, result = NaN (Not a number), since imaginary numbers cannot be represented -ONLY returns 1 positive sqrt value

2D array length

-use arrayname.length to determine # rows in 2D array (because length = # of rows) -to call length of column, pinpoint row before using ".length' -to check how many columns the x row of an array has, call exampleVariableOne[x-1].length

indentation

-used to make code readable -increase indentation by 2 spaces for statements in {} & keep consistent

fields

-variable inside a class -to declare: dataType variableNameOne, variableNameTwo; -to initialize: dataType variableNameOne = initialValueOne, variableNameTwo = initialValueTwo;

field variables

-variables declared as a member of a class or declared outside of any method/ constructor within the class -can be called by any method in the class -available as long as it belongs to an active instance (i.e. any method in the class is used)

local variables

-variables declared inside a method that can only be utilized & referenced in the method -if we want to use it in multiple classes, take it out of the method, otherwise there will be a compile-time error

Every public class must be in a separate "_______" file and the filename should match the class name.

.java

access control

Ex: public or private -when unspecified, becomes private (which means it can only be used by methods of THAT CLASS and not the main method)

switch statement

compares different primitive data types, String values, & other objects to test whether they are equal to a certain value -just 1 case is selected per execution of switch statement, & data type of expression must match datatype of label **if no case labels match value of expression, default case executed **break statement follows each to stop anything after case from being executed

methods

constructed out of statements between {} -always followed by () -general format: {visibility}{returnType}{functionName}{type parameterName} **MUST HAVE a returnType & functionName -void return type, so it doesn't return anything -if more than 1 parameter, separate using commas --all parameters are copies of the original value, & original value WILL NOT CHANGE unless assigned to something else -does not require visibility (can be public/ private in header)

1D Array Length

# elements it can hold -last index of array: array.length -1 -length() method in String class works different than array.length();

String length

# of characters (e.g. punctuation/ spaces) in a String -general format: Stringname.length; //name.length tells program to use length method in String class -empty String has 0 length -takes no parameters BUT () are necessary syntax to call the method

encapsulation

withholding object details from other parts of program while allowing its use (fields & methods hidden in object) -object only used through access methods (keep object consistent & secure)

iterating through 2D array

-nested for loop: 1 for loop inside another -1st for loop loops through each row of 2D array one by one as 2nd (nested) for loop loops through columns -nested for loops run row by row to check each column within row before moving on to next row

ArrayLists

-ArrayList object contains array of object references & methods for managing array -elements must be object references, not primitive data -has to be imported from java.util package through java.util.ArrayList -general format: ArrayList<object Type> name = new ArrayList<objectType> (); -by default, ArrayLists have 10 cells , but to start with an initial capacity you can put the # in () -doesn't limit size ArrayList can expand to since size will automatically increase with no info lost

object equality

-REMEMBER default objects method only checks whether 2 objects have SAME REFERENCE ⊕returns false for exact same statements because objects are individual -to solve?: create equals() method to override default

immutability

-String objects designed such that there is no way to alter their data once object is created -BUT EVEN IF YOU CAN'T CHANGE, you can still reassign String references, which only replaces reference (i.e. both Strings exist but only 1 is used) Ex: String varOne = "Hi"; //remains unaltered since OBJECTS are immutable //varOne is a reference variable that DOESN'T contain the object, only its reference

increments

-can exist as either prefix or postfix (++variableName or variableName++) -same for decrement (--variableName or variableName--) -what is the difference?: in the postfix, the original value is returned before it is incremented or decremented

type casts

-casting for objects -general format: (requiredType)(expression/ variable) -consider: is class a descendant of another class (has parent class)?→ if yes, can create objects incorporating the 2 classes -EVERY class is a descendant of class called Object in java.lang -BUT can't actually call method OR access variable from TypeCastTest Class because have to specify which instance you're referring to when accessing any class members -Ex: if call Object exampleVariableOne = newTypeCastTest() →exampleVariableOne becomes instance of Object AND TypeCastTest

compareTo

-compares 1st letter of 1 String object to 1st letter of another String object & returns # indicating which comes 1st in lexicographic order (A, B, C, etc.) -if 1st 2 letters of String are same, next letters compared, etc. until it finds 1st instance of difference -returns 0 if 2 Strings are equal, # less than 0 if 1st String larger, & # > 0 if 2nd String larger -uppercase > lowercase -general format: exampleVarOne.compareTo(example varTwo)

2D arrays

-index of both row & column start at 0 -general format; varOne [row][column] -all elements must have same data type

IndexOf

-indexing starts with 0 1. String.indexOf() returns index within String of 1st occurrence of specified character or -1 if character doesn't occur -can put valid index after String Ex: String varOne = "Hello Word!"; System.out.println(varOne.indexOf('W'));

Upper & Lower Case

-makes String all upper or lower case -general format: toLowerCase() & toUpperCase() -can also use for personal preference or when comparing 2 Strings while ignoring case differences [can also use compareToIgnoreCase() & equalsIgnoreCase()]

charAt

-method returns single character from specified index -general format: stringname.charAt(any integer); -return value: single character char, NOT String -index CAN'T be negative/ greater than Stringname.length() -1

How do you initialize 2D arrays?

1) datatype[][] arrayname = new data type [3][1] //creates 2D array with 3 rows & 1 column 2) datatype [] arrayname ] {{varOne, varTwo},{varThree, varFour}} creates 2D array with 2 rows & 2 coumns Ex: to assign 'A' to 3rd row & 1st column: NOT varOne[3][1] = 'A' BUT varOne [2][0] = 'A'

null value

1. "Null" signifies "no object" 2. happens when declare new String object & forget to initialize -can also be initialized with null when: String varOne = null; -String class holds reference to object, so null tells us nothing is referenced by the String -not when String initialized to empty String value

What are the 2 main features of programs?

1. class definition: keyword class begins class definition for class named name -code for class between {} -name in UpperCamelCase 2. main method: declared with signature

Strings

1. creating Strings: use double quotes or create new object -String stringName = "some"; -String stringName = newString("some"); **use new because String is a class in java.lang package -empty String creation: leave() blank or (" ")

What are the 3 parts of a class?

1. fields: contain declared/ initialized variables 2. constructors: initialize class objects 3. methods: sets of code referred to by name & called upon at any point in the program

What are the 2 types of field variables?

1. instance variables: non-static fields with values unique to each instance of the class 2. class variables: declared with static

naming conventions

1. lowerCamelCase: method, non-constant field, & parameter names 2. UpperCamelCase: class names 3. CONSTANT_CASE: constants

static

1. static variables belong to class rather than class instance 2. only allocated to memory 1 time (when class loads) 3. don't need to create new object to access them -call class name & use dot notation to specify member you want to access

super

Java reserved word super calls method in superclass to invoke constructor of superclass (REMEMBER private methods can't be accessed by subclass) -super usually used to call constructor of superclass, but can also be used to call methods

What is the general format for adding ArrayList methods?

Since ArrayList is class.... ArrayListName.method;

Substring

String made from another String -to make: 1) substring(int startIndex) returns new String with characters between "startIndex" & final index of called String 2) substring(int startIndex, int endIndex) -if startIndex = total String length, empty String created -returns new String with characters starting at startIndex & ending at endIndex - 1 (includes 1st character & excludes last character of parameters entered)

How do you read Strings?

String name = readLine("Enter name:"); System.out.println(name);

printing

System.out.print/println(" "); -"System" accesses all methods/ classes of the System class, whereas "out" accesses all methods/ classes related to output

relational operator

a type of arithmetic operator that tests relations between 2 operands (==, !=, >=, <=, <, >_ -also += and -= to add/ subtract values together BEFORE assigning them to a variable

dot notation

accesses members of classes (variables & methods) -only public methods/ variables can be accessed through object -trying to access private member of class →run time error -this.variableName is a reference to the current object whose method/ constructor is being invoked (but usually necessary)

casting

action of converting between 2 data types -Ex: double to int cuts off decimal places -format: (int) (# to be converted) -converting int to a double adds .0 to the int

escape sequence

allow you to get out of String to print characters that cannot exist within the String 1. \\ permits '\' to be included in String with 1 '\' used to escape String & 2nd added to String itself 2. \" on each side of characters allows for String to print " " in output 3. \n makes new line

conditional AND

allows for checking whether 2 values are both true/both false before statement executed -only true if expressions on either side of && are true

inheritance

allows programmers to define new classes based on an existing class -build on "superclass" & add more variables & methods -divisible into subclasses, which are used to construct objects that look like superclass's objects, but with added features -for subclass use Java reserved word extends -to create objects: SuperClass objectname = new SubClass(); BECAUSE subclass extends from superclass

How do you read booleans?

boolean varOne = readBoolean("varOne true or false?"); System.out.println(varOne);

break

breaks loop in program & continues running statements after loop -general format: if(condition) { break }

compiler

creates Java bytecode files from each class in source file -file extension: ".class" to ensure that no matter what operating system, Java bytecode can run & be read by any JVM (Java Virtual Machine

trim

creates new String without added spaces at start or end while leaving spaces in between -useful with input user data -general format: (varOne.trim())

else if statements

creates unlimited amount of outcomes for the programmer -each statement has own expression that returns true/ false -only 1st expression that evaluates to true is executed & everything else is ignored -if none true → else executed

interpreter (JVM)

decodes & executes every bytecode statement for JVM

How do you read doubles?

double varOne = readDouble("Enter varOne: ") System.out.println(varOne);

while statement

executes statements while result from block (group of statements in {}) is true & terminates when result is false

Math class

functions mostly use double as parameter & return double value Ex: Math.PI represents value of pi ** period used to access field & method in math class -to avoid syntax errors, methods & field names have to match names written in Javadoc for mathclass

runtime errors

happen after code is compiled & causes the program to crash because program cannot understand it -Ex: division by 0 -can be fixed by isolating with comments

float

have decimal point & no commas -represented "f" or "F" at the end to ask for a single-precision point literal to deal with 32-bit floating point **even without "f," it assumes float is declared & initialized -used with decimals, but double preferred since double uses 64 bit numbers, so might be rounded as a float

char (AKA character)

holds single character with apostrophe on each side -special chars (punctuation & spaces) can be represented

mixed expressions

if any operands are floating point, the whole arithmetic expression becomes floating point

iterating through 1D array

in for statement, condition count< exampleVariableOne.length ensures that every element in array is added to sum before for loop terminated -"<" because last array index is array.length-1, so count cannot equal/ be greater than exampleVariableOne.length -System.out.println(array name) won't work

Describe the indices of an ArrayList.

index of ArrayList starts at 0 & cannot = the size of the ArrayList

How do you read integers?

int varOne = readInt("Enter varOne: "); System.out.println(varOne);

Java Class File (.class)

loaded by JVM & executes main method (entry point for application)

for statement

loop control statement that allows for 1 or more statements to be run many times in succession -general format: for(initializing; control; step) 1. initializing initializes new variable 2. control is tested every time the loop executes (if true, loop continues to run, but if it's false the loop terminates) 3. step changes value of initialized variable by whatever is necessary ("+=,"-=," "++," "--")

infinite loops

loops that never end (i.e. never return false) -TIP: to debug, print out loop control variable after running statements inside loop to check what cause is

doubles

might have "d" or "D" at end -default: 0.0

syntax errors

misuse of reserved words, misspell variables/ function names or missing, etc.

expressions

mixtures of literals, operators, variable names, & () used to calculate a value -expression on the right evaluated 1st -any # with decimal becomes float or double

What are the properties of objects?

objects have 3 properties: 1) each object has a unique "identity" that distinguishes it 2) each object has current "state," meaning it contains a value with the ability to change 3) each object has behavior, which enables it to call & run methods -refer to instance of class, which allows for creation of class instances & storage of them in variables called objects

logic errors

occur after program runs when prints incorrect/ unexpected output -debug by printing different variables & validating values

conditional OR

only checks if 1 of the boolean values is true

loop control variables

ordinary int variables used to dictate how many times loop will execute -not all loops have them -normally incremented/ decremented by a certain value every time loop is executed (required because otherwise infinite loop) -any variable inside loop can only be used in loop body

imports

pre-built classes & methods organized into packages 1. use Java.util.class (means that in this package there is a class with this name) 2. use ".*" to import ALL CLASSES in package -java.lang package auto-imported (e.g. String, Math)

How do you declare classes?

public class(parameters) { class body}

arrays

used to systematically organize & process data -made of cells used to store a value -all values must be same data type -to initialize 1D arrays: 1) datatype[] arrayname= {valueOne, valueTwo, valueThree} 2) datatype[] arrayname = new data type [array length]; **length can't be altered once given -to access elements in array: arrayname[index]

constants

variables that don't change, & will throw a compile time error if changed -include final -if you want to change its value, change the value of what variable is initialized

long

range for a long is greater than that of an int -can use "l" or "L" at end -useful for HUGE #s -uses scientific notation (e.g. 1.51E + 1 = 1.51 * 10^1) -scientific notation can be printed as an output if passes a certain number

byte

range: -128 to 127 -should only use if SURE values will not exceed this range -default: 0 -use int

short

range: -32, 768 to 32, 767 -just use int

absolute value

returns absolute value of an int or double -general format: Math.abs(parameter) -returns same data type as input, & no outputs are rounded

floor

returns largest whole # that is less than or equal to input -ONLY takes/ returns double values, EVEN if non-decimal inputted -since data precision is not hindered when converting from int to double, Java will not throw any errors -primary use: rounding -general format: Math.floor(parameter);

random

returns random # between 0.0 & 1.0 (including 0.0 & not including 1.0) with uniform distance from range -general format: Math.random() with no parameters -returns double value -if you want to increase range from 0.0 to 100.0, multiply the expression by 101 & cast double to int to truncate decimal & yield whole #

control flow statements

separates "true branch" statement from 'false branch" statement with else -put boolean in {}

constructors

what functions create object of a class (initialize variables) -often used with parameters stored in data portion of created object -name has to match class name -if no constructor defined, Java creates default constructor that can initialize variables & allocate memory -has no return type (not even void) -header format: public ClassName() {

Java source file

source code: group of text listing commands that can be compiled and executed by a machine but written by people to govern JVM actions -compiled into bytecode, which can then be interpreted by the JVM & converted into machine language -has file extension ".java" (file that is converted into Java bytecode file AKA class file)

How do you create an object?

syntax: ClassName variableName = new Constructorname(parameters) -constructor name = class name -when create new ArrayList with reserved word new, makes object from original ArrayList & stores it in name of ArrayList -allows for use of dot notation (.) & for calling of methods/ variables that exist in original ArrayList -BUT still need main method Ex: ExampleClass exampleClass = new ExampleClasss();

What is the general format for the ArrayList add method?

syntax: add(value) or add(index, value) -adds elements to END of ArrayList UNLESS add a specific index as a parameter, which will add value at specified index & move everything else after it up by 1 index

What is the general format for the ArrayList get method?

syntax: get(index) accesses element at certain index

What is the general format for the ArrayList remove method?

syntax: remove(index) -removes element from ArrayList -can return reference to deleted element if called -shifts other elements down an index to account for deleted element

What is the general format for the ArrayList set method?

syntax: set(index, value) -replaces value at "index" with new value

What is the general format for the ArrayList remove method?

syntax: size() returns length of ArrayList

power

takes in 2 double values & returns result of 1st input raised to power of 2nd input -general format: Math.pow(inputOne, inputTwo); -if power function has expressions that use operation (e.g. fractions), could result in 0.0, so rather use decimal since expression before power method run Ex: Math.pow(4, 1/2) → 1.0 since 1/2 = 0

Why are main methods static?

there are NO objects that exist for class when JVM calls main method, & then objects created in them

return

used to cause whole method to return certain value & IGNORE statements underneath it -breaks loop & exits method immediately after being called -returns output of method without executing unnecessary code -TIP: put return statement after for/ while loop in case potential compile-time errors due to method not being able to return nothing

Boolean expressions

used to compare #s, boolean values, String values, etc.

boolean

used to represent true/ false value, which allows statements and methods to be executed based on the truthfulness of the statement

comments

when adding lengthy comments, use /* at the start and */ at the end of lines


Related study sets

Marketing Revision Quiz 1 What is Marketing?

View Set

Health Assessment Exam 2 - Practice Questions

View Set

Digestion of Carbohydrates and Proteins

View Set

20.1 -- VENTURE CAPITAL & FINANCE ROUNDS

View Set

Foundations 2 Module 3 Rationale

View Set