first commit

This commit is contained in:
Boris
2024-01-15 20:14:10 +00:00
commit 8c81ee28b7
3106 changed files with 474415 additions and 0 deletions

View File

@@ -0,0 +1,26 @@
#BlueJ class context
comment0.params=
comment0.target=Clock()
comment0.text=\n\ Constructor\ for\ objects\ of\ class\ Clock\n
comment1.params=
comment1.target=void\ start()
comment1.text=\n\ \n
comment2.params=
comment2.target=void\ stop()
comment2.text=\n\ \n
comment3.params=
comment3.target=void\ step()
comment3.text=\n\ \n
comment4.params=
comment4.target=void\ showAbout()
comment4.text=\n\ 'About'\ function\:\ show\ the\ 'about'\ box.\n
comment5.params=
comment5.target=void\ quit()
comment5.text=\n\ Quit\ function\:\ quit\ the\ application.\n
comment6.params=
comment6.target=void\ makeFrame()
comment6.text=\n\ Create\ the\ Swing\ frame\ and\ its\ content.\n
comment7.params=frame
comment7.target=void\ makeMenuBar(javax.swing.JFrame)
comment7.text=\n\ Create\ the\ main\ frame's\ menu\ bar.\n\ \n\ @param\ frame\ \ \ The\ frame\ that\ the\ menu\ bar\ should\ be\ added\ to.\n
numComments=8

View File

@@ -0,0 +1,182 @@
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
/**
* A very simple GUI (graphical user interface) for the clock display.
* In this implementation, time runs at about 3 minutes per second, so that
* testing the display is a little quicker.
*
* @author Michael Kölling and David J. Barnes
* @version 2016.02.29
*/
public class Clock
{
private JFrame frame;
private JLabel label;
private ClockDisplay clock;
private boolean clockRunning = false;
private TimerThread timerThread;
/**
* Constructor for objects of class Clock
*/
public Clock()
{
makeFrame();
clock = new ClockDisplay();
}
/**
*
*/
private void start()
{
clockRunning = true;
timerThread = new TimerThread();
timerThread.start();
}
/**
*
*/
private void stop()
{
clockRunning = false;
}
/**
*
*/
private void step()
{
clock.timeTick();
label.setText(clock.getTime());
}
/**
* 'About' function: show the 'about' box.
*/
private void showAbout()
{
JOptionPane.showMessageDialog (frame,
"Clock Version 1.0\n" +
"A simple interface for the 'Objects First' clock display project",
"About Clock",
JOptionPane.INFORMATION_MESSAGE);
}
/**
* Quit function: quit the application.
*/
private void quit()
{
System.exit(0);
}
/**
* Create the Swing frame and its content.
*/
private void makeFrame()
{
frame = new JFrame("Clock");
JPanel contentPane = (JPanel)frame.getContentPane();
contentPane.setBorder(new EmptyBorder(1, 60, 1, 60));
makeMenuBar(frame);
// Specify the layout manager with nice spacing
contentPane.setLayout(new BorderLayout(12, 12));
// Create the image pane in the center
label = new JLabel("00:00", SwingConstants.CENTER);
Font displayFont = label.getFont().deriveFont(96.0f);
label.setFont(displayFont);
//imagePanel.setBorder(new EtchedBorder());
contentPane.add(label, BorderLayout.CENTER);
// Create the toolbar with the buttons
JPanel toolbar = new JPanel();
toolbar.setLayout(new GridLayout(1, 0));
JButton startButton = new JButton("Start");
startButton.addActionListener(e -> start());
toolbar.add(startButton);
JButton stopButton = new JButton("Stop");
stopButton.addActionListener(e -> stop());
toolbar.add(stopButton);
JButton stepButton = new JButton("Step");
stepButton.addActionListener(e -> step());
toolbar.add(stepButton);
// Add toolbar into panel with flow layout for spacing
JPanel flow = new JPanel();
flow.add(toolbar);
contentPane.add(flow, BorderLayout.SOUTH);
// building is done - arrange the components
frame.pack();
// place the frame at the center of the screen and show
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation(d.width/2 - frame.getWidth()/2, d.height/2 - frame.getHeight()/2);
frame.setVisible(true);
}
/**
* Create the main frame's menu bar.
*
* @param frame The frame that the menu bar should be added to.
*/
private void makeMenuBar(JFrame frame)
{
final int SHORTCUT_MASK =
Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
JMenuBar menubar = new JMenuBar();
frame.setJMenuBar(menubar);
JMenu menu;
JMenuItem item;
// create the File menu
menu = new JMenu("File");
menubar.add(menu);
item = new JMenuItem("About Clock...");
item.addActionListener(e -> showAbout());
menu.add(item);
menu.addSeparator();
item = new JMenuItem("Quit");
item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, SHORTCUT_MASK));
item.addActionListener(e -> quit());
menu.add(item);
}
class TimerThread extends Thread
{
public void run()
{
while (clockRunning) {
step();
pause();
}
}
private void pause()
{
try {
Thread.sleep(300); // pause for 300 milliseconds
}
catch (InterruptedException exc) {
}
}
}
}

