Java school Project

Get help on programming - C++, Java, Delphi, etc.
Law
Registered User
Posts: 1569
Joined: 12 Sep 2003, 02:00
Location: UND

Post by Law »

So peeps. how'd your prodject go?
Image

MAY THE CHEESE BE WITH YOU!
{PK} Law
amdretard
Registered User
Posts: 1177
Joined: 26 Oct 2002, 02:00
Location: Cape town

Post by amdretard »

The project went fine I got 38 out of 50
and DJt loafed my half finished pro off me and he claimed he edited it but iu have my doubts

But it is now the 4th term and we now have new one
And one again i need help/ advice from supreme beings higher than myself
here it is
INTRODUCTION
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.
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 class

note i am in grade 10 and these options will all be using random options and not sensor based as we have not learnt that

thanks for the help
My friends think i need a life. What do you think?
User avatar
rustypup
Registered User
Posts: 8872
Joined: 13 Dec 2004, 02:00
Location: nullus pixius demonica
Contact:

Post by rustypup »

forget trying to accomplish this in a main method - this method is primarily used as the entrance point only. Using it for anything else is ill-advised, to say the least. Added to this is the sad fact that this will take a number of classes to accomplish.

consider breaking this project into three distinct areas.

1) Elevator
2) Building
3) Passenger

Spend some time describing the elevator Object. Keep in mind that this Elevator will need to provide the Building and the Passenger with a usable interface, (Building's requirement's are not the same as Passenger's).

If this is the case, as you are developing the Elevator, consider:

1) Building should handle movement automation. Building would allot activity schedules to Elevator. So Building is a natural manager Object to your Elevator - "its almost teatime/lunch time/quitting time, would Elevator B please go to the busiest Floor..." or "Someone pushed a button on Floor 7, would Elevator A please go there"... you get the picture... In order to do this, consider the structure of the instructions, (Building issues a goto instruction, but Elevator's doors are open or it is overweight ! what now? ).

2) Passenger - not strictly necessary, but could be worth some points. Passenger would require interface to Building, (maybe through a Floor type Object), and to Elevator. Passenger activity is severely limited, (Push up/down button for Floor- climb on/off or push emergency button on Elevator) - but passenger plays a role when it comes to mimicking this sort of model. By providing the Passenger with two directional options, (random except for floor 1 and 12), and 11 Floor selections, (Passenger selecting its current Floor is just silly), you could sit back and watch as the Building starts recording seemingly *natural* Elevator behaviour....

