48024 - Programming 2


Patterns

Pattern Modello 模様

Patterns are this lame concept of mapping a desired action with a block of code, making programming more depressing and making you contemplate suicide.

Sum pattern

Pattern that finds the sum of a collection of items

type sum = 0;
for (item) {
 sum = sum + item;
}

Output pattern

Pattern that prints an item

System.out.println("label"+value);

Read pattern

Pattern that reads an item

System.out.println("prompt");
type var = readingFunction();

Read loop pattern

Pattern that reads items until signaled to stop

type var = readPattern();
while (var != delimeter) {
 f(var);
 var = readPattern();
}

Count pattern

Pattern that counts a collection of items

int count= 0;
for (item) {
 ++count;
}

Array loop pattern

Pattern that loops over every item in an array

for (int i = 0; i < array.length; i++) {
 f(array[i]);
}

Max pattern

Pattern that finds the maximum value in a collection of items

type max = 0;
for (item) {
 if (item > max) {
  max = item;
 }
}

String loop pattern

Pattern that loops over every character of a string

for (int i = 0; string.length; i++) {
 f(string.charAt(i));
}

For-each loop pattern

Pattern that loops over every item in an array

for (type item : array) {
 f(item);
}

Merged read loop pattern

Pattern that reads an item, but more efficiently than the read loop pattern

type var;
while ((var = readPattern()) != delimeter) {
 f(var);
}

Any pattern

Pattern that loops over every item in a collect and returns a boolean value if any item passes a designated test

boolean anyPattern(type[] array) { for (type item : array) {
 if (condition) {
  return true;
 return false;
}

Constructor from literals pattern

Pattern of a constructor that takes literals

public Constructor() {  this.a = 1;  this.b = 2; }

Constructor from user pattern

Pattern of a constructor that takes from user input

public Constructor() {  this.a = readPattern("a: ");  this.b = readPattern("b: "); }

Constructor from parameters pattern

Pattern of a constructor that takes parameters

public Constructor() {  this.a = readPattern();  this.b = readPattern(); }

Getter pattern

Pattern of a function that returns a class object

public type getPattern() {  return this.obj; }

Setter pattern

Pattern of a function that defines sets a class object

public void setPattern(type a) {  this.obj = a; }

toString pattern

Pattern that represents a class object as a string

Menu pattern

Pattern that produces an iterative menu system

Class pattern

Pattern that does something?

Remove all matches pattern

Pattern that removes all matching instances in a list

Remove one match pattern

Pattern that removes the first matching instance in a list

Lookup pattern

Pattern that returns the first matching instance in a list

Find all matches pattern

Pattern that returns all matching instances in a list

Copy list pattern

Pattern that creates a second instance of a list

Observer pattern

Pattern that sets up an observer; an object that executes when a graphical object is interacted with, containing the following steps

Inner classes pattern

Pattern that encapsulates a class within another class, this allows the inner class to access members from the outer class

Anonymous inner classes pattern

Pattern that defines the implementation of an interface instance while being instantiated

Lambda expresions pattern

Shorthand syntax for creating a function

start pattern

Pattern that initialises and executes GUI stage

setOnAction pattern

TextField getter pattern

Pattern that returns value in a TextField

private int getValue() {
 return Integer.parseInt(textFieldObj.getText());
}

TextField setter pattern

Pattern that sets a value in a TextField

private void setValue(int value) {
 return textFieldObj.setText(""+ value);
}

Process

Goal-Plan-Key

Method of programming without patterns, where you visualise a goal, plan how you want to execute it, and find a key line of code that is the core of said plan, then build the rest of the code around the key. Who would've guessed...

Incremental goals Scopi incrementale 段階的な目標

Assume your goal is too broad; start with a smaller goal and slowly grow up to the main goal.

Modulo モジュロ

The operator that returns the remainder of dividing two numbers. Has useful properties, as you know

Debugging デバッグ

Removing errors from code through tests. Instead of testing every possible value, select a finite set of values that represent each type of possible scenario

Methods and Strings

Method Metodo メソッド

Function created by a programmer (not built into language). Two types:

Side effect Effetto collaterale 副作用

When a function method changes a value; that is bad

Java string methods

Classes

Design rules Regole di disegno デサイン規則

Access modifiers

Software design process

Lists

Array list

List of an arbitrary size, and when exhausted is replaced and copied over to a larger array list

Accessing is in \(O(1)\), and writing is in \(O(n)\), where \(n\) is the distance from the end of the list

Linked list

List made of objects that have the content and a pointer to the next object

Writing is in \(O(1)\), and accessing is in \(O(n)\), where \(n\) is the distance from the start of the list

Generics

Method of specifying types for lists

System design

Interfaces

Layout of the methods of a range of classes

Polymorphism

The idea of an object becing able to identify with a more general class as well as its own class, for instance, squares and triangles can find common ground as polygons and be stored in lists together

Superclasses

Act of a class having subclasses that are related to its superclass. If the subclass uses a superclass method differently to the superclass, we declare it abstract in the superclass

GUI

Node

Graphical object

Scene

Tree of nodes

Stage

Locaction where a stage is displayed

Application

Main method for setting up stage

Methods

Observer

Object that monitors for a specific action of an object and on said action executes a method

Event-driven programming

Event

An interaction in a GUI application, such as a button being clicked or a mouse dragged on an object

EventHandler<X>

Interface applied to observer class that handles event X

Lambda

Shortcut for creating functions with no label

Model View Control

FXML

XML used to represent JavaFX graphical objects, written in .fxml files

text

JavaFX and XML object that represents text that can be set

label

JavaFX and XML object that represents a label for an input form

CSS

CSS can be used with FXML, however before each attribute you must add "-fx-", and in the .fxml file, you denote classes as "styleClass" instead of HTML's class

styleClass

Model

Code containing logic for the data objects

JavaFX properties

View

Code representing graphical objects that are observed

@FXML

Annotation that fills a JavaFX node with FXML data based on an ID that is the same as the variable's name

Namespace

A namespace is an attribute in an XML object to help avoid collisions with other objects with the same name but different meaning

Control

Code containing observers that observe the view and parse data to the model for logic

onAction

JavaFX intiialize method

Binding

Changing property 1 when property 2 is changed with method bind(). You can bind through FXML by writing the property inside a text attribute with {} wrapping around any referenced objects or java code

Bidirectional binding

Binding as well as changin property 2 when property 1 is changed with method bindBidirectional()

Methods

GUI Lists and Tables

Packages

Collection of related classes, listed as a reverse domain name withsub-package names (if applicable)

ListView

Java/FXML object that displays a list of items of a certain type (either String or an object with a toString() method). There are two modes for this object:

You can set a placeholder for an empty ListView

UTS ViewLoader

To make new windows, UTS made a package (au.edu.uts.ap.javafx) that allows to open v, however requires your controllers to extend to the Controller<X> class, where X is the name of the class you are controlling for

Methods

Closing a window

use stage.close() in the control controlling the window you desire to close

TableView

Java/FXML object that displays a list of items of a certain type (either String or an object with a toString() method). It has a row for each item and a column for each item's property.

You can set a placeholder for an empty TableView

Change Listener