View File

@@ -0,0 +1,20 @@
#BlueJ class context
comment0.params=
comment0.target=ClockDisplay()
comment0.text=\n\ Constructor\ for\ ClockDisplay\ objects.\ This\ constructor\ \n\ creates\ a\ new\ clock\ set\ at\ 00\:00.\n
comment1.params=hour\ minute
comment1.target=ClockDisplay(int,\ int)
comment1.text=\n\ Constructor\ for\ ClockDisplay\ objects.\ This\ constructor\n\ creates\ a\ new\ clock\ set\ at\ the\ time\ specified\ by\ the\ \n\ parameters.\n
comment2.params=
comment2.target=void\ timeTick()
comment2.text=\n\ This\ method\ should\ get\ called\ once\ every\ minute\ -\ it\ makes\n\ the\ clock\ display\ go\ one\ minute\ forward.\n
comment3.params=hour\ minute
comment3.target=void\ setTime(int,\ int)
comment3.text=\n\ Set\ the\ time\ of\ the\ display\ to\ the\ specified\ hour\ and\n\ minute.\n
comment4.params=
comment4.target=java.lang.String\ getTime()
comment4.text=\n\ Return\ the\ current\ time\ of\ this\ display\ in\ the\ format\ HH\:MM.\n
comment5.params=
comment5.target=void\ updateDisplay()
comment5.text=\n\ Update\ the\ internal\ string\ that\ represents\ the\ display.\n
numComments=6

View File

@@ -0,0 +1,84 @@
/**
* The ClockDisplay class implements a digital clock display for a
* European-style 24 hour clock. The clock shows hours and minutes. The
* range of the clock is 00:00 (midnight) to 23:59 (one minute before
* midnight).
*
* The clock display receives "ticks" (via the timeTick method) every minute
* and reacts by incrementing the display. This is done in the usual clock
* fashion: the hour increments when the minutes roll over to zero.
*
* @author Michael Kölling and David J. Barnes
* @version 2016.02.29
*/
public class ClockDisplay
{
private NumberDisplay hours;
private NumberDisplay minutes;
private String displayString; // simulates the actual display
/**
* Constructor for ClockDisplay objects. This constructor
* creates a new clock set at 00:00.
*/
public ClockDisplay()
{
hours = new NumberDisplay(24);
minutes = new NumberDisplay(60);
updateDisplay();
}
/**
* Constructor for ClockDisplay objects. This constructor
* creates a new clock set at the time specified by the
* parameters.
*/
public ClockDisplay(int hour, int minute)
{
hours = new NumberDisplay(24);
minutes = new NumberDisplay(60);
setTime(hour, minute);
}
/**
* This method should get called once every minute - it makes
* the clock display go one minute forward.
*/
public void timeTick()
{
minutes.increment();
if(minutes.getValue() == 0) { // it just rolled over!
hours.increment();
}
updateDisplay();
}
/**
* Set the time of the display to the specified hour and
* minute.
*/
public void setTime(int hour, int minute)
{
hours.setValue(hour);
minutes.setValue(minute);
updateDisplay();
}
/**
* Return the current time of this display in the format HH:MM.
*/
public String getTime()
{
return displayString;
}
/**
* Update the internal string that represents the display.
*/
private void updateDisplay()
{
displayString = hours.getDisplayValue() + ":" +
minutes.getDisplayValue();
}
}

View File

