AP Comp Sci - Semester 1 Finals

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/13

flashcard set

Earn XP

Description and Tags

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

14 Terms

1
New cards

Static methods and variables

Static:

  • variable: shared across the class rather than to individual object/instance (ex: cumulative count of every time user creates a new obj)

  • method: non static methods can use static vars, but static methods can’t use instance vars.

  • method call in main class: call ClassName.method() instead of objOfClass.

2
New cards

Public vs Private

Public: variable or method can be accessed from outside classes

Private: variable or method can only be accessed within the class

3
New cards

Classes

main class ex:

public class ClassName {

public static void main(String[] args) {

random class ex:

public ClassName {

}

4
New cards

instance variables

5
New cards

declaring vs initializing

6
New cards

Constructors

allows user to create new objects for that class by filling in different parameters. There can be multiple different constructor versions for one class

ex:

private String s;
private int i;
private double d;
private boolean b;

public ClassName() {
  s = "";
  i = 0;
  d = 0.0;
  b = false;
}
public ClassName(String s, int in) {
  this.s = s;
  i = in;
  d = 0.0;
  b = false;
}
//etc. can be any combo; just make sure to match same types

//implementing in main class:
ClassName objName = new ClassName();
ClassName objName2 = new ClassName("hi", 5);

7
New cards

getters, setters, and toString

//getter
public int getNum() {
  return num;
}
//setter
public void setNum(int n) {
  num = n;
}
//toString
public String toString() {
  return "number: " + num;
}



// call in main class:
int n = objName.getNum(); //or can print
objName.setNum(5);
System.out.println(objName);

8
New cards

importing - scanner and random

// USING SCANNERS
import java.util.Scanner;

Scanner input = new Scanner(System.in);
System.out.println("Please enter your name:");
String name = input.nextLine;
// if var = int: input.nextInt; char: nextChar, double: nextDouble

//USING RANDOM
import java.util.Random;
int range = max - min
(int)(Math.random() * (range + 1)) + min

9
New cards

adding strings, ints, and doubles

ex:

String s = “you”

int i = 5

int j = 9

return i + j => 14

return “hi there “ + i + j => hi there 59

return “hi there ” + s => hi there you

10
New cards

converting char/str/int/doub

int can be added to doub, but doub has to be converted to int first if you want to add it to int.

(int)doubleNum => trunkates (gets rid of everything after decimal point. rounds down)

ints and doubles can be turned into str: (String)intNum

11
New cards

DeMorgan’s laws

!(A && B) == !A || !B

!(A || B) == !A && !B

12
New cards

for loops and iterations

for loop: 

for (var initialization; boolean expression; increment)

for (int i = 0; i < 10; i++){}

# iterations:

|limit - initialization|/update

OR (range/interval) + 1(if ≤ or ≥)

EX

for(int i = 12; i <= 374; i += 18) =>

374 - 12 = 362

362/18 = 20

20 + 1 = 21

13
New cards

JavaDocs comments format

example

/**

explains what method does

Initializes a Power object.

post + pre conditions to be met

Precondition:

(for getters + setters)

ActivityLog object must be initialized

(for constructors)

Power object must take a String theName and int theStrength

Postcondition: Instance variables String name and int strength are initialized with String theName, and int theStrength.

parameters taken in

@param theName - String to initialize instance variable name for Power object

@param theStrength - int to initialize instance variable strength for Power object

AND/OR: return

@return strength -the strength of the Power object

*/

14
New cards

compareTo

str1.compareTo(str2)

  • if str1 > str2: +num

  • if str1 < str2: -num

  • if str1 = str2: 0

if Strings: will compare by alphabetical order. also case sensitive

ex: s1 = “hello”

s2 = “jello”

system.out.println(s1.compareTo(s2))

^ returns -2 (‘h’ is 2 times lower than ‘j’ bc j comes after h)