/* Craig Persiko
CourseTest.java
Defines and tests a class called Course which
holds information about a college course.
*/
class Course
{
// declare instance variables:
private String dept, number, name;
// constructor definitions:
public Course()
{
// Empty constructor just to allow Course to be
// constructed with null strings for its
// instance variables.
}
// constructor to set values for instance variables
public Course(String d, String num, String name)
{
dept = d;
setNumber(num); // mutator method call, to check length
this.name = name;
}
// other method definitions:
public void setDept(String d)
{
dept = d;
}
// mutator for Course Number.
// Enforces rule that it must be 5 characters or less.
public void setNumber(String n)
{
if(n.length() <= 5)
number = n;
else // too long: store first 5 chars only.
number = n.substring(0, 5);
}
public void setName(String n)
{
name = n;
}
public String getDept()
{
return dept;
}
public String getNumber()
{
return number;
}
public String getName()
{
return name;
}
public void printCourse()
{
System.out.print(dept + number);
System.out.println(": " + name);
}
} // end of class Course definition
class CourseTest
{
public static void main(String[] args)
{
String desc;
Course cp110b = new Course();
Course cp111b = new Course("CS", "111B", "Programming Fundamentals I, Java");
cp110b.setDept("CS");
cp110b.setNumber("110B with too long a name");
cp110b.setName("Programming Fundamentals I, C++");
desc = cp111b.getDept() + cp111b.getNumber() + cp111b.getName();
System.out.println(desc);
cp110b.printCourse();
}
}
/* Output:
$ javac CourseTest.java
$ ls Course*
Course.class CourseTest.class CourseTest.java
$ java CourseTest
CS111BProgramming Fundamentals I, Java
CS110B : Programming Fundamentals I, C++
$
*/
syntax highlighted by Code2HTML, v. 0.9