CS3101-3: Programming Languages: Java
Homework 4

Out: Oct 1 7PM
Reading: sections on networking and threads.
DUE: Wed, Oct 8, 11PM (on the button)

Goal:

  1. Learn to create Java packages
  2. Learn to manipulate Java threads
  3. Have Fun!



  1. NOTE ALL CLASSES IN THE HOMEWORK should be part of
    package homework4;
    which should be placed on top of each file, and all files be inside a sub directory called homework4
    NOTE2: and executing main from a level above, Java homework4\somemainclass etc
    define :
    public abstract Worker

    this class will contain the following fields:
    int id This represents the unique ID of the worker
    long start_time This represents the starting time of when the worker started to work
    long total_work_time Total amount of work time used
    long creation_time When the worker was created
    Boolean workDone if the work is done set to true, else false (default)
    boolean busy set to true if in middle of working String work This is the work object you are working with
    boolean haveWork set to true if work pending


    In addition the following methods should be declared abstract:

    long getCreationTime (void)
    void setID(int)
    long getTotalWorkTime(void)
    void setCreation(long)
    void setStartTime(long)
    void addToTotalTime(void)
    boolean isBusy(void)
    boolean isWorkDone(void)
    String getWork(void)
    void DoThisWork(String)
    This string passed here represents some instructions to the worker

    Now create a subclass called WebWorker which extends the worker, and implements Runnable
    Lookup Runnable in the api now
    The basic idea of the class is to create an object which is capable of going to a Internet URL page. fetch the page, computer some information from the page (see below which info has to be collected). then save the information and wait for more stuff to do. please remember the work should all be defined in the run method. if you do not have a page to fetch you should sleep until you have some work pending. try to have the worker sleep for 1 + random(0-5) seconds.

    Include a main which takes a web page as a command line argument and prints out the following information about the web page:
    The name of the web page
    The title of the web page
    The size of the web page
    Time of last edit

    THEN: output the time the worker was created, amount of time used to work (end-start).

    If you fail to fetch, just return a message saying its not possible to get it.

  2. Create a second class called Example4b, this class will do the following:
    In the main, it will open a txt file of URL addresses,
     start 5 webworkers each with own unique ID.
    put all the workers in a thread group (Lookup api).
    start a simple loop. for each worker, if busy ignore,
    if have results save in hashtable (see following explanation)
    if not busy, assign a URL to it.
    once we check all workers, pause for 3 seconds then loop again. once we are all done, print out the following summary:

    for each worker, how much total time was used. for each worker, how many URLs were processed by that worker.

    in addition a file called out.txt should be generated. for each URL, have the collected information gathered there.

    Hint: you mind need a few hashtables.



    Note on Hashtables:
    Lookup API.
    A hashtable is a simple table with each row having a key and some other information. for example imagine the class list as a list of your information, and your students id as a key for each of you. in our case, the id of the worker can be the key and you can store any other information in relationship to the key. please note, stored objects should be non primitives, which means if you want to store an int, you use the Integer class.
    Note: before inserting into the hashtable, do a check if the key is already in the table or else it will be overwritten. example if the key is a String name = "Shlomo" and the Hashtable hash = new Hashtable();

    if( ! hash.containsKey(name) )
    {
        hash.put(name,new Integer(1));
    }
    else {
    Integer I = (Integer)hash.get(name);
    hash.put(name,new Integer(I.intValue() ++));
    }