Send a link to your students to track their progress
17 Terms
1
New cards
GUIs
JFrame - windows, ActionListener - listens for actions from elements. Polymorphism then provides the mechanism for allowing many implementations for that one click-reactor interface. Swing GUI components (like `JFrame`) automatically use at least two threads. Control layout through BorderLayout (North, West, South, etc). JPanel can hold multiple components
2
New cards
Java over C
Java is object oriented and Java is platform independent
public class Ex0 { public static void main(String[] args) { Thread t = new Foo();
t.start(); int x = 5 * 5; System.out.println(x); } } public class Foo extends Thread { public void run() { int x = 7 * 7; System.out.println(x); } } ```
5
New cards
Join Threads
Java provides another method called `join` that forces the thread it is called upon to wait (to finish) before allowing the other thread to continue. *What?*
For example, if you have the following code inside a `main` method as above:
```java t.start(); ... try { t.join(5000); } catch (InterruptedException e) { System.out.println("Interrupted while joining a thread..."); } ```
this code will force the `main` thread (where we started `t`) to wait for five seconds, potentially allowing the `run()` method of `t` to finish. You can also call `join()` without any argument to force the wait until `t` finishes completely.
The `isAlive()` method can be called on a thread to check if it is still running.
6
New cards
Race Conditions
In general, parallel programming is fraught with peril! If you’re not careful, you can end up with bugs whose occurrence or precise behavior depends on the exact order in which the instructions of each thread are executed relative to one another. This kind of bug is called a **race condition**.
7
New cards
Synchronization
```javascript public synchronized void doSomething() { // code to be executed in a synchronized manner } ```
In this example, the `doSomething()` method is synchronized, which means that only one thread can execute this method at a time for a given object.
8
New cards
Socket Class
In Java, the `Socket` class is used as the **client** socket in a two-way communication with a **server**. Key to this idea is that the socket is *connected* to the server at a ipaddress/host and port. The main constructor you’ll use is
```javascript Socket(String host, int port); //Creates a TCP stream socket and connects //it to the specified port number on the named host. ```
> Note that all `Socket`s in java are TCP (SOCKSTREAM) sockets, if you want to use a different socket type, you’ll need to use one of the other socket classes, like `DatagramSocket`.
Importantly, the `Socket` is to connect to the server, but communication with the server is a two-way procedure. So once we connect to the server, we can *both* read and write to the socket via the socket’s `InputStream` and `OutputStream`. And like with other I/O we can wrap those streams in other buffered readers/writers.
try{ PrintWriter pw = new PrintWriter(sock.getOutputStream()); pw.println("HelloWorld"); pw.close(); //close the stream sock.close();//close the socket }catch(Exception e){ System.err.print("IOException"); System.exit(1); }
} } ```
10
New cards
Server Socket Class
The server side of this works pretty much like the client side, except for an additional step of the server needing to *accept* an incoming connect. The key class is the `ServerSocket` which we initialize like so
```javascript ServerSocket serverSocket = new ServerSocket(portNumber); ```
The `portNumber` is which port we want to be listening for on an incoming connection. Once the `serverSocket` is established we call `accept()`, which returns a new `Socket` that is used to communicate with that client.
//start the thread (new ClientHandler(clientSock)).start();
//continue looping }catch(Exception e){} //exit serve if exception } }
private class ClientHandler extends Thread{
Socket sock; public ClientHandler(Socket sock){ this.sock=sock; }
public void run(){ PrintWriter out=null; BufferedReader in=null; try{ out = new PrintWriter(sock.getOutputStream()); in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
//close the connections out.close(); in.close(); sock.close();
}catch(Exception e){}
//note the loss of the connection System.out.println("Connection lost: "+sock.getRemoteSocketAddress()); }
}
public static void main(String args[]){ EchoSocketServer server = new EchoSocketServer(2021); server.serve(); }
} ```
13
New cards
Testing
Fault localization - find problem. Generally write test cases before writing code (Test Driven Development)
**verification** versus **validation**. The former checks that your implementation meets the requirements that were written, while the latter measures if the requirements themselves where what should have been written.
Unit Tests - per method, module/integration tests - how do they work together, system testing - highest level
14
New cards
Input Domain Partitioning
Using Input Domain Partitioning, developers partition the possible input space into some number of classes, and then *draw samples from those classes in all possible combinations with respect to classes*.
15
New cards
Junit
Java, like many/most other languages, has unit testing frameworks that make is easy for developers to define assertions and other behavior, and re-run automated test suites. `Junit` is such a unit testing framework for Java; recall, that unit testing is often meant to test specific methods and/or classes. The basic idea is to write *assertions* about what results are expected for certain inputs to specific methods. In Junit, this is all just java code; the test harness then knows what to consider a test case when the developer adds an `@Test` annotation to the top of any such method:
16
New cards
Action Listener
```javascript b.addActionListener(new ButtonClickListenerEx1(l)); public class ButtonClickListenerEx1 implements ActionListener {
private JLabel label;
public ButtonClickListenerEx1(JLabel label) { this.label = label; //save the label to modify }
public void actionPerformed(ActionEvent e) { if (label.getText().equals("Hello")) { label.setText("Goodbye"); //flip text back and forth }else{ label.setText("Hello"); } } } or b.addActionListener(new ActionListener() { //implement the one method here //shares the name space with the whole class //has access to the label field above public void actionPerformed(ActionEvent e) { if (label.getText().equals("Hello")) { label.setText("Goodbye"); }else { label.setText("Hello"); } } }); ```