@@ -0,0 +1,17 @@
#BlueJ class context
comment0.params=rollOverLimit
comment0.target=NumberDisplay(int)
comment0.text=\n\ Constructor\ for\ objects\ of\ class\ NumberDisplay.\n\ Set\ the\ limit\ at\ which\ the\ display\ rolls\ over.\n
comment1.params=
comment1.target=int\ getValue()
comment1.text=\n\ Return\ the\ current\ value.\n
comment2.params=
comment2.target=java.lang.String\ getDisplayValue()
comment2.text=\n\ Return\ the\ display\ value\ (that\ is,\ the\ current\ value\ as\ a\ two-digit\n\ String.\ If\ the\ value\ is\ less\ than\ ten,\ it\ will\ be\ padded\ with\ a\ leading\n\ zero).\n
comment3.params=replacementValue
comment3.target=void\ setValue(int)
comment3.text=\n\ Set\ the\ value\ of\ the\ display\ to\ the\ new\ specified\ value.\ If\ the\ new\n\ value\ is\ less\ than\ zero\ or\ over\ the\ limit,\ do\ nothing.\n
comment4.params=
comment4.target=void\ increment()
comment4.text=\n\ Increment\ the\ display\ value\ by\ one,\ rolling\ over\ to\ zero\ if\ the\n\ limit\ is\ reached.\n
numComments=5

View File

@@ -0,0 +1,70 @@
/**
* The NumberDisplay class represents a digital number display that can hold
* values from zero to a given limit. The limit can be specified when creating
* the display. The values range from zero (inclusive) to limit-1. If used,
* for example, for the seconds on a digital clock, the limit would be 60,
* resulting in display values from 0 to 59. When incremented, the display
* automatically rolls over to zero when reaching the limit.
*
* @author Michael Kölling and David J. Barnes
* @version 2016.02.29
*/
public class NumberDisplay
{
private int limit;
private int value;
/**
* Constructor for objects of class NumberDisplay.
* Set the limit at which the display rolls over.
*/
public NumberDisplay(int rollOverLimit)
{
limit = rollOverLimit;
value = 0;
}
/**
* Return the current value.
*/
public int getValue()
{
return value;
}
/**
* Return the display value (that is, the current value as a two-digit
* String. If the value is less than ten, it will be padded with a leading
* zero).
*/
public String getDisplayValue()
{
if(value < 10) {
return "0" + value;
}
else {
return "" + value;
}
}
/**
* Set the value of the display to the new specified value. If the new
* value is less than zero or over the limit, do nothing.
*/
public void setValue(int replacementValue)
{
if((replacementValue >= 0) && (replacementValue < limit)) {
value = replacementValue;
}
}
/**
* Increment the display value by one, rolling over to zero if the
* limit is reached.
*/
public void increment()
{
value = (value + 1) % limit;
}
}

View File

@@ -0,0 +1,16 @@
Project: clock-display-with-GUI
Authors: Michael Kölling and David J. Barnes
This project is part of the 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
It is discussed in chapter 3.
This project adds a simple GUI (graphical user interface) to the
clock display project. When the clock is run, it runs at approximately
3 minutes per second, so that the display can be tested a little more
easily.

View File

@@ -0,0 +1,61 @@
#BlueJ package file
dependency1.from=Clock
dependency1.to=ClockDisplay
dependency1.type=UsesDependency
dependency2.from=ClockDisplay
dependency2.to=NumberDisplay
dependency2.type=UsesDependency
objectbench.height=100
objectbench.width=484
package.editor.height=394
package.editor.width=681
package.editor.x=70
package.editor.y=80
package.numDependencies=2
package.numTargets=3
package.showExtends=true
package.showUses=true
project.charset=UTF-8
readme.editor.height=606
readme.editor.width=838
readme.editor.x=53
readme.editor.y=67
target1.editor.height=700
target1.editor.width=900
target1.editor.x=60
target1.editor.y=96
target1.height=50
target1.name=ClockDisplay
target1.naviview.expanded=true
target1.showInterface=false
target1.type=ClassTarget
target1.typeParameters=
target1.width=110
target1.x=230
target1.y=140
target2.editor.height=700
target2.editor.width=900
target2.editor.x=145
target2.editor.y=101
target2.height=50
target2.name=NumberDisplay
target2.naviview.expanded=true
target2.showInterface=false
target2.type=ClassTarget
target2.typeParameters=
target2.width=120
target2.x=350
target2.y=230
target3.editor.height=843
target3.editor.width=976
target3.editor.x=101
target3.editor.y=35
target3.height=50
target3.name=Clock
target3.naviview.expanded=true
target3.showInterface=false
target3.type=ClassTarget
target3.typeParameters=
target3.width=80
target3.x=110
target3.y=50

