Assignment 1

 

CS 3500 - Summer I 2014 - Assignment #1

Posted: Monday, May 5, 2014

Due: Wednesday, May 7, 2014 at 11:59 pm


The purposes of this assignment are:

* To review basic Java programming concepts

* To review static methods in Java

* To review the Java API (application programming interface)

* To acquaint you with the development system you choose to use

* To learn the required process for submitting homework in this course

* To begin our study of abstraction-based software design with an

   algebraic specification for an abstract data type of immutable sets


You will complete an implementation in Java of the ClassRoll ADT that is specified below. Collaboration between students is forbidden on this assignment. You are responsible for keeping your code hidden from all other students. You will submit this assignment within Web-CAT.


The assignment shall be implemented in Java and will be tested using Sun's Java 2 Runtime Environment, Standard Edition, version 1.6.0 on the Linux machines in the labs. Your assignment must run in the designated environment without any changes.


Part of your grade will depend on the quality and correctness of your code, part will depend on the readability of your code (comments and indentation), and part will depend on how well you follow the procedure above for submitting your work. Assignments submitted between 12:00 am and 11:59 pm the day after the due date will receive a 20 percentage point penalty on the assignment.


This assignment is worth 100 points: 35 points for automated code style, 35 points for automated testing, and 30 points from grader.


--------------------------------------------------------------------------------


Your assignment is to write the code for ClassRoll that implements the specification below. You are expected to use the recipe for translating an algebraic specification into Java.


--------------------------------------------------------------------------------


Specification of the ClassRoll ADT.


ClassRoll is an immutable abstract data type that represents the class roll for a course. ClassRoll can be viewed as a set of Students. (Note: You can download Student.java from the course directory /course/cs3500su14/Assignments/A01. You do not have to submit this file to Web-CAT.)


The code for this implementation shall be in the default package and shall define a public class named ClassRoll. You will follow Java convention of defining every class and interface in a file that bears its name (and, of course, with the .java suffix). The exception to this convention is nested classes. The operations of the ClassRoll class shall be provided by the following public methods of the ClassRoll class:


Signature:


  Public static methods:


    emptyRoll   :                               ->  ClassRoll

    enroll      : ClassRoll x Student           ->  ClassRoll

    numStudents : ClassRoll                     ->  int

    noStudents  : ClassRoll                     ->  boolean

    inClass     : ClassRoll x Student           ->  boolean

    isSubset    : ClassRoll x ClassRoll         ->  boolean

    drop        : ClassRoll x Student           ->  ClassRoll

    combine     : ClassRoll x ClassRoll         ->  ClassRoll

    inCommon    : ClassRoll x ClassRoll         ->  ClassRoll

    swap        : ClassRoll x Student x Student ->  ClassRoll


  Public dynamic methods (for which the receiver is an ClassRoll):

    toString    :                               ->  String

    equals      : Object                        ->  boolean

    hashCode    :                               ->  int


Restrictions:


Null arguments may not be passed to any of the above methods except for equals(Object).


Algebraic specification:


    ClassRoll.numStudents(ClassRoll.emptyRoll())  =  0

    ClassRoll.numStudents(ClassRoll.enroll(cr, s0))

        =  ClassRoll.numStudents(cr)       if ClassRoll.inClass(cr, s0)

    ClassRoll.numStudents(ClassRoll.enroll(cr, s0))

        =  1 + ClassRoll.numStudents(cr)   if !(ClassRoll.inClass(cr, s0))


    ClassRoll.noStudents(ClassRoll.emptyRoll())  =  true

    ClassRoll.noStudents(ClassRoll.enroll(cr, s0))  =  false


    ClassRoll.inClass(ClassRoll.emptyRoll(), s)  =  false

    ClassRoll.inClass(ClassRoll.enroll(cr, s0), s)

        =  true                            if s.equals(s0)

    ClassRoll.inClass(ClassRoll.enroll(cr, s0), s)

        =  ClassRoll.inClass(cr, s)        if !(s.equals(s0))


    ClassRoll.isSubset(ClassRoll.emptyRoll(), cr2)  =  true

    ClassRoll.isSubset(ClassRoll.enroll(cr, s0), cr2)

        =  ClassRoll.isSubset(cr, cr2)     if ClassRoll.inClass(cr2, s0)

    ClassRoll.isSubset(ClassRoll.enroll(cr, s0), cr2)

        =  false                           if !(ClassRoll.inClass(cr2, s0))


    ClassRoll.drop(ClassRoll.emptyRoll(), s)  =  ClassRoll.emptyRoll()

    ClassRoll.drop(ClassRoll.enroll(cr, s0), s)

        =  ClassRoll.drop(cr, s)           if s.equals(s0)

    ClassRoll.drop(ClassRoll.enroll(cr, s0), s)

        =  ClassRoll.enroll(ClassRoll.drop(cr, s), s0)

                                           if !(s.equals(s0))


    ClassRoll.combine(ClassRoll.emptyRoll(), cr2)  =  cr2

    ClassRoll.combine(ClassRoll.enroll(cr, s0), cr2)

        =  ClassRoll.combine(cr, cr2)       if ClassRoll.inClass(cr2, s0)

    ClassRoll.combine(ClassRoll.enroll(cr, s0), cr2)

        =  ClassRoll.enroll(ClassRoll.combine(cr, cr2), s0)

                                           if !(ClassRoll.inClass(cr2, s0))


    ClassRoll.inCommon(ClassRoll.emptyRoll(), cr2) 

        =  ClassRoll.emptyRoll()

    ClassRoll.inCommon(ClassRoll.enroll(cr, s0), cr2)

        =  ClassRoll.enroll(ClassRoll.inCommon(cr, cr2), s0)

                                           if ClassRoll.inClass(cr2, s0)

    ClassRoll.inCommon(ClassRoll.enroll(cr, s0), cr2)

        =  ClassRoll.inCommon(cr, cr2)

                                           if !(ClassRoll.inClass(cr2, s0))

   

    ClassRoll.swap(ClassRoll.emptyRoll(), s1, s2) = ClassRoll.emptyRoll()

    ClassRoll.swap(ClassRoll.enroll(cr, s0), s1, s2)

        = ClassRoll.enroll(ClassRoll.swap(cr, s1, s2), s2)

                                           if s1.equals(s0)

    ClassRoll.swap(ClassRoll.enroll(cr, s0), s1, s2)

        = ClassRoll.enroll(ClassRoll.swap(cr, s1, s2), s0)

                                           if !(s1.equals(s0))


    cr.toString() = "There are " + ClassRoll.numStudents(cr)

                    + " students in this course."


Values of the ClassRoll ADT shall also implement the public dynamic methods equals(Object) and hashCode() such that


    If cr1 is a value of the ClassRoll ADT, then


       cr1.equals(null) returns false.


    If cr1 is a value of the ClassRoll ADT, but x is not, then


        cr1.equals(x) returns false.


    If cr1 and cr2 are values of the ClassRoll ADT, then

    cr1.equals(cr2) if and only if


       for every Student s


           ClassRoll.inClass(cr1, s) if and only if ClassRoll.inClass(cr2, s)


    If cr1 and cr2 are values of the ClassRoll ADT, and


        cr1.equals(cr2)


    then cr1.hashCode() == cr2.hashCode().


--------------------------------------------------------------------------------


Warning:


Do not wait until the last minute to submit your work to Web-CAT. Do it early and often - there is no limit on the number of submissions and you can retrieve your earlier submissions as if they were saved in a version control system. It takes a bit of time to do the automatic grading and the system can be slow when too many students are submitting simultaneously. Also, use the feedback Web-CAT to improve your code.