Posted: 14 Sep 2005, 07:14
So peeps. how'd your prodject go?
An archive of the South African PCFormat forums.
https://tuhinga.ron2k.za.net/pcformat/
I was thinking about tihs and i want to do a aplet GUI but apart from that i am quite confused as what of this things should be in the main method and what should be in the classINTRODUCTION
A floors building with 12 floors has 3 elevators – named north, south and west. All the elevators run from 7am to 7 pm. After 7pm only the west elevator is open. All other elevators must be closed down. The north elevator is wider than the other two and can be used for the delivery of goods to other floors. The elevators operate on an auto status but have a manual override in the event of emergencies or for controlling flow of users. A safety feature of the elevator prevents the south them operating if the weight exceeds 2000kg.
REQUIRED TO DO
Using the above information you are required to create a program using classes. The program must have at least the following abilities:
1. Status of the elevators – stopped, moving, floor number, closed, open, maintenance. This is a constant display of the lifts moving.
2. Show the time of day. The display of status must be chronological.
3. Your program should contain the current date using the date class. (Date date = new Date().)
4. The operator mode of the elevator – auto, manual.
5. The elevator is switched to manual mode when
· There’s an emergency such as a failure or need to override auto mode.
· When the north elevator is needed to carry goods (deliveries) for say more than 30 minutes
6. During an emergency such as a failure (lift stuck) an alarm must be set.
7. The elevators must sound a warning if the capacity exceeds 2000kg. The lift doors will stay open until the correct weight is detected.
Your code must contain the necessary attributes and methods to deal with the situations outlined. The main class must contain the instantiated object and must contain the calling statements. The scenarios for the lift must be randomized but allow for manual overrides as indicated. A menu option is required.
err... no... probably closer to 50+ classes.. depending on how robust you want this to be...amdretard wrote:suggesting is 4 differnet class files all with these attributes
Code: Select all
public viod setWhatever( int t)
Code: Select all
setWhatever(1)
Code: Select all
public class elevator
{
private String time = "";
private String Status;
private double floor = Math.random () * 11 + 1;
private String Operator = "automated";
int maintaince = 0;
int sec = 45;
int min = 29;
int hour = 19;
private boolean Overload = false;
//setting time. This method is going to be run each second
public void setTime (String t)
{
sec++;
if (sec == 60)
{
min++;
sec = 0;
}
if (min == 60)
{
hour++;
min = 0;
}
if (hour == 24)
hour = 0;
if ((hour >= 7) && (hour < 19))
time = hour + ":" + min + ":" + sec;
}
//setting status
public void setStatus (double s)
{
if (maintaince == 1)
Status = "on maintainence";
else
{
s = Math.random () * 5 + 1;
if (s == 3)
Status = "running ";
if (s == 2)
{
Status = "stopped at floor";
if (s == 1)
Status = "Doors open";
if (s == 4)
Status = "door closed";
}
}
} //setting floor runs on random unless 'f' equals 1
public void setFloor (int f)
{
if (maintaince == 1)
{
floor = f;
}
else
{
if (floor == 12)
floor--;
else
{
double r = Math.random () * 1;
if (r == 0)
{
floor++;
}
else
floor--;
}
}
}
// selection of auto mode or manual /delvery
public void setOperator (int o)
{
if (o == 1)
{
Operator = "Manaul mode engaged for maintianence or emergencie";
maintaince = 1;
}
else
Operator = "automated mode engaged";
}
//overload on random
public void setOverload (double o)
{
o = Math.random () * 4;
if (o == 0)
{
Overload = true;
System.out.println ("Your load exceeds 2000Kg lose weight please");
}
else
Overload = false;
}
// accessor for all the above methods
public String getTime ()
{
return time;
}
public String getOverload ()
{
return Overload;
}
public String getStatus ()
{
return Status;
}
public double getFloor ()
{
return floor;
}
public String getOperator ()
{
return Operator;
}
//to string
public String toString ()
{
return new String ("The time is now " + time
+ "\tThe elevator is now at floor " + floor
+ "\tThe elevator is in the following mode " + Operator
+ "\tThe elevator is/has " + Status
+ "\n");
}
}
Code: Select all
//assuming SimpleDateFormat member sdf
//assuming that time is of type java.util.Date
public String getTime()
{
return sdf.format(time);
//code producing formatted String
//see Date and SimpleDateFormat
// this String is produced out of scope...
// if no reference is maintained by the
// calling Object, this String is available
// for garbage collection...
}
public void setTime(Date d)
{
time = d;
//a set method sets an encapsulated member
//not typically used to operate on an external member
}
Code: Select all
Elevator ele = new Elevator(int currentFloor, String op);
Code: Select all
ele.setStatus(String state);
Code: Select all
String time = ele.getTime();
//or
System.out.println("Elevator: time:-"+ele.getTime());
sounds as if someone needs to clear his mind and stop teaching then...amdretard wrote:The Teacher did not want us to use the Whole date thing
Code: Select all
package elements;
public interface Panel {
public int getNumberOfOptions();
public void arm(int option);
public void disarm(int option);
public boolean isArmed(int option);
public boolean hasArmedOptions();
public void addPanelListener(PanelPressListener ppl);
public void removePanelListener(PanelPressListener ppl);
}
Code: Select all
package elements;
public class PanelPressedEvent {
public static enum PRESS_TYPE{ARM, DISARM, UNKNOWN};
private int option;
private Object source;
public PanelPressedEvent(Object orig, int selection)
{
this.setSource(orig);
this.setOption(selection);
}
public int getOption() {
return option;
}
public void setOption(int option) {
this.option = option;
}
public Object getSource() {
return source;
}
public void setSource(Object source) {
this.source = source;
}
}
Code: Select all
package elements;
public interface PanelPressListener {
public void panelPressed(PanelPressedEvent ppe, PanelPressedEvent.PRESS_TYPE type);
}
Code: Select all
package elements;
import java.util.ArrayList;
public class DefaultPanel implements Panel{
private ArrayList<PanelPressListener> panelListeners;
private int[][] options;
public DefaultPanel(int numberOfChoices)
{
options = new int[numberOfChoices][1];
panelListeners = new ArrayList<PanelPressListener>();
}
public void arm(int option) {
if(option>0&&option<=options.length)
{
options[option-1][0]=1;
this.notifyListeners(option, true);
}
}
public void disarm(int option) {
if(option>0&&option<=options.length)
{
options[option-1][0]=0;
this.notifyListeners(option, false);
}
}
public int getNumberOfOptions() {
return options.length;
}
public boolean hasArmedOptions() {
for(int i = options.length-1;i>=0;i--)
if(options[i][0]>0)
return true;
return false;
}
public boolean isArmed(int option) {
if(option>0&&option<=options.length)
return options[option-1][0]==1?true:false;
return false;
}
public String toString()
{
String retval = "PANEL:\n======\nOPT:";
for(int i = 0;i<options.length;i++)
retval+="["+(i+1)+"]";
retval+="\nARM:";
for(int i = 0;i<options.length;i++)
retval+="["+(i+1>9?" ":"")+(options[i][0]>0?"X":" ")+"]";
retval+="\n======\n";
return retval;
}
public void addPanelListener(PanelPressListener ppl) {
panelListeners.add(ppl);
}
public void removePanelListener(PanelPressListener ppl) {
panelListeners.remove(ppl);
}
private void notifyListeners(int opt, boolean arm)
{
if(panelListeners.size()>0)
{
PanelPressedEvent ppe = new PanelPressedEvent(this, opt);
for(PanelPressListener ppl:panelListeners)
ppl.panelPressed(ppe, arm?PanelPressedEvent.PRESS_TYPE.ARM:PanelPressedEvent.PRESS_TYPE.DISARM);
}
}
}
Code: Select all
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import javax.swing.*;
import javax.swing.JApplet;
import java.util.*; //Import util for Random Numbers
public class Elevator10 extends JFrame
{
boolean clicked; //no integer called clicked so i created for u
elevator east = new elevator ();
elevator north = new elevator ();
elevator south = new elevator ();
//set up input
BufferedReader reader = new BufferedReader
(new InputStreamReader (System.in));
public Elevator10 ()
{
super ("elevators");
setSize (800, 600);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
setVisible (true);
Container cont = getContentPane ();
cont.setBackground (Color.blue);
BorderLayout lay = new BorderLayout ();
cont.setLayout (lay);
setContentPane (cont);
}
//**************************************I had to seperate the main void from the paint void
// public boolean mouseDown (Event evt, int x, int y) // This has to be outside of another void as well as it is also a void (Remember public String getFloor() or whatever - it reutrns a value
// {
// clicked = true;
// repaint ();
//
// return true; //if clicked return true
// }
public void paint (Graphics g)
{
int select = 1;
resize (800, 600);
g.setColor (Color.yellow);
g.fillRect (0, 0, 800, 600);
g.setColor (Color.red);
g.drawString ("Welcome to Elevator 1.0", 280, 250);
try
{ //u forgot to add the try and catch - vital in case of errors
Thread.sleep (5000);
}
catch (InterruptedException e)
{
System.out.println ("Menu");
System.out.println ("1.Automode");
System.out.println ("2.Manual");
System.out.println ("3.emergency button");
int option = Integer.parseInt (reader.readLine ());
if (option == 2)
{
System.out.println ("Choose lift ( A= all N=north S=South W =west)");
int optionM = Integer.parseInt (reader.readLine ());
if (optionM = "a")
north.setStatus (1);
south.setStatus (1);
west.setStatus (1);
{
System.out.println ("What floor do you want the lifts to be at");
int Floor = Integer.parseInt (reader.readLine ());
{
select = 2;
int floorOverride = Integer.parseInt (reader.readLine ());
north.getFloor (floorOverride);
east.getFloor (floorOverride);
south.getFloor (floorOverride);
}
}
}
boolean clicked = false;
int nr = 1;
int sr = 1;
int north1 = 1; //****** i had to change the integer names because they were the same as your elevators - the program gets confused
int south1 = 1; //******
int east1 = 1; //******
boolean manual = false;
while (clicked = true)
{
switch (select)
{case 1:
north.setTime ("r"); // this has to be setTime(String here);
g.setColor (Color.blue);
g.drawString ("The time is now" + north.getTime (), 750, 10);
System.out.println ("the time is " + north.getTime ());
try
{
Thread.sleep (1000);
}
catch (InterruptedException f)
{
}
String time = north.getTime ();
if (time == "19:0:0") //time has to be a string to equal a string (time is an int)
{
nr = 0;
sr = 0;
}
if (time == "07:0:0")
{
nr = 1;
sr = 1;
}
if (nr == 1)
{
north.setFloor (1); //*****Check your class with this - u need to have setFloor(int);
south.setFloor (1);
} //******
east.setFloor (1); //*******
case 2:
{
if (nr == 1) // careful with all the if statements remember it has to be nf == (number here) not nf = (number here)
{
int nf = north.getFloor (); //*****
System.out.print (nf);
g.setColor (Color.black);
g.drawLine (40, 50, 0, 600);
g.drawLine (140, 50, 0, 600);
if (nf == 1)
{
g.setColor (Color.blue);
g.drawRect (51, 650, 91, 99);
}
if (nf == 2)
{
g.setColor (Color.blue);
g.drawRect (51, 600, 91, 99);
}
if (nf == 3)
{
g.setColor (Color.blue);
g.drawRect (51, 550, 91, 99);
}
if (nf == 4)
{
g.setColor (Color.blue);
g.drawRect (51, 500, 91, 99);
}
if (nf == 5)
{
g.setColor (Color.blue);
g.drawRect (51, 450, 91, 99);
}
if (nf == 6)
{
g.setColor (Color.blue);
g.drawRect (51, 400, 91, 99);
}
if (nf == 7)
{
g.setColor (Color.blue);
g.drawRect (51, 350, 91, 99);
}
if (nf == 8)
{
g.setColor (Color.blue);
g.drawRect (51, 300, 91, 99);
}
if (nf == 9)
{
g.setColor (Color.blue);
g.drawRect (51, 250, 91, 99);
}
if (nf == 10)
{
g.setColor (Color.blue);
g.drawRect (51, 200, 91, 99);
}
if (nf == 11)
{
g.setColor (Color.blue);
g.drawRect (51, 150, 91, 99);
}
if (nf == 12)
{
g.setColor (Color.blue);
g.drawRect (51, 100, 91, 99);
}
}
if (sr == 1)
{
int sf = south.getFloor ();
if (sf == 1)
{
g.setColor (Color.black)
;
g.drawLine (200, 50, 0, 600);
g.drawLine (140, 50, 0, 600);
if (sf == 1)
{
g.setColor (Color.red);
g.drawRect (201, 650, 91, 99);
}
if (sf == 2)
{
g.setColor (Color.red);
g.drawRect (201, 600, 91, 99);
}
if (sf == 3)
{
g.setColor (Color.red);
g.drawRect (201, 550, 91, 99);
}
if (sf == 4)
{
g.setColor (Color.red);
g.drawRect (201, 500, 91, 99);
}
if (sf == 5)
{
g.setColor (Color.red);
g.drawRect (201, 450, 91, 99);
}
if (sf == 6)
{
g.setColor (Color.red);
g.drawRect (201, 400, 91, 99);
}
if (sf == 7)
{
g.setColor (Color.red);
g.drawRect (201, 350, 91, 99);
}
if (sf == 8)
{
g.setColor (Color.red);
g.drawRect (201, 300, 91, 99);
}
if (sf == 9)
{
g.setColor (Color.red);
g.drawRect (201, 250, 91, 99);
}
if (sf == 10)
{
g.setColor (Color.red);
g.drawRect (201, 200, 91, 99);
}
if (sf == 11)
{
g.setColor (Color.red);
g.drawRect (201, 150, 91, 99);
}
if (sf == 12)
{
g.setColor (Color.red);
g.drawRect (201, 100, 91, 99);
}
}
int ef = east.getFloor (); // again with the double change it to be an integer
g.setColor (Color.black);
g.drawLine (340, 50, 0, 600);
g.drawLine (440, 50, 0, 600);
if (ef == 1)
{
g.setColor (Color.yellow);
g.drawRect (341, 650, 91, 99);
}
if (ef == 2)
{
g.setColor (Color.yellow);
g.drawRect (341, 600, 91, 99);
}
if (ef == 3)
{
g.setColor (Color.yellow);
g.drawRect (341, 550, 91, 99);
}
if (ef == 4)
{
g.setColor (Color.yellow);
g.drawRect (341, 500, 91, 99);
}
if (ef == 5)
{
g.setColor (Color.yellow);
g.drawRect (341, 450, 91, 99);
}
if (ef == 6)
{
g.setColor (Color.yellow);
g.drawRect (341, 400, 91, 99);
}
if (ef == 7)
{
g.setColor (Color.yellow);
g.drawRect (341, 350, 91, 99);
}
if (ef == 8)
{
g.setColor (Color.yellow);
g.drawRect (341, 300, 91, 99);
}
if (ef == 9)
{
g.setColor (Color.yellow);
g.drawRect (341, 250, 91, 99);
}
if (ef == 10)
{
g.setColor (Color.yellow);
g.drawRect (341, 200, 91, 99);
}
if (ef == 11)
{
g.setColor (Color.yellow);
g.drawRect (341, 150, 91, 99);
}
if (ef == 12)
{
g.setColor (Color.yellow);
g.drawRect (341, 100, 91, 99);
}
repaint ();
}
}
}
}
}
public static void main (String [] args) throws IOException
{
Elevator10 e = new Elevator10 ();
}
}
}
Code: Select all
public class elevator
{
private String time = "";
private String Status;
private int floor = (int) (Math.random () * 11 + 1); // i changed it from being a double to being an int
private String Operator = "automated";
private int maintaince = 0;
private int sec = 45;
private int min = 59;
private int hour = 18;
private boolean Overload;
private int y = 0;
public void setTime (String t)
{
sec++;
if (sec == 60)
{
min++;
sec = 0;
}
if (min == 60)
{
hour++;
min = 0;
}
if (hour == 24)
hour = 0;
if (hour == 19)
{
y = 1;
}
if (hour == 7)
{
y = 0;
}
time = hour + ":" + min + ":" + sec;
}
public void setStatus (double s)
{
if (maintaince == 1)
Status = "on maintainence";
else
{
s = Math.random () * 5 + 1;
if (s == 3)
Status = "running ";
if (s == 2)
Status = "stopped at floor";
if (s == 1)
Status = "Doors open";
if (s == 4)
Status = "door closed";
}
}
public void setFloor (int f)
{
if (y == 1)
{
floor = floor;
}
else
{
if (maintaince == 1)
{
floor = f;
}
else
{
if (floor == 0)
{
floor++;
}
if (floor == 12)
{
floor--;
}
else
{
int r = (int) Math.random () * 2;
if (r == 0)
{
floor++;
}
if (r == 1)
{
floor--;
}
}
}
}
}
public void setOperator (int o)
{
if (o == 1)
{
Operator = "Manaul mode engaged for maintianence or emergencie";
maintaince = 1;
}
else
Operator = "automated mode engaged";
}
public void setOverload (double o)
{
o = Math.random () * 4;
if (o == 0)
{
Overload = true;
System.out.println ("Your load exceeds 2000Kg lose weight please");
}
else
Overload = false;
}
public String getTime ()
{
return time;
}
public boolean getOverload () //ERROR was Here : you use public STRING when t should have been public boolean
{
return Overload;
}
public String getStatus ()
{
return Status;
}
public int getFloor ()
{
return floor;
}
public String getOperator ()
{
return Operator;
}
public String toString ()
{
return new String ("The time is now " + time
+ "\tThe elevator is now at floor " + floor
+ "\tThe elevator is in the following mode " + Operator
+ "\tThe elevator is/has " + Status
+ "\n");
}
}
please help for monday
Code: Select all
package timers;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Timer;
public class ElevatorClock {
public static enum CLOCK_FIELD{HOURS, MINUTES, SECONDS};
//using bytes as no value extends 255
private byte hours, minutes, seconds;
//specifies whether this clock incerements or decrements.
private boolean tickerDir;
//Timer requried to automate...
private Timer clock;
//ArrayList used to store interested listeners
private ArrayList<ElevatorClockListener> listeners;
//HashMap used to store event keys/values
private HashMap<String, String> installedAlerts;
public ElevatorClock()
{
this(true);
}
public ElevatorClock(boolean positiveClock)
{
super();
tickerDir = positiveClock;
listeners = new ArrayList<ElevatorClockListener>();
installedAlerts = new HashMap<String, String>();
}
public void set(CLOCK_FIELD id, int value)
{
switch(id)
{
case HOURS:{
hours = (value<=24&&value>=0)?(byte)value:hours;
return;
}
case MINUTES:{
minutes = (value<=59&&value>=0)?(byte)value:minutes;
return;
}
case SECONDS:{
seconds = (value<=59&&value>=0)?(byte)value:seconds;
return;
}
}
}
public int get(CLOCK_FIELD id)
{
switch(id)
{
case HOURS:{return hours;}
case MINUTES:{return minutes;}
case SECONDS:{return seconds;}
default:{return 0;}
}
}
public void add(CLOCK_FIELD id, int value)
{
switch(id)
{
case HOURS:{
if(hours+value>23||hours+value<0)
hours = value<0?(byte)23:0;
else
hours+=value;
return;
}
case MINUTES:{
if(minutes+value>59||minutes+value<0)
{
minutes = value<0?(byte)59:0;
if(value<0)
this.add(CLOCK_FIELD.HOURS, -1);
else
this.add(CLOCK_FIELD.HOURS, 1);
}
else
minutes+=value;
return;
}
case SECONDS:{
if(seconds+value>59||seconds+value<0)
{
seconds = value<0?(byte)59:0;
if(value<0)
this.add(CLOCK_FIELD.MINUTES, -1);
else
this.add(CLOCK_FIELD.MINUTES, 1);
}
else
seconds+=value;
return;
}
}
}
public void start()
{
this.start(1000);
}
public void start(long steps)
{
if(clock!=null)
clock.cancel();
clock = new Timer(this.getClass().getName());
clock.schedule(new ClockTicker(this, tickerDir), 1000, steps);
}
public void stop()
{
clock.cancel();
}
public void reset()
{
this.stop();
this.set(CLOCK_FIELD.HOURS, 0);
this.set(CLOCK_FIELD.MINUTES, 0);
this.set(CLOCK_FIELD.SECONDS, 0);
}
public void installAlert(int hrs, int min, int scnds, String command)
{
if(hrs<0||hrs>23||min<0||min>59||scnds<0||scnds>59)
return;
installedAlerts.put(hrs+":"+min+":"+scnds, command);
}
public void disposeAlert(int hrs, int min, int scnds)
{
this.disposeAlert(hrs+":"+min+":"+scnds);
}
public void disposeAlert(String id)
{
if(installedAlerts.containsKey(id))
installedAlerts.remove(id);
}
public HashMap<String, String> getAlerts()
{
return installedAlerts;
}
public String getKeyValue()
{
return hours+":"+minutes+":"+seconds;
}
public void notifyListeners(ElevatorClockAlert alert)
{
for(ElevatorClockListener ecl:listeners)
ecl.processAlert(this, alert);
}
public void addClockListener(ElevatorClockListener ecl)
{
listeners.add(ecl);
}
public void removeClockListener(ElevatorClockListener ecl)
{
listeners.remove(ecl);
}
//note that this mehtod will return a correctly formatted value
public String toString()
{
return (hours<10?"0":"")+hours+":"+(minutes<10?"0":"")+minutes+":"+(seconds<10?"0":"")+seconds;
}
}
Code: Select all
package timers;
//TimerTask is required for use with a Timer..
import java.util.TimerTask;
public class ClockTicker extends TimerTask{
private ElevatorClock owner;
private boolean increment;
public ClockTicker(ElevatorClock control, boolean dir)
{
super();
owner = control;
increment = dir;
}
public void run()
{
//if incrementing, add one second, else subtract one second..
owner.add(ElevatorClock.CLOCK_FIELD.SECONDS, (increment?1:-1));
//scan for an installed alert event..
String keyId = owner.getKeyValue();
if(owner.getAlerts().containsKey(keyId))
owner.notifyListeners(new ElevatorClockAlert(keyId, owner.getAlerts().get(keyId)));
}
}
Code: Select all
package timers;
public class ElevatorClockAlert{
private String alertID, actionCommand;
public ElevatorClockAlert(String id, String command)
{
alertID = id;
actionCommand = command;
}
public String getActionCommand() {
return actionCommand;
}
public String getAlertID() {
return alertID;
}
}
Code: Select all
package timers;
public interface ElevatorClockListener {
public void processAlert(ElevatorClock source, ElevatorClockAlert alert);
}
Code: Select all
public class MyClass{
}
..
class SomeOtherClass{
}
class AnotherOne{
}
practice, practice, practice...amdretard wrote:@ Rustpup
You know a lot but most of those things you are trying to show me go over my head but i will try and learn some of it