View File

@@ -0,0 +1,20 @@
#BlueJ class context
comment0.params=
comment0.target=ClockDisplay()
comment0.text=\n\ Constructor\ for\ ClockDisplay\ objects.\ This\ constructor\ \n\ creates\ a\ new\ clock\ set\ at\ 00\:00.\n
comment1.params=hour\ minute
comment1.target=ClockDisplay(int,\ int)
comment1.text=\n\ Constructor\ for\ ClockDisplay\ objects.\ This\ constructor\n\ creates\ a\ new\ clock\ set\ at\ the\ time\ specified\ by\ the\ \n\ parameters.\n
comment2.params=
comment2.target=void\ timeTick()
comment2.text=\n\ This\ method\ should\ get\ called\ once\ every\ minute\ -\ it\ makes\n\ the\ clock\ display\ go\ one\ minute\ forward.\n
comment3.params=hour\ minute
comment3.target=void\ setTime(int,\ int)
comment3.text=\n\ Set\ the\ time\ of\ the\ display\ to\ the\ specified\ hour\ and\n\ minute.\n
comment4.params=
comment4.target=java.lang.String\ getTime()
comment4.text=\n\ Return\ the\ current\ time\ of\ this\ display\ in\ the\ format\ HH\:MM.\n
comment5.params=
comment5.target=void\ updateDisplay()
comment5.text=\n\ Update\ the\ internal\ string\ that\ represents\ the\ display.\n
numComments=6

View File

@@ -0,0 +1,84 @@
/**
* The ClockDisplay class implements a digital clock display for a
* European-style 24 hour clock. The clock shows hours and minutes. The
* range of the clock is 00:00 (midnight) to 23:59 (one minute before
* midnight).
*
* The clock display receives "ticks" (via the timeTick method) every minute
* and reacts by incrementing the display. This is done in the usual clock
* fashion: the hour increments when the minutes roll over to zero.
*
* @author Michael Kölling and David J. Barnes
* @version 2016.02.29
*/
public class ClockDisplay
{
private NumberDisplay hours;
private NumberDisplay minutes;
private String displayString; // simulates the actual display
/**
* Constructor for ClockDisplay objects. This constructor
* creates a new clock set at 00:00.
*/
public ClockDisplay()
{
hours = new NumberDisplay(24);
minutes = new NumberDisplay(60);
updateDisplay();
}
/**
* Constructor for ClockDisplay objects. This constructor
* creates a new clock set at the time specified by the
* parameters.
*/
public ClockDisplay(int hour, int minute)
{
hours = new NumberDisplay(24);
minutes = new NumberDisplay(60);
setTime(hour, minute);
}
/**
* This method should get called once every minute - it makes
* the clock display go one minute forward.
*/
public void timeTick()
{
minutes.increment();
if(minutes.getValue() == 0) { // it just rolled over!
hours.increment();
}
updateDisplay();
}
/**
* Set the time of the display to the specified hour and
* minute.
*/
public void setTime(int hour, int minute)
{
hours.setValue(hour);
minutes.setValue(minute);
updateDisplay();
}
/**
* Return the current time of this display in the format HH:MM.
*/
public String getTime()
{
return displayString;
}
/**
* Update the internal string that represents the display.
*/
private void updateDisplay()
{
displayString = hours.getDisplayValue() + ":" +
minutes.getDisplayValue();
}
}

View File

@@ -0,0 +1,17 @@
#BlueJ class context
comment0.params=rollOverLimit
comment0.target=NumberDisplay(int)
comment0.text=\n\ Constructor\ for\ objects\ of\ class\ NumberDisplay.\n\ Set\ the\ limit\ at\ which\ the\ display\ rolls\ over.\n
comment1.params=
comment1.target=int\ getValue()
comment1.text=\n\ Return\ the\ current\ value.\n
comment2.params=
comment2.target=java.lang.String\ getDisplayValue()
comment2.text=\n\ Return\ the\ display\ value\ (that\ is,\ the\ current\ value\ as\ a\ two-digit\n\ String.\ If\ the\ value\ is\ less\ than\ ten,\ it\ will\ be\ padded\ with\ a\ leading\n\ zero).\n
comment3.params=replacementValue
comment3.target=void\ setValue(int)
comment3.text=\n\ Set\ the\ value\ of\ the\ display\ to\ the\ new\ specified\ value.\ If\ the\ new\n\ value\ is\ less\ than\ zero\ or\ over\ the\ limit,\ do\ nothing.\n
comment4.params=
comment4.target=void\ increment()
comment4.text=\n\ Increment\ the\ display\ value\ by\ one,\ rolling\ over\ to\ zero\ if\ the\n\ limit\ is\ reached.\n
numComments=5

View File

@@ -0,0 +1,70 @@
/**
* The NumberDisplay class represents a digital number display that can hold
* values from zero to a given limit. The limit can be specified when creating
* the display. The values range from zero (inclusive) to limit-1. If used,
* for example, for the seconds on a digital clock, the limit would be 60,
* resulting in display values from 0 to 59. When incremented, the display
* automatically rolls over to zero when reaching the limit.
*
* @author Michael Kölling and David J. Barnes
* @version 2016.02.29
*/
public class NumberDisplay
{
private int limit;
private int value;
/**
* Constructor for objects of class NumberDisplay.
* Set the limit at which the display rolls over.
*/
public NumberDisplay(int rollOverLimit)
{
limit = rollOverLimit;
value = 0;
}
/**
* Return the current value.
*/
public int getValue()
{
return value;
}
/**
* Return the display value (that is, the current value as a two-digit
* String. If the value is less than ten, it will be padded with a leading
* zero).
*/
public String getDisplayValue()
{
if(value < 10) {
return "0" + value;
}
else {
return "" + value;
}
}
/**
* Set the value of the display to the new specified value. If the new
* value is less than zero or over the limit, do nothing.
*/
public void setValue(int replacementValue)
{
if((replacementValue >= 0) && (replacementValue < limit)) {
value = replacementValue;
}
}
/**
* Increment the display value by one, rolling over to zero if the
* limit is reached.
*/
public void increment()
{
value = (value + 1) % limit;
}
}

View File

@@ -0,0 +1,26 @@
Project: clock-display
Authors: David Barnes and Michael Kölling
This project is part of the 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
It is discussed in chapter 3.
To use this project, create an instance of class ClockDisplay. Then inspect this
instance and leave the object inspector window open. With the inspector window open,
call the object's methods, such as timeTick and setTime. Watch the
"displayString" in the inspector.
The displayString field simulates the actual clock display device. Were this
program running in a real clock, it would change the real, physical display
hardware instead of this string. The timeTick method would be triggered once
every minute by some timer hardware.
Everything else could be pretty much the same.
Note: This project does not implement adequate handling of errors.

View File

@@ -0,0 +1,45 @@
#BlueJ package file
dependency1.from=ClockDisplay
dependency1.to=NumberDisplay
dependency1.type=UsesDependency
objectbench.height=124
objectbench.width=774
package.editor.height=371
package.editor.width=666
package.editor.x=70
package.editor.y=80
package.numDependencies=1
package.numTargets=2
package.showExtends=true
package.showUses=true
project.charset=UTF-8
readme.editor.height=626
readme.editor.width=875
readme.editor.x=53
readme.editor.y=28
target1.editor.height=745
target1.editor.width=845
target1.editor.x=81
target1.editor.y=119
target1.height=60
target1.name=ClockDisplay
target1.naviview.expanded=true
target1.showInterface=false
target1.type=ClassTarget
target1.typeParameters=
target1.width=110
target1.x=150
target1.y=100
target2.editor.height=792
target2.editor.width=873
target2.editor.x=97
target2.editor.y=188
target2.height=60
target2.name=NumberDisplay
target2.naviview.expanded=true
target2.showInterface=false
target2.type=ClassTarget
target2.typeParameters=
target2.width=110
target2.x=290
target2.y=180

View File

