Difference between revisions of "SET08112"

From Progzoo
Jump to: navigation, search
(SET08112 ArrayList Interface Tutorial)
(Problem 1 - Sort a List)
Line 54: Line 54:
 
       names.add(line);
 
       names.add(line);
 
     java.util.Collections.sort(names);
 
     java.util.Collections.sort(names);
 +
    for (String s:names)
 +
      System.out.println(s);
 +
  }
 +
}
 +
</answer>
 +
</question>
 +
 +
==Problem 2 - List Routes==
 +
<question className="SortStudents" title="Using Sort" copyfile="students.txt">
 +
The file year1.txt contains a list of all students in year one. Each line contains the name of the student and the name of the route (programme) they follow separated by a tab.
 +
AITKEN, HANNAH BENG (HONS) COMPUTING F/T
 +
COCHRANE, LEE  BENG (HONS) COMPUTING F/T
 +
CURSTON, REBECCA BSC (HONS) DIGITAL MEDIA F/T
 +
...
 +
Write a program to read in this file and output a list of routes with no duplicates.
 +
You can use a program very similar to the one above – but this time:
 +
*Use line.split("\t") to get the route
 +
*Use contains to only add the route if it is not already in the ArrayList
 +
*You do not need to sort before output
 +
 +
<prog>
 +
import java.util.ArrayList;
 +
import java.io.BufferedReader;
 +
import java.io.FileReader;
 +
 +
public class SortStudents{
 +
  public static void main(String[]argv) throws Exception{
 +
    ArrayList&lt;String> names = new ArrayList&lt;String>();
 +
    String line;
 +
    BufferedReader fh
 +
      = new BufferedReader(new FileReader("students.txt"));
 +
    while (null!=(line=fh.readLine())){
 +
      String [] st = line.split("\t");
 +
      names.add(st[1]);
 +
    }
 +
    for (String s:names)
 +
      System.out.println(s);
 +
  }
 +
}
 +
</prog>
 +
<answer>
 +
import java.util.ArrayList;
 +
import java.io.BufferedReader;
 +
import java.io.FileReader;
 +
 +
public class SortStudents{
 +
  public static void main(String[]argv) throws Exception{
 +
    ArrayList&lt;String> names = new ArrayList&lt;String>();
 +
    String line;
 +
    BufferedReader fh
 +
      = new BufferedReader(new FileReader("students.txt"));
 +
    while (null!=(line=fh.readLine())){
 +
      String [] st = line.split("\t");
 +
      if (!names.contains(st[1]))
 +
        names.add(st[1]);
 +
    }
 
     for (String s:names)
 
     for (String s:names)
 
       System.out.println(s);
 
       System.out.println(s);

Revision as of 13:53, 26 January 2011

SET08112 ArrayList Interface Tutorial

In this tutorial you will use the ArrayList and the HashMap or TreeMap to solve some common problems. You will use methods such as:

  • java.util.Collections.sort
  • add
  • get

Problem 1 - Sort a List

The file from http://www.dcs.napier.ac.uk/~cs66/SET08112/tut2/students.txt contains a list of students:

ADAMS, ROBERT
HASTIE, MARTIN
OTHIENO, JUDITH
ANDERSON, ROSS
...

Create a programme SortStudents.java that reads in this file and outputs the students in Alphabetical order.

  • You can use an ArrayList to hold the students.
  • ArrayList<String> names = new ArrayList<String>();
  • You can use java.util.Collections.sort to sort the ArrayList


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

Problem 2 - List Routes

The file year1.txt contains a list of all students in year one. Each line contains the name of the student and the name of the route (programme) they follow separated by a tab.

AITKEN, HANNAH 	BENG (HONS) COMPUTING F/T
COCHRANE, LEE  	BENG (HONS) COMPUTING F/T
CURSTON, REBECCA	BSC (HONS) DIGITAL MEDIA F/T
...

Write a program to read in this file and output a list of routes with no duplicates. You can use a program very similar to the one above – but this time:

  • Use line.split("\t") to get the route
  • Use contains to only add the route if it is not already in the ArrayList
  • You do not need to sort before output



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