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.
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()); }
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) ; } ... |