@@ -0,0 +1,14 @@
#BlueJ class context
comment0.params=server\ user
comment0.target=MailClient(MailServer,\ java.lang.String)
comment0.text=\n\ Create\ a\ mail\ client\ run\ by\ user\ and\ attached\ to\ the\ given\ server.\n
comment1.params=
comment1.target=MailItem\ getNextMailItem()
comment1.text=\n\ Return\ the\ next\ mail\ item\ (if\ any)\ for\ this\ user.\n
comment2.params=
comment2.target=void\ printNextMailItem()
comment2.text=\n\ Print\ the\ next\ mail\ item\ (if\ any)\ for\ this\ user\ to\ the\ text\ \n\ terminal.\n
comment3.params=to\ message
comment3.target=void\ sendMailItem(java.lang.String,\ java.lang.String)
comment3.text=\n\ Send\ the\ given\ message\ to\ the\ given\ recipient\ via\n\ the\ attached\ mail\ server.\n\ @param\ to\ The\ intended\ recipient.\n\ @param\ message\ The\ text\ of\ the\ message\ to\ be\ sent.\n
numComments=4

View File

@@ -0,0 +1,58 @@
/**
* A class to model a simple email client. The client is run by a
* particular user, and sends and retrieves mail via a particular server.
*
* @author David J. Barnes and Michael Kölling
* @version 2016.02.29
*/
public class MailClient
{
// The server used for sending and receiving.
private MailServer server;
// The user running this client.
private String user;
/**
* Create a mail client run by user and attached to the given server.
*/
public MailClient(MailServer server, String user)
{
this.server = server;
this.user = user;
}
/**
* Return the next mail item (if any) for this user.
*/
public MailItem getNextMailItem()
{
return server.getNextMailItem(user);
}
/**
* Print the next mail item (if any) for this user to the text
* terminal.
*/
public void printNextMailItem()
{
MailItem item = server.getNextMailItem(user);
if(item == null) {
System.out.println("No new mail.");
}
else {
item.print();
}
}
/**
* Send the given message to the given recipient via
* the attached mail server.
* @param to The intended recipient.
* @param message The text of the message to be sent.
*/
public void sendMailItem(String to, String message)
{
MailItem item = new MailItem(user, to, message);
server.post(item);
}
}

View File

@@ -0,0 +1,17 @@
#BlueJ class context
comment0.params=from\ to\ message
comment0.target=MailItem(java.lang.String,\ java.lang.String,\ java.lang.String)
comment0.text=\n\ Create\ a\ mail\ item\ from\ sender\ to\ the\ given\ recipient,\n\ containing\ the\ given\ message.\n\ @param\ from\ The\ sender\ of\ this\ item.\n\ @param\ to\ The\ intended\ recipient\ of\ this\ item.\n\ @param\ message\ The\ text\ of\ the\ message\ to\ be\ sent.\n
comment1.params=
comment1.target=java.lang.String\ getFrom()
comment1.text=\n\ @return\ The\ sender\ of\ this\ message.\n
comment2.params=
comment2.target=java.lang.String\ getTo()
comment2.text=\n\ @return\ The\ intended\ recipient\ of\ this\ message.\n
comment3.params=
comment3.target=java.lang.String\ getMessage()
comment3.text=\n\ @return\ The\ text\ of\ the\ message.\n
comment4.params=
comment4.target=void\ print()
comment4.text=\n\ Print\ this\ mail\ message\ to\ the\ text\ terminal.\n
numComments=5

View File

@@ -0,0 +1,64 @@
/**
* A class to model a simple mail item. The item has sender and recipient
* addresses and a message string.
*
* @author David J. Barnes and Michael Kölling
* @version 2016.02.29
*/
public class MailItem
{
// The sender of the item.
private String from;
// The intended recipient.
private String to;
// The text of the message.
private String message;
/**
* Create a mail item from sender to the given recipient,
* containing the given message.
* @param from The sender of this item.
* @param to The intended recipient of this item.
* @param message The text of the message to be sent.
*/
public MailItem(String from, String to, String message)
{
this.from = from;
this.to = to;
this.message = message;
}
/**
* @return The sender of this message.
*/
public String getFrom()
{
return from;
}
/**
* @return The intended recipient of this message.
*/
public String getTo()
{
return to;
}
/**
* @return The text of the message.
*/
public String getMessage()
{
return message;
}
/**
* Print this mail message to the text terminal.
*/
public void print()
{
System.out.println("From: " + from);
System.out.println("To: " + to);
System.out.println("Message: " + message);
}
}

View File

