Java assignment
lab-classes/LabClass.class
public synchronized class LabClass { private String instructor; private String room; private String timeAndDay; private java.util.ArrayList students; private int capacity; public void LabClass(int); public void enrollStudent(Student); public int numberOfStudents(); public void setRoom(String); public void setTime(String); public void setInstructor(String); public void printList(); }
lab-classes/LabClass.ctxt
#BlueJ class context comment0.params=maxNumberOfStudents comment0.target=LabClass(int) comment0.text=\n\ Create\ a\ LabClass\ with\ a\ maximum\ number\ of\ enrolments.\ All\ other\ details\n\ are\ set\ to\ default\ values.\n comment1.params=newStudent comment1.target=void\ enrollStudent(Student) comment1.text=\n\ Add\ a\ student\ to\ this\ LabClass.\n comment2.params= comment2.target=int\ numberOfStudents() comment2.text=\n\ Return\ the\ number\ of\ students\ currently\ enrolled\ in\ this\ LabClass.\n comment3.params=roomNumber comment3.target=void\ setRoom(java.lang.String) comment3.text=\n\ Set\ the\ room\ number\ for\ this\ LabClass.\n comment4.params=timeAndDayString comment4.target=void\ setTime(java.lang.String) comment4.text=\n\ Set\ the\ time\ for\ this\ LabClass.\ The\ parameter\ should\ define\ the\ day\n\ and\ the\ time\ of\ day,\ such\ as\ "Friday,\ 10am".\n comment5.params=instructorName comment5.target=void\ setInstructor(java.lang.String) comment5.text=\n\ Set\ the\ name\ of\ the\ instructor\ for\ this\ LabClass.\n comment6.params= comment6.target=void\ printList() comment6.text=\n\ Print\ out\ a\ class\ list\ with\ other\ LabClass\ details\ to\ the\ standard\n\ terminal.\n numComments=7
lab-classes/LabClass.java
lab-classes/LabClass.java
import
java
.
util
.
*
;
/**
* The LabClass class represents an enrolment list for one lab class. It stores
* the time, room and participants of the lab, as well as the instructor's name.
*
*
@author
Michael Kölling and David Barnes
*
@version
2011.07.31
*/
public
class
LabClass
{
private
String
instructor
;
private
String
room
;
private
String
timeAndDay
;
private
ArrayList
<
Student
>
students
;
private
int
capacity
;
/**
* Create a LabClass with a maximum number of enrolments. All other details
* are set to default values.
*/
public
LabClass
(
int
maxNumberOfStudents
)
{
instructor
=
"unknown"
;
room
=
"unknown"
;
timeAndDay
=
"unknown"
;
students
=
new
ArrayList
<
Student
>
();
capacity
=
maxNumberOfStudents
;
}
/**
* Add a student to this LabClass.
*/
public
void
enrollStudent
(
Student
newStudent
)
{
if
(
students
.
size
()
==
capacity
)
{
System
.
out
.
println
(
"The class is full, you cannot enrol."
);
}
else
{
students
.
add
(
newStudent
);
}
}
/**
* Return the number of students currently enrolled in this LabClass.
*/
public
int
numberOfStudents
()
{
return
students
.
size
();
}
/**
* Set the room number for this LabClass.
*/
public
void
setRoom
(
String
roomNumber
)
{
room
=
roomNumber
;
}
/**
* Set the time for this LabClass. The parameter should define the day
* and the time of day, such as "Friday, 10am".
*/
public
void
setTime
(
String
timeAndDayString
)
{
timeAndDay
=
timeAndDayString
;
}
/**
* Set the name of the instructor for this LabClass.
*/
public
void
setInstructor
(
String
instructorName
)
{
instructor
=
instructorName
;
}
/**
* Print out a class list with other LabClass details to the standard
* terminal.
*/
public
void
printList
()
{
System
.
out
.
println
(
"Lab class "
+
timeAndDay
);
System
.
out
.
println
(
"Instructor: "
+
instructor
+
" Room: "
+
room
);
System
.
out
.
println
(
"Class list:"
);
for
(
Student
student
:
students
)
{
student
.
print
();
}
System
.
out
.
println
(
"Number of students: "
+
numberOfStudents
());
}
}
lab-classes/package.bluej
#BlueJ package file dependency1.from=LabClass dependency1.to=Student dependency1.type=UsesDependency package.editor.height=427 package.editor.width=673 package.editor.x=163 package.editor.y=63 package.numDependencies=1 package.numTargets=2 package.showExtends=true package.showUses=true project.charset=UTF-8 readme.editor.height=607 readme.editor.width=815 readme.editor.x=8 readme.editor.y=45 target1.editor.height=784 target1.editor.width=882 target1.editor.x=50 target1.editor.y=48 target1.height=60 target1.name=Student target1.naviview.expanded=true target1.showInterface=false target1.type=ClassTarget target1.width=100 target1.x=330 target1.y=210 target2.editor.height=730 target2.editor.width=826 target2.editor.x=50 target2.editor.y=60 target2.height=60 target2.name=LabClass target2.naviview.expanded=true target2.showInterface=false target2.type=ClassTarget target2.width=100 target2.x=190 target2.y=110
lab-classes/README.TXT
Project: lab-classes Authors: David J. Barnes and Michael Kölling This project is part of the material for the book Objects First with Java - A Practical Introduction using BlueJ Fifth edition David J. Barnes and Michael Kölling Pearson Education, 2012 It is discussed in chapter 1. The purpose of this project is to illustrate object interaction and to examine some simple code. See chapters one and three of the book. To use this project, create some Student objects, then create a LabClass object. Add the students to the class using the "enrolStudent" method. Print a class list.
lab-classes/Student.class
public synchronized class Student { private String name; private String id; private int credits; public void Student(String, String); public String getName(); public void changeName(String); public String getStudentID(); public void addCredits(int); public int getCredits(); public String getLoginName(); public void print(); }
lab-classes/Student.ctxt
#BlueJ class context comment0.params=fullName\ studentID comment0.target=Student(java.lang.String,\ java.lang.String) comment0.text=\n\ Create\ a\ new\ student\ with\ a\ given\ name\ and\ ID\ number.\n comment1.params= comment1.target=java.lang.String\ getName() comment1.text=\n\ Return\ the\ full\ name\ of\ this\ student.\n comment2.params=replacementName comment2.target=void\ changeName(java.lang.String) comment2.text=\n\ Set\ a\ new\ name\ for\ this\ student.\n comment3.params= comment3.target=java.lang.String\ getStudentID() comment3.text=\n\ Return\ the\ student\ ID\ of\ this\ student.\n comment4.params=additionalPoints comment4.target=void\ addCredits(int) comment4.text=\n\ Add\ some\ credit\ points\ to\ the\ student's\ accumulated\ credits.\n comment5.params= comment5.target=int\ getCredits() comment5.text=\n\ Return\ the\ number\ of\ credit\ points\ this\ student\ has\ accumulated.\n comment6.params= comment6.target=java.lang.String\ getLoginName() comment6.text=\n\ Return\ the\ login\ name\ of\ this\ student.\ The\ login\ name\ is\ a\ combination\n\ of\ the\ first\ four\ characters\ of\ the\ student's\ name\ and\ the\ first\ three\n\ characters\ of\ the\ student's\ ID\ number.\n comment7.params= comment7.target=void\ print() comment7.text=\n\ Print\ the\ student's\ name\ and\ ID\ number\ to\ the\ output\ terminal.\n numComments=8
lab-classes/Student.java
lab-classes/Student.java
/**
* The Student class represents a student in a student administration system.
* It holds the student details relevant in our context.
*
*
@author
Michael Kölling and David Barnes
*
@version
2011.07.31
*/
public
class
Student
{
// the student's full name
private
String
name
;
// the student ID
private
String
id
;
// the amount of credits for study taken so far
private
int
credits
;
/**
* Create a new student with a given name and ID number.
*/
public
Student
(
String
fullName
,
String
studentID
)
{
name
=
fullName
;
id
=
studentID
;
credits
=
0
;
}
/**
* Return the full name of this student.
*/
public
String
getName
()
{
return
name
;
}
/**
* Set a new name for this student.
*/
public
void
changeName
(
String
replacementName
)
{
name
=
replacementName
;
}
/**
* Return the student ID of this student.
*/
public
String
getStudentID
()
{
return
id
;
}
/**
* Add some credit points to the student's accumulated credits.
*/
public
void
addCredits
(
int
additionalPoints
)
{
credits
+=
additionalPoints
;
}
/**
* Return the number of credit points this student has accumulated.
*/
public
int
getCredits
()
{
return
credits
;
}
/**
* Return the login name of this student. The login name is a combination
* of the first four characters of the student's name and the first three
* characters of the student's ID number.
*/
public
String
getLoginName
()
{
return
name
.
substring
(
0
,
4
)
+
id
.
substring
(
0
,
3
);
}
/**
* Print the student's name and ID number to the output terminal.
*/
public
void
print
()
{
System
.
out
.
println
(
name
+
", student ID: "
+
id
+
", credits: "
+
credits
);
}
}