CS256: Lab 7


Due: See Moodle for the due date and time

Goals:

1) Learn to create and manipulate objects

2) Learn to use ‘dot notation’ and the indexOf() methods supported by String objects.

3) Practice local variables and if statements.

 

Requirements:

0)    Download the Student class and save it in your lab7 folder.

 

Modify the Student class as follows:

1) Provide new methods to check the data stored in the name and credits fields.    You must use the following method signatures for these methods:

 

    /**

     * Return true if this student's name contains a comma

     * Otherwise return false.

     */

    public boolean isLegalName()

 

    /**

     * Return true if this student has credits that are >= 0.

     * Otherwise, return false.

     */

    public boolean hasLegalCredits()

 

2) Provide a new method to return and clear the value stored in the credits field.    You must use the following method signature for this method:

 

    /**

     * Return the value stored in the credits field and

     * set the credits field to 0.

     */

    public int getAndClearCredits()

 

3) Modify the  print() method to print an additional line underneath the student’s name and id.  If the student has 90 or more credits, print “   upper class”.  Otherwise, print “   lower class”.  Here is an example of the output of the print() method called on a Student object whose name is “McGee, JimBob” and whose ID is “12345”, note the indentation on the upper/lower class line:

McGee, JimBob (12345)

   upper class

 

4) Turn in your assignment by running the autograder

Approach:

What will you do to test your code to ensure that you have correctly implemented the requirements?

·         Create a Student object with name “JimBob McGee”

·         Call your isLegalName() method.  It should display false in the method result window.

·         Call the changeName() method and change the name to “McGee, JimBob”.

·         Call your isLegalName() method again.  It should display true in the method result window.

·         Call hasLegalCredits(), when the credits fields holds each of the following values: -1, 0, 89, 90, 91, 110.  Why is it important to test right around 90?  How do you get -1 into the credits field?

·         Create a new Student object and call hasLegalCredits() before calling addCredits().

·         Put in 5 credits then call getAndClearCredits().  Ensure that 5 appears in the method result window.  Inspect the Student object and ensure that 0 appears in the credits field.

·         Call your print() method on a student that has 89, 90, and 91 credits.  The run I/O window should display lower, upper, and upper class, respectively, for each of these three calls.

2% Extra Credit:

Modify the print() method to show the student’s initials underneath their upper or lower class status.  The example print() output shown above would read as follows after doing the extra credit problem:

McGee, JimBob (12345)

   upper class

   JM

You may assume that the name is always given in the form “last, first” with a comma followed by a space separating the last character of the last name from the first character of the first name.

Note: A person’s initials are the first character of their first name followed by the first character of their last name.

Hint: Use both charAt() and indexOf()

 

Note:  The autograder will give a score of up to 100 (102 if you do the extra credit) for the ‘functional’ portion of the lab.  From this score, I will remove points for: missing name at the top of the code, poor indentation, poorly structured code, missing or incorrect synthesis question answers.