@@ -0,0 +1,14 @@
#BlueJ class context
comment0.params=
comment0.target=MailServer()
comment0.text=\n\ Construct\ a\ mail\ server.\n
comment1.params=who
comment1.target=int\ howManyMailItems(java.lang.String)
comment1.text=\n\ Return\ how\ many\ mail\ items\ are\ waiting\ for\ a\ user.\n\ @param\ who\ The\ user\ to\ check\ for.\n\ @return\ How\ many\ items\ are\ waiting.\n
comment2.params=who
comment2.target=MailItem\ getNextMailItem(java.lang.String)
comment2.text=\n\ Return\ the\ next\ mail\ item\ for\ a\ user\ or\ null\ if\ there\n\ are\ none.\n\ @param\ who\ The\ user\ requesting\ their\ next\ item.\n\ @return\ The\ user's\ next\ item.\n
comment3.params=item
comment3.target=void\ post(MailItem)
comment3.text=\n\ Add\ the\ given\ mail\ item\ to\ the\ message\ list.\n\ @param\ item\ The\ mail\ item\ to\ be\ stored\ on\ the\ server.\n
numComments=4

View File

@@ -0,0 +1,69 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;
/**
* A simple model of a mail server. The server is able to receive
* mail items for storage, and deliver them to clients on demand.
*
* @author David J. Barnes and Michael Kölling
* @version 2016.02.29
*/
public class MailServer
{
// Storage for the arbitrary number of mail items to be stored
// on the server.
private List<MailItem> items;
/**
* Construct a mail server.
*/
public MailServer()
{
items = new ArrayList<>();
}
/**
* Return how many mail items are waiting for a user.
* @param who The user to check for.
* @return How many items are waiting.
*/
public int howManyMailItems(String who)
{
int count = 0;
for(MailItem item : items) {
if(item.getTo().equals(who)) {
count++;
}
}
return count;
}
/**
* Return the next mail item for a user or null if there
* are none.
* @param who The user requesting their next item.
* @return The user's next item.
*/
public MailItem getNextMailItem(String who)
{
Iterator<MailItem> it = items.iterator();
while(it.hasNext()) {
MailItem item = it.next();
if(item.getTo().equals(who)) {
it.remove();
return item;
}
}
return null;
}
/**
* Add the given mail item to the message list.
* @param item The mail item to be stored on the server.
*/
public void post(MailItem item)
{
items.add(item);
}
}

View File

@@ -0,0 +1,33 @@
Project: mail-system
Authors: David Barnes and Michael Kölling
This project is part of the 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
It is discussed in chapter 3.
This project simulates a simple email system. Mail clients simulate email programs
of different users. If you create two or more email clients, they can send messages
to each other. (Note this this is a simulation: you cannot really send email. All
messages are exchanged only between the email client objects in this project.)
To use this project,
- create a MailServer instance.
- create a MailClient instance. Here you have to pass the mail server as a
parameter, and you have to make up a name for this user.
- create a second MailClient object with the same mail server and a different user
name.
- Use the "sendMessage" method of a MailClient object to send a message to the
other mail client.
- Use the "printNextMessage" method of the second mail client to receive the message.
The purpose of this project is to demonstrate object interaction. It includes external
method calls (one object calling methods of another object) and object creation
statements.
The debugger can be used to investigate these object interactions.

View File

@@ -0,0 +1,64 @@
#BlueJ package file
dependency1.from=MailClient
dependency1.to=MailServer
dependency1.type=UsesDependency
dependency2.from=MailClient
dependency2.to=MailItem
dependency2.type=UsesDependency
dependency3.from=MailServer
dependency3.to=MailItem
dependency3.type=UsesDependency
objectbench.height=89
objectbench.width=766
package.editor.height=365
package.editor.width=658
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=776
readme.editor.width=876
readme.editor.x=53
readme.editor.y=24
target1.editor.height=749
target1.editor.width=844
target1.editor.x=113
target1.editor.y=40
target1.height=60
target1.name=MailItem
target1.naviview.expanded=true
target1.showInterface=false
target1.type=ClassTarget
target1.typeParameters=
target1.width=100
target1.x=400
target1.y=220
target2.editor.height=767
target2.editor.width=863
target2.editor.x=53
target2.editor.y=60
target2.height=60
target2.name=MailServer
target2.naviview.expanded=true
target2.showInterface=false
target2.type=ClassTarget
target2.typeParameters=
target2.width=100
target2.x=90
target2.y=160
target3.editor.height=527
target3.editor.width=712
target3.editor.x=132
target3.editor.y=79
target3.height=60
target3.name=MailClient
target3.naviview.expanded=false
target3.showInterface=false
target3.type=ClassTarget
target3.typeParameters=
target3.width=100
target3.x=250
target3.y=80