3) Each Elevator would handle a 2 queues of requests, based on its current Direction, of *planned* floor visits. The Building would be responsible for allocating new entries in its queues, based on their interaction. (Note, these queues could be managed by Building itself, but would make more sense if automatically managed by Elevator with Building interfering only when necessary.

Direction here is a biggy. Having the Building instruct Elevator A to go to Floor x in response to Passenger z's pushing of a button seems simple, until you realise that Passenger z is on the 2nd Floor, whereas Passenger k, (10th Floor), pushed his/her button just after z... at the same time, Elevator A is going up, Elevator B is coming down, where are these two in relation to the button pushers? Should Building re-route A to the 10th floor , with the plan to collect Passenger k, and then Passenger z on the return trip? or could Elevator B take care of Passenger z on its route down?

This sort of complexity is commonplace in real life and the manner in which we approach it from the outset determines whether or not success will follow...

The OO approach is a good starting point, depending on the amount of time you have available...
Most people would sooner die than think; in fact, they do so - Bertrand Russel
amdretard
Registered User
Posts: 1177
Joined: 26 Oct 2002, 02:00
Location: Cape town

Post by amdretard »

So what you are suggesting is 4 differnet class files all with these attributes
My friends think i need a life. What do you think?
axe346
Registered User
Posts: 67
Joined: 01 Oct 2005, 02:00
Location: Worcester
Contact:

Post by axe346 »

Where do you guys use JAVA. I can only see it being used in JScript.
C++ programer.
Believe is for those who aren`t sure. I know -The Collector. It was my brother who broke it , I just fixed it (and know it will never work) - Personal qoute
User avatar
hamin_aus
Forum Moderator
Posts: 18363
Joined: 28 Aug 2003, 02:00
Processor: Intel i7 3770K
Motherboard: GA-Z77X-UP4 TH
Graphics card: Galax GTX1080
Memory: 32GB G.Skill Ripjaws
Location: Where beer does flow and men chunder
Contact:

Post by hamin_aus »

People who need to run applications across different platforms would use Java.
Successful Java apps include Oracle and Limewire.
C++ is faster, but thats about all.
C# will pwn them both eventually :D
Image
User avatar
rustypup
Registered User
Posts: 8872
Joined: 13 Dec 2004, 02:00
Location: nullus pixius demonica
Contact:

Post by rustypup »

amdretard wrote:suggesting is 4 differnet class files all with these attributes
err... no... probably closer to 50+ classes.. depending on how robust you want this to be...

when I get a break later I will post some outlines... :)

@axe346 - java!=javascript... java is compiled, *platform independant*, bytecode, whereas jscript is precisely that, endless reams of script....

check out Sun Microsystems for further detail... and discover how simple GUI programming becomes when you move to a fully* OOAD compliant language... ;)

*ok.. almost fully compliant :twisted:
Most people would sooner die than think; in fact, they do so - Bertrand Russel
amdretard
Registered User
Posts: 1177
Joined: 26 Oct 2002, 02:00
Location: Cape town

Post by amdretard »

I need help
first one question when

Code: Select all

public viod setWhatever( int t)
When it is run in the main method eg

Code: Select all

setWhatever(1)
will it take 1 and in the main method will it make t 1

second i have finished my class will this work and am i going in the right direction

please note i am gr10 and have only just learnt classes

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");
    }
}


also when i run return Overlaod it doesnt like it
My friends think i need a life. What do you think?
amdretard
Registered User
Posts: 1177
Joined: 26 Oct 2002, 02:00
Location: Cape town

Post by amdretard »

anyone
My friends think i need a life. What do you think?
User avatar
rustypup
Registered User
Posts: 8872
Joined: 13 Dec 2004, 02:00
Location: nullus pixius demonica
Contact:

Post by rustypup »

1) Naming conventions! Classes start with capitals, methods lowercase, (except the constructor/s), variables lowercase, (mostly). The reason for this is to quickly identify what is a variable and what is a class reference when reading your code...

2) Try to avoid initialising variables outside of the constructor/init methods... also, to enforce encapsulation, declare all members as private ... at present your "time" members can be modified externally...

3) No constructor... although you are provided with a default, this is more of a hindrance than anything else...

4) Your method usage is, while novel, not precisely accurate...

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
}
spend some more time with your notes on class structure.

the flow would be:
1) Instantiate an Elevator:

Code: Select all

Elevator ele = new Elevator(int currentFloor, String op);
2) Modify as necessary:

Code: Select all

ele.setStatus(String state);
3) Query as necessary:

Code: Select all

String time = ele.getTime();
//or 
System.out.println("Elevator: time:-"+ele.getTime());
note that the first instance will retain a reference to the returned variable, therefore preventing garbage collection. in the second instance, as no reference is retained, the returned String is available for collection... it is important to grasp this early on.

<only first glance... vows to offer some alternatives before weekend>
Most people would sooner die than think; in fact, they do so - Bertrand Russel
amdretard
Registered User
Posts: 1177
Joined: 26 Oct 2002, 02:00
Location: Cape town

Post by amdretard »

The Teacher did not want us to use the Whole date thing and he needs to edited it to see if the elevator stops working when the times are reached thanks for the help

Question 1

how would i read from the keyboard for my aplets

Question 2

and how do i create frames as i want the menu to change but not the picture showing the elevators
My friends think i need a life. What do you think?
amdretard
Registered User
Posts: 1177
Joined: 26 Oct 2002, 02:00
Location: Cape town

Post by amdretard »

anyone

Is their a simple way to make buttons
My friends think i need a life. What do you think?
User avatar
hamin_aus
Forum Moderator
Posts: 18363
Joined: 28 Aug 2003, 02:00
Processor: Intel i7 3770K
Motherboard: GA-Z77X-UP4 TH
Graphics card: Galax GTX1080
Memory: 32GB G.Skill Ripjaws
Location: Where beer does flow and men chunder
Contact:

Post by hamin_aus »

Image
User avatar
rustypup
Registered User
Posts: 8872
Joined: 13 Dec 2004, 02:00
Location: nullus pixius demonica
Contact:

Post by rustypup »

amdretard wrote:The Teacher did not want us to use the Whole date thing
sounds as if someone needs to clear his mind and stop teaching then...

Simply put, using a Calendar instance and a SpinnerModel is not that complex...

as for your little project,
1) First let's get the logical design out of the way before concerning ourselves with a GUI... a GUI is a GUI, but if the model is flawed, the GUI is useles...
2) In the design phase, consider what events are likley to be required by the GUI and include them upfront... this way the GUI elements need only be attached through an event model onto the underlying data structure...
3) You only really need to design a single Elevator. If done correctly, you can then proceed to attach multiples thereof to a building...

I personally don't enjoy producing design shortcuts, as in most cases it will provide problems aplenty later, but for the first case, let's consider one of the central shared elements in the project, a Panel with buttons....

Both the Elevator and the Floor have a Panel with a number of buttons on so it would logically follow that you extract from each their common functions and produce a re-usable interface as opposed to a fixed class.. the upside being that any element interfacing with a Panel type element doesn't need to undertsand where it is or what it's attached too...

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);
}
Note the provision for a listener attachment. Next, let's produce the event model... firstly the actual event element...

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;
	}
}
Ok.. so this element provides a simple mask to determine whether a button has been pressed or cleared, and an index to the actual selection... next required bit is the listener interface...

Code: Select all

package elements;
public interface PanelPressListener {
	public void panelPressed(PanelPressedEvent ppe, PanelPressedEvent.PRESS_TYPE type);
}
Plain and simple, the press event and it's type... The reason this is an INTERFACE and not a CLASS is so that any Object can behave as a listener on this type of element, as long as it implements PanelPressListener...

finally, a fully realised Panel class....

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);
		}
	}
}
A few notes on the above...
1) Note that the option array is 2 dimensional.. the option position and whether it's set or not... for a Floor type Panel, the number of options is almost exculsively 2, (up and down... until someone decides to go sideways..).. choices then would be , create a FloorPanel implementation or manage a two state DefaultPanel as is...
2) When an option is armed, its sub-array is set to 1, disarmed this value would be 0...
3) while arrays are always indexed from zero, we must presume that the majority of users will not be pushing such on a panel, (there is no Floor Zero). As such , I have assumed that the indexes provided will always be over by 1....

It is important to note the power of using an interface here... Should you need to implement a 12+1 model, (Note that it is common practice not to have a 13th floor in some countries), you could easily do so without affecting the Elevator or the Floor... all they will require is that it be of type Panel....
Most people would sooner die than think; in fact, they do so - Bertrand Russel
amdretard
Registered User
Posts: 1177
Joined: 26 Oct 2002, 02:00
Location: Cape town

Post by amdretard »

hi agian need help ironing out a error
the program is not running for some reason

help me please

my main

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 ();
        }
    }
}


my class

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
[/quote]
My friends think i need a life. What do you think?
amdretard
Registered User
Posts: 1177
Joined: 26 Oct 2002, 02:00
Location: Cape town

Post by amdretard »

anybody
My friends think i need a life. What do you think?
RuadRauFlessa
Registered User
Posts: 20576
Joined: 19 Sep 2003, 02:00
Location: Bloodbank

Post by RuadRauFlessa »

Whose app is this??

Has anybody thaught of using an alogrithm for the up and down instead of if statements. Propper AI usually irons out most of the errors if done correctly.
:rock: :rock: :rock: :rock: :rock: :rock: :rock: :rock: :rock: :rock:
Spoiler (show)
Intel Core i7-2600k @ 3.4GHz
Corsair Vengence 2x4GB DDR3 2000MHz
Thermaltake Toughpower 850W
ASUS nVidia GTX560 1GB
CoolerMaster HAF 932
User avatar
rustypup
Registered User
Posts: 8872
Joined: 13 Dec 2004, 02:00
Location: nullus pixius demonica
Contact:

Post by rustypup »

a timer element implementation allowing you to register pre-set event periods which could be used to trigger other events..

First the main clock element....

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;
	}
}
...using this class, I could install a preset alert by specifying hour, minute and second... thus installAlert(0, 19, 23, "GOTO") would trigger at 00:19:23, passing the value "GOTO" as the action command...

Note , as well, that this element is instantiated as IDLE... ie, you are required to excplicitly call start()/start(someInt) to initiate the task..

next, the actual timer...

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)));
	}
}
so now we have a Clock and an alert stack, we need to clarify the event itself, (ElevatorClockAlert above...)

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;
	}
}
simply put, this event carries the necessary info to registered ElevatorClockListener objects... which would implement..

Code: Select all

package timers;

public interface ElevatorClockListener {
	public void processAlert(ElevatorClock source, ElevatorClockAlert alert);
}
as for the problems you are experiencing.... bear with me...

1) Check your naming conventions, (again)
2) the new keyword requires a constructor...
SomeClass myVar = new SomeClass()
3) You cannot define non-static methods within another, static, one.. (you are attempting to override Component's paint() method within a sub-classes main() method...
4) When two class templates occur in the same source file, only ONE may declare itself as public, all others must be purely class..
eg..

Code: Select all

public class MyClass{
}
..
class SomeOtherClass{
}
class AnotherOne{
}
...just after a quick glance... again... your main() method should be used only as an entrance point.... don't try to jury-rig it for anything else...

let's start with the basics, break the project into manageable bits, and progress at logical pace... try to ignore any input challenges at this point, that is secondary.... trust me :wink:

do you have a time limit here? ( I have some time tomorrow to look at this...maybe... )
Most people would sooner die than think; in fact, they do so - Bertrand Russel
amdretard
Registered User
Posts: 1177
Joined: 26 Oct 2002, 02:00
Location: Cape town

Post by amdretard »

Thanks for the help

IT was handed in to day didnt work brillaintly but it worked to do the basics
But one thingi didnt fix was when the applet came up it was pushed to the side so only half of it was visible the other was off the screen

thanks agian for the help

@ 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
My friends think i need a life. What do you think?
DJT
I Bribed Ron With Foundry And All I Got Was This Lousy Title
Posts: 9387
Joined: 09 Aug 2002, 02:00
Location: Locked Down
Contact:

Post by DJT »

Yeah, thanks guys, I took some of the advise you gave here.
Want to support my creative work? Drop me a like and subscribe below. It will be greatly appreciated!
Clarke Media - YouTube Channel
Clarke Media - Facebook
User avatar
rustypup
Registered User
Posts: 8872
Joined: 13 Dec 2004, 02:00
Location: nullus pixius demonica
Contact:

Post by rustypup »

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
practice, practice, practice... :D

I have trouble even thinking b2b... a definite must when dealing with an OO language is some form of OO basics.. it just makes more sense when you understand them... also, breaking an app into logical pieces may *sound* complicated, but it really does reduce the bugs and it keeps everything clean and simple...
Most people would sooner die than think; in fact, they do so - Bertrand Russel
Post Reply