-
#include <stdio.h>
-
#include <stdlib.h>
-
-
typedef struct node
-
{
-
int key;
-
struct node *next;
-
}list;
-
-
void print(list *root)
-
{
-
list *i;
-
for(i=root; i!=NULL; i=i->next)
-
printf(" %d",i->key);
-
printf("\n");
-
}
-
//create a singly linked list
-
list *addFirst(list *root, int val)
-
{
-
list *p;
-
p=(list*) malloc(sizeof(list));
-
p->key=val;
-
p->next=root;
-
root=p;
-
return root;
-
}
-
list *addLast(list *root, int val)
-
{
-
list *p;
-
p=(list*)malloc(sizeof(list));
-
p->next=NULL;
-
p->key=val;
-
if(root==NULL) root=p;
-
else {
-
list *i;
-
for(i=root; i->next!=NULL;i=i->next);
-
i->next=p;
-
}
-
return root;
-
}
-
list *create(list *root)
-
{
-
root=NULL;
-
int val;
-
while(1==scanf("%d",&val))
-
{
-
if(val>0) root=addFirst(root,val);
-
else root=addLast(root,val);
-
print(root);
-
}
-
addLast(root,-1);
-
return root;
-
}
-
-
list *findKey(list *root, int key)
-
{
-
list *i;
-
for(i=root; i!=NULL; i=i->next)
-
if(i->key==key) return i;
-
return NULL;
-
}
-
int deleteList(list* root)
-
{
-
list *p;
-
while(root!=NULL)
-
{
-
p=root;
-
root=root->next;
-
free(p);
-
}
-
}
-
-
int main()
-
{
-
list *root;
-
root=create(root);
-
print(root);
-
if(findKey(root,0)!=NULL) printf("key 0 has been found");
-
deleteList(root);
-
system("pause");
-
return 0;
-
}
Operations on lists
Posted by marian
on June 29, 2009
Waiting Queues (Java homework)
Posted by marian
on June 29, 2009
-
package waitingQueues;
-
-
import javax.swing.JApplet;
-
import waitingQueues.controller.Controller;
-
import waitingQueues.controller.ControllerInterface;
-
import waitingQueues.model.Model;
-
import waitingQueues.model.ModelInterface;
-
import waitingQueues.view.View;
-
import waitingQueues.view.ViewInterface;
-
-
/**
-
* This the main class. It instantiates the Model, View and Controller from the MVC model
-
* @author Marian Dan
-
*
-
*/
-
public class Main extends JApplet{
-
/*
-
* The default constructor on the Main class.
-
*/
-
public Main(){
-
-
//create an instance of the user interface/ the view
-
ui = new View();
-
//create an instance of the model we are working on
-
model=new Model();
-
//create an instance of the controller that manages the
-
//interactions between the model and the
-
controller=new Controller(ui,model);
-
}
-
//the application is designed to run both as an applet or as an
-
//application
-
/**
-
* Overrides the default init method of the JApplet
-
*/
-
public void init(){
-
-
}
-
/**
-
* Overrides the default start method of the JApplet
-
*/
-
public void start(){
-
-
}
-
/**
-
* Overrides the default stoo method of the JApplet
-
*/
-
public void stop(){
-
-
}
-
/**
-
* Overrides the default destroy method of the JApplet
-
*/
-
public void destroy(){
-
-
}
-
-
/**
-
* The standard public String toString() is overridden
-
*/
-
public String toString(){
-
return "This is the main application";
-
}
-
/**
-
* Check to see if this is the instance of the main application
-
*/
-
public boolean equals(Object other) {
-
if(this==other) return true;
-
else return false;
-
}
-
-
/**
-
* For running the application as an application
-
*/
-
public static void main(String args[]) {
-
final Main application = new Main();
-
}
-
private static final long serialVersionUID = 2028805364097710940L;
-
private final ViewInterface ui;
-
private final ModelInterface model;
-
private final ControllerInterface controller;
-
-
}
-
package waitingQueues;
-
-
/**
-
* The purpose of this class is to provide means of treating exceptions,
-
* that arise when the program is running. The class WaitingQueuesException
-
* inherits the methods of the default Java class Exception.
-
*/
-
public class WaitingQueuesException extends Exception{
-
/**
-
* Default constructor
-
* @param String
-
*/
-
public WaitingQueuesException(String mess){
-
//this.printStackTrace();
-
message=mess;
-
System.out.println("Exception: # "+message);
-
}
-
/**
-
* This method overrides the default toString() method
-
*
-
*/
-
public String toString()
-
{
-
return message;
-
}
-
/**
-
* The method returns the message associated with the exception
-
*/
-
public String getMessage(){
-
return message;
-
}
-
/**
-
* Check to see if this is the instance of the View
-
*/
-
public boolean equals(Object other) {
-
if(other instanceof WaitingQueuesException){
-
WaitingQueuesException m=(WaitingQueuesException)other;
-
if(m.toString().equals(message)) return true;
-
else return false;
-
-
}else return false;
-
}
-
private static final long serialVersionUID = 4593665563049627269L;
-
//stores the error message
-
private String message="";
-
-
}
-
package waitingQueues;
-
/**
-
* Stores constants regarding the application limitations such as the maximum number of
-
* clients or queues
-
* @author Marian Dan
-
*
-
*/
-
public class Constants {
-
/**
-
* Stores the minimum limit for the number of queues
-
*/
-
public static final int MINIMUM_NUMBER_OF_QUEUES =1;
-
/**
-
* Stores the minimum limit for the waiting time
-
*/
-
public static final int MINIMUM_WAITING_TIME =1;
-
/**
-
* Stores the minimum limit for the service time
-
*/
-
public static final int MINIMUM_SERVICE_TIME =1;
-
/**
-
* Stores the minimum limit for the arrival time
-
*/
-
public static final int MINIMUM_ARRIVAL_TIME =1;
-
/**
-
* Stores the minimum limit for the number of clients
-
*/
-
public static final int MINIMUM_NUMBER_OF_CLIENTS =1;
-
-
/**
-
* Stores the maximum limit for the number of queues
-
*/
-
public static final int MAXIMUM_NUMBER_OF_QUEUES =1000;
-
/**
-
* Stores the maximum limit for the waiting time
-
*/
-
public static final int MAXIMUM_WAITING_TIME =1000;
-
/**
-
* Stores the maximum limit for the service time
-
*/
-
public static final int MAXIMUM_SERVICE_TIME =1000;
-
/**
-
* Stores the maximum limit for the arrival time
-
*/
-
public static final int MAXIMUM_ARRIVAL_TIME =1000;
-
/**
-
* Stores the maximum limit for the number of clients
-
*/
-
public static final int MAXIMUM_NUMBER_OF_CLIENTS =1000;
-
}
-
package waitingQueues.view;
-
-
import java.awt.Dimension;
-
import java.awt.event.ActionListener;
-
-
import javax.swing.JButton;
-
import javax.swing.JEditorPane;
-
import javax.swing.JFrame;
-
import javax.swing.JLabel;
-
import javax.swing.JOptionPane;
-
import javax.swing.JPanel;
-
import javax.swing.JScrollPane;
-
import javax.swing.JTextField;
-
-
import waitingQueues.WaitingQueuesException;
-
-
public class InputForm extends JFrame {
-
public InputForm() {
-
// let decorations be available
-
JFrame.setDefaultLookAndFeelDecorated(true);
-
// Create and set up the window.
-
panel.setLayout(null);
-
// set the bounds of the panel
-
panel.setBounds(0, 0, 300, 530);
-
-
clientsLabel.setBounds(10, 10, 120, 20);
-
clientsTxt.setBounds(140, 10, 120, 20);
-
-
queuesLabel.setBounds(10, 40, 120, 20);
-
queuesTxt.setBounds(140, 40, 120, 20);
-
-
arrivalTimeLabel.setBounds(10, 70, 100, 20);
-
arrivalTimeMinTxt.setBounds(140, 70, 50, 20);
-
arrivalTimeMaxTxt.setBounds(210, 70, 50, 20);
-
-
serviceTimeLabel.setBounds(10, 100, 100, 20);
-
serviceTimeMinTxt.setBounds(140, 100, 50, 20);
-
serviceTimeMaxTxt.setBounds(210, 100, 50, 20);
-
-
simulationForTimeLabel.setBounds(100, 130, 50, 20);
-
simulateForTxt.setBounds(210, 130, 50, 20);
-
-
simulationTimeLabel.setBounds(100, 160, 70, 20);
-
simulateTxt.setBounds(210, 160, 50, 20);
-
simulateTxt.setEditable(false);
-
-
simulateBtn.setBounds(10, 130, 85, 20);
-
descriptionLabel.setBounds(10, 190, 100, 20);
-
-
// create a scrolling area for processing area
-
scrolledContent = new JScrollPane(content);
-
// set just the horizontal scroll bar
-
scrolledContent
-
.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
-
// set the prefferred size
-
scrolledContent.setPreferredSize(new Dimension(300, 220));
-
// set the minim size of scrooll pane
-
content.setEditable(false);
-
content.setText("");
-
scrolledContent.setMinimumSize(new Dimension(10, 10));
-
scrolledContent.setBounds(10, 210, 270, 250);
-
averageTimeLabel.setBounds(10, 470, 80, 20);
-
averageTimeTxt.setBounds(100, 470, 180, 20);
-
averageTimeTxt.setEditable(false);
-
-
// add the scrolled panel to the frame
-
-
panel.add(clientsLabel);
-
panel.add(queuesLabel);
-
panel.add(arrivalTimeLabel);
-
panel.add(serviceTimeLabel);
-
panel.add(simulationForTimeLabel);
-
panel.add(simulationTimeLabel);
-
panel.add(clientsTxt);
-
panel.add(queuesTxt);
-
panel.add(arrivalTimeMinTxt);
-
panel.add(arrivalTimeMaxTxt);
-
panel.add(serviceTimeMinTxt);
-
panel.add(serviceTimeMaxTxt);
-
panel.add(simulationTimeLabel);
-
panel.add(simulateForTxt);
-
panel.add(simulateTxt);
-
panel.add(simulateBtn);
-
panel.add(descriptionLabel);
-
panel.add(scrolledContent);
-
panel.add(averageTimeTxt);
-
panel.add(averageTimeLabel);
-
-
// add the panel to the frame
-
this.add(panel);
-
// set the size of the frame
-
this.setSize(300, 530);
-
// Display the window.
-
this.setVisible(true);
-
// set the default close operation, i.e. what happens when you click on
-
// X
-
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-
// it should not be resizable
-
this.setResizable(false);
-
// set the title of the application
-
this.setTitle("Queue simulation <<Input form>> Marian Dan Alexandru");
-
}
-
-
protected int getNumberOfQueues() throws WaitingQueuesException {
-
int n = 0;
-
-
try {
-
n = Integer.parseInt(queuesTxt.getText());
-
} catch (NumberFormatException ex) {
-
throw new WaitingQueuesException(
-
"The input is not valid.\nPlease enter a postive integer for the number of queues.");
-
}
-
-
return n;
-
}
-
-
protected int getNumberOfClients() throws WaitingQueuesException {
-
int n = 0;
-
try {
-
n = Integer.parseInt(clientsTxt.getText());
-
} catch (NumberFormatException ex) {
-
throw new WaitingQueuesException(
-
"The input is not valid.\nPlease enter a postive integer for the number of clients.");
-
}
-
return n;
-
}
-
-
protected int getMinimumArrivalTime() throws WaitingQueuesException {
-
int n = 0;
-
-
try {
-
n = Integer.parseInt(arrivalTimeMinTxt.getText());
-
} catch (NumberFormatException ex) {
-
throw new WaitingQueuesException(
-
"The input is not valid.\nPlease enter a postive integer for the minimum arrival time.");
-
}
-
-
return n;
-
}
-
-
protected int getMaximumArrivalTime() throws WaitingQueuesException {
-
int n = 0;
-
-
try {
-
n = Integer.parseInt(arrivalTimeMaxTxt.getText());
-
} catch (NumberFormatException ex) {
-
throw new WaitingQueuesException(
-
"The input is not valid.\nPlease enter a postive integer for the maximum arrival time.");
-
}
-
-
return n;
-
}
-
-
protected int getMinimumServiceTime() throws WaitingQueuesException {
-
int n = 0;
-
-
try {
-
n = Integer.parseInt(serviceTimeMinTxt.getText());
-
} catch (NumberFormatException ex) {
-
throw new WaitingQueuesException(
-
"The input is not valid.\nPlease enter a postive integer for the minimum service time.");
-
}
-
-
return n;
-
}
-
-
protected int getMaximumServiceTime() throws WaitingQueuesException {
-
int n = 0;
-
-
try {
-
n = Integer.parseInt(serviceTimeMaxTxt.getText());
-
} catch (NumberFormatException ex) {
-
throw new WaitingQueuesException(
-
"The input is not valid.\nPlease enter a postive integer for the maximum service time.");
-
}
-
return n;
-
}
-
-
protected int getTimeForSimulation() throws WaitingQueuesException {
-
int n = 0;
-
-
try {
-
n = Integer.parseInt(simulateForTxt.getText());
-
} catch (NumberFormatException ex) {
-
throw new WaitingQueuesException(
-
"The input is not valid.\nPlease enter a postive integer for the simulation time period.");
-
}
-
-
return n;
-
}
-
-
protected void setSimulationTime(String time) {
-
simulateTxt.setText("" + time);
-
}
-
-
protected void setAverageTime(String time) {
-
averageTimeTxt.setText("" + time);
-
}
-
-
protected void addToMessageQueue(String message) {
-
String s = content.getText();
-
s = message + s;
-
content.setText(s);
-
}
-
-
protected final void showMessage(String message, String title) {
-
JOptionPane.showMessageDialog(null, message, title,
-
JOptionPane.WARNING_MESSAGE);
-
}
-
-
protected final void showErrorMessage(String message) {
-
JOptionPane.showMessageDialog(this, message, "Error",
-
JOptionPane.ERROR_MESSAGE);
-
}
-
-
/**
-
* Add an action listener to the Simulate button
-
*
-
* @param e
-
*/
-
protected final void addSimulateBtnListener(ActionListener e) {
-
simulateBtn.addActionListener(e);
-
}
-
-
public String toString() {
-
String s = "";
-
try {
-
s += "Clients: " + getNumberOfClients() + "\n" + "Queues: "
-
+ getNumberOfQueues() + "\n" + "Min Arrival Time: "
-
+ getMinimumArrivalTime() + "\n" + "Max Arrival Time: "
-
+ getMaximumArrivalTime() + "\n" + "Min Service Time: "
-
+ getMinimumServiceTime() + "\n" + "Max Service Time: "
-
+ getMaximumServiceTime() + "\n" + "Simulate for: "
-
+ getTimeForSimulation() + "\n" + "Simulation time: "
-
+ simulateTxt.getText() + "\n";
-
} catch (Exception e) {
-
// do nothing
-
}
-
return s;
-
}
-
-
protected void enableNewSimulation(boolean value){
-
simulateBtn.setEnabled(value);
-
}
-
-
private final JPanel panel = new JPanel();
-
private final JLabel clientsLabel = new JLabel("Number of clients:");
-
private final JLabel queuesLabel = new JLabel("Number of queues:");
-
private final JLabel arrivalTimeLabel = new JLabel("Arrival time:");
-
private final JLabel serviceTimeLabel = new JLabel("Service time:");
-
private final JLabel simulationForTimeLabel = new JLabel("For time:");
-
private final JLabel simulationTimeLabel = new JLabel("Time:");
-
private final JLabel descriptionLabel = new JLabel("Description:");
-
private final JLabel averageTimeLabel = new JLabel("Average time:");
-
-
private final JButton simulateBtn = new JButton("Simulate");
-
-
private final JTextField clientsTxt = new JTextField();
-
private final JTextField queuesTxt = new JTextField();
-
private final JTextField arrivalTimeMinTxt = new JTextField();
-
private final JTextField arrivalTimeMaxTxt = new JTextField();
-
private final JTextField serviceTimeMinTxt = new JTextField();
-
private final JTextField serviceTimeMaxTxt = new JTextField();
-
private final JTextField simulateForTxt = new JTextField();
-
private final JTextField simulateTxt = new JTextField();
-
private final JTextField averageTimeTxt = new JTextField();
-
private final JEditorPane content = new JEditorPane();
-
private final JScrollPane scrolledContent;
-
private static final long serialVersionUID = -1994987175182483198L;
-
-
}
-
package waitingQueues.view;
-
-
import java.awt.Dimension;
-
import java.util.ArrayList;
-
import java.util.Iterator;
-
-
import javax.swing.JFrame;
-
import javax.swing.JPanel;
-
import javax.swing.JScrollPane;
-
-
import waitingQueues.WaitingQueuesException;
-
-
public class OutputForm extends JFrame {
-
public OutputForm() {
-
// let decorations be available
-
JFrame.setDefaultLookAndFeelDecorated(true);
-
// Create and set up the window.
-
-
array = new ArrayList();
-
-
panel.setLayout(null);
-
// set the bounds of the panel
-
panel.setBounds(0, 0, OUTPUT_FORM_WIDTH, OUTPUT_FORM_HEIGHT);
-
-
// create a scrolling area for processing area
-
scrolledContent = new JScrollPane(panel);
-
// scrolledContent = new JScrollPane();
-
// set just the horizontal scroll bar
-
scrolledContent
-
.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
-
// set the prefferred size
-
scrolledContent.setPreferredSize(new Dimension(
-
OutputForm.OUTPUT_FORM_WIDTH, OUTPUT_FORM_HEIGHT));
-
// set the minim size of scrooll pane
-
scrolledContent.setMinimumSize(new Dimension(10, 10));
-
scrolledContent.setBounds(0, 0, OutputForm.OUTPUT_FORM_WIDTH,
-
OUTPUT_FORM_HEIGHT);
-
-
// add the scrolled panel to the frame
-
this.add(scrolledContent);
-
-
panel.setPreferredSize(new Dimension(OUTPUT_FORM_WIDTH,
-
OUTPUT_FORM_HEIGHT));
-
-
this.setPreferredSize(new Dimension(OUTPUT_FORM_WIDTH + 40,
-
OUTPUT_FORM_HEIGHT));
-
pack();
-
-
// set the size of the frame
-
this.setLocation(320, 0);
-
this.setSize(OUTPUT_FORM_WIDTH + 40, OUTPUT_FORM_HEIGHT);
-
// Display the window.
-
this.setVisible(true);
-
// set the default close operation, i.e. what happens when you click on
-
// X
-
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-
// it should not be resizeble
-
// this.setResizable(false);
-
// set the title of the application
-
this.setTitle("Queue display");
-
}
-
-
protected void addQueue(int iId) throws WaitingQueuesException {
-
Iterator it;
-
boolean found = false;
-
-
it = array.iterator();
-
while (it.hasNext()) {
-
Object obj = it.next();
-
// find the type of the object in order to draw it
-
if (obj != null && (obj instanceof QueueView)) {
-
QueueView q = (QueueView) obj;
-
if (q.equals(iId)) {
-
found = true;
-
break;
-
}
-
}
-
}
-
if (!found) {
-
QueueView queue = new QueueView(iId);
-
queue.setBounds(0, getNumberOfExistingQueues()
-
* QueueView.QUEUE_VIEW_HEIGHT, OUTPUT_FORM_WIDTH,
-
QueueView.QUEUE_VIEW_HEIGHT);
-
panel.setPreferredSize(new Dimension(OUTPUT_FORM_WIDTH,
-
(getNumberOfExistingQueues() + 1)
-
* QueueView.QUEUE_VIEW_HEIGHT));
-
-
array.add(queue);
-
panel.add(queue);
-
pack();
-
} else {
-
throw new WaitingQueuesException("Queue already present");
-
}
-
//TODO
-
//System.out.println("ouput form "+iId);
-
}
-
-
private int getNumberOfExistingQueues() {
-
return array.size();
-
}
-
-
protected void setQueueContent(int iId, String s[]) {
-
Iterator it;
-
it = array.iterator();
-
while (it.hasNext()) {
-
Object obj = it.next();
-
// find the type of the object in order to draw it
-
if (obj != null && (obj instanceof QueueView)) {
-
QueueView q = (QueueView) obj;
-
if (q.equals(iId)) {
-
q.setQueueInformation(s);
-
}
-
}
-
}
-
}
-
-
protected void reset() {
-
for (int i = 0; i < array.size(); i++) {
-
Object obj=array.get(i);
-
if (obj instanceof QueueView) {
-
QueueView q = (QueueView) obj;
-
q.setVisible(false);
-
panel.remove(q);
-
q = null;
-
//TODO
-
//System.out.println("ouput form xx");
-
}
-
}
-
array.clear();
-
//pack();
-
}
-
-
private final JPanel panel = new JPanel();
-
private final JScrollPane scrolledContent;
-
protected static final int OUTPUT_FORM_WIDTH = 400;
-
protected static final int OUTPUT_FORM_HEIGHT = 400;
-
private ArrayList array;
-
}
-
package waitingQueues.view;
-
-
import java.awt.Dimension;
-
-
import javax.swing.JEditorPane;
-
import javax.swing.JLabel;
-
import javax.swing.JPanel;
-
import javax.swing.JScrollPane;
-
import javax.swing.JTextField;
-
-
public class QueueView extends JPanel{
-
public QueueView(int iId){
-
super();
-
-
this.iId=iId;
-
this.setLayout(null);
-
-
nameLabel = new JLabel("Queue #"+(iId+1)+":");
-
nameLabel.setBounds(0,0,100,20);
-
//create a scrolling area for processing area
-
scrolledContent = new JScrollPane(content);
-
//set just the horizontal scroll bar
-
scrolledContent.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
-
//set the prefferred size
-
scrolledContent.setPreferredSize(new Dimension(OutputForm.OUTPUT_FORM_WIDTH-110, QUEUE_VIEW_HEIGHT));
-
//set the minim size of scrooll pane
-
scrolledContent.setMinimumSize(new Dimension(10, 10));
-
//just text
-
//content.setContentType("text");
-
//it shouldn't be editable
-
content.setEditable(false);
-
scrolledContent.setBounds(100, 0, OutputForm.OUTPUT_FORM_WIDTH-100, QUEUE_VIEW_HEIGHT);
-
//scrolledContent.setVisible(true);
-
-
this.add(scrolledContent);
-
this.add(nameLabel);
-
}
-
-
public int getId(){
-
return iId;
-
}
-
-
public void setQueueInformation(String s[]){
-
String x="";
-
for(int i=0; i<s.length; i++){
-
x+=" "+s[i];
-
}
-
content.setText(x);
-
}
-
-
public boolean equals(int iId){
-
return this.iId==iId;
-
-
}
-
private int iId;
-
private final JScrollPane scrolledContent;
-
private final JLabel nameLabel;
-
private final JTextField content = new JTextField();
-
protected static final int QUEUE_VIEW_HEIGHT = 40;
-
private static final long serialVersionUID = -912479900798989604L;
-
}
-
package waitingQueues.view;
-
-
import java.awt.event.ActionListener;
-
-
import waitingQueues.WaitingQueuesException;
-
-
public class View implements ViewInterface {
-
public View() {
-
inputForm = new InputForm();
-
outputForm = new OutputForm();
-
// outputForm.setVisible(false);
-
}
-
-
public int getNumberOfQueues() throws WaitingQueuesException {
-
int n = 0;
-
if (inputForm != null) {
-
n = inputForm.getNumberOfQueues();
-
}
-
return n;
-
}
-
-
public int getNumberOfClients() throws WaitingQueuesException {
-
int n = 0;
-
if (inputForm != null) {
-
n = inputForm.getNumberOfClients();
-
}
-
return n;
-
}
-
-
public int getMinimumArrivalTime() throws WaitingQueuesException {
-
int n = 0;
-
if (inputForm != null) {
-
n = inputForm.getMinimumArrivalTime();
-
}
-
return n;
-
}
-
-
public int getMaximumArrivalTime() throws WaitingQueuesException {
-
int n = 0;
-
if (inputForm != null) {
-
n = inputForm.getMaximumArrivalTime();
-
}
-
return n;
-
}
-
-
public int getMinimumServiceTime() throws WaitingQueuesException {
-
int n = 0;
-
if (inputForm != null) {
-
n = inputForm.getMinimumServiceTime();
-
}
-
return n;
-
}
-
-
public int getMaximumServiceTime() throws WaitingQueuesException {
-
int n = 0;
-
if (inputForm != null) {
-
n = inputForm.getMaximumServiceTime();
-
}
-
return n;
-
}
-
-
public int getTimeForSimulation() throws WaitingQueuesException {
-
int n = 0;
-
if (inputForm != null) {
-
n = inputForm.getTimeForSimulation();
-
}
-
return n;
-
}
-
-
public void setSimulationTime(String time) {
-
if (inputForm != null) {
-
inputForm.setSimulationTime(time);
-
}
-
}
-
-
public void showMessage(String message, String title) {
-
if (inputForm != null) {
-
inputForm.showMessage(message, title);
-
}
-
}
-
-
public void showErrorMessage(String message) {
-
if (inputForm != null) {
-
inputForm.showErrorMessage(message);
-
}
-
}
-
-
public void enableNewSimulation(boolean value){
-
if (inputForm != null) {
-
inputForm.enableNewSimulation(value);
-
}
-
}
-
-
public void addQueue(int iId) throws WaitingQueuesException {
-
outputForm.addQueue(iId);
-
}
-
-
public void setQueueContent(int iId, String s[]) {
-
outputForm.setQueueContent(iId, s);
-
}
-
-
public void setVisibilityOfOutput(boolean isVisible) {
-
outputForm.setVisible(isVisible);
-
}
-
-
/**
-
* Add an action listener to the Simulate button
-
*
-
* @param e
-
*/
-
public void addSimulateBtnListener(ActionListener e) {
-
if (inputForm != null) {
-
inputForm.addSimulateBtnListener(e);
-
}
-
}
-
-
public void appendMessageToMessageQueue(String message) {
-
inputForm.addToMessageQueue(message);
-
}
-
-
public String toString() {
-
return inputForm.toString();
-
}
-
-
public void reset() {
-
outputForm.reset();
-
}
-
-
public void setAverageTime(String time) {
-
inputForm.setAverageTime(time);
-
}
-
-
private static InputForm inputForm;
-
private static OutputForm outputForm;
-
}
-
package waitingQueues.view;
-
-
import java.awt.event.ActionListener;
-
-
import waitingQueues.WaitingQueuesException;
-
-
public interface ViewInterface {
-
public int getNumberOfQueues() throws WaitingQueuesException;
-
-
public int getNumberOfClients() throws WaitingQueuesException;
-
-
public int getMinimumArrivalTime() throws WaitingQueuesException;
-
-
public int getMaximumArrivalTime() throws WaitingQueuesException;
-
-
public int getMinimumServiceTime() throws WaitingQueuesException;
-
-
public int getMaximumServiceTime() throws WaitingQueuesException;
-
-
public void showMessage(String message, String title);
-
-
public void showErrorMessage(String message);
-
-
public int getTimeForSimulation() throws WaitingQueuesException;
-
-
public void setSimulationTime(String time);
-
-
public void addQueue(int iId) throws WaitingQueuesException;
-
-
public void setQueueContent(int iId, String s[]);
-
-
public void appendMessageToMessageQueue(String message);
-
-
public void setVisibilityOfOutput(boolean isVisible);
-
-
public void reset();
-
-
public void setAverageTime(String time);
-
-
public void enableNewSimulation(boolean value);
-
-
/**
-
* Add an action listener to the Simulate button
-
*
-
* @param e
-
*/
-
public void addSimulateBtnListener(ActionListener e);
-
-
}
-
package waitingQueues.model;
-
-
//import waitingQueues.Constants;
-
/**
-
* Stores information about the person, such waiting time, arrival time and
-
* service time
-
*/
-
public class Person implements PersonInterface {
-
/**
-
* Default constructor
-
*
-
* @param iPersonId
-
* -id of person
-
*/
-
public Person(int iPersonId) {
-
personId = iPersonId;
-
}
-
-
/**
-
* Cosntructor
-
*
-
* @param iPersonId -
-
* person id
-
* @param iServiceTime -
-
* service time
-
* @param iArrivalTime -
-
* arrival time
-
*/
-
public Person(int iPersonId, int iServiceTime, int iArrivalTime) {
-
personId = iPersonId;
-
serviceTime = iServiceTime;
-
arrivalTime = iArrivalTime;
-
}
-
-
/**
-
* Returns the id of the person.
-
*/
-
public int getPersonId() {
-
return personId;
-
}
-
-
/*
-
* private void setServiceTime(int value) { // precondition if (value <
-
* Constants.MINIMUM_SERVICE_TIME || value > Constants.MAXIMUM_WAITING_TIME)
-
* throw new IllegalArgumentException("Value " + value + " violates the
-
* precondition"); serviceTime = value; // postcondition if (serviceTime !=
-
* value) new AssertionError("In Person, value " + value + " violates the
-
* postcondition"); }
-
*/
-
/**
-
* Increments the waiting time with the amount given by the parameter.
-
*/
-
public void incrementWaitingTime(int amount) {
-
waitingTime += amount;
-
}
-
-
/**
-
* Decrements the service time with the amount given by the parameter.
-
*/
-
public void decrementServiceTime(int amount) {
-
serviceTime -= amount;
-
}
-
-
/*
-
* private void setArrivalTime(int value) { // precondition if (value <
-
* Constants.MINIMUM_SERVICE_TIME || value > Constants.MAXIMUM_WAITING_TIME)
-
* throw new IllegalArgumentException("In Person, value " + value + "
-
* violates the precondition"); serviceTime = value; // postcondition if
-
* (serviceTime != value) new AssertionError("In Person, value " + value + "
-
* violates the postcondition"); }
-
*/
-
-
/**
-
* Returns the arrival time
-
*/
-
public int getArrivalTime() {
-
return arrivalTime;
-
}
-
-
/**
-
* Destroys the object.
-
*/
-
public void destroy() {
-
serviceTime = 0;
-
waitingTime = 0;
-
arrivalTime = 0;
-
}
-
-
/**
-
* It returns a string containing the parameters of the client.
-
*/
-
public String toString() {
-
String s = "";
-
s += "(" + personId + ")" + "[" + serviceTime + "]" + "{" + waitingTime
-
+ "}" + "#" + arrivalTime;
-
return s;
-
}
-
-
/**
-
* Returns true if the person has been served.
-
*/
-
public boolean personWasServed() {
-
return serviceTime == 0;
-
}
-
-
/**
-
* Compare two persons according to their arrival time.
-
*/
-
public int compareTo(Object obj) {
-
if (!(obj instanceof Person))
-
throw new AssertionError(
-
"Invalid object violates the precondition. Cannot compare objects of this type");
-
-
Person p = (Person) obj;
-
if (arrivalTime < p.arrivalTime)
-
return -1;
-
else if (arrivalTime > p.arrivalTime)
-
return 1;
-
else
-
return 0;
-
}
-
/**
-
* Returns a copy of the object.
-
*/
-
public Object clone() {
-
Object object = new Person(personId, serviceTime, arrivalTime);
-
((Person) object).waitingTime = waitingTime;
-
return object;
-
}
-
/**
-
* Returns the waiting time
-
* @return the waiting time
-
*/
-
public int getWaitingTime() {
-
return waitingTime;
-
}
-
/**
-
* Returns the service time
-
* @return the service time
-
*/
-
public int getServiceTime() {
-
return serviceTime;
-
}
-
-
private int serviceTime;
-
private int waitingTime;
-
private int arrivalTime;
-
private final int personId;
-
}
-
package waitingQueues.model;
-
-
public interface PersonInterface extends Comparable, Cloneable{
-
/**
-
* Destroys the object.
-
*/
-
public void destroy();
-
/**
-
* Returns the id of the person.
-
*/
-
public int getPersonId();
-
/**
-
* Increments the waiting time with the amount given by the parameter.
-
*/
-
public void incrementWaitingTime(int amount);
-
/**
-
* Decrements the service time with the amount given by the parameter.
-
*/
-
public void decrementServiceTime(int amount);
-
/**
-
* It returns a string containing the parameters of the client.
-
*/
-
public String toString();
-
/**
-
* Returns true if the person has been served.
-
*/
-
public boolean personWasServed();
-
/**
-
* Returns the waiting time
-
* @return the waiting time
-
*/
-
public int getWaitingTime();
-
/**
-
* Returns the service time
-
* @return the service time
-
*/
-
public int getServiceTime();
-
/**
-
* Returns the arrival time
-
*/
-
public int getArrivalTime();
-
/**
-
* Compare two persons according to their arrival time.
-
*/
-
public Object clone();
-
}
-
package waitingQueues.model;
-
-
import waitingQueues.Constants;
-
import waitingQueues.WaitingQueuesException;
-
-
/**
-
* The purpose of the class is to implement the QueueuADT and manage the
-
* operations on queues
-
*
-
* @author Marian Dan
-
*
-
*/
-
public class Queue implements QueueADT {
-
/**
-
* Default constructor
-
*/
-
public Queue() {
-
this.size = 0;
-
front = null;
-
rear = null;
-
}
-
-
/**
-
* Checks if the queue is full
-
*
-
* @return true ( if the queue is full)
-
* @invariant isProperFormed()==true
-
*/
-
public boolean isFull() {
-
return size == Constants.MAXIMUM_NUMBER_OF_QUEUES;
-
}
-
-
/**
-
* Checks if the queue is empty
-
*
-
* @return true ( if the queue is empty)
-
* @invariant isProperFormed()==true
-
*/
-
public boolean isEmpty() {
-
return size == 0;
-
}
-
-
/**
-
* Enqueues an object.
-
*
-
* @precondition o!=null
-
* @postcondition rear==o
-
* @invariant isProperFormed()==true
-
*/
-
public void enqueue(Object o) throws WaitingQueuesException {
-
assert (o != null) : "You whant to enter a null object in the Queue ?";
-
// precondition
-
if (isFull())
-
throw new WaitingQueuesException("The queue is full.");
-
-
size++;
-
-
Element element = new Element(o);
-
// TODO
-
// System.out.println("queue "+((Person)o).toString());
-
if (front == null) {
-
rear = element;
-
front = element;
-
} else {
-
rear.setNext(element);
-
rear = element;
-
}
-
// postcondition
-
if (rear != element)
-
throw new AssertionError(
-
"The value for the next element was not assigned");
-
-
}
-
-
/**
-
* Dequeues the front of returns it.
-
*
-
* @return the object that has been removed from the queue
-
* @precondition this.isEmpty()==false
-
* @postcondition size decreases with one
-
* @invariant isProperFormed()==true
-
*/
-
public Object dequeue() throws WaitingQueuesException {
-
// precondition
-
if (isEmpty())
-
throw new WaitingQueuesException("The queue is empty.");
-
if (front == null)
-
throw new AssertionError(
-
"For some reason the queue does not have a front element.");
-
Object o = front.getData();
-
size–;
-
front = front.getNext();
-
assert (o != null) : "Returning null object from dequeue";
-
return o;
-
-
}
-
-
/**
-
* Destroys the object.
-
*
-
* @invariant isProperFormed()==true
-
*/
-
public void destroy() {
-
try {
-
while (!isEmpty()) {
-
Object obj = dequeue();
-
if (obj instanceof Element) {
-
Element element = (Element) obj;
-
element.destroy();
-
element = null;
-
}
-
obj = null;
-
}
-
} catch (WaitingQueuesException e) {
-
System.out.println("queue " + e.getMessage());
-
}
-
size = 0;
-
front = null;
-
rear = null;
-
-
}
-
-
/**
-
* Returns a copy of the object.
-
*
-
* @postcondition object!=null
-
* @invariant isProperFormed()==true
-
*/
-
public Object clone() {
-
Object object = new Queue();
-
Queue queue = (Queue) object;
-
if (front != null) {
-
Element i = front;
-
while (i != null) {
-
Person person = (Person) i.getData();
-
try {
-
queue.enqueue(person.clone());
-
} catch (Exception e) {
-
e.printStackTrace();
-
}
-
i = i.getNext();
-
}
-
}
-
assert (object == null) : "Cloning null object";
-
return object;
-
}
-
-
/**
-
* Returns true if the element is proper formed
-
*
-
* @return true if the element is proper formed
-
*/
-
public boolean isProperFormed() {
-
/**
-
* if the queue has size 1 then it stores exactly one object that object
-
* is stored both in rear and front
-
*/
-
if (size == 1 && rear == front && rear != null)
-
return true;
-
/**
-
* if the queue has maximum size then it must be full
-
*/
-
if (size == Constants.MAXIMUM_NUMBER_OF_QUEUES && this.isFull())
-
return true;
-
/**
-
* if the queue has size 0 then it does not store anything and therefore
-
* it must be empty
-
*/
-
if (size == 0 && rear == null && front == null && this.isEmpty())
-
return true;
-
/**
-
* if the queue has its size between the boudaries then rear and front
-
* must be different than null
-
*/
-
if (size > 1 && size < Constants.MAXIMUM_NUMBER_OF_QUEUES
-
&& size > Constants.MINIMUM_NUMBER_OF_QUEUES && rear != null
-
&& front != null)
-
return true;
-
-
// in any other case return false
-
return false;
-
}
-
-
/*
-
* public String[] printQueueContent() { if (isEmpty()) { return null; }
-
* String array[] = new String[size]; int k = 0; if (front != null) {
-
* Element i = front; while (i != null) { array[k] = i.getData().toString() + " ";
-
* i = i.getNext(); // TODO System.out.println("queueu " + array[k]); k++; } }
-
* return array; }
-
*/
-
-
/**
-
*
-
* @invariant isProperFormed()==true
-
*/
-
private static int getSize(QueueADT queue) {
-
QueueADT q = (QueueADT) queue.clone();
-
-
if (q.isEmpty()) {
-
return 0;
-
}
-
-
int k = 0;
-
-
while (!q.isEmpty()) {
-
try {
-
q.dequeue();
-
k++;
-
} catch (WaitingQueuesException e) {
-
e.printStackTrace();
-
}
-
}
-
return k;
-
}
-
-
/**
-
* Returns the string representation of the elements of the queue
-
* @precondition queue!=null
-
* @postcondition return is not null
-
* @invariant isProperFormed()==true
-
*/
-
public static String[] printQueueContent(QueueADT queue) {
-
QueueADT q = (QueueADT) queue.clone();
-
-
if (q.isEmpty()) {
-
return null;
-
}
-
String array[] = new String[Queue.getSize(q)];
-
int k = 0;
-
-
while (!q.isEmpty()) {
-
try {
-
Object object = q.dequeue();
-
if (!(object instanceof Person))
-
throw new AssertionError(
-
"For some reason the queue returned an invalid object.");
-
-
Person i = (Person) object;
-
array[k] = i.toString() + " ";
-
//System.out.println("queueu " + array[k]);
-
k++;
-
} catch (WaitingQueuesException e) {
-
e.printStackTrace();
-
}
-
}
-
return array;
-
}
-
-
/**
-
* Manages the stored elements
-
*
-
*/
-
public class Element {
-
private Element() {
-
next = null;
-
data = null;
-
}
-
-
/**
-
* @precondition obj!=null
-
* @postcondition data!=null && next==null
-
* @param obj
-
*/
-
private Element(Object obj) {
-
assert (obj == null) : "Object to be stored is null";
-
next = null;
-
data = obj;
-
}
-
-
/**
-
* @precondition eElement!=null
-
* @postcondition next==null
-
* @invariant isProperFormed()==true
-
* @param eElement
-
*/
-
private void setNext(Element eElement) {
-
assert (eElement == null) : "Next element is null";
-
next = eElement;
-
assert (eElement != next) : "Assignment error";
-
}
-
-
/**
-
*
-
* @return
-
* @invariant isProperFormed()==true
-
*/
-
private Element getNext() {
-
return next;
-
}
-
-
/**
-
*
-
* @return
-
* @invariant isProperFormed()==true
-
*/
-
private Object getData() {
-
return data;
-
}
-
-
private void destroy() {
-
next = null;
-
data = null;
-
}
-
-
private boolean isProperFormed() {
-
if (data == null && next == null)
-
return true;
-
if (next != null && data != null)
-
return true;
-
return false;
-
}
-
-
private Element next = null;
-
private Object data;
-
}
-
-
/*
-
* public int getSize() { return size; }
-
*/
-
-
private int size;
-
private Element front;
-
private Element rear;
-
}
-
package waitingQueues.model;
-
-
import waitingQueues.WaitingQueuesException;
-
-
public interface QueueADT extends Cloneable{
-
/**
-
* Checks if the queue is full
-
*
-
* @return true ( if the queue is full)
-
*/
-
public boolean isFull();
-
/**
-
* Checks if the queue is empty
-
*
-
* @return true ( if the queue is empty)
-
*/
-
public boolean isEmpty();
-
/**
-
* Enqueues an object.
-
*/
-
public void enqueue(Object o) throws WaitingQueuesException;
-
/**
-
* Dequeues the front of returns it.
-
* @return the stored object
-
*/
-
public Object dequeue() throws WaitingQueuesException;
-
/**
-
* Destroys the object.
-
*/
-
public void destroy();
-
/**
-
* Returns a copy of the object.
-
*/
-
public Object clone();
-
}
-
package waitingQueues.model;
-
-
import java.util.ArrayList;
-
-
import waitingQueues.Constants;
-
import waitingQueues.WaitingQueuesException;
-
-
/**
-
* The purpose of the Model is to set the input parameters on which the
-
* simulation will run, to generate the persons and them queues, manages those
-
* and retrieve the queue content.
-
*
-
* @author Marian Dan
-
*
-
*/
-
public class Model implements ModelInterface {
-
/**
-
* Default constructor
-
*/
-
public Model() {
-
availableQueues = -1;
-
personArray = new ArrayList();
-
officeOpened = false;
-
message = "";
-
simulationTime = 0;
-
averageTime = 0.0;
-
}
-
-
/**
-
* It passes a message to the controller that will redirect it to the view.
-
* The message will be displayed in the description field in the InputForm.
-
*/
-
public String passMessage() {
-
String mess = message;
-
message = "";
-
return mess;
-
}
-
-
/**
-
* Returns a String representation of the simulation time.
-
*/
-
public String getSimulationTime() {
-
return "" + simulationTime;
-
-
}
-
-
/**
-
* It returns true if the office has been opened.
-
*
-
* @return true if the office has been opened, false otherwise
-
*/
-
public boolean hasTheOfficeOpened() {
-
// return availableQueues >= 0;
-
return officeOpened;
-
}
-
-
/**
-
* Returns an array of strings containing a representation of person’s
-
* characteristics.
-
*
-
* @return
-
*/
-
public String[] getQueueContent(int index) {
-
// precondition
-
if (array == null)
-
throw new AssertionError("For some reason the queue is invalid");
-
if (index < 0 || index >= array.length)
-
throw new IllegalArgumentException("Values " + index
-
+ " violate the precondition");
-
-
if (array[index] == null) {
-
String s[] = new String[1];
-
s[0] = "EMPTY";
-
return s;
-
} else {
-
// TODO
-
// System.out.println("model —–" + index);
-
return Queue.printQueueContent(array[index]);
-
}
-
}
-
-
/**
-
* It returns a string containing the parameters of the model such as the
-
* number of queues or clients.
-
*/
-
public String toString() {
-
String s = "";
-
s += "Clients: " + iNumberOfClients + "\n" + "Queues: "
-
+ iMaximumNumberOfQueues + "\n" + "Min Arrival Time: "
-
+ iMinimumArrivalTime + "\n" + "Max Arrival Time: "
-
+ iMaximumArrivalTime + "\n" + "Min Service Time: "
-
+ iMinimumServiceTime + "\n" + "Max Service Time: "
-
+ iMaximumServiceTime + "\n" + "Simulate for: "
-
+ "Simulation time: " + simulationTime + "\n";
-
return s;
-
-
}
-
-
/**
-
* Sets the parameters of the simulations.
-
*
-
* @param numberOfClients -
-
* clients
-
* @param maximumNumberOfQueues -
-
* queues
-
* @param minimumArrivalTime -
-
* minimum arrival time
-
* @param maximumArrivalTime -
-
* maximum arrival time
-
* @param minimumServiceTime -
-
* minimum service time
-
* @param maximumServiceTime -
-
* maximum service time
-
*/
-
public void setSimulationParameters(int numberOfClients,
-
int maximumNumberOfQueues, int minimumArrivalTime,
-
int maximumArrivalTime, int minimumServiceTime,
-
int maximumServiceTime) {
-
-
setNumberOfClients(numberOfClients);
-
setMaximumNumberOfQueues(maximumNumberOfQueues);
-
setServiceTimeLimits(minimumServiceTime, maximumServiceTime);
-
setArrivalTimeLimits(minimumArrivalTime, maximumArrivalTime);
-
array = new QueueADT[maximumNumberOfQueues];
-
queueFinishTime = new int[maximumNumberOfQueues];
-
-
for (int i = 0; i < maximumNumberOfQueues; i++) {
-
queueFinishTime[i] = 0;
-
}
-
for (int i = 0; i < numberOfClients; i++) {
-
PersonInterface p = createPerson(i);
-
personArray.add(p);
-
}
-
-
/*
-
* if (personArray != null) java.util.Collections.sort(personArray);
-
*/
-
}
-
-
/**
-
* It simulates the queue operations
-
*/
-
public void simulate() throws WaitingQueuesException {
-
-
++simulationTime;
-
-
if (simulationTime > 0) {
-
// compute waiting times
-
updateWaitingAndServingTime();
-
for (int i = 0; i < array.length; i++)
-
movingFromThisQueue(i);
-
}
-
-
// add arrived persons into the queue
-
-
if (personArray != null) {
-
boolean found;
-
do {
-
found = false;
-
for (int i = 0; i < personArray.size(); i++) {
-
Object obj = personArray.get(i);
-
if (!(obj instanceof PersonInterface))
-
throw new AssertionError(
-
"The array contains an invalid object, that is not a person");
-
-
PersonInterface p = (PersonInterface) obj;
-
if (p.getArrivalTime() == (simulationTime)) {
-
addPersonToQueue(p);
-
p.incrementWaitingTime(1);
-
totalTime++;
-
// TODO
-
// System.out.println("model *"+p.toString());
-
personArray.remove(i);
-
found = true;
-
break;
-
}
-
// System.out.println("model +"+" "+p.getArrivalTime()+"
-
// "+simulationTime+(p.getArrivalTime() == simulationTime));
-
}
-
} while (found);
-
}
-
-
}
-
-
/**
-
* Resets the model.
-
*/
-
public void reset() {
-
// addMessage("The model has been reset.");
-
if (array != null) {
-
for (int i = 0; i < array.length; i++) {
-
Object obj = array[i];
-
if (obj instanceof QueueADT) {
-
QueueADT q = (QueueADT) obj;
-
q.destroy();
-
q = null;
-
}
-
array[i] = null;
-
}
-
}
-
array = null;
-
if (personArray != null) {
-
for (int i = 0; i < personArray.size(); i++) {
-
Object obj = personArray.get(i);
-
if (obj instanceof QueueADT) {
-
PersonInterface q = (PersonInterface) obj;
-
q.destroy();
-
q = null;
-
}
-
// personArray.remove(i);
-
}
-
personArray.clear();
-
// personArray = new ArrayList();
-
}
-
availableQueues = -1;
-
officeOpened = false;
-
simulationTime = 0;
-
totalTime = 0;
-
nrPersons = 0;
-
}
-
-
/**
-
* Returns a string representation of the average time.
-
*/
-
public String getAverageTime() {
-
double displayTime = averageTime;
-
return "" + displayTime;
-
}
-
-
/**
-
* Returns true if all the persons were served.
-
*/
-
public boolean allPersonsServed() {
-
if (simulationTime <= iMaximumArrivalTime)
-
return false;
-
boolean ok = true;
-
for (int i = 0; i < array.length; i++)
-
if (!array[i].isEmpty())
-
ok = false;
-
return ok;
-
}
-
-
private void setServiceTimeLimits(int minimumServiceTime,
-
int maximumServiceTime) {
-
// precondition
-
if (minimumServiceTime < Constants.MINIMUM_SERVICE_TIME
-
|| minimumServiceTime > Constants.MAXIMUM_SERVICE_TIME
-
|| maximumServiceTime < Constants.MINIMUM_SERVICE_TIME
-
|| maximumServiceTime > Constants.MAXIMUM_SERVICE_TIME
-
|| minimumServiceTime > maximumServiceTime)
-
throw new IllegalArgumentException("Values " + minimumServiceTime
-
+ "," + maximumServiceTime + " violate the precondition");
-
iMinimumServiceTime = minimumServiceTime;
-
iMaximumServiceTime = maximumServiceTime;
-
// postcondition
-
if (minimumServiceTime != iMinimumServiceTime
-
|| maximumServiceTime != iMaximumServiceTime)
-
throw new AssertionError("Values " + minimumServiceTime + ","
-
+ maximumServiceTime + " violate the postcondition");
-
}
-
-
private void setArrivalTimeLimits(int minimumArrivalTime,
-
int maximumArrivalTime) {
-
// precondition
-
if (minimumArrivalTime < Constants.MINIMUM_ARRIVAL_TIME
-
|| minimumArrivalTime > Constants.MAXIMUM_ARRIVAL_TIME
-
|| maximumArrivalTime < Constants.MINIMUM_ARRIVAL_TIME
-
|| maximumArrivalTime > Constants.MAXIMUM_ARRIVAL_TIME
-
|| minimumArrivalTime > maximumArrivalTime)
-
throw new IllegalArgumentException("Values " + minimumArrivalTime
-
+ "," + maximumArrivalTime + " violate the precondition");
-
iMinimumArrivalTime = minimumArrivalTime;
-
iMaximumArrivalTime = maximumArrivalTime;
-
// postcondition
-
if (minimumArrivalTime != iMinimumArrivalTime
-
|| maximumArrivalTime != iMaximumArrivalTime)
-
throw new AssertionError("Values " + minimumArrivalTime + ","
-
+ maximumArrivalTime + " violate the postcondition");
-
}
-
-
private void setNumberOfClients(int numberOfClients) {
-
// precondition
-
if (numberOfClients < Constants.MINIMUM_NUMBER_OF_CLIENTS
-
|| numberOfClients > Constants.MAXIMUM_NUMBER_OF_CLIENTS)
-
throw new IllegalArgumentException("Values " + numberOfClients
-
+ " violate the precondition");
-
-
iNumberOfClients = numberOfClients;
-
// postcondition
-
if (numberOfClients != iNumberOfClients)
-
throw new AssertionError("Values " + iNumberOfClients + ","
-
+ numberOfClients + " violate the postcondition");
-
}
-
-
private void setMaximumNumberOfQueues(int maximumNumberOfQueues) {
-
// precondition
-
if (maximumNumberOfQueues < Constants.MINIMUM_NUMBER_OF_QUEUES
-
|| maximumNumberOfQueues > Constants.MAXIMUM_NUMBER_OF_QUEUES)
-
throw new IllegalArgumentException("Values "
-
+ maximumNumberOfQueues + " violate the precondition");
-
-
iMaximumNumberOfQueues = maximumNumberOfQueues;
-
-
// postcondition
-
if (maximumNumberOfQueues != iMaximumNumberOfQueues)
-
throw new AssertionError("Values " + iNumberOfClients + ","
-
+ maximumNumberOfQueues + " violate the postcondition");
-
}
-
-
/*
-
* private void openNewQueue() { if (availableQueues + 1 >
-
* iMaximumNumberOfQueues) throw new AssertionError( "Maximum number of
-
* queues was reached. No other queues can be openned."); QueueADT queue =
-
* new Queue(); int openedQueue = -1; for (int i = 0; i < availableQueues;
-
* i++) if (array[i] == null) openedQueue = i; if (openedQueue == -1)
-
* openedQueue = ++availableQueues; array[openedQueue] = queue;
-
* addMessage("Opened a new customer queue, the queue number # " +
-
* openedQueue); }
-
*/
-
-
private void openTheOffice() {
-
if (hasTheOfficeOpened())
-
throw new AssertionError("The office has been already openned.");
-
if (!hasTheOfficeOpened()) {
-
// openNewQueue();
-
officeOpened = true;
-
// addMessage("The office has been open");
-
}
-
}
-
-
private void addPersonToQueue(PersonInterface person)
-
throws WaitingQueuesException {
-
// check person
-
if (!hasTheOfficeOpened()) {
-
openTheOffice();
-
}
-
int n;
-
n = optimization(person);
-
if (n == -1)
-
throw new AssertionError("Invalid queue functionning");
-
-
addMessage("Person " + person.getPersonId() + " entered queue #" + n);
-
array[n].enqueue(person);
-
nrPersons++;
-
}
-
-
private void movingFromThisQueue(int queueId) throws WaitingQueuesException {
-
-
if (array[queueId] != null) {
-
Object temp = ((QueueADT) array[queueId]).clone();
-
Object old = ((QueueADT) array[queueId]).clone();
-
-
QueueADT q = (QueueADT) temp;
-
QueueADT temp2 = new Queue();
-
-
PersonInterface person;
-
Object obj = null;
-
if (q.isEmpty()) {
-
return;
-
} else {
-
while (!q.isEmpty()) {
-
try {
-
obj = q.dequeue();
-
if (!q.isEmpty())
-
temp2.enqueue(obj);
-
} catch (WaitingQueuesException e) {
-
e.printStackTrace();
-
}
-
}
-
}
-
if (obj != null) {
-
assert (obj instanceof PersonInterface) : "cast error in model";
-
person = (PersonInterface) obj;
-
array[queueId] = temp2;
-
int n = optimization(person);
-
if (n != queueId) {
-
addMessage("Person " + person.getPersonId()
-
+ "abandonned queue " + queueId + " and entered "
-
+ n);
-
array[n].enqueue(person);
-
} else
-
array[queueId] = (QueueADT) old;
-
-
}
-
}
-
return;
-
-
}
-
-
private int optimization(PersonInterface person) {
-
for (int i = 0; i < array.length; i++)
-
if (array[i] == null)
-
array[i] = new Queue();
-
int min = computeQueueFinishTime(array[0]);
-
int index_min = 0;
-
if (min != 0)
-
for (int i = 1; i < array.length; i++) {
-
int size = computeQueueFinishTime(array[i]);
-
if (size == 0) {
-
min = 0;
-
index_min = i;
-
break;
-
} else if (min > size) {
-
min = size;
-
index_min = i;
-
}
-
}
-
return index_min;
-
}
-
-
private static int computeQueueFinishTime(QueueADT queue) {
-
if (queue == null)
-
return 0;
-
-
QueueADT q = (QueueADT) queue.clone();
-
-
if (q.isEmpty()) {
-
return 0;
-
}
-
-
int n = 0;
-
while (!q.isEmpty()) {
-
try {
-
Object object = q.dequeue();
-
if (!(object instanceof Person))
-
throw new AssertionError(
-
"For some reason the queue returned an invalid object.");
-
Person i = (Person) object;
-
n += i.getServiceTime();
-
n++;
-
} catch (WaitingQueuesException e) {
-
e.printStackTrace();
-
}
-
}
-
if (n == 0) {
-
throw new AssertionError("The queue should not be void");
-
}
-
return n;
-
}
-
-
private static void updateQueueWaitingTime(QueueADT queue) {
-
if (queue == null)
-
return;
-
QueueADT q = queue;
-
QueueADT temp = new Queue();
-
if (q.isEmpty()) {
-
return;
-
}
-
-
while (!q.isEmpty()) {
-
try {
-
Object object = q.dequeue();
-
if (!(object instanceof Person))
-
throw new AssertionError(
-
"For some reason the queue returned an invalid object.");
-
PersonInterface i = (PersonInterface) object;
-
i.incrementWaitingTime(1);
-
totalTime++;
-
temp.enqueue(i);
-
} catch (WaitingQueuesException e) {
-
e.printStackTrace();
-
}
-
}
-
-
while (!temp.isEmpty()) {
-
try {
-
Object object = temp.dequeue();
-
q.enqueue(object);
-
} catch (WaitingQueuesException e) {
-
e.printStackTrace();
-
}
-
}
-
-
temp.destroy();
-
temp = null;
-
-
}
-
-
private static void updateQueueServiceTime(QueueADT queue, int queueId) {
-
if (queue == null)
-
return;
-
QueueADT q = queue;
-
QueueADT temp = new Queue();
-
if (q.isEmpty()) {
-
return;
-
}
-
-
if (!q.isEmpty()) {
-
try {
-
// get the first entry
-
Object object = q.dequeue();
-
if (!(object instanceof Person))
-
throw new AssertionError(
-
"For some reason the queue returned an invalid object.");
-
Person i = (Person) object;
-
i.decrementServiceTime(1);
-
if (i.personWasServed()) {
-
addMessage("Person " + i.getPersonId()
-
+ " was served and exited queue #" + queueId);
-
i.destroy();
-
i = null;
-
} else
-
temp.enqueue(i);
-
} catch (WaitingQueuesException e) {
-
e.printStackTrace();
-
}
-
}
-
-
while (!q.isEmpty()) {
-
try {
-
Object object = q.dequeue();
-
temp.enqueue(object);
-
} catch (WaitingQueuesException e) {
-
e.printStackTrace();
-
}
-
}
-
-
while (!temp.isEmpty()) {
-
try {
-
Object object = temp.dequeue();
-
q.enqueue(object);
-
} catch (WaitingQueuesException e) {
-
e.printStackTrace();
-
}
-
}
-
temp.destroy();
-
temp = null;
-
}
-
-
private void updateWaitingAndServingTime() {
-
for (int i = 0; i < array.length; i++)
-
if (array[i] != null) {
-
updateQueueServiceTime(array[i], i);
-
updateQueueWaitingTime(array[i]);
-
queueFinishTime[i] = computeQueueFinishTime(array[i]);
-
}
-
// System.out.println(" model …. " + totalTime + " " + nrPersons);
-
this.averageTime = totalTime / (1.0 * nrPersons);
-
}
-
-
/*
-
* public int availableOffices() { return availableQueues; }
-
*/
-
-
private static void addMessage(String sMessage) {
-
message += "\n" + sMessage;
-
}
-
-
private PersonInterface createPerson(int id) {
-
int service = randomNumber(iMinimumServiceTime, iMaximumServiceTime);
-
int arrival = randomNumber(iMinimumArrivalTime, iMaximumArrivalTime);
-
PersonInterface p = new Person(id, service, arrival);
-
// TODO
-
//System.out.println(p.toString());
-
return p;
-
}
-
-
private static int randomNumber(int minimumValue, int maximumValue) {
-
if (minimumValue < 0 || maximumValue < 0)
-
throw new IllegalArgumentException("Values " + minimumValue + ","
-
+ maximumValue + " violates the precondition");
-
-
int n;
-
do {
-
n = minimumValue + (int) (Math.random() * maximumValue);
-
} while (n < minimumValue || n > maximumValue);
-
-
if (!(n >= minimumValue || n <= maximumValue))
-
throw new AssertionError("Invalid random number " + n
-
+ " was generated.");
-
return n;
-
-
}
-
-
// private static ArrayList array = null;
-
private static QueueADT[] array;
-
private int queueFinishTime[];
-
private int availableQueues;
-
private int iNumberOfClients;
-
private int iMaximumNumberOfQueues;
-
private int iMinimumArrivalTime;
-
private int iMaximumArrivalTime;
-
private int iMinimumServiceTime;
-
private int iMaximumServiceTime;
-
private static String message;
-
private boolean officeOpened;
-
private int simulationTime;
-
private ArrayList personArray;
-
-
private double averageTime;
-
private static int nrPersons = 0;
-
private static int totalTime = 0;
-
-
}
-
package waitingQueues.model;
-
-
import waitingQueues.WaitingQueuesException;
-
/**
-
* Interface for the Model
-
* @author Marian Dan
-
*
-
*/
-
public interface ModelInterface {
-
//public int availableOffices();
-
-
/**
-
* It passes a message to the controller that will redirect it to the view.
-
* The message will be displayed in the description field in the InputForm.
-
*/
-
public String passMessage();
-
/**
-
* Sets the parameters of the simulations.
-
*
-
* @param numberOfClients -
-
* clients
-
* @param maximumNumberOfQueues -
-
* queues
-
* @param minimumArrivalTime -
-
* minimum arrival time
-
* @param maximumArrivalTime -
-
* maximum arrival time
-
* @param minimumServiceTime -
-
* minimum service time
-
* @param maximumServiceTime -
-
* maximum service time
-
*/
-
public void setSimulationParameters(int numberOfClients,
-
int maximumNumberOfQueues, int minimumArrivalTime,
-
int maximumArrivalTime, int minimumServiceTime,
-
int maximumServiceTime);
-
/**
-
* It simulates the queue operations
-
*/
-
public void simulate() throws WaitingQueuesException;
-
/**
-
* Resets the model.
-
*/
-
public void reset();
-
/**
-
* Returns a String representation of the simulation time.
-
*/
-
public String getSimulationTime();
-
/**
-
* Returns an array of strings containing a representation of person’s
-
* characteristics.
-
*
-
* @return
-
*/
-
public String[] getQueueContent(int index);
-
/**
-
* Returns a string representation of the average time.
-
*/
-
public String getAverageTime();
-
/**
-
* Returns true if all the persons were served.
-
*/
-
public boolean allPersonsServed();
-
}
-
package waitingQueues.controller;
-
-
import java.awt.event.ActionEvent;
-
import java.awt.event.ActionListener;
-
import waitingQueues.Constants;
-
import waitingQueues.WaitingQueuesException;
-
import waitingQueues.model.ModelInterface;
-
import waitingQueues.view.ViewInterface;
-
-
import java.util.Timer;
-
import java.util.TimerTask;
-
-
/**
-
*
-
* The purpose of the Controller class is to address / handle de interactions
-
* between the Model and the View. The Controller handles all the events that
-
* may arise during the program execution, such as the user clicking on a
-
* button, and triggers changes in the Model. In this way all the three
-
* components the Controller, Model and View are interconnected.
-
*
-
* @author Marian Dan
-
*
-
*/
-
public class Controller implements ControllerInterface {
-
/**
-
* Default constructor (no arguments) – should be avoided
-
*
-
*/
-
public Controller() {
-
this(null, null);
-
}
-
-
/**
-
* Constructor for the controller class It manages the interaction between
-
* the user, View and Model
-
*
-
* @param view
-
* @param model
-
* @pre view !=null && model!=null
-
* @post this.view!=null && this.model!=null
-
*/
-
public Controller(ViewInterface view, ModelInterface model) {
-
assert (view != null) : "view is null";
-
assert (model != null) : "model is null";
-
-
this.view = view;
-
this.model = model;
-
// assign the button listener
-
view.addSimulateBtnListener(new SimulateListener());
-
}
-
-
/**
-
* The class manages the events that happen when the user presses on the
-
* button
-
*
-
*/
-
private final class SimulateListener implements ActionListener {
-
private String oldParameters = "";
-
-
/**
-
* @pre e!=null
-
*/
-
public final void actionPerformed(ActionEvent e) {
-
try {
-
assert (e != null) : "Null reference in SimulateListener";
-
// /System.out.println("controller simulate \n" +
-
// view.toString());
-
int clients = view.getNumberOfClients();
-
int queues = view.getNumberOfQueues();
-
int minArrivalTime = view.getMinimumArrivalTime();
-
int maxArrivalTime = view.getMaximumArrivalTime();
-
int minServiceTime = view.getMinimumServiceTime();
-
int maxServiceTime = view.getMaximumServiceTime();
-
int simulateForTime = view.getTimeForSimulation();
-
// check if the parameters have changed
-
String currentParameters = "" + clients + queues
-
+ minArrivalTime + maxArrivalTime + minServiceTime
-
+ maxServiceTime;
-
if (clients < 1 || queues < 1 || minArrivalTime < 1
-
|| maxArrivalTime < 1 || minServiceTime < 1
-
|| maxServiceTime < 1 || simulateForTime < 1)
-
throw new WaitingQueuesException(
-
"Parameters are integer numbers greather or equal to one");
-
if (minArrivalTime > maxArrivalTime
-
|| minServiceTime > maxServiceTime)
-
throw new WaitingQueuesException(
-
"The maximum time must be at least equal to the \nminimum time, and not less as you've entered.");
-
-
if (maxArrivalTime > Constants.MAXIMUM_ARRIVAL_TIME)
-
throw new WaitingQueuesException(
-
"The maximum arrival time must be at most "
-
+ Constants.MAXIMUM_ARRIVAL_TIME);
-
if (maxServiceTime > Constants.MAXIMUM_SERVICE_TIME)
-
throw new WaitingQueuesException(
-
"The maximum service time must be at most "
-
+ Constants.MAXIMUM_SERVICE_TIME);
-
-
if (minArrivalTime < Constants.MINIMUM_ARRIVAL_TIME)
-
throw new WaitingQueuesException(
-
"The minimum arrival time must be at least "
-
+ Constants.MINIMUM_ARRIVAL_TIME);
-
if (minServiceTime < Constants.MINIMUM_SERVICE_TIME)
-
throw new WaitingQueuesException(
-
"The minimum service time must be at least "
-
+ Constants.MINIMUM_SERVICE_TIME);
-
-
if (currentParameters.compareTo(oldParameters) != 0
-
|| model.allPersonsServed()) {
-
oldParameters = currentParameters;
-
-
view.reset();
-
model.reset();
-
-
model.setSimulationParameters(clients, queues,
-
minArrivalTime, maxArrivalTime, minServiceTime,
-
maxServiceTime);
-
-
for (int i = 0; i < view.getNumberOfQueues(); i++) {
-
view.addQueue(i);
-
String s[] = { "EMPTY" };
-
view.setQueueContent(i, s);
-
}
-
view.setVisibilityOfOutput(true);
-
view.setSimulationTime(model.getSimulationTime());
-
view.enableNewSimulation(false);
-
startSimulation(simulateForTime);
-
// view.enableNewSimulation(true);
-
} else {
-
// simulateModel();
-
view.enableNewSimulation(false);
-
startSimulation(simulateForTime);
-
// view.enableNewSimulation(true);
-
}
-
-
} catch (WaitingQueuesException er) {
-
view.showErrorMessage(er.getMessage());
-
}
-
return;
-
}
-
}
-
-
/**
-
* The method initializes the model and runs the simulation on it
-
*
-
* @throws WaitingQueuesException
-
*/
-
private void simulateModel() throws WaitingQueuesException {
-
model.simulate();
-
String empty[] = { "EMPTY" };
-
for (int i = 0; i < view.getNumberOfQueues(); i++) {
-
String s[] = model.getQueueContent(i);
-
if (s != null) {
-
view.setQueueContent(i, s);
-
} else
-
view.setQueueContent(i, empty);
-
view.setAverageTime(model.getAverageTime());
-
}
-
view.setSimulationTime(model.getSimulationTime());
-
view.appendMessageToMessageQueue(model.passMessage());
-
}
-
-
/**
-
* It starts the simulation
-
*
-
* @param simulationTimeAmount -
-
* the amount of time for which which the simulation is run
-
*/
-
private void startSimulation(int simulationTimeAmount) {
-
timer = new Timer();
-
remainingSimulationTime = simulationTimeAmount;
-
timer.schedule(new Task(), 1000);
-
-
}
-
-
/**
-
* The class is used to creat a timer task (that uses a thread that executes
-
* the simulation at specific time intervals)
-
*/
-
private class Task extends TimerTask {
-
public void run() {
-
try {
-
if (remainingSimulationTime > 0 && !model.allPersonsServed()) {
-
// do smth
-
remainingSimulationTime–;
-
simulateModel();
-
timer.schedule(new Task(), 1000);
-
} else {
-
timer.cancel(); // Terminate the thread
-
view.enableNewSimulation(true);
-
if (model.allPersonsServed())
-
view.showMessage(model.getAverageTime(),
-
"Final average time is:");
-
}
-
} catch (WaitingQueuesException er) {
-
view.showErrorMessage(er.getMessage());
-
timer.cancel();
-
timer = null;
-
view.enableNewSimulation(true);
-
}
-
}
-
}
-
-
/*
-
* private class ToDo { Timer timer; int seconds;
-
*
-
* public ToDo(int seconds) { timer = new Timer(); this.seconds = seconds;
-
* timer.schedule(new ToDoTask(), seconds * 1000); }
-
*
-
* class ToDoTask extends TimerTask { public void run() {
-
* System.out.println("controller OK, It's time to do something!"); if
-
* (remainingSimulationTime > 0) { // do smth remainingSimulationTime–;
-
* System.out.println("controller " + remainingSimulationTime);
-
* timer.schedule(new ToDoTask(), seconds * 1000); } else timer.cancel(); //
-
* Terminate the thread } } }
-
*/
-
-
private int remainingSimulationTime = 0;
-
private ViewInterface view;
-
private ModelInterface model;
-
private Timer timer;
-
}
-
package waitingQueues.controller;
-
-
/**
-
* The Controller interface is a dump file
-
* @author Marian Dan
-
*
-
*/
-
public interface ControllerInterface {
-
-
}
Memory architecture simulation (Java homework)
Posted by marian
on June 29, 2009
The homework was structured as follows:
memory
|-controller
|Â Â Â |-Controller.java
|Â Â Â |-ControllerInterface.java
|-model
|-view
|-Main.java
|-MemoryException
Main.java
-
package memory;
-
-
import javax.swing.JFrame;
-
-
import memory.Main;
-
import memory.controller.Controller;
-
import memory.controller.ControllerInterface;
-
import memory.model.Model;
-
import memory.model.ModelInterface;
-
import memory.view.View;
-
import memory.view.ViewInterface;
-
-
public class Main {
-
/*
-
* The default constructor on the Main class.
-
*/
-
public Main(){
-
// let decorations be available
-
JFrame.setDefaultLookAndFeelDecorated(true);
-
// Create and set up the window.
-
-
//create an instance of the user interface/ the view
-
final ViewInterface ui = new View();
-
//create an instance of the model we are working on
-
final ModelInterface model=new Model();
-
//create an instance of the controller that manages the
-
//interactions between the model and the
-
final ControllerInterface controller=new Controller(ui,model);
-
}
-
//the application is designed to run both as an applet or as an
-
//application
-
public void init(){
-
-
}
-
public void start(){
-
-
}
-
public void stop(){
-
-
}
-
public void destroy(){
-
-
}
-
//for running as an application
-
public static void main(String args[]) {
-
final Main application = new Main();
-
}
-
-
/**
-
* The standard public String toString() is overridden
-
*/
-
public String toString(){
-
return "This is the main application";
-
}
-
/**
-
* Check to see if this is the instance of the main application
-
*/
-
public boolean equals(Object other) {
-
if(this==other) return true;
-
else return false;
-
}
-
-
private static final long serialVersionUID = 2028805364097710940L;
-
-
}
-
package memory;
-
-
/**
-
* The purpose of this class is to provide means of treating exceptions,
-
* that arise when the program is running. The class FlipFlopException
-
* inherits the methods of the default Java class Exception.
-
*/
-
public class MemoryException extends Exception{
-
/**
-
* Default constructor
-
* @param String
-
*/
-
public MemoryException(String mess){
-
message=mess;
-
//System.out.println("Exception: "+message);
-
}
-
/**
-
* This method overrides the default toString() method
-
*
-
*/
-
public String toString()
-
{
-
return message;
-
}
-
/**
-
* Check to see if this is the instance of the View
-
*/
-
public boolean equals(Object other) {
-
if(other instanceof MemoryException){
-
MemoryException m=(MemoryException)other;
-
if(m.toString().equals(message)) return true;
-
else return false;
-
-
}else return false;
-
}
-
private static final long serialVersionUID = 4593665563049627269L;
-
//stores the error message
-
private String message="";
-
}
-
package memory.controller;
-
-
import java.awt.event.ActionEvent;
-
import java.awt.event.ActionListener;
-
import java.awt.event.MouseEvent;
-
import java.util.StringTokenizer;
-
import javax.swing.JComboBox;
-
-
import memory.MemoryException;
-
import memory.model.ModelInterface;
-
import memory.view.MemoryShape;
-
import memory.view.Shape;
-
import memory.view.ShapeMouseInterface;
-
import memory.view.SignalShape;
-
import memory.view.ViewInterface;
-
import memory.view.WorkingPanel;
-
-
public final class Controller implements ControllerInterface {
-
/**
-
* Default constructor (no arguments) – should be avoided
-
*
-
*/
-
public Controller() {
-
this(null, null);
-
}
-
-
public Controller(ViewInterface view, ModelInterface model) {
-
this.view = view;
-
this.model = model;
-
view.addWriteBtnListener(new WriteListener());
-
view.addMemoryBtnListener(new MemoryListener());
-
view.addReturnBtnListener(new ReturnListener());
-
view.addComboBoxListener(new ChoiceComboBoxListener());
-
view.addWorkingPanelMouseListener(new WorkingPanelMouseListener());
-
-
}
-
-
private static final class WriteListener implements ActionListener {
-
public final void actionPerformed(ActionEvent e) {
-
//System.out.println("write");
-
reprogramming = true;
-
return;
-
}
-
}
-
-
private static final class MemoryListener implements ActionListener {
-
public final void actionPerformed(ActionEvent e) {
-
//System.out.println("memory");
-
view.setInputPanel();
-
return;
-
}
-
}
-
-
private static final class ReturnListener implements ActionListener {
-
public final void actionPerformed(ActionEvent e) {
-
int type = 0;
-
int depth = 0;
-
int length = 0;
-
//System.out.println("return");
-
String name = "";
-
if(shapeToBeProgrammed==null) reprogramming=false;
-
try {
-
if (!reprogramming) {
-
System.out.println("not programming");
-
name = view.getMemoryName();
-
if (name == null)
-
throw new MemoryException("Please enter memory name");
-
if (name.length() == 0)
-
throw new MemoryException("Please enter memory name");
-
if (temporaryChoice == null) {
-
throw new MemoryException(
-
"Please chose a value for the memory type.");
-
}
-
if (temporaryChoice.length() == 0) {
-
throw new MemoryException(
-
"Please chose a value for the memory type.");
-
}
-
type = 0;
-
if (temporaryChoice == "PROM")
-
type = 1;
-
else if (temporaryChoice == "EEPROM")
-
type = 2;
-
else if (temporaryChoice == "UVPROM")
-
type = 3;
-
else if (temporaryChoice == "RAM")
-
type = 4;
-
if (type == 0)
-
throw new MemoryException("Invalid choice");
-
try {
-
length = Integer.parseInt(view.getWordSize());
-
} catch (NumberFormatException ex) {
-
throw new MemoryException("The size is not valid");
-
}
-
try {
-
depth = Integer.parseInt(view.getMemoryDepth());
-
} catch (NumberFormatException ex) {
-
throw new MemoryException(
-
"The depth of memory is not valid");
-
}
-
};
-
if(reprogramming){
-
int mid = shapeToBeProgrammed.getId();
-
-
depth=model.getMemoryDepth(mid);
-
length=model.getMemoryWdth(mid);
-
-
}
-
String s = view.getInitialData();
-
int[][] a = new int[1 << depth][length];
-
StringTokenizer pass1 = new StringTokenizer(s);
-
String s2 = "";
-
while (pass1.hasMoreElements())
-
s2 += pass1.nextElement();
-
StringTokenizer terms = new StringTokenizer(s2, "10", true);
-
-
for (int i = 0; i < 1 << depth; i++)
-
for (int j = 0; j < length; j++)
-
if (!terms.hasMoreTokens())
-
throw new MemoryException(
-
"Not all portions of the memory are initialized.\nPlease try again");
-
else {
-
try {
-
String x = terms.nextToken();
-
-
a[i][j] = Integer.parseInt(x);
-
if (a[i][j] != 0 && a[i][j] != 1)
-
throw new MemoryException(
-
"Please enter a sequence of 1's and 0's.");
-
-
} catch (NumberFormatException ex) {
-
throw new MemoryException(
-
"The initial data contains invalid characters.");
-
}
-
}
-
view.unsetInputPanel();
-
view.resetInput();
-
if (!reprogramming) {
-
int id = model.addMemory(name, depth, length, type, a);
-
view.addMemoryBox(model.getName(id), id, depth, length);
-
temporaryChoice = "";
-
} else {
-
//System.out.println("reprogramming");
-
int mid = shapeToBeProgrammed.getId();
-
//System.out.println("reprogramming memory with");
-
/*for(int i=0; i<a.length; i++)
-
{
-
for(int j=0; j<a[i].length; j++)
-
System.out.println(a[i][j]);
-
}*/
-
-
model.reprogram(mid, a);
-
reprogramming = false;
-
}
-
} catch (MemoryException exception) {
-
view.showErrorMessage(exception.toString());
-
}finally{
-
shapeToBeProgrammed=null;
-
reprogramming = false;
-
}
-
return;
-
}
-
}
-
-
private static final class ChoiceComboBoxListener implements ActionListener {
-
public final void actionPerformed(ActionEvent e) {
-
JComboBox cb = (JComboBox) e.getSource();
-
String choice = (String) cb.getSelectedItem();
-
temporaryChoice = choice;
-
//System.out.println(choice);
-
return;
-
}
-
}
-
-
private final static class WorkingPanelMouseListener implements
-
ShapeMouseInterface {
-
public WorkingPanelMouseListener() {
-
panel = view.getWorkingPanel();
-
}
-
-
public final void mousePressed(MouseEvent evt) {
-
// User has pressed the mouse. Find the shape that the user has
-
// clicked on. If there is a shape at the position when the mouse
-
// was clicked, then start dragging it.
-
// panel=view.getWorkingPanel();
-
int x = evt.getX();
-
int y = evt.getY();
-
for (int i = 0; i < panel.getVectorSize(); i++) {
-
// check shapes from back to front (from the first to the last)
-
Object obj = panel.getVectorElement(i);
-
// if this a memory shape
-
if (obj instanceof MemoryShape) {
-
MemoryShape s = (MemoryShape) obj;
-
if (s.containsPoint(x, y)) {
-
shapeBeingDragged = s;
-
shapeToBeProgrammed=s;
-
// get the coordinates
-
//System.out.println("Default is : "
-
// + shapeBeingDragged.getId());
-
-
oldX = x;
-
oldY = y;
-
-
if (reprogramming) {
-
view.writeToMemory();
-
}
-
-
} else {
-
// it is not a memory, see if it is in the signals
-
int n = s.getInputSignalVectorSize();
-
//System.out.println("n= " + n);
-
// detrmine the input
-
for (int j = 0; j < n; j++) {
-
Object o = s.getInputSignalVectorElement(j);
-
// if this is a signal shape
-
-
if (o instanceof SignalShape) {
-
// cast
-
SignalShape sig = (SignalShape) o;
-
// is the pointer inside the shape
-
if (sig.containsPoint(x, y)) {
-
String inputText = view
-
.inputDialog("Please enter the signal value");
-
-
int signal = 0;
-
// do {
-
try {
-
try {
-
signal = Integer
-
.parseInt(inputText);
-
-
} catch (NumberFormatException ex) {
-
throw new MemoryException(
-
"The initial data contains invalid characters.\nPlease enter only 0 or 1 for signal value");
-
}
-
if (signal == 0 || signal == 1) {
-
// get id of the memory
-
int mid = s.getId();
-
sig.setLabel("\n" + signal);
-
// get id of signal
-
int sid = sig.getId();
-
-
model.setInputSignal(mid, sid,
-
signal);
-
String soutput[] = model
-
.getOutputSignal(mid);
-
//System.out.println("output");
-
for (int k = 0; k < soutput.length; k++) {
-
Object o2 = s
-
.getOutputSignalVectorElement(k);
-
if (o2 instanceof SignalShape) {
-
SignalShape sigout = (SignalShape) o2;
-
sigout.setLabel("\n"
-
+ soutput[k]);
-
}
-
//System.out.print(soutput[k]);
-
}
-
//System.out.println("<<>> " + mid
-
// + " " + sid + " " + signal);
-
panel.repaint();
-
break;
-
} else
-
throw new MemoryException(
-
"Please enter only 0 or 1.");
-
} catch (MemoryException exception) {
-
view.showErrorMessage(exception
-
.toString());
-
}
-
// } while (signal != 1 && signal != 0);
-
}
-
}
-
}
-
}
-
}
-
}
-
//System.out.println("salut");
-
panel.repaint();
-
}
-
-
public final void mouseDragged(MouseEvent evt) {
-
// the mouse is moving =>move the dragged shape
-
// get the mouse coordinates
-
int x = evt.getX();
-
int y = evt.getY();
-
-
if (shapeBeingDragged != null) {
-
shapeBeingDragged.moveBy(x – oldX, y – oldY);
-
// set the old coordinates
-
oldX = x;
-
oldY = y;
-
}
-
// System.out.println("mouse moved "+x+" "+y+"\n");
-
panel.repaint();
-
}
-
-
public final void mouseReleased(MouseEvent evt) {
-
shapeBeingDragged = null;
-
panel.repaint();
-
}
-
-
public final void mouseMoved(MouseEvent evt) {
-
-
}
-
-
public final void mouseClicked(MouseEvent evt) {
-
-
}
-
-
public final void mouseEntered(MouseEvent evt) {
-
-
}
-
-
public final void mouseExited(MouseEvent evt) {
-
-
}
-
/**
-
* The shape that is currently being dragged non-null value : dragging is in
-
* progress
-
*/
-
private Shape shapeBeingDragged = null;
-
-
/**
-
* The old X coordinate of the mouse
-
*/
-
private int oldX;
-
-
/**
-
* The old Y coordinate of the mouse
-
*/
-
private int oldY;
-
private WorkingPanel panel;
-
}
-
/**
-
* The standard public String toString() is overridden
-
*/
-
public String toString(){
-
return "The controller";
-
}
-
/**
-
* Check to see if this is the instance of the controller
-
*/
-
public boolean equals(Object other) {
-
if(this==other) return true;
-
else return false;
-
}
-
-
private static ViewInterface view;
-
private static ModelInterface model;
-
private static String temporaryChoice;
-
private static boolean reprogramming = false;
-
private static Shape shapeToBeProgrammed= null;
-
/**
-
* The mouse listener for the graphicPanel (controls the motion of states,
-
* transitions, guides)
-
*/
-
// private ShapeMouseInterface graphicPanelMouseHandler;
-
-
}
-
package memory.controller;
-
-
public interface ControllerInterface {
-
-
}
-
package memory.view;
-
-
-
-
public class Point {
-
/**
-
* Default constructor
-
*/
-
public Point() {
-
this(null,0,0);
-
}
-
/**
-
* Cosntructor
-
* @param referencePoint
-
*/
-
public Point(Point referencePoint) {
-
this(referencePoint,0,0);
-
-
}
-
/**
-
* Constructor
-
* @param referencePoint
-
* @param dx
-
* @param dy
-
*/
-
public Point(Point referencePoint, int dx, int dy){
-
this.dx=dx;
-
this.dy=dy;
-
//this.referencePoint = null;
-
this.referencePoint = referencePoint;
-
}
-
/**
-
* Returns the X coordinate
-
* @return
-
*/
-
public int getX() {
-
if (referencePoint != null) {
-
return referencePoint.getX() + dx;
-
} else
-
return dx;
-
}
-
/**
-
* Returns the Y coordinate
-
* @return
-
*/
-
public int getY() {
-
if (referencePoint != null) {
-
return referencePoint.getY() + dy;
-
} else
-
return dy;
-
}
-
/**
-
* Sets the X coordinate
-
* @param x
-
*/
-
public void setX(int x) {
-
dx = x;
-
}
-
/**
-
* Sets the Y coordinate
-
* @param y
-
*/
-
public void setY(int y) {
-
dy = y;
-
}
-
-
public void setReferencePoint(Point p) {
-
referencePoint = p;
-
}
-
/**
-
* The standard public String toString() is overridden in order to give
-
* formatted printable version of the stored memory data
-
*/
-
public String toString(){
-
return getX()+" "+getY();
-
}
-
/**
-
* We make the following assumptions: Two points are equivalent if
-
* they have both the same coordinates
-
*/
-
public boolean equals(Object other) {
-
boolean b = false;
-
try {
-
Point objectRom = null;
-
// optimistic approach
-
objectRom = (Point) other;
-
if (objectRom.getX()==getX() && objectRom.getY()==getY())
-
return true;
-
else return false;
-
} catch (ClassCastException e) {
-
e.printStackTrace();
-
}
-
return b;
-
}
-
-
private int dx;
-
private int dy;
-
private Point referencePoint;
-
}
-
package memory.view;
-
-
import java.awt.BasicStroke;
-
import java.awt.Color;
-
import java.awt.Dimension;
-
import java.awt.Font;
-
import java.awt.FontMetrics;
-
import java.awt.Graphics;
-
import java.awt.Graphics2D;
-
import java.awt.RenderingHints;
-
import java.awt.geom.Rectangle2D;
-
import java.util.StringTokenizer;
-
-
public class Shape {
-
public Shape() {
-
from = new Point();
-
to = new Point(from);
-
}
-
-
public Shape(String label, int id) {
-
this();
-
this.label = label;
-
this.id = id;
-
}
-
-
/**
-
* Function that determines whether the coordinates (x,y) are inside the
-
* shape
-
*
-
* @param int
-
* @param int
-
* @return boolean
-
*/
-
public boolean containsPoint(int x, int y) {
-
/*
-
* if (x >= this.from.x && x <= this.from.x + width && y >= this.from.y &&
-
* y <= this.from.y + height) return true; else return false;
-
*/
-
if (x >= from.getX() && x <= to.getX() && y >= from.getY()
-
&& y <= to.getY()) {
-
//System.out.println("OK " + x + " " + y + " >> " + from.getX()
-
// + " " + from.getY() + " " + to.getX() + " " + to.getY());
-
return true;
-
} else {
-
//System.out.println("ERR " + x + " " + y + " >> " + from.getX()
-
// + " " + from.getY() + " " + to.getX() + " " + to.getY());
-
return false;
-
}
-
-
}
-
-
protected void draw(Graphics g) {
-
Graphics2D g2 = (Graphics2D) g;
-
BasicStroke bs; // Reference to to BasicStroke
-
Rectangle2D rectangle; // Reference to rectangle
-
float[] solid = { 12.0f, 0.0f }; // Solid line style
-
-
// Set rendering hints to improve display quality
-
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
-
RenderingHints.VALUE_ANTIALIAS_ON);
-
-
// Set the basic stroke
-
bs = new BasicStroke(3.0f, BasicStroke.CAP_SQUARE,
-
BasicStroke.JOIN_MITER, 1.0f, solid, 0.0f);
-
g2.setStroke(bs);
-
-
// Define ellipse
-
// rectangle = new Rectangle2D.Double(from.x + dx + 0.0, from.y + +dy +
-
// 0.0, to.x-from.x+0.0, to.y-from.y + 0.0);
-
// rectangle = new
-
// Rectangle2D.Double(to.getX()-from.getX(),to.getY()-from.getY() ,
-
// to.getX(), from.getY());
-
rectangle = new Rectangle2D.Double(from.getX(), from.getY(), to.getX()
-
– from.getX(), to.getY() – from.getY());
-
g2.setColor(color);
-
g2.fill(rectangle);
-
g2.setFont(myFont);
-
g2.setBackground(Color.CYAN);
-
g2.setColor(Color.black);
-
g2.draw(rectangle);
-
StringTokenizer token = new StringTokenizer(label, "\n");
-
int i = 20;
-
while (token.hasMoreTokens()) {
-
g2.drawString(token.nextToken(), from.getX() + textX, from.getY()
-
+ i);
-
i += 20;
-
}
-
}
-
-
/**
-
* Get the preferred dimensions of the figure
-
*
-
* @param Graphics
-
* g
-
* @return Dimension
-
*/
-
protected final Dimension getPreferredSize(Graphics g) {
-
Font font = g.getFont();
-
StringTokenizer token = new StringTokenizer(label, "\n");
-
if (font != null) {
-
g.setFont(myFont);
-
// get font metrics (it looks nice if we take care of this)
-
FontMetrics fm = g.getFontMetrics(font);
-
-
int width = 0;
-
int height = 0;
-
while (token.hasMoreTokens()) {
-
width = Math.max((fm.stringWidth(token.nextToken()) * 5) / 2,
-
width);
-
height += 20;
-
}
-
// width+=40; height+=20;
-
// to.setX(from.getX()+width);
-
// to.setY(from.getY()+height);
-
to.setX(width);
-
to.setY(height);
-
-
return new Dimension(width, height);
-
} else {
-
return new Dimension(100, 100);
-
}
-
}
-
-
/**
-
* Modifies the coordinates with dx, dy
-
*
-
* @param newX
-
* @param newY
-
*/
-
public void moveBy(int dx, int dy) {
-
from.setX(from.getX() + dx);
-
from.setY(from.getY() + dy);
-
// from.setX(dx);
-
// from.setY(dy);
-
// System.out.println(dx+" "+dy);
-
}
-
-
/**
-
* Modifies the coordinates
-
*
-
* @param newX
-
* @param newY
-
*/
-
/*
-
* public void moveXY(int newX, int newY) { int dx=to.getX()-from.getX();
-
* int dy=to.getY()-from.getY(); from.setX(newX); from.setY(newY); to=new
-
* Point(from); to.setX(newX+dx); to.setY(newY+dy);
-
* //System.out.println("newX "+x+" newY "+y); }
-
*/
-
-
/*
-
* public void resize(int width, int height) { Point p=new
-
* Point(to,width-to.getX(),height-to.getY()); to=p; }
-
*/
-
protected void setColor(Color color) {
-
this.color = color;
-
}
-
-
public int getId() {
-
return id;
-
}
-
-
protected Point getFromPoint() {
-
return from;
-
}
-
-
protected Point getToPoint() {
-
return to;
-
}
-
-
protected void setTextPosition(int x) {
-
textX = x;
-
}
-
-
protected void setFromPoint(Point p) {
-
from = p;
-
to.setReferencePoint(p);
-
}
-
-
protected void setLabel(String s) {
-
label = s;
-
}
-
-
protected String getLabel() {
-
return label;
-
}
-
/**
-
* The standard public String toString() is overridden
-
*/
-
public String toString(){
-
return label+" with id "+id;
-
}
-
/**
-
* Check to see if this is the instance of a shape
-
*/
-
public boolean equals(Object other) {
-
if(other instanceof Shape)
-
return true;
-
else
-
return false;
-
}
-
-
private Font myFont = new Font("SansSerif", Font.ITALIC, 18);
-
private Color color = Color.yellow;
-
private int textX = 0;
-
/*
-
* private int width = 40; private int height= 40;
-
*/
-
private Point from = new Point();
-
private Point to = new Point();
-
private int id;
-
private String label;
-
}
-
package memory.view;
-
-
import java.awt.event.MouseListener;
-
import java.awt.event.MouseMotionListener;
-
-
public interface ShapeMouseInterface extends MouseListener, MouseMotionListener{
-
}
-
package memory.view;
-
import java.awt.Color;
-
-
public class SignalShape extends Shape{
-
public SignalShape(int id,String label){
-
super(label,id);
-
this.label=label;
-
setColor(Color.WHITE);
-
}
-
public void setLabel(String s){
-
value=s;
-
super.setLabel(label+value);
-
}
-
/**
-
* The standard public String toString() is overridden
-
*/
-
public String toString(){
-
return "Signal "+ label+ " from "+super.toString();
-
}
-
/**
-
* Check to see if this is the instance of a Signalshape
-
*/
-
public boolean equals(Object other) {
-
if(other instanceof SignalShape)
-
return true;
-
else
-
return false;
-
}
-
private String value="";
-
private String label;
-
}
-
package memory.view;
-
-
import javax.swing.JButton;
-
import javax.swing.JComboBox;
-
import javax.swing.JEditorPane;
-
import javax.swing.JFrame;
-
import javax.swing.JLabel;
-
import javax.swing.JOptionPane;
-
import javax.swing.JPanel;
-
import javax.swing.JScrollPane;
-
import javax.swing.JTextField;
-
-
import java.awt.Color;
-
import java.awt.Dimension;
-
import java.awt.event.ActionListener;
-
-
public final class View extends JFrame implements ViewInterface{
-
public View(){
-
//we shall manage layout, so put it to null
-
panel.setLayout(null);
-
workingPanel.setLayout(null);
-
commandPanel.setLayout(null);
-
inputPanel.setLayout(null);
-
inputPanel.setVisible(false);
-
-
//set the bounds of the panel
-
panel.setBounds(0, 0, 600, 600);
-
inputPanel.setBounds(0, 0, 600, 600);
-
-
workingPanel.setBounds(0, 0, 600, 500);
-
-
commandPanel.setBounds(0,510,600,600);
-
commandRegionLabel.setBounds(0,0, 100, 20);
-
writeBtn.setBounds(50,20,100,20);
-
memoryBtn.setBounds(400,20,100,20);
-
-
-
commandPanel.add(commandRegionLabel);
-
commandPanel.add(writeBtn);
-
commandPanel.add(memoryBtn);
-
//commandPanel.add(promBtn);
-
//commandPanel.add(eepromBtn);
-
//commandPanel.add(uvpromBtn);
-
-
memoryNameLabel.setBounds(10,10,100,20);
-
memoryTypeLabel.setBounds(10,40,100,20);
-
depthLabel.setBounds(10,70,100,20);
-
sizeLabel.setBounds(10,100,100,20);
-
initializationLabel.setBounds(10,120,100,20);
-
-
-
//create a scrolling area for processing area
-
initializedContent = new JScrollPane(editorPane);
-
//set just vertical scrollbar and not horizontal
-
initializedContent
-
.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
-
//set the prefferred size
-
initializedContent.setPreferredSize(new Dimension(400, 400));
-
//set the minim size of scrooll pane
-
initializedContent.setMinimumSize(new Dimension(10, 10));
-
//just text
-
editorPane.setContentType("text");
-
//it shouldn't be editable
-
editorPane.setEditable(true);
-
initializedContent.setBounds(10, 140, 580, 400);
-
returnBtn.setBounds(250,540,100,20);
-
nameTxt.setBounds(120,10, 100, 20);
-
choice.setBounds(120,40, 100, 20);
-
choice.setEditable(true);
-
depthTxt.setBounds(120,70,100,20);
-
lengthTxt.setBounds(120,100,100,20);
-
-
inputPanel.add(memoryNameLabel);
-
inputPanel.add(nameTxt);
-
inputPanel.add(memoryTypeLabel);
-
inputPanel.add(depthLabel);
-
inputPanel.add(sizeLabel);
-
inputPanel.add(initializationLabel);
-
inputPanel.add(initializedContent);
-
inputPanel.add(returnBtn);
-
inputPanel.add(choice);
-
inputPanel.add(lengthTxt);
-
inputPanel.add(depthTxt);
-
-
-
panel.add(inputPanel);
-
panel.add(workingPanel);
-
panel.add(commandPanel);
-
-
//add the panel to the frame
-
this.add(panel);
-
//set the size of the frame
-
this.setSize(600, 600);
-
// Display the window.
-
this.setVisible(true);
-
//set the default close operation, i.e. what happens when you click on X
-
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-
//it should not be resizeble
-
this.setResizable(false);
-
//set the title of the application
-
this.setTitle("Memory simulation application – Marian Dan Alexandru");
-
-
-
}
-
-
/**
-
* Add an action listener to the Signal button
-
* @param e
-
*/
-
public final void addWriteBtnListener(ActionListener e) {
-
writeBtn.addActionListener(e);
-
}
-
-
/**
-
* Add an action listener to the PROM button
-
* @param e
-
*/
-
/*public final void addPromBtnListener(ActionListener e) {
-
promBtn.addActionListener(e);
-
}*/
-
-
/**
-
* Add an action listener to the EEPROM button
-
* @param e
-
*/
-
/*public final void addEepromlBtnListener(ActionListener e) {
-
eepromBtn.addActionListener(e);
-
}*/
-
-
/**
-
* Add an action listener to the UVPROM button
-
* @param e
-
*/
-
/*public final void addUvpromBtnListener(ActionListener e) {
-
uvpromBtn.addActionListener(e);
-
}*/
-
public final void addMemoryBtnListener(ActionListener e) {
-
memoryBtn.addActionListener(e);
-
}
-
/**
-
* Add an action listener to the Return button
-
* @param e
-
*/
-
public final void addReturnBtnListener(ActionListener e) {
-
returnBtn.addActionListener(e);
-
}
-
/**
-
* Add an action listener to the combo box
-
* @param e
-
*/
-
public final void addComboBoxListener(ActionListener e) {
-
choice.addActionListener(e);
-
}
-
public void setInputPanel(){
-
workingPanel.setVisible(false);
-
commandPanel.setVisible(false);
-
inputPanel.setVisible(true);
-
}
-
public void unsetInputPanel(){
-
if(writeToMemory){
-
memoryNameLabel.setVisible(true);
-
memoryTypeLabel.setVisible(true);
-
depthLabel.setVisible(true);
-
sizeLabel.setVisible(true);
-
-
choice.setVisible(true);
-
depthTxt.setVisible(true);
-
lengthTxt.setVisible(true);
-
nameTxt.setVisible(true);
-
}
-
workingPanel.setVisible(true);
-
commandPanel.setVisible(true);
-
inputPanel.setVisible(false);
-
}
-
public final void showMessage(String message, String title){
-
JOptionPane.showMessageDialog(null,
-
message,
-
title,
-
JOptionPane.WARNING_MESSAGE);
-
}
-
public final void showErrorMessage(String message) {
-
JOptionPane.showMessageDialog(this, message, "Error",
-
JOptionPane.ERROR_MESSAGE);
-
}
-
public final String inputDialog(String message){
-
return JOptionPane.showInputDialog(message);
-
}
-
public final String getWordSize(){
-
return lengthTxt.getText();
-
}
-
public final String getMemoryDepth(){
-
return depthTxt.getText();
-
}
-
public final String getInitialData(){
-
return editorPane.getText();
-
}
-
public final String getMemoryName(){
-
return nameTxt.getText();
-
}
-
public final void resetInput(){
-
nameTxt.setText("");
-
choice.setSelectedIndex(0);
-
depthTxt.setText("");
-
lengthTxt.setText("");
-
editorPane.setText("");
-
}
-
public final Shape addMemoryBox(String name, int id, int depth, int width){
-
Shape p=new MemoryShape(id, name,depth,width);
-
workingPanel.addShape(p);
-
return p;
-
}
-
public final WorkingPanel getWorkingPanel(){
-
return workingPanel;
-
}
-
public final void addWorkingPanelMouseListener(ShapeMouseInterface l){
-
workingPanel.addWorkingPanelMouseListener(l);
-
}
-
public final void writeToMemory(){
-
resetInput();
-
setInputPanel();
-
memoryNameLabel.setVisible(false);
-
memoryTypeLabel.setVisible(false);
-
depthLabel.setVisible(false);
-
sizeLabel.setVisible(false);
-
-
choice.setVisible(false);
-
depthTxt.setVisible(false);
-
lengthTxt.setVisible(false);
-
nameTxt.setVisible(false);
-
-
writeToMemory=true;
-
}
-
-
/**
-
* The standard public String toString() is overridden
-
*/
-
public String toString(){
-
return "The view";
-
}
-
/**
-
* Check to see if this is the instance of the View
-
*/
-
public boolean equals(Object other) {
-
if(this==other) return true;
-
else return false;
-
}
-
-
-
private final JPanel panel = new JPanel();
-
private final WorkingPanel workingPanel = new WorkingPanel();
-
private final JPanel commandPanel = new JPanel(