CSCE 314

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Regular Grammar

A -> aB A -> a A -> e A, B are Non-terminal symbols a is a terminal symbol e is empty string

Regular Expression Example 1

A = {a, ɛ} B = {b, c} A*B = {ɛ, b, c, ab, , ac, aab, aac, aaab, aaac, ...}

Parsing Quiz Q3: Left-factoring the following grammar will yield which 2 grammar statements? A → αB | αC - A → αC - A → αX - A → αB - X → B | C

A → αX X → B | C

Another Polymorphism Example

Animal: + display(): void Dog extends Animal: + bark(): void + display(): void Bird extends Animal: + chirp(): void + display(): void Chihuahua extends Dog: + bark(): void + display(): void Bluebird extends Bird: + chirp(): void + display(): void Animal a = new Chihuahua(); a.display(); // okay a.bark(); // wrong Dog d = new Chihuahua(); d.bark(); //okay Bird b = new Bluebird(); b.chirp(); // okay

Parsing Quiz Q2: How can you fix BAD predictive grammar?

By using left-factoring

User Input in Scheme

CHAR (define val #\) (display "Enter a char\n") (set! val (read-char)) STRING ^same thing by strings and (read-line) NUMBERS ^same thing but just read (define sum (+ (read) (read)))

ASCII

Capitals come before (less than) lowercase

Comparable Sort

Collections.sort(Collection<E> c)

Parsing Quiz Q8: Consider the left recursive grammar E → E + E / E × E / a Which of the following grammar is equivalent grammar with left recursion eliminated? E → aA A → + E A / × E A / ε E → + E A / × E A E → aA A → + E A / × E A

E → aA A → + E A / × E A / ε

Lists in Scheme

ELEMENTS: - Each element is immutable - Each element consists of two nodes: one pointer to the value, and another pointer to the next node - The last element points to null - Each element can only hold one value, but it can be a list, function, expression, etc LIST: - The list is mutable - Heterogenous: can contain various datatypes in one list - Recursive structure (linked list) - NOT accessible by index number

Grammars Quiz Q8: Consider this context-free grammar: S → 1S | 0A0S | ε A → 1A | ε Which of the following is correct for this grammar: - Strings generated will always have an even number of 0's. - Strings generated will always have an even number of 1's.

Even amount of 0's

Differences between Abstract & Implements

Feature: types of methods member variables subclass uses to connect members in general overall length of code inheritance Abstract: Abstract-Non Abstract freedom of declaration "extends" freedom of specifier more freedom, more code subclass can only inherit 1 class (abstract class) Implements: Abstract default to final "implements" public by default very short subclass can implement as many interfaces we want

List and Recursion Quiz Q2: Which of the following are valid methods on lists? - Filter - Map - Double - Flatten

Filter, Map, and Flatten

Pattern

Grammar that Defines Token - The rules which characterize the set of lexemes for a token

Encapsulation

Keeping details (like data and procedures) together in one part of a program so that programmers working on other parts of the program don't need to know about them. note, Java does not have the & operation everything is pass by value, not reference

Trees Quiz Q10: Select all options that apply to a binary search tree (BST) - Left value is less than parent node and right value is more than parent node - Has at least 2 children - There is no particular order - May have many links to many children

Left value is less than parent node and right value is more than parent node

Custom Methods within Objects

Mutators - change the value of data members Accessors - just retrieve data members - You can overload these functions

Comparator Sort

NAME comp = new NAME(); Collections.sort(Collection<E> c, comp);

Overriding vs Overloading

Overriding when return type, name, parameters the same in 2 or more inherited (related) objects. Overloading when name the same but parameters different (overloading is illegal when extending a superclass) in 1 object

Lexical And Syntactical Analysis Quiz

See the other questions on canvas

Trees Quiz

See the other questions on canvas

JVM Breakdown

There are numerous important parts that work together to allow the JVM to function - ClassLoader (loading the .class Java file) - Runtime Data Area - Execution Engine Built-in Class Loader - Loading is the conversion of compiled *.class files from bytecode to java classes and Interfaces.

Describing Semantics

There are three approaches to defining "deeper" semantics - Operational semantics - Axiomatic semantics - Denotational semantics - However, capturing what a program in some programming language means is very difficult - We can't really do it in any practical sense

List and Recursion Quiz Q1: Each element in a list has a pointer to which of the following? - Previous Node - Starting Node - Value - Next Node

Value and Next Node

Scheme Functions Quiz Q8: Variables can have which of the following - Value - Focus - Scope - Type

Value, Scope, and Type

Regular Expression Example 2

[abc]** * = (a | b | c)* = {ɛ, a, b, c, aa, ab, ac, bb, ba, bc, ca, cb, cc, ...}

Mathematical Function

a mapping of members of one set, called the domain set, to another set, called the range set

Regular Expression Example 3

a(ab)*a = {aa, aaba, aababa, aabababa, ...}

Regular Expression Example 4

a.[bc]+ = a.(b | c)+

Helpful WSL commands

given ::, gsi -> interpreter mode for gambit scheme gsi filename -> runs code ls -> lists all files in the current directory mkdir -> makes a new folder/directory touch -> makes a new file cd foldername -> opens a directory cd .. -> opens parent folder

Templated Stack in Java

import java.util.*; public class PLStack<T> { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ArrayList<T> stackList = new ArrayList<T>(); ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ private int size = 0; ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ public T pop() { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ T temp = stackList.get(size - 1); ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ stackList.remove(size-1); ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ size = size - 1; ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ return temp; ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ } ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ public void push(T e) { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ stackList.add(e); ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ size = size + 1; ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ } ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ public T peek() {return stackList.get(size - 1);} ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ public int getSize() {return stackList.size();} ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ public String toString(){ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ String temp = ""; ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ for(int i = 0; i < getSize(); ++i) { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ temp = temp + stackList.get(i) + " "; ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ } ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ return temp; ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ } }

ArrayList indexOf Method

import java.util.ArrayList; ArrayList<OBJECT> anyname = new ArrayList<OBJECT>(); Takes in an element E, and uses the equal function defined in the class to compare each element until the desired element E is found in the list. If not found, return -1 size - current # of elements capacity - the amount of elements the array is can hold

Scheme Functions Quiz Q4: Which of the following can act as both a function and a variable? - and - or - not

not

Inheritance and Class Member Visibility

public (UML +) - access members via the subclass class functions protected (UML #) - DIRECT access member via ONLY the subclass class functions - items are "shared within the family" protected final - this can be the only instance of this named function/variable private (UML -) - NOT accessible in the subclass class! UNLESS YOU USE AN ACCESSOR METHOD!!

Example using Extends

public class Cube { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎private double side; ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎public void setSide( ... ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎public ... } public class ExtendedCube extends Cube { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎public double getSurfaceArea( ... ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎public ... }

Data types in Java

short, int, long float, double, long double char, String boolean const is called final in java

String Tokenizer Methods

tokenizer.hasMoreTokens() - return true if there are more tokens ahead String word = tokenizer.nextToken() - grabs the next words int count = tokenizer.countTokens() - returns the number of tokens

Using a Collection as a parameter

void printList(ArrayList<Employee> x) { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ Iterator <Employee> student = x.iterator(); ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ while (student.hasNext()) { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ System.out.println(student.next()); ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ } }

EMPTY CHARACTER

‏‏‏‏‎ ‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎

Booleans

#t #f

A More Complex List Example

'(A (B C) D (E (F G))) - let L denote the list above (cdr L) -> ((B C) D (E (F G))) (cddr L) -> (D (E (F G))) (cdddr L) -> ((E (F G))) (cddddr L) -> () (cadddr L) -> (E (F G))

Making Lists

'a -> denotes the string a #\ -> char (list 2 3 4 5 6 7) (list 'six 'Kevin 12) (list 'how (list 'to 'love) 'yourself)

Arithmetic Functions

(+ 7 3) -> 10 (/ 5.5 0.7) -> 7.857...

Trees in Scheme

(2^height) - 1 = number of max nodes a tree can hold - Inherently recursive because each node can be viewed as another tree. - Uses lists - Each node has a datum (value at 0) and children nodes

List and Recursion Quiz Q5: Condense (car (car (cdr ...

(caadr ...

Spread Parameter

(define (add x .rest) ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎... )

Making Functions

(define (name parameters) ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎​​‎‎‎‎‎‎‎meat ) (define (add x y) ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎(+ x y) ) NOTE: pure functions do not display since printing is a side effect

Filter Function

(filter positive? '(1 -2 3 4 -5)) -> (1 3 4)

Sort Function

(sort list1 operation) (sort '(1 2 3 4 5) >) -> (5 4 3 2 1) (sort '("B" "A" "able" "b") string<?) -> ("A" "B" "able" "b") (sort (list #\z #\t #\c) char<?) -> (#\c #\t #\z)

Constraints and Bindings

- Java does this to error check TYPES: - extends Object (most basic) - extends Comparable

Imperative

- Like C++ or Java, these languages can return voids - The basic method of computation is changing stored values, which is called side effects

Context-Free Grammar

- Only production rules follow: A -> W, where W is a word of any combination between non-terminal and terminal symbols, including e, the empty string.

And function

- Takes in multiple parameters (and (< 5 7) (> 7 5)) -> #t (and #t #t #f) -> #f

Display Function

- The same as print (display something)

Ambiguity

- When a sentence can be represented by more than one parse tree

Java Runtime Environment (JRE)

- a JRE is a collection of tools specifically for executing Java applications and programs. - included as part of a JDK - Contains the JVM and different libraries - required to execute Java programs; cannot compile or debug code with JRE - Like JDK, JRE also has different versions

Unified Modeling Language (UML)

- a general-purpose, developmental modeling language in the field of software engineering that is intended to provide a standard way to visualize the design of a system.

Datatypes in Scheme

- numbers: int, complex, real - char - bool - string - function

Language

- set of sentences, combination of keywords, etc

Scheme Functions Quiz Q1: What is the output? (define (multiply x y z) (* x y z)) (print (multiply 2 5 10))

100

Regular Tree

Nodes with unlimited amount of links

string

""

If statement

(if (condition) if_true if_false)

Super Call Example

-

char

#\

Grammars Quiz Q2: T/F: Tokens are a type of lexeme?

T

List and Recursion Quiz Q8: What will be the output of the following code? (define x (list 1 2 3)) (define y x) (display (eq? x y)) (display (equal? x y)) (newline) (define y (list 1 2 3)) (display (eq? x y)) (display (equal? x y))

#t #t #f #t eq? checks if the objects use the same memory equal? compares each elements in a list

Currying and Higher Order Functions Quiz Q3: What is the output of the following code? (display (map + '(1 2 3) '(4 5 6))) (newline) (display (apply + 1 2 3 '(4 5 6)))

(5 7 9) 21

Currying and Higher Order Functions Quiz Q4: What is the output of the code below? (display (sort '("a" "A" "b" "B" "c" "C") string>?))

(c b a C B A)

List and Recursion Quiz Q4: For an un-nested list (i.e. a list which contains only atoms and no other list as its elements), which of the following will DEFINITELY throw an error - (cddddr - (caaddr - (cadddr

(caaddr... this is because multiple car (car (car ... will access the first element, and if that is element is a list, then it will access the first element of the first element of the original list. In this case, we are talking about an un-nested list

Recursion with Lists example 3

(define (append m n) ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ (cond ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ [(null? m) n] ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ [else (cons (car m) (append (cdr m) n))] ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ) )

Currying in Scheme

(define (curry f) ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎(lambda (x) ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎(lambda(y) (f x y)) ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎) ) ((curry sort) '(7 3 8 2)) > )

Flattening a Nested List

(define (flatten lis) ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ (cond ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ [(null? lis) '()] ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ [(list? (car lis)) (append (flatten (car lis)) (flatten (cdr lis)))] ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ [else (cons (car lis) (flatten (cdr lis)))] ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ) ) (flatten '(1 2 3 4)) -> (1 2 3 4) (flatten '(1 (2 7) 3 4)) -> (1 2 7 3 4)

Scheme Functions Quiz Q9: Define a function "increment" that takes an input x and adds 10 to this input.

(define (increment x) (+ x 10))

Recursion with Lists example 2

(define (list-sum lis) ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ (cond ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ [(null? lis) 0] ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ [else (+ (car lis) (list-sum (cdr lis)))] ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ) )

Creating a higher-order function example

(define (member-if procedure lis) ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎(cond ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎[(null? lis) #f] ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎[(proc (car ls)) ls] ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎[else (member-if procedure (cdr lis))] ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎) ) (member-if positive? '(0 -2 -4 -7 7 -1)) -> (7 -1)

Recursion with Lists example 1

(define (member? e lis) ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎(cond ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎[(null? lis) #f] ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎[(eq? e (car lis)) #t] ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎[else (member? e (cdr lis))] ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎) ) (member? 2 '(1 2 3)) -> #t (member? 4 '(1 2 3)) -> #f (member? 'B '(A B C)) -> #t (member? "B" '(A B C)) -> #f (member? "B" '("A" "B" "C")) -> #t

A More Complicated Lambda

(define name (lambda (parameters) (meat))) (define double-any (lambda (f x) (f x x))) (double-any + 10) -> 20 (double-any cons a) -> a . a

Defining Lists with Variables

(define x '(a b)) (define y x) ; x is a pointer to (a b). y is then created to point to the same list (set! y '(3 4)) ; now y points to a separate list (define x '(a)) (define y (list x x)) ; y consists of two elements with each value pointing to the same list as x points to (define x '(a)) (define y (cons x x)) ; y consists of one element with each node pointing to the same list as x points to

List and Recursion Quiz Q7: For the list (1 2 3 4 5 6 7 8 9), write the code that would print 5 as the output

(display (car (cddddr '(1 2 3 4 5 6 7 8 9))))

Scheme Functions Quiz Q6: Given the following function in scheme (define square (lambda (f x) (f x x))) which of the following statements would correctly display that 5^2 is 25? - (display (square 5 5) - (display (square x 2)) - (display square 5) - (display (square * 5)

(display (square * 5)

String Functions

(make-string n c) - returns a string of length n of the character c (string-length s) - return length of the string s (string=? s1 s2) (string-ref s i) - returns the char at index i of s (starts at 0) (string-set! s i c) - sets the ith character of s to c (substring s b e) - returns a substring beginning at b and ending at (e - 1) (string-append s1 s2 ...) - connects s1 to s2 to ... (string->list s) - converts a strings into a list of characters (list->string lis) - convers a list of characters into a string (string-copy s) - copies the string s

Currying and Higher Order Functions Quiz Q5: Which of the following uses of the map function would replicate y = 2x+5 on a input of (1 5 7) resulting in the answer of (7 15 19)? - (map (lambda (x) (+(* x x) 5)) '(1 5 7)) - (map (lambda (x) (+(+ x x) 5)) '(1 5 7)) - (map (lambda (x) (+(* x 2) 5)) '(1 5 7)) - (map (lambda (x) (*(+ x x) 5)) '(1 5 7))

(map (lambda (x) (+(+ x x) 5)) '(1 5 7)) (map (lambda (x) (+(* x 2) 5)) '(1 5 7))

Map Function

(map operation list1 list2 ...) (map + '(1 2 3) '(4 5 6)) -> (5 7 9) (map (lambda (x) (* x x)) '(1 2 3)) -> (1 4 9)

Useful List Functions

(null? S) (list? S) (equal? S1 S2) -> compares each element (eq? S1 S2) -> compares the two objects

Scheme Functions Quiz Q5: Which of the following functions has no side effects? - define - display - +

+

Cond Function

- A better if statement - Like a switch statement (cond ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎[(condition1) if_true] ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎[(condition2) if_true] ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎... ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎[else ...] )

Token

- A category of lexemes (identifiers) such as reserved words, identifiers, operators, and literals or constants. - If you put a lexeme "int", it will be in reserved words. - If you put a lexeme *, it will be in operators. - If you put a lexeme with the name of a class that you assigned, it will be in identifiers. - If you put a lexeme 3, it will be in literals or constants.

Binary Search Tree

- A data structure very similar to a tree with the following additional restrictions. Each node can have only 0, 1 or 2 leaf nodes. All left nodes and all of its descendants have smaller values that the root node, while all right nodes and all of its descendants have larger values than the root node. - There is a clear order in BSTs

Scheme

- A dialect of LISP, but Scheme is more simple and pretty - Scoping is static - Dynamic datatypes - No distinction between data and code

Functional

- A programming style where the basic method of computation is the application of functions to arguments - Returns a value no matter what

Greedy

- A style of grammar that uses the current symbol to get as close to the desired sentence as possible - Bad stuff here - Greedy typically generates a leftmost derivation tree - Spare is the exact opposite: leave everything for the end - Spare usually generates a rightmost derivation tree

Literal Lists

- A temporary list - If it is a character, it is automatically a char "a" -> string '(1 2 3 4 5) '()

Finite State Machine (FSM) or Finite Automaton (FA)

- A visual representation of a grammar/expression

Syntactic Analysis (Parsing)

- After scanning - Analysis of syntax and tokens and creates a Parse Tree - Actions dictated by token order - Updates symbol table - Creates a representation of the source code with tokens - Also generates errors

Vectors

- Almost the exact same as ArrayList - Both can shrink or grow - The one and major difference is that: Vector is synchronized, whereas ArrayList is unsynchronized

Top Down Parsing with Predictive Grammars

- Also known as Predictive Parsers - This is a top-down parser than never backtracks - We use a parsing table for predictive parsers step - stack - input - action stack and input end with $ pop(); push(...); next ++ Accept when stack and input are just $

Top Down Parsing (LL(k))

- Also known as recursive descent - A simple and general parsing strategy - Grammar cannot be left-recursive since it will generate an infinite loop - Unpopular due to backtracking -> inefficient - In practice, this backtracking is eliminated by further restricting the grammar to be predictive - Think like a robot - step by step, left to right [] means start TOP DOWN PARSER: - root to leaves - order is of leftmost derivation - traces the tree in preorder -> LL(k): predictive parser: left to right parse, left-most derivation

Bottom Up Parsing (LR(k))

- Also known as shift reduce parsing - This is the preferred method in practice - Less needy than top-down parsing, but just as efficient - This does not require predictive nor right recursive grammar - left recursive and/or non-predictive grammar works as well BOTTOM-UP PARSER: - leaves to root - order is of the reverse of a rightmost derivation -> LR(k): shift-reduce parser: left to right parse, right-most derivation - Simply make a rightmost parse tree and flip it, and then, you'll get the answer - GREEDY - GREEDY - GREEDY

Abstract Methods

- An abstract method is like a placeholder for a method that will be fully defined in a descendent class(es) - it postpones the definition and body of the method - think of it as the implementation is being differed to be defined later - in theory, abstract methods can set standards of what the sub-classes must later name and implement - EVERY subclass should have these method(s) but the class itself must be abstract!! - has no method body (no {}s), and ends with a semicolon in place of its body. - the methods cannot be private - you can still call the SUB-CLASS version of the method to get information Bad Part - if you have ONE abstract method, it must be inside an abstract class

cons Function

- Appends an new element to the front of a list (cons new_item original_list) (cons 1 '(2 3)) -> (1 2 3) (cons 'A '()) -> (A) (cons '() '(A B)) -> (() A B) However, if you cons two elements together, you get a pair, which does not point to any other element. Instead, both nodes point to separate values (cons 1 2) -> (1 . 2) (cons (cons 1 2) (cons 3 4)) -> ((1 . 2) 3 . 4)

Mapping with Lists

- Applies a given rule (function) to all data in a list - So, a function will be a parameter (define (double x) (* x 2)) (define (increment x) (+ x 1)) (define (mapcar f lis) ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ (if (null? lis) '() ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ (cons (f (car lis)) (mapcar f (cdr lis)))) ) (mapcar double '(1 2 3 4)) -> (2 4 6 8) (mapcar increment '(1 2 3 4)) -> (2 3 4 5)

Apply Function

- Applies a procedure to a list (apply function optional_values list1) (apply max '(1 2 3)) -> 3 (apply + 100 12 '(3 5)) -> 120 (apply - 100 '(17 12)) -> 71; 100 - 17 - 12

Base Class Constructors and Super

- Base class constructor is called before Derived class constructor - any SUBCLASS will need to call the BASE CLASS's constructor using the "super" reserved word IF A BASE CLASS constructor is present - Only in a derived class can call "super" IF the method/member variable is public or protected!! Not private!! you can also do super.public/protected_function() think of super as a variable for the base class version of a derived class

Lexical Analysis (Scanner)

- Before parsing - Scans input (source code) and generates a Token Stream - Removes whitespace - Identifies tokens - Makes symbol table - Generates Errors - Sends tokens to Parser

Bytecode

- Bytecode is technically a type of machine code, but it is not binary - Bytecode is machine code built for the JAVA VIRTUAL MACHINE (JVM) - Bytecode is the .class file that was compiled from Java code using the command: javac Program.java - can be run on any machine that has the JVM installed using the command: java Program

Comparator

- Can create an unlimited number of comparators - Can compare homo and hetero - Like compareTo, it always return -1, 0, 1 - We make our own compareTo function for objects, rather than using one in Comparable - A complete different object EX: (integers ascending) class AGE implements Comparator<Employee> { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ public int compare(Employee a, Employee b) { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ if(a.getAge() < b.getAge()) return -1; ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ else if(a.getAge() > b.getAge()) return 1; ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ else return 0; ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ } } EX: (strings) class NAME implements Comparator<Employee> { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ public int compare(Employee a, Employee b) { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ if(a.getLast().equlas(b.getLast())) ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ return a.getFirst().compareTo(b.getFirst()); ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ else return a.getLast().compareTo(b.getLast()); ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ } }

Append Function

- Combines two or more lists (append '(1 2) '(3 4)) -> (1 2 3 4) (append '(1 2)) -> (1 2) (append '(A B) '((X) Y)) -> (A B (X) Y) (append) -> () (append '(1 2) '(3 4) '(5 6 7)) -> (1 2 3 4 5 6 7)

Abstract and Implement as "Standards"

- using inheritance to force code completion - forcing the programmer to complete items, NOT the program - An Abstract class is meant to be extended (or meant to be a SUPER CLASS) - It is a generalization for more specific sub-classes, but you cannot create an INSTANCE of this abstract class EX: Employee Lupoli = new Employee(...); // Nope Employee Lupoli = new SalariedEmployee(...);// Yup!! Polymorphism!! can have: - constructors, BUT, the sub-class must call it - data members - public or protected methods cannot have: - private methods - private variables could be accessed by those public methods

JVM: Class Loader

- Compiling a .java file results in a .class file - This .class file goes into various steps when run on any platform using the JVM - The Class Loader has three main jobs, in order: 1) Loading - responsible for placing the java file (.class) & placing it into memory 2) Linking - Verification - This stage checks the structural correctness of the class (i.e. was it made with the right version) - Preparation - This stage allocates memory for any static fields or variables and sets them to their default values. (i.e. "static final int count = 20;" would be set to 0) - Resolution - At this stage all symbolic references are then pointed to actual references in the runtime constant pool 3) Initialization - This phase will initialize the static or instance variables to their assigned values, working from top to bottom in the hierarchy - After linking the static variables that were originally to "java defined defaults" are now set to "user defined defaults" thus our statement from earlier, "static final int count = 20", would now be set to 20

Interfaces

- Completely abstract - all data members/methods are abstract - uses public final static data members - meant to be "implemented" (used by a sub-class) - cannot create a direct instance - no constructor - this "class" is not meant to be instantiated, just implemented - sub-class that are implementing this Interface would have their own constructors Example: // INTERFACE BASE CLASS // notice "able" verb naming of class public interface Auctionable { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ // data member ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ public String condition = "New"; ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ // available conditions ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ public static final int NEW = 0; ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ public static final int LIKE_NEW = 1; ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ public static final int REFURBISHED = 2; ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ public static final int USED = 3; ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ // abstract methods to be implemented ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ public String getDescription(); ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ public int getCondition(); } //subclass inheriting the interface public class Car implements Auctionable { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ private String make, model; ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ public Car (String make, String model) { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ this.make = make; ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ this.model = model; ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ } ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ public String getMake() { return make; } ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎public String getModel(){ return model; } ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎public String toString() { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ return make + " " + model; ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ } ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ public String getDescription() { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎return "Low Mileage, New tires, AM/FM/CD"; ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎} ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎public int getCondition(){ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ return LIKE_NEW; ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ } }

Define Function

- Create a new function or variable (define name "kevin"); name cannot be changed unless (set! name "kaithlyn"); now name is kaithlyn

Java Collections

- Data structure in which objects are stored - Objects can be added, deleted, traversed, sorted in a collection - Predefined structure for storing a group of elements and behaving like an objective Class<object> name = new Class<object>(); LinkedList<Car> car = new LinkedList<Car> (); - Four Basic Collection Types: -> Lists: ArrayList, LinkedList, Vector, Stack -> Sets: HashSet, LinkedHashSet, TreeSet -> Maps: HashMap, LinkedHashMap, TreeMap -> Queue: PriorityQueue, Deque, ArrayDeque

Regular Grammar & Regular Expression

- Every regular expression can be expressed by regular grammar - Every regular grammar can be expressed by regular expression - The only rule is that an identifier must begin with a letter, followed by any letter or digit - Regular Grammar - breaks down string in LHS and RHS - Regular Expression - one big long string Ex: Grammar: ID -> LETTER REST REST -> LETTER REST | DIGIT REST | ɛ Expression: ID: LETTER (LETTER | DIGIT)*

The ToString Function

- Function is added to the class - Used to display an instance of an object - Called implicitly when printing an instance public String ToString() { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎return "..."; }

Functions in Scheme

- Functions should come first (function element1 element2) (+ 1 2); = 1 + 2 NOTE: () are replaceable with [] and even {} NOTE: operations such at + - != < > <= >= are considered functions

Higher Order Functions

- Functions that take other functions as parameters in order to work. - Used for mapping, filtering, folding, and sorting lists

Not everything needs to be Tokenized!

- If there are MANY pieces of data on one line, then Tokenize - But if there is only one piece of data per line, simply read String from file, and convert to proper datatype Tokenizable data example: Kevin CPSC A&M 19 Kaithlyn ACCT TJC 21 Ashley NURS? UTT 20 Ivan MATH TJC 20 Data that does not need to be Tokenized: 10 12 90 23 58 91 2 Tokenizable Code: int num1 = Integer.parseInt(tokenizer.nextToken()); String word1 = tokenizer.nextToken(); Non-tokenizing Code: total += Double.parseDouble(infile.nextLine());

Object Profile

- In this order Data members Constructors Accessors Mutators Operators (compareTo/equals) toString

Importance of JVM

- It is integral to Java's feature of being platform independent - BUT, the JVM needs to be installed on every system in order to run! - With the JVM converting the Java code into bytecode, it can take any Java application and run it on any operating system - Allows Java its "Write Once, Run Anywhere" (or WORA) mantra - The JVM also manages and attempts to optimize memory being used by a program - Unlike C++, there is no need to allocate and deallocate dynamic memory like pointers and arrays - It has a built- in Garbage Collector that can reclaim memory when needed

Parsing Quiz Q7: Which of the following is correct about a top down parser? (select all that apply) - It traces or builds the parse tree in preorder - Recursive descent parser easily implements a top-down parser for simple grammars - Its order is that of a rightmost derivation - It begins at the leaves

- It traces or builds the parse tree in preorder - Recursive descent parser easily implements a top-down parser for simple grammars

Intro to Java FX

- Java FX is a GUI (graphical user interface) that ties into the logic of your application - Has buttons and other interactive objects --module-path "C:\Users\ktang\Downloads\openjfx-20_windows-x64_bin-sdk\javafx-sdk-20\lib" --add-modules javafx.controls,javafx.fxml

Java Memory Management (JMM)

- Java memory management is a subsystem of Java that consists - JVM Memory Structure - Garbage Collection - JVM Memory Structure contains areas in memory like the heap and method area covered in JVM Topic - Garbage Collection deals with interacting with and removing instance variables to prevent memory errors

Type Erasure

- Java's JVM (Java Virtual Machine) handles generics rather oddly - Type parameters are actually replaced with ordinary Java types such as Object - Each type parameter is replaced with its bound (or Object if not bounded) - Converted into compile-time checks and execution-time casts - Compiler retains that is was using a Generic class <String>, <Custom>, etc - biggest issue is the ability to NOT be able to create objects of a generic type such as a = new E()

Lexical And Syntactical Analysis Quiz Q6: Which of the following statements are true regarding Lexical Analyzers and Parsers? - The lexical analyzer updates symbol table entries - Lexical analyzer creates the symbol table - Both lexical analyzer and Parser generate errors - The parser sends token to the lexical analyzer

- Lexical analyzer creates the symbol table - Both lexical analyzer and Parser generate errors

Creating a Generic Class

- Like a Template in C++ Type <T> - Can return T, accept T as a parameter, just think T as a data type EX: import java.util.ArrayList; public class Hold <T> { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ArrayList <T> holdBlock = new ArrayList<T>(); ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ public T getFirst() { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ return holdBlock.get(0); ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ } ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ public int getLength() { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ return holdBlock.size(); ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ } ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ public void add(T p) { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ holdBlock.add(p); ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ } ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ public boolean isEmpty() { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ if(holdBlock.isEmpty()) ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ return true; ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ return false; ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ } }

Java Collection Types

- Linked Lists - Arrays of Objects (not simple data type) - Queues - Sets - Maps - Collections are Heterogeneous - Can have different class types in one collection - CANNOT USE BASIC DATA TYPES - use custom classes (objects) we create

Lambda Functions

- Nameless functions that are immediately deleted after being using to avoid memory usage - Like a local variable ((lambda (parameters) (meat)) values_to_be_passed_in) ((lambda (x) (*x x)) 4) -> 16

Collection Class Method Summary

- Note that Collection is a class itself and all other collections are simply derived from it binarySearch(List<E> list, E key, Comparator<E> c) - LIST MUST BE SORTED FIRST - searches the list for key and returns its index disjoint(Collection<E> c1, Collection<E> c2) - returns true if c1 and c2 have no elements in common fill(List<E> list, E e) - replaces all elements in list with e frequency(Collection<E> c, E e) - returns the number of elements in c that are equal to e max(Collection<E> c) - returns the maximum element of c, according to the natural ordering of its elements max(Collection<E> c, Comparator<E> comp) - returns the maximum element of c, according to the order induced by comp min(Collection<E> c) - returns the minimum element of c, according to the natural ordering of its elements min(Collection<E> c, Comparator<E> comp) - returns the minimum element of c, according to the order induced by comp nCopies(int n, E e) - returns an immutable list consisting of n copies of e replaceAll(List<E> list, E old, E new) - replaces are elements equal to old with the element new reverse(List<E> list) - reverse the order of list shuffle(List<E> list) - randomly permutes the list shuffle(List<E> list, Random r) - randomly permutes the list using a given function r sort(List<E> list) - sorts list in ascending order, according to the natural ordering of its elements sort(List<E>, Comparator<E> c) - sorts list according to the order induced by c swap(List<E> list, int i , int j) - swaps the elements at index i and index j

Associativity

- Operations of the same procedure - Usually evaluated from left to right - For example, A + B + C is equal to (A + B) + C in a tree - However, some operators work differently such as unary - power ^ NOT ! bitwise comparator ~ - These go from right to left - These should use right recursion - For example, 1 ** *10^7 should be 1 ** *(10^7), not (1 * 10)^7

Regular Expression Operators

- Operators go after a character or groups - Ex: a+ (a | b)* (ab)? etc... + greater than or equal to 1: A+ = AA* ** * greater than or equal to 0: A* = A+ | ɛ ? 0 or 1: A? = A | ɛ | or: (A | B) = {A, B} . matches any character except ɛ ɛ is epsilon, or the empty string ( ) a sequence: (ab)* = {ɛ, ab, abab, ababab, ...} [ ] a grouping of characters: [abc] = (a | b | c) HIGHER PRECEDENCE () *+ Concatentation | Everything else is associative LOWER PRECEDENCE

Currying

- Partial function application - Takes a function that requires more than one parameter and breaks it down into a series of unary (one) parameter functions - Therefore, a curried function will only take one parameter at a time when called - Enables us to wait to pass in all of the variables needed - Parameters can be function - Can pass in some parameters and we can decide when the rest gets passed in. - If the curry function finishes, then it will finally return the value

Regular Expressions

- Refers to syntax within a programming language that is designed to make things easier to read or to express Union: a choice between lexemes - (a | b) Concatenation: one lexeme followed by another - ab Kleene Closure: 0 or more repetitions of this pattern - a*

Another Mapping Example

- Remember that operators such as != < > <= >= == are functions (define (sorted? lis compare) ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ (cond ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ [(<= (length lis) 1) #t] ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ [(compare (car lis) (cadr lis)) (sorted? (cdr lis) compare)] ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ [else #f] ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ) ) (sorted? '(1 2 2 3 7) <=) -> #t (sorted? '("a" "b" "e" "c") string<?) -> #f

Length Function

- Returns the length (top nodes) in a list (length (list 'A '(B C) 'D)) -> 3 (length (list)) -> 0 (length '(1 2 3)) -> 3

How to fix left recursive Grammar

- Since Top-Down Parsing LL(k) cannot use left recursive grammar, we need to fix it Example: <term> -> <term> * <factor> ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎| <term> / <factor> ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎| <factor> ...processing magic stuff... <term> -> <factor <term2> <term2> -> * <factor> <term2> ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎| / <factor> <term2> ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎| ɛ

How to fix non-predictive Grammar

- Since Top-Down Parsing LL(k) is very slow and inefficient with non-predictive grammar, we need to fix it Example: E -> T + E | T T -> (E) | int | int * T ...more magic... E -> TX X -> + E | ‎ɛ T -> (E) | int Y Y -> * T | ɛ‏‏‎

Using Generics Methods to Sort

- Sorting require a comparable in the the current object's class public class Sort { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ // since this is public STATIC, no need to create an instance of Sort ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ // but use Sort.bubbleSort(z) to use ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ public static <T extends Comparable<T>> void bubbleSort(T[] a) { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ for(int i = 0; i < a.length - 1; i++) { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ for(int j = 0; j < a.length - 1 - i; j++) { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ if(a[j+1].compareTo(a[j]) < 0) { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ T tmp = a[j]; ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ a[j] = a[j+1]; ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ a[j+1] = tmp; ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ } ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ } ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ } ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ } } these functions can only take in classes that implement comparable EX: class Employee implements Comparable <Employee> { ... } If the class is inherited from a Super Class, then the generic sort is changed to: public static <T extends Comparable<? super T>> void bubbleSort(T[] a) { ... }

Semantics

- Syntax is about form or structure - Semantics is about meaning. This determines how programs behave when executed on computers - The boundary between syntax and semantics is not always clear

Or function

- Takes in multiple parameters (or (< 5 7) (> 7 5)) -> #t (or #t #t #f) -> #t

Not function

- Takes in one parameter - Can act as both a function and a variable (not (< 5 7)) -> #f (not #f) -> #t

Program Verification

- Tests if the program behaves the way it is required to - basically debugging - Program Verification is the process of formally proving that the computer program does exactly what is stated in the program's specification - This requires a formal specification for what the program should do -

JVM: Execution Engine

- The Execution Engine is responsible for taking the bytecode it is given and converting it into machine code to be executed - It contains four important parts 1) Interpreter - Which reads, interprets and executes the code line by line. This is assisted by the other three portions 2) JIT Compiler - Just In Time compiler assists the Interpreter by speeding up compile time This is accomplished by compiling similar things, such as repeated function calls, at the same time rather than one at a time 3) Garbage Collector - This portion assists in memory management by collecting and removing memory that is no longer needed from the heap, increasing efficiency slightly. 4) Native Methods Libraries/Interface - The libraries contain libraries which are needed by the interpreter and the interface allows easier communication between the execution engine and the native method libraries.

Polymorphism Rules

- The method must be defined as a method in the base class - The subclass method must have the same "signature" as the base class method - The visibility of the subclass method cannot be more restrictive - The subclass method return type must be the same as the base class method or a type that is a subclass of the base class - The derived class method call must be made using a variable of the base class type

Parsing (for Grammars)

- The second step of the compiling process (after scanning) - The parser a component of the syntactic analyzer that constructs a parse tree from a string (code) - Generally, parsers can be classified into two types: 1) TOP DOWN PARSER: - root to leaves - order is of leftmost derivation - traces the tree in preorder -> LL(k): predictive parser: left to right parse, left-most derivation 2) BOTTOM-UP PARSER: - leaves to root - order is of the reverse of a rightmost derivation -> LR(k): shift-reduce parser: left to right parse, right-most derivation Notes: -k is the symbol for look-ahead - we look k characters ahead of the current character. - k is typically 1

Maximum Function Example

- There's a built in function max that return the maximum from a given set of numbers (max 1 2 3) -> 3 (max -10 19) -> 19 (define (maxi a) ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ (if (null? (cdr a)) (car a) ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ (max (car a) (maxi(cdr a))) ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ) )

Minimum Function Example

- There's a built in function min that return the minimum from a given set of numbers (min 1 2 3) -> 1 (min -10 19) -> -10 (define (mini a) ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ (if (null? (cdr a)) (car a) ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ (min (car a) (mini(cdr a))) ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ) )

Deterministic Finite Automaton (DFA)

- This is not equal to a simple FA - This a finite automaton that has at most one transition from a state for each - Only one choice per input per state - Can have multiple accept states - Regular expressions can be represented by DFAs and vice versa

Functional Languages

- Treat functions as objects that can be stored, passed as arguments, composed, etc - Purity: prohibits side effects - Recursion: the main way to iterate in functional languages

Pure Functional Language

- Tries to prohibit side effects or at least use immutable data - Instead of altering existing values, altered copies are created and the original is preserved - Uses referential transparency

Grammars

- Used to describe the syntax of a programming language - There are two types we will be discussing: 1) Context-Free 2) Regular <> and/or UPPERCASE indicate a non-terminal symbol lowercase and/or double circle represents a terminal symbol bolded words are often special words or lexemes meta symbol is the arrow ->

String Tokenizer

- Used to separate a string into tokens (generally individual words) by using delimiters String line = infile.nextLine(); StringTokenizer tokenizer = new StringTokenizer(line); - The tokenizer basically becomes a list of words kinda like split in Python

Input in Java

- Uses the scanner class import java.util.Scanner; next() will read input sc.reset() to clear CHAR: char letter = sc.next().charAt(0); STRING: String name = sc.next(); DOUBLE double price = sc.nextDouble(); INT int score = sc.nextInt(); FLOAT float amount = sc.nextFloat(); LINE - this gets an entire line, ignoring whitespace String fullName = sc.nextLine();

Comparing Values

- We can compare simple data types with <, >, ==, etc - Strings use compareTo - Integers use <, >, ==, etc - However, if we want to compare objects, we need to compare by a certain aspect such as age, name, rank, gpa, income, etc

Inheritance

- When multiple classes are very similar in nature - "IS" relationship - Has a base class that other classes called derived classes will share common variables and methods - In practice, each derived (or subclass) class should be more specialized. Example: the base class is Employee. Some subclasses include a professor, a janitor, etc. - Subclass functions have higher priority over the base class functions

Multiple Object Generic Class

- While the first generic class example accepted one generic object <T>, generic class can accept unlimited number of generic objects EX: public class Pair<T, S> { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ private T first; ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ private S second; ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ public Pair(T first, S second) { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ this.first = first; ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ this.second = second; ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ } ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ public T getFirst() { return first; } ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ public S getSecond() { return second; } ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ public void setFirst(T first) { this.first = first; } ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ public void setSecond(S second) { this.second = second; } ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ public String toString() { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ return "Pair [first=" + first + ", second=" + second + "]"; ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ } }

Wildcards

- Wildcards are both a convenience feature (more concise syntax), and to add support for co/contravariance for type parameters EX: public static void printAll (List<?> l) { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ for (Object o : l) System.out.println(o); } ? extends B -> means any subtype of B - ? is parent to B ? super B -> means any supertype of B - ? is inherited from B ? -> means any type

car and cdr Function

- You can combine car and cdr into cadr - You can also do multiple in one like cddddr or caadr - Max operation is cddddr and cadddr (car(cdr ... -> cadr (cdr(cdr ... -> cddr (car(car ... -> caar (car(car(cdr ... -> caadr (car '(A B C D E F)) -> A (cdr '(A B C D E F)) -> (B C D E F) (cadr '(A B C D E F)) -> B (cddr '(A B C D E F)) -> (C D E F) (caddr '(A B C D E F)) -> C (cdddr '(A B C D E F)) -> (D E F) NOTE: these examples are for literal lists: If you have a defined list, do: (car ____) You can also do (car (list _ _ _ ))

Java Development Kit (JDK)

- a JDK is a collection of tools for developing Java applications and software such as compiling and debugging - Comes with different editions, such as Java Standard Edition and Java Enterprise Edition - Contains the JRE (Java Runtime Environment) - JDK is Operating System (OS) specific

Sentence

- a string of characters over some alphabet

Lexeme

- an individual keyword, operator, etc - the lowest level of syntax ex: const, {}, , ,cout, <<, ", 5, ...

Polymorphism

- basic definition: the ability to take on many shapes - in programming allows an object of one type be used as a reference to objects of other types and leave it up to the compiler to call the correct method for each of the different object types the allows a single method call to behave differently, depending on the specific type of object it is associated with You can create the overall OBJECT, then define the exact "behaviors" later!!! Example: public class Dog { public void bark() {} } public class EnglishSpaniel extends Dog { public void bark() { System.out.println( "\n I say old chap... I'm english"); } } public class Chihuahua extends Dog { public void bark(){ System.out.println( "\n Yo quiero Taco Bell... I'm Spanish"); } } public class FrenchPoodle extends Dog { public void bark(){ System.out.println( "\n Bonjour mon ami... I'm French"); } } public class Driver { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ public static void main(String [] args) { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ Dog [] dogCollection = {new Chihuahua(), new FrenchPoodle(), new EnglishSpaniel()}; ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ for (int i = 0; i < dogCollection.length; i++) ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ dogCollection[i].bark(); ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ Dog Jason = new FrenchPoodle(); ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ Dog Killer = new Chihuahua(); ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ } }

Scheme Functions Quiz Q2: Which of the following choices are typical naming conventions used in Scheme? - Using PascalCase for predicate function names - - between words in function names - Camel case for function names - ? at the end of predicate functions

- between words in function names ? at the end of predicate functions

Operations on Lists

- car - accesses the value of the first element (single) - cdr - accesses all items after the current element (multiple) - cons - adding to a list

Ignoring Data in a File

- you have to read in EACH piece of data - However, you can make a variable as a trash can EX: String [] lname = new String[155]; String ignore = ""; int [] scores1 = new int[155]; int [] scores2 = new int[155]; int i = 0; while(infile.hasNextLine()) { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ String line = infile.nextLine(); ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ StringTokenizer tokenizer = new StringTokenizer(line); ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ lname[i] = tokenizer.nextToken(); ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ignore = tokenizer.nextToken(); ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ scores1[i] = Integer.parseInt(tokenizer.nextToken()); ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ scores2[i] = Integer.parseInt(tokenizer.nextToken()); ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ System.out.println(lname[i]+' '+scores1[i]+' '+scores2[i]); ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ i++; }

String Functions in Java

.length() -> int size .charAt(index) -> same thing as [] .toLowerCase() -> string .indexOf(char) -> return index of char .contains(char or string) -> return bool if in string .endsWith(char or string) -> return bool .replace(old, new char) -> string .substring(start, finish(optional)) -> string COMPARING STRINGS: string1.compareTo(string2) will output 1 if string1 is > string2 0 if string1 == string 2 -1 if string1 < string2 string1.equals(string2) outputs a boolean

Trees Quiz Q11: In scheme, every node of a tree has a datum and ___ or more children

0

Grammars Quiz Q9: Using the grammar below S → 1S | 0A0S | ε A → 1A | ε Match the leftmost derivation in order of steps for the string 01101100 S -> 0A0S -> 01A0S -> 011A0S -> ( 1 ) -> ( 2 ) -> ( 3 ) -> ( 4 )

1) 011ε0S 2) 01101S 3) 011011S 4) 0110110A0S

Parsing Quiz Q9: Match the following expression to their type of recursion 1. A->A*B B->int 2. A->B*int B->A | int 3. A->B*A B->int 4. A->int*B B->A

1) Direct Left Recursion 2) Indirect Left Recursion 3) Direct Right Recursion 4) Indirect Right Recursion

A Good Programming Language:

1) is maintained 2) has documentation 3) sizable usage 4) exercises and examples 5) training - Syntax and Semantics together define a language

List and Recursion Quiz Q10: For the following code, write the output: (define (series n) (if (< n 2) 1 (* n (series (- n 1))) ) ) (series 5) (series 3) (series 0) (series 6)

120 6 1 720

Parsing Quiz Q1: Given the following grammar: E -> E + T | T T -> T*F | F F -> (E) | id How many of the above rules are left recursive productions?

2

Scheme Functions Quiz Q3: What is the output of the following code? ((lambda (x) (/ (+ x 1) ( - x 1))) 3)

2

Grammars Quiz Q5: Which of the following are true? 1. Left associativity is used when operations are not evaluated from left to right like power(^) and bitwise compliment(~). 2. Fixing ambiguity usually requires adding more rules, better precedence order, and not as many | in a single sentence 3. You can change the derivation order from left to right within the same derivation as long as the derivation is in its first 5 steps. 4. Derivation order does not change the semantics of the expression if it unambiguous.

2 and 4

Trees Quiz Q9: What is the maximum number of nodes in a binary tree with a height of 8?

255 2^height - 1

Currying and Higher Order Functions Quiz Q10: What will be the output of the following use of the apply function? (apply * 3 '(1 3 4))

36

List and Recursion Quiz Q3: What is the output of the following code? (length '(a (bc) d (e f g) h))

5

Currying and Higher Order Functions Quiz Q8: What is the output of the following code? (define (curry-sub f) (lambda (x) (lambda (y) (lambda (z) ((f x (+ y z)))))) (display ((((curry-sub -) 15) 5) 1))

9

Files in Scheme

; File: filename.scm ; Written by: Vittoria Cicalese, Kevin Tang, Jerry Tran ; Date: 1/29/23 ; TAMU email: [email protected], [email protected], [email protected] ; Class: CSCE 314 ; Description: Lab 2 Question 0 - All files must end with (exit)

Grammars Quiz Q1: <expr> -> <expr> + <term> | <expr> - <term> | <term> <term> -> <term> * <factor> | <term> / <factor> | <factor> <factor> -> __________? | <exp> <exp> ( <expr> ) | <id> <id> A | B | C | ... In order to implement power (**) in the above grammar what do we need to add into the blank above?

<exp> ** <factor>

Currying and Higher Order Functions Quiz Q2: Which of the following options filled into the blanks results in the below scheme code correctly outputting the desired result? (sort '( 7 3 9) ___) desired result = (9 7 3) (sort '("zoo" "alpha" "car") ___) desired result = ("alpha" "car" "zoo")

>, string<?

Lexical And Syntactical Analysis Quiz Q1: Match the following regular expression characters to their meanings: ? + . $ *

? = 0 or 1 = optional + = 1 or more . = match any character except epsilon = wildcard $ = Delimiter character * = 0 or more

Objects

An instance is a type of object Ex: Human (object) and Kevin Tang (instance) - Objects are simple programmer defined data types. - Java is based on objects, defined by classes - By default, every member is private - Data members should be private, whereas the member functions should be public Ex: public class Employee { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎public String name; ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎public String department; ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎public String title; ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎public int salary; } Employee EM001 = new Employee(); ... You can define these in a separate .java file. All instances are created in the main/Driver Always compile from the main/driver file

Referential Transparency

Any two expressions in the program that have the same value can be substituted for one another anywhere in the program, without affecting the action of the program

List and Recursion Quiz Q9: Elements in a list - Are immutable - Hold only one value per element - Are mutable - Hold multiple values per element

Are immutable and only hold one value per element - While individual elements in a list are not changeable, the list itself is changeable

ArrayList Method Summary (very very similar to Vector)

ArrayList() - constructs an empty list with an initial capacity of 10 ArrayList(Collection<E> c) - constructs a list containing the elements in c in the order they are return by the collection's iterator ArrayList(int cap) - constructs a list with a capacity of cap add(E e) - appends e to the end of the list add(int i, E e) - insert e into index i addAll(Collection<E> c) - appends all elements in c in order to the end of the list addAll(int i, Collection<E> c) - inserts all elements in c in order to the index i clear() - removes all elements from list clone() - return a shallow copy of this list contains(E e) - returns true if the list contains e get(int i) - reads and returns the element at index i of this list (cannot return null - must be used with try and catch) indexOf(E e) - returns the index in this list of the first occurrence of e or -1 if the list does not contain e lastIndexOf(E e) - returns the index in this list of the last occurrence of e, or -1 if the list does not contain e isEmpty() - true if empty list (no elements) remove(int i) - removes the element at index i in this list remove(E e) - removes the first occurrence of e in this list set(int i, E e) - replaces the element at index i with e size() - returns the number of elements in this list

Currying and Higher Order Functions Quiz Q7: Which of the following programming constructs is NOT about function composition (that we have in Mathematics)? - Currying - Conditional Statements - Higher-order Functions

Conditional Statements

Java IO - reading

EXAMPLE: reading - everything is a string import java.io.*; import java.util.*; public class FileIO2 { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ public static void main(String[] args) { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ boolean debug = true; ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ int [] scores = new int[10]; ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ Scanner infile = null; ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ try { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ infile = new Scanner(new FileReader("scores.txt")); ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ } ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ catch (FileNotFoundException e) { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ System.out.println("File not found"); ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ e.printStackTrace(); // prints error(s) ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ System.exit(0); // Exits entire program ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ } ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ int s; // score ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ int i = 0; // (index) ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ while(infile.hasNextLine()) { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ String line = infile.nextLine(); ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ StringTokenizer tokenizer = new StringTokenizer(line); ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ // You should know what you are reading in ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ s = Integer.parseInt(tokenizer.nextToken()); ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ if(debug) { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ System.out.println("score received: " + s); ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ } ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ scores[i++] = s; ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ } ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ infile.close( ); ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ } }

Types of Parameters

Explicit: specific parameter is passed Implicit: parameter is an object variable Scheme accepts both

Currying and Higher Order Functions Quiz Q1: T/F: Currying is a useful tool in some languages such as JavaScript but is not supported in scheme or any scheme flavors.

F

Scheme Functions Quiz Q10: T/F: When you make changes to a file, the Scheme interpreter automatically reloads the file with the new changes

F

Currying and Higher Order Functions Quiz Q9: T/F: Is the output of this code (a b)? (define (member-if proc ls) (cond ((null? ls) #f) ((proc (car ls)) ls) (else (member-if proc (cdr ls))))) (member-if char? '(1 2 #\a 3 4 #\b))

F - The output is actually (a 3 4 b) - This is because the ((proc (car ls)) ls) statements will return the rest of the list whenever an element is a char

Top Down Parsing Example

Grammar: E -> T + E | E T -> (E) | int | int * T Input: int * int beginning:‏‏ E‏‏ start production: T + E‏‏‎‏ try first production:‏‏‎ (E) + E‏‏‎ does not match - try second production: int + E matches! - try third production: int * T + E better match! - keep third - move on: int * (E) + E ... keeps trying all combinations ... E T int * T int * int

Tokens

Groups of Items - A group or classification of Lexemes - Examples include <identifier>, <number>, <operator>, etc... - An identifier is a string consisting of only letters and characters that starts with a letter

Precedence in Grammar

HIGHER LEVELS IN THE TREE = lower precedence + - * / ^ ( ) LOWER LEVELS IN THE TREE = higher precedence EX: <assign> -> <id> = <expr> <expr> -> <expr> + <term> | <term> <term> -> <term> * <factor> | <factor> <factor> -> (<expr>) | <id> <id> -> A | B | C ...

Scheme Functions Quiz Q7: Which of the following will display true? I. (display (number? 10.0)) II. (display (char? 10)) III. (display (real? 10)) IV. (display (boolean? 10))

I and III

Comparable

Introspective - Inside the class itself - uses compareTo function - Can be used to compare homogeneous or heterogeneous objects - Can only have ONE compareTO function EX: (comparing to multiple objects; hetero) public class Employee implements Comparable<Object> { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ... ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ... ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ public int compareTo(Object x) { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ if(x instanceof Employee) { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ Employee e = (Employee) x; ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ if(this.getLast().equals(e.getLast())) ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ return this.getFirst().compareTo(e.getFirst()); ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ else ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ return this.getLast().compareTo(e.getLast()); ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ } ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ else if (x instanceof IndexCard) { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ IndexCard e = (IndexCard) x; ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ if(this.getLast().equals(e.getLast()))t ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ return this.getFirst().compareTo(e.getFirst()); ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ else ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ return this.getLast().compareTo(e.getLast()); ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ } ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ else { return -1; } // not a match ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ } ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ... ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ... } EX: (homo) public class Employee implements Comparable<Employee> { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ... ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ... ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ public int compareTo(Employee e) { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ if(this.getLast().equals(e.getLast())) ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ return this.getFirst().compareTo(e.getFirst()); ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ else ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ return this.getLast().compareTo(e.getLast()); ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ } ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ... }

Lexeme

Items - The actual sequence of characters (a string) that matches a pattern an dis classed by a token Examples: Identifier Token: x, count, name, etc... Integer Token: -12, 101, 1, 0, etc...

Iterator Example

Iterator <Employee> iter = Radford.iterator(); while (iter.hasNext()) { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ Employee aPerson = iter.next(); ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ System.out.print(aPerson.firstname + " "); ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ System.out.println(aPerson.lastname); ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ //OR System.out.println(aPerson); if we have a toString() }

Opening files for Reading

Manual opening: infile = new Scanner(new FileReader("document.txt")); Using user input: String fileName = sc.next(); ... infile = new Scanner(new FileReader(fileName)); FILE LOCATION: Parallel with source: ... new Scanner(new FileReader("document.txt")); Inside Source: ... new Scanner(new FileReader("./src/document.txt")); Inside a package: ... new Scanner(new FileReader("./src/dataSet/document.txt")); infile.close( ); - must write to save and close file

Currying and Higher Order Functions Quiz Q6: Match the common high order library functions with the code snippets such that the intended result is obtained. (_____ + '(2 2 2) '(1 3 5)) -> (3 5 7) (_____ ' (8 7 4) <) -> ( 4 7 8) (_____ even? ' (1 3 6 4)) -> (6 4) (_____ + '(1 3 5)) -> 9

Map Sort Filter Apply

Parsing Quiz Q5: Consider the below expression S ->A | B*A A -> B | C+B B -> B* int | double * B | int | double Can we extract int*int using a top down parser?

No because it is left recursive

Predictive Grammar

Non-Predictive Example: A -> B + C | B - See how this production can result in two sequences that start with the same symbol? - A predictive grammar has production rules where each result must start with a different symbol Any other symbol after the first can be the same/different it doesn't matter. Ex: A -> B + C | C

Polymorphism using Arrays & ArrayList

Normal Array: Dog [] pound = new Dog[3]; pound[0] = new EnglishSpaniel(); pound[1] = new FrenchPoodle(); pound[2] = new Chihuahua(); displayArray(pound); The Collection ArrayList: Employee Lupoli = new SalariedEmployee("Lupoli", new HireDate(7,1,2010), 80000); Employee Dima = new HourlyEmployee("Dima", new HireDate(8,18,2013), 50, 10); HourlyEmployee Steve = new HourlyEmployee("Steve", new HireDate(1,23,2023), 50, 10000000); ArrayList Workers = new ArrayList<Employee>(); Workers.add(Lupoli); Workers.add(Dima); Workers.add(Steve); while (list.hasNext()) { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ System.out.println(list.next()); }

Java Packaging

Packages = folders java -> lang -> a lot other classes - We import these classes: import java.lang.System; - To import all classes in a folder import java.lang.*; this is actually imported by default by many ide's

Shift Reduce Parsing

Parser action is determined by: 1) current look ahead 2) rightmost symbol in parser stack __lhs___ ^ ___rhs___ lhs: scanned portion of the input ^: dividing point or look ahead rhs: unscanned portion of the input Two actions: 1) Shift: move ^ to the right and add lookahead to stack 2) Reduce: apply inverse derivation - Shifts occur when there is a match or partial match - Reductions occur when there is no match - Simply make a rightmost parse tree and flip it, and then you'll get the answer pretty simply step - parse stack - look ahead - unscanned - action

Java IO - reading 2

import java.io.*; import java.util.*; public class FileIOTest { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ public static void main(String[] args) { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ boolean debug = true; ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ Employee [] CCBC = new Employee[10]; ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ for(int i = 0; i < CCBC.length; i++) { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ CCBC[i] = new Employee(); ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ } ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ Scanner infile = null; ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ try { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ infile = new Scanner(new FileReader("document.txt")); ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ } ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ catch (FileNotFoundException e) { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ System.out.println("File not found"); ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ e.printStackTrace(); // prints error(s) ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ System.exit(0); // Exits entire program ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ } ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ String name, department, title; ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ int salary; ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ int i = 0; ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ while(infile.hasNextLine()) { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ String line = infile.nextLine(); ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ StringTokenizer tokenizer = new StringTokenizer(line); ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ // You should know what you are reading in ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ name = tokenizer.nextToken(); ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ department = tokenizer.nextToken(); ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ title = tokenizer.nextToken(); ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ salary = Integer.parseInt(tokenizer.nextToken()); ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ CCBC[i] = new Employee(name, department, title, salary); ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ i++; ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ if(debug) { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ System.out.println("Employees name: " + name); ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ System.out.println("Employees department: " + department); ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ System.out.println("Employees title: " + title); ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ System.out.println("Employees salary: " + salary); ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ } ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ } ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ infile.close( ); ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ } }

JOptionPane

import javax.swing.*; - two types of output: input or message MESSAGES: 3 types JOptionPane.showMessageDialog (null, "Message", "Title", JOptionPane.__________); 1) .INFORMATION_MESSAGE 2) .WARNING_MESSAGE 3) .ERROR_MESSAGE INPUT: String s = JOptionPane.showInputDialog (null, "Enter an Integer: ");

User Defined Packages

package packagename; - imports come after package declaration Example: package practice: //imports public class Practice { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ public stat void main(String[] args) { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ System.out.println("Hello"); ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ } } ... //in some other class import practice.*;

Inheritance with Abstraction Example

public abstract class Employee { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ protected String name; ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ protected HireDate hireDate; ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ public abstract double getMonthlyPay(); ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ // abstract since this method will be defined differently in the sub classes ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ // both sub-class definitions need to return a double and be of the same name ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ public Employee(String name, HireDate hireDate) { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ this.name = name; ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ this.hireDate = hireDate; ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ } ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ public boolean samePay(Employee other) { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ return (this.getMonthlyPay() == other.getMonthlyPay()); ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ } ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ // setters and getters ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ public boolean equals(Object obj) { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ // ... }

Designing for Polymorphism

public class SuperClass { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ // inherited members ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ // constuctor to fill all inherited values ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ // inherited methods ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ // final methods } public class SubClass { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ // un-inherited members, unique items to this subclass ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ // constructor ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ // un-inherited methods, unique methods to this subclass ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ // overridden methods, more specific methods that the superclass had, but need more IF the superclass does not have it finalized ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ // class specific functions }

Grammars Quiz Q6: <S> -> <X> x <X> -> x | y <X> | <Y> <Y> -> y <Z> <Z> -> z | z <Z> Which of the following sentences below are constructable with the above grammar? (select all that apply) - yyzzx - xx - xxzy - xyx

yyzzx xx

Comments in Java

Same as C++: - use // for single line comment - use ./* */ for multi line comment

Output in Java

System.out.println( ... ) -> automatic newline at the end System.out.print(...) -> no newline at the end can concatenate between various data types: int x = 4; System.out.println("X is of value: " + x);

Grammars Quiz Q3: T/F: Meaning of list in Grammar is different than the list data structure.

T

Grammars Quiz Q4: T/F: If the greedy parse-tree and the spare parse-tree are different, it points towards ambiguity in the grammar.

T

Lexical And Syntactical Analysis Quiz Q2: T/F: The string "akz@874fmp" matches both the regular grammar and the regular expression: S -> A C B A -> a A | b A | c A | ... | z A | Ɛ B -> 0 B | 1 B | 2 B | ... | 9 B | A | Ɛ C -> @ | _ | - Regular Expression: [a-z0-9]*[@_-][a-z0-9]*

T

Lexical And Syntactical Analysis Quiz Q4: T/F: Given A = {Super, Iron, ε}, and B = {Man, Woman}. Now consider the set A*B, where "*" is a Kleene closure operator. Will "SuperIronMan" be in the set A*B?

T

List and Recursion Quiz Q6: T/F: Lists have side effects.

T

Trees Quiz Q13: T/F: We can use lists to represent trees in scheme, and recursion to traverse the tree.

T

Escape Sequences in Java

Tab \t Goto beginning of next line \r \\ \" \' \n \b \f

Built-in Conditional Functions

all functions are in this format (condition? x) eq? should be used for single elements (compares elements) - Checks if two parameters represent the same object in memory equal? should be used to more complex objects (compares objects) - Checks whether two lists, vectors, etc. are equivalent based on their elements number? real? rational? for floats integer? char? boolean? symbol? zero? null? for lists even? odd?

Left Recursion

calls itself leftmost <expr> -> <expr> + <term> | <term> <term> -> <term> * <factor> | <factor> - There's also indirect left recursion: A -> BC B -> A

Right Recursion

calls itself rightmost <factor> -> <exp> ** <factor> | <exp> <exp> -> (<expr>) | <id> - There's also indirect right recursion: A -> BC C -> A

Helpful EMACS commands

ctrl + a -> move cursor to the start of the current line ctrl + e -> move cursor to the end of the current line ctrl + k -> delete current line ctrl + y -> insert the most recent deleted line ctrl + - -> undo ctrl + x + s -> save without exit ctrl + x + c -> save and exit comments start with ; in Scheme

Lexical And Syntactical Analysis Quiz Q7: Which of the following string would match this Regular Expression: $[abcd]*ef$ - dcbaef - abcd - abcde

dcbaef

Escape Sequences

\t \\ \n \b \" \' (newline)

JVM: Runtime Data

***Method Area*** shared resource that stores all the space in memory where class-level data (such as static variables) are stored - class structures - superclass names - constructors - modifiers - functions, etc... - the reason why if you try to do something like Scanner.nextInt() it knows exactly if it can do it or not do it static variable - declared with the prefix "static" outside of a method - are accessible throughout the entire class - lifetime is the entire runtime of the program ***Heap area*** - is the space in memory where objects and instance variables are held - instance variables are declared inside of an object and are accessible to methods within that class. - instance variables have a lifetime that is the same as the lifetime of the object it's declared in - do you remember scope? - more covered below in JMM ***Stack Area*** - this is the area in memory where stacks (yes those stacks) are created - for each method that is called in a thread, a stack is created inside of the stack frame for that thread - remember call and values stacks? ***Stack Frame*** - is the holder for each method's stack - contains three parts 1) Local Variable Array: The array of the method's local variables 2) Operand Stack: Acts as a workspace for parameter operations of the called method 3) Frame Data: Where symbols that correspond to the method are stored ***PC Register*** - The PC (as in Program Counter) Register stores the address of the currently executing instruction in the a thread. - There is only one PC register for each thread, and has a small, fixed space. ***Native Method Stack*** - Similar to the method stack area, the native method stack area contains native methods that are written in different languages. - Native Methods - methods that have been implemented into a Java program, but have been written in a different language, most commonly C or C++

Overriding inherited methods

- constructors cannot be overridden - a function with static cannot be overridden Example: public class Base { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ private int data; ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ public Base () {data = 0;} ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ public Base (int n) {data = n;} ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ public int getData() {return data;} ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ public String toString() { return "base's data " + data; } } public class Derived extends Base { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ private int subData; ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ public Derived (int b, int d) { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ super(b); // sending 'b' to super class ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ subData = d; ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ } ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ public int getSubData() {return subData;} ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ public String toString() { //override inherited ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ return super.toString() + "\n" + ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ "subclass's data " + getSubData(); ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ } }

The Extends Reserved word

- extends can be thought of as a "better version", more detailed version you are "extending a template" - "extends" induces a subtyping relation example: Part Time professor would be a "subtype" of Employee S <: T (S is a subtype of T) P ⊢ Q (called a turnstile) means that Q is derivable from P Or from P, I know that Q...

Parse Trees

- graphical representation showing the hierarchical syntax structure of the sentence of the language they define - needs to know the grammar and desired sentence

Iterators in Arrays/Vectors/LinkedLists

- import java.util.Iterator; - Used to traverse a collection Iterator <OBJ> it = Collection.iterator(); - Two important functions: hasNext() - true if there is a next element - used to check if at the end of a Collection next() - moves to the next element in the Collection

LinkedList in Java

- import java.util.LinkedList; - All links are named next - All elements must by of the same object - Object is the most basic data type, but in these notes, Object is just a placeholder for the class name of an object LinkedList <Object> list = new LinkedList<Object>(); Object obj1 = new Object(...); Object obj2 ... list.add(obj1) list.add(obj2)

Scanner Actions

- lookup: determines if the lexeme is a reserved word - getchar: reads the next char from the input and places it into a variable "nextChar" - addchar: adds to "nextChar"

Regular Binary Tree

- only 2 children per node - children nodes may link up back to parent - left and right nodes follow a pattern

Java Virtual Machine (JVM)

- part of JRE (Java Runtime Environment) which is a set of components used to create and run a Java application - JVM is a run-time engine to run Java applications - It also the translator for the machine's (computer) particular OS and architecture such as Windows/MAC/Linux/etc... - But notice the JVM should also be installed on that particular machine!! Overall process from start to finish: - STEP 1: Create java code that compiles correctly - STEP 2: The java compiler generates the bytecode when you run your java program. - STEP 3: The Java virtual machine (JVM) uses that bytecode as its machine language, because it is a VIRTUAL MACHINE. - STEP 4: The JVM translates this bytecode into machine code that can be used by your OS (such as Mac, Windows, or Linux). - STEP 5: The machine code can now be executed by the computer.

Generics

- provide flexible type safety to your code, reduce the need for casting with collections and move many common errors from run time to compile time - CANNOT USE BASIC DATA TYPES - Must use object - Integer instead of int - Double instead of double - No casting needed

Instanceof

- subclass recognition using instanceOf - use if you wish access to specific subclass, but need to cast after recognition EX: public void display(Person x) { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ if(x instanceof Chef) { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ Chef temp = (Chef) x; ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ } ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ... }

The Final keyword

- the final keyword prevents an overriding of methods and member variables, classes, etc... - not a bad idea to have the base class as final

Machine Code

- the lowest level of code, A.K.A, binary code - syntax of the machine code varies depending on CPU and computer's operating system - Similar to how Java and Scheme can both use loops, but have different ways of writing the code to do the same task Binary code - is the ones and zeros that can be read by the central processing unit (CPU) of the machine, hence the name "machine language."

Semantics

- the meaning of the expressions, statements, and program units

Derivations

- the obtaining or developing of something from a source or origin - Steps from the starting symbol and multiple production rules to produce the desired sentence - Each derived line is called sentential form - There are three main derivation orders: 1) Leftmost 2) Rightmost 3) In Order

Syntax

- the physical layout or structure of the expressions, statements, and program units

Defining the Exact Behaviors

- this means we can create the Object (using the Super class) without determining which exact base object it is - can have some decision about it later in the code WHILE the code is running!! Example: Dog Amy; // not fully instantiated // MUCH later in the code boolean flag = true; if(flag == true) { Amy = new EnglishSpaniel(); } else { Amy = new FrenchPoodle(); }

Linked List Method Summary

LinkedList() - constructs an empty list LinkedList(Collection<E> c) - constructs a list containing the elements in c in the order they are return by the collection's iterator add(E e) - add e to end of list add(int i, E e) - insert e into index i addAll(Collection<E> c) - appends all elements in c in order to the end of the list addAll(int i, Collection<E> c) - inserts all elements in c in order to the index i addFirst(E e) - add e to beginning of list clear() - removes all elements from list clone() - return a shallow copy of this list contains(E e) - returns true if the list contains e get(int i) - reads and returns the element at index i of this list (cannot return null - must be used with try and catch) getFirst() - reads and returns the head of this list getLast() - reads and returns the tail of this list indexOf(E e) - returns the index in this list of the first occurrence of e or -1 if the list does not contain e lastIndexOf(E e) - returns the index in this list of the last occurrence of E, or -1 if the list does not contain e listIterator(int i) - returns a list-iterator of the elements in this list (in proper sequence), starting at index i peek() - retrieves, but does not remove, the head of this list (can return null) poll() - retrieves and removes the head of this list remove() - retrieves and removes the head of this list remove(int i) - removes the element at index i in this list remove(E e) - removes the first occurrence of the specified element in this list removeLast() - removes and returns the last element from this list size() - returns the number of elements in this list

Java IO - writing

PrintWriter outfile = new PrintWriter("doc.txt"); outfile.println( ... ); outfile.println( var1 + var2 + var3 + "" + var4 ...); SOME TIPS: - Identify types of variables you will be writing OUT to that file, unless you want to hard code an output - Identify the ORDER you wish to place the data to the file - Next declare an initialized variable for each of them - Then open the file for OUTPUT - Write data - Close file outfile.close();

Collections and compareTo

Within the Employee Class: public int compareTo(Employee x) { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ if (this.age == x.age) ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ { return 0; } ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ else if (this.age < x.age) ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ { return -1; } ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ else // (this.age == x.age) ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ { return 1; } } Within the collection class public int compareTo(Object that) { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ Employee x = (Employee) that; //casting‏‏ ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ if (this.age == x.age) ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ { return 0; } ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ else if (this.age < x.age) ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ { return -1; } ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ else // (this.age == x.age) ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ { return 1; } } Using the default compareTo to compare strings: public int compareTo(Employee x) { ‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ return this.getName().compareTo(x.getName()); }

Syntax vs Semantics Examples

Wrong Syntax, Correct Semantics - "I is a University Student" Correct Syntax, Wrong Semantics - "A rectangle loves chicken rice" Wrong Syntax Wrong Semantics - "House worm chair long fast twenty-two"

Lexical And Syntactical Analysis Quiz Q3: Select the regular expressions that would match the below strings 1. abcdef6aaabbb 2. 012aa3fcedd0 3. fdabec872 4. 0123987654 - [a-f0-9]* - [0-9]*[a-f]*[0-9]?[a-f]*[0-9]* - [0-9]*[a-f]+[0-9]?[a-f]+[0-9]* - [0-9]+[a-f0-9]+[0-9]*

[a-f0-9]* [0-9]*[a-f]*[0-9]?[a-f]*[0-9]*


Set pelajaran terkait

Accounting for managers Midterm Study guide

View Set

SLP 108- All Quiz Questions (Final Prep)

View Set

Exam 3- Hospice and palliative care

View Set

Personal Finance Insurance Review

View Set

Advanced Public Health Nutrition Exam 1

View Set

Box and Whisker Plots, Box and Whisker Plots, box and whisker plots

View Set