Results 1 to 11 of 11
  1. #1
    Join Date
    Mar 2004
    Posts
    149

    Exclamation In desperate and urgent need of help woith java, not very complex

    I am doing a project based on a leisure centre, where the members must swipe thier cards through a gate, each member has a permission rating (ie 1 2 3) and based on that value the gate allows them acces to the area, so a member with permission level 2 cannot get it into a level 3 permission area.

    What i want to do is to have a member object (mr jones, miss collins etc) compare its permission with a specific gate object (eg gate0 gate1, gate2 etc) .

    Instead of having the member object having a method (i think that is the right word) for every possible gate, i want the member to be able to type in a string (eg "gate0") and this decides what gate object the permission values are compared too.

    If the user does not have the sufficient level of permission the system denies access (note that a level 3 permission gives access to the levels below ie. levels 2 and 1)


    I wish to have a messgae that says you went through gate when the permission values are high enough (eg grants acces)

    and a message that says you didnt go through the gate (eg denies access) when the permissions are not high enough

    This my code for the member class

    public class Member
    {

    public int idNumber;
    public String name;
    public String type;
    public int permission;
    public int current_zone;
    public String gate;



    public Member (int id, String n, String t, int p, int z)
    {
    idNumber = id;
    name = n;
    type = t;
    permission = p;
    current_zone = z;
    }

    public void enterGatey (String gate)
    {
    if (gate == "gate0" )
    if (Gate.permission_needed >= permission)
    System.out.println(" you went through the gate");

    Gate.rating = 1;


    }




    }

    This is my code for the Gate class

    public class Gate
    {

    public int gate_number;
    public int access_zone;
    public int exit_zone;
    public static int permission_needed;
    public int security;
    public static int rating;




    public Gate (int gateNumber, int accessZone, int exitZone, int permissionNeeded)
    {
    gate_number = gateNumber;
    access_zone = accessZone;
    exit_zone = exitZone;
    permission_needed = permissionNeeded;

    }





    }

    The classes compile, but I dont know how to use static variables, dont know what they do really, if i take the statics out then i get an error saying

    non-static variable permission_needed cannot be referanced from a static context


    If you can help with this then plz post, im in very desperate need of this code. Im using the bluej ide coz my uni forces me too!
    Last edited by death_entry; 03-06-2004 at 08:56 AM.

  2. #2
    Join Date
    Apr 2003
    Posts
    55
    From what I can see, is that in your 'entergatey' method in your member class, you didn't ref a Gate instance...

    Try this...

    public void enterGatey(String gate, Gate mygate)
    {
    if(gate=="gate0")
    {
    if(mygate.permission_needed>=permission)
    {
    System.out.println("you went through the gate");
    mygate.rating=1;
    }
    }
    }

    see... In the enterGatey method, in my arugment list, i declare a gate instance variable...

    Also, in your member class, you will need to make the "mygate" ref when your declaring your public / private variables, so that you can use that gate instance through out your member object.

    public Gate mygate = new Gate();

    Cedric

  3. #3
    Join Date
    Mar 2004
    Posts
    149
    the 1st bit of code works, thanks!

    but for the bit where i need to make referance to it do u mean



    public class Member
    {

    public int idNumber;
    public String name;
    public String type;
    public int permission;
    public int current_zone;
    public String gate;
    public Gate mygate = new Gate();


    when it compiles it says

    Gate(int,int,int,int) in Gate cannot be applied to ()

    what does that mean? Im tyring to figure it out, but this is all new to me so im not sure whats going on

  4. #4
    Join Date
    Apr 2003
    Posts
    55
    Ok,

    Im sorry i did catch this in my last post. What you did was overloaded your Gate Object's constructor function.

    Let me explan if you don't know what i mean.

    When you create a class, by default the class creates a blank constructor function. A constructor function is the function that is automatically called when a new instance of an object is created. The constructor function is the function with the same name as the class itself. this is your code below...

    public Gate (int gateNumber, int accessZone, int exitZone, int permissionNeeded)
    {
    gate_number = gateNumber;
    access_zone = accessZone;
    exit_zone = exitZone;
    permission_needed = permissionNeeded;
    }

    See your Gate method has the same name as your class Gate so the JVM saw this and said, "Ok, he must want to modify the constructor"

    Now when you said Gate myGate = new Gate(); the statement is right but look at your required arument list in your method. You have 4 required Int's (int gateNumber, int accessZone, int exitZone, int permissionNeeded) So theres 2 ways you can handle this.

    1.) say something like this...
    Gate mygate = new Gate(gateNumber, accessZone, exitZone, permissionNeeded);
    and having those 4 int's being the 'acutal' variable names of the ints you need there.

    or

    2.Add this method to your Gate Object...
    public Gate ()
    {
    gate_number = 0;
    access_zone = 0;
    exit_zone = 0;
    permission_needed = 0;
    }

    This will create a Gate object without error when you say...
    Gate myGate = new Gate();
    Then you can manually add/set the member variables inside the object like...
    mygate.gatenumber = whatever;
    mygate.access_zone = whatever;
    and so on (and whatever is being either a hard coded int like 4 or a int variable name)

    I hope this made sense, try that, and it will work, if you have more problems, just post.

    Cedric

  5. #5
    Join Date
    Mar 2004
    Posts
    149
    what i dont get is why you need

    public Gate mygate = new Gate();

    everything seems to work if its not there. I have made 2 member objects with permissions (1 then 2) and gates with permissions 1,2,3 and the 1st member only goes through the the 1st gate and the 2nd member can go through the first 2 gates. which is whats meant to happen.

    I think that ur 2nd solution means that the gates values are hard coded, id like them to be added when the gate is made which is whats happening.

    Im looking at ur 1st suggestion at the moment.

    like this

    public class Member
    {

    public int idNumber;
    public String name;
    public String type;
    public int permission;
    public int current_zone;
    public String gate;
    Gate mygate = new Gate(gateNumber, accessZone, exitZone, permissionNeeded);
    it says cannot resolve symbol gateNumber

    i jst dont get why that line is needed, and why i cant just leave it outds
    Last edited by death_entry; 03-07-2004 at 02:21 PM.

  6. #6
    Join Date
    Apr 2003
    Posts
    55
    Ok, maybe i was just looking at it wrong.

    My thinking was that each member objected, needed a reference of a Gate, and if thats not the case then go ahead and take it out.

    But still, in your Gate class, you should still make a constructor function with no arugments (just makes it more scaliable later, if someone take over your Gate Class and needs to make a new gate but don't know for sure all the gate details till later)

    But take out the Gate refence and try it, if it works case solved
    its hard to trouble shoot these things without the .java file/s in front of you.

    Cedric

  7. #7
    Join Date
    Mar 2004
    Posts
    149
    thanks for ur help, now i can go to 3rd year at uni

    I dont think il be needing the constructor with no args but il keep a watch out for it.

    many many mnay thanks

  8. #8
    Join Date
    Apr 2003
    Posts
    55
    Not a problem,

    any more questions, just post.

    Cedric

  9. #9
    Join Date
    Mar 2004
    Posts
    149
    I want to be able to make a non-fixed 2D array that needs to store the following.

    Idnumber------Zone
    101----------------1
    102----------------1
    103----------------3

    I need to be able to update the zone value how do u do this also. So it says something like find idnumber 101 and change the value next to it

    Do u know how?

  10. #10
    Join Date
    Apr 2003
    Posts
    55
    Yeah,

    I know how to do this, I would use an arraylist though. Im at work at the moment, but when I get home, Ill show you my sol.

    Cedric

  11. #11
    Join Date
    Mar 2004
    Posts
    149
    I was thinking that could u have an array of objects, (eg members) and then the object has 2 variables memberid and zone. Then how do u get a gate to change the zone id, how do u add objects, and how do u search and print off say all the memberids in zone 2?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •