first commit
This commit is contained in:
11
Semester 1/Programming 1/Java/examples/projects/chapter04/club/Club.ctxt
Executable file
11
Semester 1/Programming 1/Java/examples/projects/chapter04/club/Club.ctxt
Executable file
@@ -0,0 +1,11 @@
|
||||
#BlueJ class context
|
||||
comment0.params=
|
||||
comment0.target=Club()
|
||||
comment0.text=\n\ Constructor\ for\ objects\ of\ class\ Club\n
|
||||
comment1.params=member
|
||||
comment1.target=void\ join(Membership)
|
||||
comment1.text=\n\ Add\ a\ new\ member\ to\ the\ club's\ list\ of\ members.\n\ @param\ member\ The\ member\ object\ to\ be\ added.\n
|
||||
comment2.params=
|
||||
comment2.target=int\ numberOfMembers()
|
||||
comment2.text=\n\ @return\ The\ number\ of\ members\ (Membership\ objects)\ in\n\ \ \ \ \ \ \ \ \ the\ club.\n
|
||||
numComments=3
|
36
Semester 1/Programming 1/Java/examples/projects/chapter04/club/Club.java
Executable file
36
Semester 1/Programming 1/Java/examples/projects/chapter04/club/Club.java
Executable file
@@ -0,0 +1,36 @@
|
||||
/**
|
||||
* Store details of club memberships.
|
||||
*
|
||||
* @author (your name)
|
||||
* @version (a version number or a date)
|
||||
*/
|
||||
public class Club
|
||||
{
|
||||
// Define any necessary fields here ...
|
||||
|
||||
/**
|
||||
* Constructor for objects of class Club
|
||||
*/
|
||||
public Club()
|
||||
{
|
||||
// Initialise any fields here ...
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new member to the club's list of members.
|
||||
* @param member The member object to be added.
|
||||
*/
|
||||
public void join(Membership member)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The number of members (Membership objects) in
|
||||
* the club.
|
||||
*/
|
||||
public int numberOfMembers()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
@@ -0,0 +1,8 @@
|
||||
#BlueJ class context
|
||||
comment0.params=
|
||||
comment0.target=ClubDemo()
|
||||
comment0.text=\n\ Constructor\ for\ objects\ of\ class\ ClubDemo\n
|
||||
comment1.params=
|
||||
comment1.target=void\ demo()
|
||||
comment1.text=\n\ Add\ some\ members\ to\ the\ club,\ and\ then\n\ show\ how\ many\ there\ are.\n\ Further\ example\ calls\ could\ be\ added\ if\ more\ functionality\n\ is\ added\ to\ the\ Club\ class.\n
|
||||
numComments=2
|
36
Semester 1/Programming 1/Java/examples/projects/chapter04/club/ClubDemo.java
Executable file
36
Semester 1/Programming 1/Java/examples/projects/chapter04/club/ClubDemo.java
Executable file
@@ -0,0 +1,36 @@
|
||||
|
||||
/**
|
||||
* Provide a demonstration of the Club and Membership
|
||||
* classes.
|
||||
*
|
||||
* @author (your name)
|
||||
* @version (a version number or a date)
|
||||
*/
|
||||
public class ClubDemo
|
||||
{
|
||||
// instance variables - replace the example below with your own
|
||||
private Club club;
|
||||
|
||||
/**
|
||||
* Constructor for objects of class ClubDemo
|
||||
*/
|
||||
public ClubDemo()
|
||||
{
|
||||
club = new Club();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add some members to the club, and then
|
||||
* show how many there are.
|
||||
* Further example calls could be added if more functionality
|
||||
* is added to the Club class.
|
||||
*/
|
||||
public void demo()
|
||||
{
|
||||
club.join(new Membership("David", 2, 2004));
|
||||
club.join(new Membership("Michael", 1, 2004));
|
||||
System.out.println("The club has " +
|
||||
club.numberOfMembers() +
|
||||
" members.");
|
||||
}
|
||||
}
|
@@ -0,0 +1,17 @@
|
||||
#BlueJ class context
|
||||
comment0.params=name\ month\ year
|
||||
comment0.target=Membership(java.lang.String,\ int,\ int)
|
||||
comment0.text=\n\ Constructor\ for\ objects\ of\ class\ Membership.\n\ @param\ name\ The\ name\ of\ the\ member.\n\ @param\ month\ The\ month\ in\ which\ they\ joined.\ (1\ ...\ 12)\n\ @param\ year\ The\ year\ in\ which\ they\ joined.\n
|
||||
comment1.params=
|
||||
comment1.target=java.lang.String\ getName()
|
||||
comment1.text=\n\ @return\ The\ member's\ name.\n
|
||||
comment2.params=
|
||||
comment2.target=int\ getMonth()
|
||||
comment2.text=\n\ @return\ The\ month\ in\ which\ the\ member\ joined.\n\ \ \ \ \ \ \ \ \ A\ value\ in\ the\ range\ 1\ ...\ 12\n
|
||||
comment3.params=
|
||||
comment3.target=int\ getYear()
|
||||
comment3.text=\n\ @return\ The\ year\ in\ which\ the\ member\ joined.\n
|
||||
comment4.params=
|
||||
comment4.target=java.lang.String\ toString()
|
||||
comment4.text=\n\ @return\ A\ string\ representation\ of\ this\ membership.\n
|
||||
numComments=5
|
@@ -0,0 +1,68 @@
|
||||
/**
|
||||
* Store details of a club membership.
|
||||
*
|
||||
* @author David J. Barnes and Michael Kölling
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public class Membership
|
||||
{
|
||||
// The name of the member.
|
||||
private String name;
|
||||
// The month in which the membership was taken out.
|
||||
private int month;
|
||||
// The year in which the membership was taken out.
|
||||
private int year;
|
||||
|
||||
/**
|
||||
* Constructor for objects of class Membership.
|
||||
* @param name The name of the member.
|
||||
* @param month The month in which they joined. (1 ... 12)
|
||||
* @param year The year in which they joined.
|
||||
*/
|
||||
public Membership(String name, int month, int year)
|
||||
throws IllegalArgumentException
|
||||
{
|
||||
if(month < 1 || month > 12) {
|
||||
throw new IllegalArgumentException(
|
||||
"Month " + month + " out of range. Must be in the range 1 ... 12");
|
||||
}
|
||||
this.name = name;
|
||||
this.month = month;
|
||||
this.year = year;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The member's name.
|
||||
*/
|
||||
public String getName()
|
||||
{
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The month in which the member joined.
|
||||
* A value in the range 1 ... 12
|
||||
*/
|
||||
public int getMonth()
|
||||
{
|
||||
return month;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The year in which the member joined.
|
||||
*/
|
||||
public int getYear()
|
||||
{
|
||||
return year;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return A string representation of this membership.
|
||||
*/
|
||||
public String toString()
|
||||
{
|
||||
return "Name: " + name +
|
||||
" joined in month " +
|
||||
month + " of " + year;
|
||||
}
|
||||
}
|
152
Semester 1/Programming 1/Java/examples/projects/chapter04/club/README.TXT
Executable file
152
Semester 1/Programming 1/Java/examples/projects/chapter04/club/README.TXT
Executable file
@@ -0,0 +1,152 @@
|
||||
BlueJ project "club".
|
||||
|
||||
Authors: David J. Barnes and Michael Kölling
|
||||
|
||||
This project is supplementary material for the book
|
||||
|
||||
Objects First with Java - A Practical Introduction using BlueJ
|
||||
Sixth edition
|
||||
David J. Barnes and Michael Kölling
|
||||
Pearson Education, 2016
|
||||
|
||||
Purpose of the project
|
||||
=======================
|
||||
The project is intended to extend understanding of object
|
||||
collections. The exercises could be tackled after completing
|
||||
Section 4.6 of Chapter 4.
|
||||
|
||||
The exercise
|
||||
============
|
||||
Membership of a club is represented by an instance of the
|
||||
Membership class. A complete version of the Membership class
|
||||
is already provided for you in the club project, and it should
|
||||
not need any modification. Each Membership object contains
|
||||
details of a person's name, and the month and year in which
|
||||
they joined the club. All membership details are filled out
|
||||
when a Membership object is created.
|
||||
|
||||
Your task is to complete the Club class, an outline of which
|
||||
has been also been provided in the club project.
|
||||
|
||||
The Club class is intended to store Membership objects in a
|
||||
flexible-size collection.
|
||||
|
||||
A new Membership object is added to a Club object's collection
|
||||
via the Club object's join method, which has the following
|
||||
signature and description:
|
||||
|
||||
/**
|
||||
* Add a new member to the club's collection of members.
|
||||
* @param member The member object to be added.
|
||||
*/
|
||||
public void join(Membership member)
|
||||
|
||||
You may assume that the Membership object passed as a
|
||||
parameter has already been created and is fully initialized.
|
||||
|
||||
The Club class also defines a method to return how many
|
||||
members are in the club. It has the following signature and
|
||||
description:
|
||||
|
||||
/**
|
||||
* @return The number of members (Membership objects) in
|
||||
* the club.
|
||||
*/
|
||||
public int numberOfMembers()
|
||||
|
||||
The number of members corresponds to the number of Membership
|
||||
objects currently stored in the flexible-size collection.
|
||||
|
||||
Complete the definition of the Club class. This should
|
||||
include:
|
||||
|
||||
+ A field to refer to a flexible-size collection
|
||||
object.
|
||||
|
||||
+ A no-arg constructor that sets up an appropriate
|
||||
flexible-size collection object to store Membership
|
||||
objects.
|
||||
|
||||
+ A full definition of the join method.
|
||||
|
||||
+ A full definition of the numberOfMembers method.
|
||||
|
||||
Below are some further challenge exercises for those who wish
|
||||
to experiment with additional features of collections.
|
||||
|
||||
Possible Staged Implementation
|
||||
==============================
|
||||
This task has been broken down into suggested separate stages
|
||||
to help you create the finished version in small steps. You
|
||||
are recommended to compile and run the program after each
|
||||
stage to check that the changes you have made are correct.
|
||||
|
||||
1. Define a field that will be used to refer to a
|
||||
flexible-size collection object. Use an appropriate import
|
||||
statement for this collection. In the constructor, create the
|
||||
collection object and assign it to the field.
|
||||
|
||||
2. Complete the numberOfMembers method to return the current
|
||||
size of the collection object. Of course, until you have
|
||||
completed the join method, this will always return zero, but it
|
||||
will be ready for further testing when the join method is
|
||||
done.
|
||||
|
||||
3. Complete the join method.
|
||||
|
||||
When you wish to add a new Membership object to the Club
|
||||
object from the object bench, there are two ways you can do
|
||||
this.
|
||||
|
||||
+ Either create a new Membership object on the object
|
||||
bench, call the join method on the Club object, and click
|
||||
on the Membership object to supply the parameter.
|
||||
|
||||
+ Or call the join method on the Club object and type:
|
||||
|
||||
new Membership("members name here ...", month-number-here, year-number-here)
|
||||
|
||||
into the constructor's parameter dialogue box.
|
||||
|
||||
When you have added a new member use the numberOfMembers
|
||||
method to check: a) That the join method is adding to the
|
||||
collection, and b)That the numberOfMembers method is
|
||||
working.
|
||||
|
||||
Challenge Exercises
|
||||
===================
|
||||
These challenge exercises are designed to take the club
|
||||
project further towards a complete application. They
|
||||
could be tackled after completing Section 4.9 of Chapter 4.
|
||||
|
||||
+ Define a method in the Club class with the following
|
||||
signature and description:
|
||||
|
||||
/**
|
||||
* Return how many members joined in the given month.
|
||||
* @param month The month we are interested in.
|
||||
* @return How many members joined in the given month.
|
||||
*/
|
||||
public int joinedInMonth(int month)
|
||||
|
||||
If the month parameter is outside the valid range of 1-12,
|
||||
print an error message and return zero.
|
||||
|
||||
+ Define a method in the Club class with the following
|
||||
signature and description:
|
||||
|
||||
/**
|
||||
* Remove from the club's collection all members who joined
|
||||
* in the given month, and return them stored in a separate
|
||||
* collection object.
|
||||
* @param month The month of the Membership.
|
||||
* @param year The year of the Membership.
|
||||
*/
|
||||
public ArrayList purge(int month, int year)
|
||||
|
||||
If the month parameter is outside the valid range of 1-12,
|
||||
print an error message and return a collection object with no
|
||||
objects stored in it.
|
||||
|
||||
Note: the purge method is significantly harder to write than
|
||||
any of the others.
|
64
Semester 1/Programming 1/Java/examples/projects/chapter04/club/package.bluej
Executable file
64
Semester 1/Programming 1/Java/examples/projects/chapter04/club/package.bluej
Executable file
@@ -0,0 +1,64 @@
|
||||
#BlueJ package file
|
||||
dependency1.from=ClubDemo
|
||||
dependency1.to=Club
|
||||
dependency1.type=UsesDependency
|
||||
dependency2.from=ClubDemo
|
||||
dependency2.to=Membership
|
||||
dependency2.type=UsesDependency
|
||||
dependency3.from=Club
|
||||
dependency3.to=Membership
|
||||
dependency3.type=UsesDependency
|
||||
objectbench.height=76
|
||||
objectbench.width=823
|
||||
package.editor.height=429
|
||||
package.editor.width=715
|
||||
package.editor.x=70
|
||||
package.editor.y=80
|
||||
package.numDependencies=3
|
||||
package.numTargets=3
|
||||
package.showExtends=true
|
||||
package.showUses=true
|
||||
project.charset=UTF-8
|
||||
readme.editor.height=700
|
||||
readme.editor.width=900
|
||||
readme.editor.x=69
|
||||
readme.editor.y=65
|
||||
target1.editor.height=700
|
||||
target1.editor.width=900
|
||||
target1.editor.x=101
|
||||
target1.editor.y=57
|
||||
target1.height=60
|
||||
target1.name=ClubDemo
|
||||
target1.naviview.expanded=true
|
||||
target1.showInterface=false
|
||||
target1.type=ClassTarget
|
||||
target1.typeParameters=
|
||||
target1.width=120
|
||||
target1.x=90
|
||||
target1.y=70
|
||||
target2.editor.height=700
|
||||
target2.editor.width=900
|
||||
target2.editor.x=64
|
||||
target2.editor.y=23
|
||||
target2.height=60
|
||||
target2.name=Club
|
||||
target2.naviview.expanded=true
|
||||
target2.showInterface=false
|
||||
target2.type=ClassTarget
|
||||
target2.typeParameters=
|
||||
target2.width=110
|
||||
target2.x=290
|
||||
target2.y=180
|
||||
target3.editor.height=700
|
||||
target3.editor.width=900
|
||||
target3.editor.x=64
|
||||
target3.editor.y=42
|
||||
target3.height=60
|
||||
target3.name=Membership
|
||||
target3.naviview.expanded=true
|
||||
target3.showInterface=false
|
||||
target3.type=ClassTarget
|
||||
target3.typeParameters=
|
||||
target3.width=130
|
||||
target3.x=460
|
||||
target3.y=310
|
Reference in New Issue
Block a user