Comp sci study some classes

Glossary

  • Introduction to Computer Systems, Software Development, and Java Programming Language

  • Object-Oriented Programming (OOP), Objects, and Classes in Java, Java Methods

  • Variables, Primitive Data, and Arithmetic Operations

  • Logical and Relational Operations, Iterations, and Algorithms

  • Java Strings

  • Arrays and ArrayLists

  • Inheritance / Class Structure

  • Recursion

  • Searching and Sorting

  • Common Errors

Unit 1: Introduction to Computer Systems, Software Development, and Java Programming Language

Numbers in Computer Science - Bits and Bytes
  • Bits: Basic unit of information, either 0 or 1.

  • Bytes: Standard unit of memory = 8 bits.

  • Memory Units:

    • 1 Megabyte (MB) = $10^6$ bytes

    • 1 Gigabyte (GB) = $10^9$ bytes

    • 1 Terabyte (TB) = $10^{12}$ bytes

Binary Number System
  • Binary: Only consists of 0s and 1s. Place values are powers of 2.

  • Decimal: Uses numbers 0-9. Place values are powers of 10.

Conversion
  • Binary to Decimal:

    1. Label place values using powers of 2.

    2. Write out the expression using those powers.

    3. Sum the powers with a '1' above them.

  • Decimal to Binary:

    1. Identify highest power of 2 within the number.

    2. Write '1' or '0' per found power until reaching 0.

Java Language Basics
Structure of Java
  • Editors: Where the source code (.java files) is typed.

  • Compilers: Convert source code to machine code. More efficient overall for debugging than interpreters.

  • Interpreters: Execute Java bytecode line by line. Useful for cross-platform compatibility.

  • Debuggers: Identify errors in code and display error types and locations.

  • Java's Approach: Combines compilation and interpretation for efficiency and cross-platform support.

Java Syntax
  • Braces ({}): For defining blocks of code (loops, methods).

  • Semicolons: Required at the end of each statement, apart from control structures.

  • CamelCase for Naming:

    • Variables: lowerCamelCase (e.g., variableName).

    • Classes: UpperCamelCase (e.g., ClassName).

    • Constructor names match class names.

Unit 2: Object-Oriented Programming (OOP)

OOP Concepts
  • Objects: Instances of classes, containing fields and methods.

  • Class Syntax:

  public class ClassName {
      private int field1;
      public ClassName(int param1) {
          field1 = param1;
      }
      public void method1() {}
  }
  • Constructors: Initialize new objects with specified values.

  • Method Overloading: Two methods may have the same name but different parameters.

Unit 3: Variables, Primitive Data, and Arithmetic Operations

Primitive Data Types in Java
  • Boolean:

    • Holds true/false values.

    • Syntax: boolean b = true;

  • Byte:

    • Signed range of -128 to 127.

    • Syntax: byte b = 0;

  • Short:

    • Range: -32,768 to +32,767.

    • Syntax: short sh = 0;

  • Int:

    • Range: -2,147,483,648 to 2,147,483,647.

    • Syntax: int i = 0;

  • Long:

    • 64-bit integer, holds larger values.

    • Syntax: long l = 0;

  • Float:

    • 32-bit floating point. Syntax: float f = 0.2f;

  • Double:

    • 64-bit, double precision. Syntax: double d = 0.2;

Arithmetic Operations
  • Operators:

    • Addition: a + b

    • Subtraction: a - b

    • Multiplication: a * b

    • Division (INT): a / b (results in an integer)

    • Modulus: a % b

Special Operators
  • Compound Assignment: +=, -=, *=, /=, %=

Unit 4: Logical and Relational Operations, Iterations and Algorithms

Control Flow Statements
  • If/Else: Determines code flow based on boolean conditions.

  • Relational Operators:

    • >, <, >=, <=, ==, != for comparisons.

Logical Operators
  • ! (not), && (and), || (or).

Iteration Constructs
  • For: Executes statements a certain number of times.

  • While: Continues while a condition is true.

  • Do While: Executes at least once before checking the condition.

Unit 5: Java Strings

Strings
  • Immutable sequences of characters.

  • Methods: charAt(int index), substring(int start, int end), length(), equals(String string2).

Unit 6: Arrays and ArrayLists

Arrays
  • Fixed-size data structures holding elements of a single type.

  • Access using indices starting at 0.

    • Creation Syntax:
      java int[] array = new int[10];

ArrayLists
  • Dynamic lists that can grow in size.

  • Key Methods:

    • add(Object e): Appends at the end.

    • get(int i): Retrieves element at index i.

    • remove(int i): Deletes element at index i.

Unit 7: Inheritance / Class Structure

Inheritance
  • Reuse code from existing classes.

  • Syntax: public class Child extends Parent {}

Abstract Classes and Interfaces
  • Abstract Classes: Cannot be instantiated and can have abstract methods.

  • Interfaces: Define methods that implementing classes must provide.

Unit 8: Recursion

Recursive Functions
  • Functions that call themselves to solve smaller instances of a problem.

  • Must have a base case to prevent infinite calls.

Unit 9: Searching and Sorting Algorithms

Searching Techniques
  • Linear Search: Checks each element.

  • Binary Search: Efficiently finds elements in sorted arrays.

Sorting Techniques
  • Selection Sort, Insertion Sort, Merge Sort, Quick Sort: Various algorithms with different complexities and methodologies.

Unit 10: Common Syntax Errors and Other Errors

  • Expected: Missing semicolon.

  • Cannot Find Symbol: Variable undeclared or misspelled.

  • Incompatible Types: Wrong data type assignment.

  • Variable Might Not Have Been Initialized: Ensure all variables are assigned before use.

  • Overflow: Operations exceed data type limits.

Important Notes
  • JVM constructs handle memory management and garbage collection automatically.

  • Always close resources (like Scanners) to avoid memory leaks.

Example Code for Creating Classes
  1. Simple Class Example
    ```java
    public class Dog {
    private String name;
    private int age;
    public Dog(String name, int age) {
    this.name = name;
    this.age = age;
    }
    public void bark() {
    System.out.println(name + " says: Woof!");
    }
    }

2. **Using the Dog Class**  

java
public class Main {
public static void main(String[] args) {
Dog myDog = new Dog("Buddy", 5);
myDog.bark();
}
}

### Logic Questions Example

1. **Sum of Two Numbers**  
   - Write a method that takes two integers and returns their sum.  

java
public int sum(int a, int b) {
return a + b;
}

2. **Checking Even or Odd**  
   - Write a method to check whether a given integer is even or odd.  

java
public boolean isEven(int number) {
return number % 2 == 0;
}

3. **Factorial of a Number (Recursion)**  
   - Write a recursive method to find the factorial of a number.  

java
public int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}

4. **Prime Number Check**  
   - Write a method to check if a number is prime.  

java
public boolean isPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) return false;
}
return true;
}
```