View File

@@ -0,0 +1,17 @@
#BlueJ class context
comment0.params=rollOverLimit
comment0.target=NumberDisplay(int)
comment0.text=\n\ Constructor\ for\ objects\ of\ class\ NumberDisplay.\n\ Set\ the\ limit\ at\ which\ the\ display\ rolls\ over.\n
comment1.params=
comment1.target=int\ getValue()
comment1.text=\n\ Return\ the\ current\ value.\n
comment2.params=
comment2.target=java.lang.String\ getDisplayValue()
comment2.text=\n\ Return\ the\ display\ value\ (that\ is,\ the\ current\ value\ as\ a\ two-digit\n\ String.\ If\ the\ value\ is\ less\ than\ ten,\ it\ will\ be\ padded\ with\ a\ leading\n\ zero).\n
comment3.params=replacementValue
comment3.target=void\ setValue(int)
comment3.text=\n\ Set\ the\ value\ of\ the\ display\ to\ the\ new\ specified\ value.\ If\ the\ new\n\ value\ is\ less\ than\ zero\ or\ over\ the\ limit,\ do\ nothing.\n
comment4.params=
comment4.target=void\ increment()
comment4.text=\n\ Increment\ the\ display\ value\ by\ one,\ rolling\ over\ to\ zero\ if\ the\n\ limit\ is\ reached.\n
numComments=5

View File

@@ -0,0 +1,70 @@
/**
* The NumberDisplay class represents a digital number display that can hold
* values from zero to a given limit. The limit can be specified when creating
* the display. The values range from zero (inclusive) to limit-1. If used,
* for example, for the seconds on a digital clock, the limit would be 60,
* resulting in display values from 0 to 59. When incremented, the display
* automatically rolls over to zero when reaching the limit.
*
* @author Michael Kölling and David J. Barnes
* @version 2016.02.29
*/
public class NumberDisplay
{
private int limit;
private int value;
/**
* Constructor for objects of class NumberDisplay.
* Set the limit at which the display rolls over.
*/
public NumberDisplay(int rollOverLimit)
{
limit = rollOverLimit;
value = 0;
}
/**
* Return the current value.
*/
public int getValue()
{
return value;
}
/**
* Return the display value (that is, the current value as a two-digit
* String. If the value is less than ten, it will be padded with a leading
* zero).
*/
public String getDisplayValue()
{
if(value < 10) {
return "0" + value;
}
else {
return "" + value;
}
}
/**
* Set the value of the display to the new specified value. If the new
* value is less than zero or over the limit, do nothing.
*/
public void setValue(int replacementValue)
{
if((replacementValue >= 0) && (replacementValue < limit)) {
value = replacementValue;
}
}
/**
* Increment the display value by one, rolling over to zero if the
* limit is reached.
*/
public void increment()
{
value = (value + 1) % limit;
}
}

View File

@@ -0,0 +1,15 @@
Project: number-display
Authors: Michael Kölling and David J. Barnes
This project is part of the 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
It is discussed in chapter 3.
To use this project, create an instance of class NumberDisplay. Then inspect this
instance and leave the inspector window open. With the inspector window open,
call the object's methods, such as increment and setValue.

View File

@@ -0,0 +1,29 @@
#BlueJ package file
objectbench.height=153
objectbench.width=786
package.editor.height=357
package.editor.width=678
package.editor.x=70
package.editor.y=80
package.numDependencies=0
package.numTargets=1
package.showExtends=true
package.showUses=true
project.charset=UTF-8
readme.editor.height=626
readme.editor.width=875
readme.editor.x=53
readme.editor.y=28
target1.editor.height=796
target1.editor.width=873
target1.editor.x=234
target1.editor.y=217
target1.height=60
target1.name=NumberDisplay
target1.naviview.expanded=true
target1.showInterface=false
target1.type=ClassTarget
target1.typeParameters=
target1.width=110
target1.x=210
target1.y=100