Difference between revisions of "Assessment 2011 2"

From Progzoo
Jump to: navigation, search
Line 1: Line 1:
 +
<shell><![CDATA[
 +
import java.io.BufferedReader;
 +
import java.io.FileReader;
 +
import java.util.ArrayList;
 +
import java.util.HashMap;
 +
class Event{
 +
  public String id;
 +
  public String module;
 +
  public String etype;
 +
  public String day;
 +
  public String start;
 +
  public int duration;
 +
  public String weeks;
 +
  public String room;
 +
  public String staff;
 +
  public ArrayList<String> students;
 +
  public Event(String line){
 +
    String[] flds = line.split("\t");
 +
    id=flds[0];
 +
    module=flds[1];
 +
    etype=flds[2];
 +
    day=flds[3];
 +
    start=flds[4];
 +
    duration=Integer.parseInt(flds[5].substring(0,1));
 +
    weeks=flds[6];
 +
    room=flds[7];
 +
    staff=flds[8];
 +
    students = new ArrayList<String>();
 +
    for (int i=9;i<flds.length;i++)
 +
      students.add(flds[i]);
 +
  }
 +
}
 +
 +
public class P{
 +
  static HashMap<String,Event> events = new HashMap<String,Event>();
 +
  public static void main(String[] args) throws Exception{
 +
    BufferedReader fh =
 +
    new BufferedReader(new FileReader("timetable.txt"));
 +
    String s;
 +
    while ((s=fh.readLine())!=null){
 +
      Event e = new Event(s);
 +
      events.put(e.id,e);
 +
    }
 +
    fh.close();
 +
    doStuff(events);
 +
  }
 +
QcQ
 +
}
 +
]]></shell>
 
*[[Assessment 2011 2 Easy]]
 
*[[Assessment 2011 2 Easy]]
 
*[[Assessment 2011 2 Medium]]
 
*[[Assessment 2011 2 Medium]]
Line 26: Line 75:
 
The structure '''events''' is supplied for most questions. This is a '''HashMap''' that relates the event id to the event object.
 
The structure '''events''' is supplied for most questions. This is a '''HashMap''' that relates the event id to the event object.
  
 +
==Looking up events in the event map==
 
<question copyFile='timetable.txt'>
 
<question copyFile='timetable.txt'>
<prog>
+
If you know the event id you can use '''get''' to retrieve the Event object associated.
import java.io.BufferedReader;
+
 
import java.io.FileReader;
+
You access the public members of the object using dot notation.
public class P{
+
<prog><![CDATA[
  public static void main(String[] args) throws Exception{
+
static void doStuff(HashMap<String,Event> events){
    BufferedReader fh =
+
  Event e = event.get("SET07102.L2");
    new BufferedReader(new FileReader("timetable.txt"));
+
  System.out.println(e.room);
    String s;
+
    while ((s=fh.readLine())!=null)
+
      System.out.println(s);
+
    fh.close();
+
  }
+
 
}
 
}
</prog>
+
]]></prog>
 
</question>
 
</question>

Revision as of 00:33, 9 October 2011

The Event class contains the following public attributes:

 public String id;
 public String module;
 public String etype;
 public String day;
 public String start;
 public int duration;
 public String weeks;
 public String room;
 public String staff;
 public ArrayList<String> students;

An example event is given in the table:

 id       SET07102.P2a
 module   SET07102
 etype    Practical
 day      Wednesday
 start    14:00
 duration 2
 weeks    2-13
 room     M.Kilby.C09
 staff    Co.Set.Chisholm, Ken
 students Co.Cc1f-A       Co.Cc1f-B

The structure events is supplied for most questions. This is a HashMap that relates the event id to the event object.

Looking up events in the event map

If you know the event id you can use get to retrieve the Event object associated.

You access the public members of the object using dot notation.


[Font] [Default] [Show] [Resize] [History] [Profile]