CS200 Programming Assignment 1 (PA1)

For this assignment, you will

  1. Convert a Node class to be generic, and test it,
  2. Write a LinkedList class that is a singly-linked list of Node objects,
  3. Write a new class named Senator to hold information about a member of the US Senate,
  4. Read information from a file for each senator and store it in a linked list of nodes each holding information for one senator,
  5. Compute some averages.

Each class will be in a separate file. Each class is public.

Nodes

Convert the Node class in the following file to be generic.

Node.java
public class Node {
 
   protected Object item;
   protected Node next;
 
   Node( Object itemArg ) {
      item = itemArg;
      next = null;
   } 
 
   Node( Object itemArg, Node nextArg ) {
      item = itemArg;
      next = nextArg;
   } 
 
   public static void main( String [] args ) {
 
      Node firstNode = new Node(new String("last"));
      firstNode = new Node(new String("middle"),firstNode);
      firstNode = new Node(new String("first"),firstNode);
 
      for (Node current = firstNode; current != null; current = current.next) {
         System.out.println((String)current.item + ' ');
      }
   }
}

Notice that even the Node class includes a main method, for testing.

Edit this file so that Node is a generic class with a parameterized type for item. Test it by creating a node to hold a String, like in the orignal code. Then test it by creating a node that holds items of type Integer, the int wrapper class.

Linked List

Write a generic LinkedList class that uses your generic Node class. The nodes in the linked list are singly-linked and the list starts with a dummy node. Implement the methods required to successfully run the following tests.

LinkedList.java
   public static void main( String [] args) {
 
      LinkedList<String> list = new LinkedList<String>();
      list.insert(0,new String("middle"));
      list.insert(0,new String("first"));
      list.insert(2,new String("last"));
      System.out.println(list);
 
      LinkedList<Double> listd = new LinkedList<Double>();
      listd.insert(0,new Double(3.14));
      listd.insert(0,new Double(23.123));
      listd.insert(0,new Double(-0.32));
      System.out.println(listd);
      listd.remove(1);
      System.out.println(listd.get(1));
      System.out.println("Size of listd is " + listd.size());
      System.out.println(listd);
    }

Your output should look like

first
middle
last

-0.32
23.123
3.14

3.14
Size of listd is 2
-0.32
3.14

Senators

Write a Senator class that holds for a senator

  • senator's name as a String,
  • senator's party as a char, 'D', 'R', or 'I'
  • senator's state as a String, like 'CO',
  • percentage of senator's votes that align with the party vote as an int.

The class must include a constructor and a toString method. Include a main method to test it, such as

Senator.java
   public static void main(String [] args) {
      Senator member = new Senator("Chuck Anderson", 'D', "CO", 67);
      System.out.println(member);
   }

which should result in

Chuck Anderson (D-CO) 67

List of Senators

Write a new class named Voting that performs the following steps. The senators' voting information was obtained from this Washington Post web site.

  • Read senator information from the file senators.txt and store in a LinkedList object,
  • print the list of senators showing all information,
  • calculate and print the average voting percentage for all Democrats,
  • calculate and print the average voting percentage for all Republicans.

Your output should look exactly like the following, except that the full list of senators will appear in your output. Also, the final average percentages will not be 44.4%. In your calculations, ignore senators who are Independents, marked by 'I' instead of 'D' or 'R'.

Ron Wyden (D-OR) 96
Roger Wicker (R-MS) 91
Sheldon Whitehouse (D-RI) 97
Jim Webb (D-VA) 87
Mark Warner (D-VA) 92
George Voinovich (R-OH) 69
David Vitter (R-LA) 92
Tom Udall (D-NM) 96
Mark Udall (D-CO) 92
  .
  .
  .
Lamar Alexander (R-TN) 84
Daniel Akaka (D-HI) 97

Average party line vote percentages: R 44.4% D 44.4%

Check In

First, make sure your code compiles and runs correctly on our Linux workstations.

Then, tar together into a file named pa1.tar your files

  • LinkedList.java
  • Node.java
  • Senator.java
  • Voting.java

On Linux, do this wtih

tar cvf pa1.tar LinkedList.java Node.java Senator.java Voting.java

Then, on RamCT, click on “Programming Assignment 1” and submit your tar file. You may continue to submit new versions until the deadline of Thursday, September 12th, 10:00 PM.

Recent changes RSS feed CC Attribution-Share Alike 3.0 Unported Driven by DokuWiki