Booleans


The next small portion of the student information system that you need to build is related to billing students for a semester. For now, the amount that students are billed is based upon three things: whether or not they are in-state students, whether or not they are full-time students, and how many credit hours the students are taking. In order to support billing, you will have to update the Student class to accommodate this information.


Students are either full-time or they are part-time. Put another way, students are either full-time or they are not full-time. Any time you need to represent something in Java that can be only in one of two stateson or offyou can use a variable of the type boolean. For a boolean variable, there are two possible boolean values, represented by the literals true (on) and false (off). The type boolean is a primitive type, like int; you cannot send messages to boolean values or variables.

Create a test method in the StudentTest class named testFullTime. It should instantiate a Student, then test that the student is not full time. Full-time students must have at least twelve credit hours; a newly created student has no credit hours.

 public void testFullTime() {    Student student = new Student("a");    assertFalse(student.isFullTime()); } 

The assertFalse method is another method StudentTest inherits from junit.framework.TestCase. It takes a single boolean expression as an argument. If the expression represents the value false, then the test passes; otherwise, the test fails. In testFullTime, the test passes if the student is not full-time; that is, if isFullTime returns false.

Add a method named isFullTime to the Student class:

 boolean isFullTime() {    return true; } 

The return type of the method is boolean. By having this method return TRue, you should expect that the test failsthe test asserts that isFullTime should return false. Observe the test fail; modify the method to return false; observe the test pass.

The full-time/part-time status of a student is determined by how many credits worth of courses that the student takes. To be considered full-time, a student must have at least a dozen credits. How does a student get credits? By enrolling in a course session.


The requirement now is that when a student is enrolled in a course session, the student's number of credits must be bumped up. Simplest things first: Students need to be able to track credits, and a newly created student has no credits.


 public void testCredits() {    Student student = new Student("a");    assertEquals(0, student.getCredits());    student.addCredits(3);    assertEquals(3, student.getCredits());    student.addCredits(4);    assertEquals(7, student.getCredits()); } 

In Student:

 package sis.studentinfo; public class Student {    private String name;    private int credits;    public Student(String name) {       this.name = name;       credits = 0;    }    public String getName() {       return name;    }    boolean isFullTime() {       return false;    }    int getCredits() {    return credits;    }    void addCredits(int credits) {    this.credits += credits;    } } 

The Student constructor initializes the value of the credits field to 0, to meet the requirement that newly created students have no credits. As you learned in Lesson 2, you could have chosen to use field initialization, or to have not bothered, since Java initializes int variables to 0 by default.

Up to this point, the student should still be considered part-time. Since the number of credits is directly linked to the student's status, perhaps you should combine the test methods (but this is a debatable choice). Instead of having two test methods, testCredits and testFullTime, combine them into a single method named testStudentStatus.

 public void testStudentStatus() {    Student student = new Student("a");    assertEquals(0, student.getCredits());    assertFalse(student.isFullTime());    student.addCredits(3);    assertEquals(3, student.getCredits());    assertFalse(student.isFullTime());    student.addCredits(4);    assertEquals(7, student.getCredits());    assertFalse(student.isFullTime()); } 

Assertion Failure Messages

You can code any JUnit assertion to provide a specialized error message. JUnit displays this message if the assertion fails. While the test itself should be coded well enough to help another developer understand the implication of an assertion failure, adding a message can help expedite understanding. Here's an example:

 assertTrue(   "not enough credits for FT status",   student.isFullTime()); 

In the case of assertEquals, often the default message is sufficient. In any case, try reading the message generated and see if it imparts enough information to another developer.


This test should pass. Now modify the test to enroll the student in a back-breaking five-credit course in order to put them at twelve credits. You can use the assertTrue method to test that the student is now full-time. A test passes if the parameter to assertTrue is true, otherwise the test fails.

 public void testStudentStatus() {    Student student = new Student("a");    assertEquals(0, student.getCredits());    assertFalse(student.isFullTime());    student.addCredits(3);    assertEquals(3, student.getCredits());    assertFalse(student.isFullTime());    student.addCredits(4);    assertEquals(7, student.getCredits());    assertFalse(student.isFullTime());    student.addCredits(5);    assertEquals(12, student.getCredits());    assertTrue(student.isFullTime()); } 

The test fails. To make it pass, you must modify the isFullTime method to return TRue if the number of credits is 12 or more. This requires you to write a conditional. A conditional in Java is an expression that returns a boolean value. Change the method isFullTime in Student to include an appropriate expression:

 boolean isFullTime() {    return credits >= 12; } 

You can read this code as "return true if the number of credits is greater than or equal to 12, otherwise return false."

Refactor isFullTime to introduce a Student class constant to explain what the number 12 means.

 static final int CREDITS_REQUIRED_FOR_FULL_TIME = 12; ... boolean isFullTime() {    return credits >= CREDITS_REQUIRED_FOR_FULL_TIME; } 

Now that students support adding credits, you can modify CourseSession to ensure that credits are added to Student objects as they are enrolled. Start with the test:

 public class CourseSessionTest extends TestCase {    // ...    private static final int CREDITS = 3;    public void setUp() {       startDate = createDate(2003, 1, 6);       session = createCourseSession();    }    // ...    public void testEnrollStudents() {       Student student1 = new Student("Cain DiVoe");       session.enroll(student1);       assertEquals(CREDITS, student1.getCredits());       assertEquals(1, session.getNumberOfStudents());       assertEquals(student1, session.get(0));      Student student2 = new Student("Coralee DeVaughn");       session.enroll(student2);       assertEquals(CREDITS, student2.getCredits());       assertEquals(2, session.getNumberOfStudents());       assertEquals(student1, session.get(0));       assertEquals(student2, session.get(1));    }    // ...     private CourseSession createCourseSession() {        CourseSession session =           CourseSession.create("ENGL", "101", startDate);        session.setNumberOfCredits(CourseSessionTest.CREDITS);        return session;    } } 

A few quick changes to CourseSession make this failing test pass:

 public class CourseSession {       ...      private int numberOfCredits;     void setNumberOfCredits(int numberOfCredits) {    this.numberOfCredits = numberOfCredits;    }    public void enroll(Student student) {       student.addCredits(numberOfCredits);       students.add(student) ;    }    ... 



Agile Java. Crafting Code with Test-Driven Development
Agile Javaв„ў: Crafting Code with Test-Driven Development
ISBN: 0131482394
EAN: 2147483647
Year: 2003
Pages: 391
Authors: Jeff Langr

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net