



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The project requirements for the terp idol contest in the object-oriented programming ii course at the university of maryland, college park. Students must implement online voting using networking, streams, and multithreading. An overview of the contest rules, example tally results, and details on setting up the server and clients, as well as handling client-server requests.
Typology: Study notes
1 / 5
This page cannot be seen from the preview
Don't miss anything!




1
2
Implement online voting for a Terp Idol contest
Networking Streams Multithreading
3
As many times as they wish
4
Votes – Bart, Lisa, Homer, Lisa, Bart, Lisa Tally – Bart 2, Homer 1, Lisa 3 Homer eliminated
Votes – Lisa, Lisa, Bart, Lisa Tally – Bart 1, Lisa 3 Bart eliminated
Lisa wins (only contestant)
5
Must use a client-server model Use threads to handle multiple simultaneous clients Use synchronization to avoid data races
Server, Client ContestServer, IdolClient, IdolCommandProcessor
6
Supports multiple Clients concurrently Put each Client connection in separate thread Handles ping & disconnect
Starts server in separate (daemon) thread
Implements processing of commands
7
Creates connection to Server Sets up communication streams Handles ping & disconnect
Support Judges and Voters Different types of requests shown in the table
8
Requests & Responses
NEW ROUND NEW ROUND
TALLY TALLY <names & # votes>
INVITE
IMAGES
LOOKUP
VOTE
CONTESTANTS CONTESTANTS
Voter
DISCONNECT
PING SERVER RUNNING
Sent by Client Sends Server Returns
9
Implements processing of commands All commands in the form of Strings
Recommend DataInputStream / DataOutputStream To handle newline characters in data
10
Other Relevant Topics
11
Client Programming
12
Simple Server Programming
19
Creating Threads in Java
public class MyT implements Runnable {
public void run( ) { … // work for thread }
}
Thread t = new Thread(new MyT( )); // create thread t.start(); // begin running thread … // thread executing in parallel
20
Lock
Entity can be held by only one thread at a time
A type of synchronization Used to enforce mutual exclusion Thread can acquire / release locks Thread will wait to acquire lock (stop execution) If lock held by another thread
21
Lock Example
public class DataRace extends Thread { static int common = 0; static Object o; // all threads use o’s lock public void run() { synchronized(o) { // single thread at once int local = common; // data race eliminated local = local + 1; common = local; } } public static void main(String[] args) { o = new Object(); … } } 22
Stream Input/Output
A connection carrying a sequence of data (ordered sequence of bytes)
Files, memory, other Strings
Data consisting of characters (e.g., text files) Data consisting of raw bytes (e.g., binary files) Can buffer information
Can define stream with desired characteristics
23
Using Streams
Connects program to external data Location of stream specified at opening Only need to refer to stream
See fileExamples package 24
Scanner Class
Read primitive types & strings from input stream Including System.in (standard input) Provides methods to treat input as String, Integer… Supports String nextLine( ), int nextInt( )… Throws InputMismatchException if wrong format
25
Scanner Class Examples
// old approach to scanning input BufferedReader br = new BufferedReader( new InputStreamReader(System.in)); String name = br.readLine( ); // new approach using scanner Scanner in = new Scanner(System.in); String name = in.nextLine( ); int x = in.nextInt( );
See ScannerExample.java Note use of printf