urgent need of help, not very complex, comparing values of 2 objects


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: urgent need of help, not very complex, comparing values of 2 objects

  1. #1
    Join Date
    Mar 2004
    Posts
    1

    Exclamation urgent need of help, not very complex, comparing values of 2 objects

    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



    quote:
    --------------------------------------------------------------------------------
    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


    quote:
    --------------------------------------------------------------------------------

    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!

  2. #2
    Join Date
    Feb 2004
    Posts
    541
    ok a class can have two types of variables....class variables and instance variables. Class variables are static. This means that they are assigned memory at runtime when the program first starts, and hence they "exist" for the duration of your program. You can access them anytime you want as long as you are within scope. An instance variable is specific to an object. When you create a object using the "new" keyword all the instance variable in the class are assigned memory, and they only exist while the object exists. They have different values in each object too...they're completely seperate from each other.

    The reason the code you posted won't compile without the static variables is because you're accessing Gate.permission_needed in the member class. You don't have a gate object so there are no instance variables...ie the only variables that exists from the gate class are the ones you declared static. The other ones don't exist at all. You need to create an object of the Gate class, that way you won't need any static variables. You don't need to use static variables in your case.

  3. #3
    Join Date
    Feb 2004
    Posts
    808

    Re: urgent need of help, not very complex, comparing values of 2 objects

    Originally posted by death_entry


    If you can help with this then plz post, im in very desperate need of this code.
    You need to disconnect from the idea that what you see in a java file IS what is found in memory when you run a program.

    A java file is a BLUEPRINT or an "architect's drawing" of a class..

    In exactly the same way that you cant open the window on an architect's drawing, you cant refer to a variable inside a java class file

    In the real world, be look at the architect's drawing, then we make the building, with brcks, wood, etc.. ONLY THEN can we open the window

    Java looks at your class file and sees this is how to make an object in memory.. but only when it has made it, can you start to use it.

    We make objects with:

    <Object type> <our name for the object> = new <Object type>(<parameters given to the object upon creation>)

    Gate myGate = new Gate("number 1", 2, 2, 2)

    Gate -> the type of object (tells java to use the Gate.class file as a blueprint)
    myGate -> a name for our gate. we can forever more refer to this particular creation as "myGate"

    -

    so, do you see?

    when you say:

    Gate.permission_needed

    Gate is just a BLUEPRINT, an architects drawing.. it doesnt have a real variable that you can manipulate, no more than:

    Building.openTheWindow()

    is possible..

    -
    another way:
    imagine youre standing in the middle of a housing estate. and i say "go knock on the door of that Building"
    you say "but there are hundreds of Buildings here - which one do you mean?"


    Building is just a type - a concept. You never say "i live in Building" .. you say "I live in A building"; you live in an instance of a building, a particular one amongst hundreds that look different, but belong to the same group of things.. they all belong to the same concept, but they are all different instances of that concept.. different _realisations_ of it

    Building number_26_on_Smith_Street = new Building();

    oop, we just gave a building a name; we just made a new, real, instance of a Building. now we can say:
    number_26_on_Smith_Street.knockOnTheDoor()

    it makes so much more sense than Building.knockOnTheDoor() because it has a proper name, that represents a real actual object, and not just a concept


    is it easier to distinct Gate.java from an actual Gate object in your program? you have to make a "new Gate" before you can get any informaiton about it.. up until then, its just a list of instructions in the box, not an actual thing you can interact with..


    -

    further note: as a java newbie, you should be aware of, but never use "static" other than in "public static void main(String[] argv)" - static is very non-OO, and can lead to great misunderstandings in your progrmamming career. When the time is right, you will be taught/will implicitly understand what static is for.. at the moment, just know that static is bad. never use "static", and if the compiler is telling you about static contexts etc.. then youve forgotten to make an INSTANCE of an object before trying to use it.. k?
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  4. #4
    Join Date
    Feb 2004
    Posts
    808

    Re: urgent need of help, not very complex, comparing values of 2 objects

    Originally posted by death_entry

    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.


    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;


    heres some more info:

    you need to separate your gates and your members.. at the moment, your Member class contains references to a Gate..
    in real life, people dont have Gates, nor are they born with the knowledge of how to enter them, so you should redesign your sytem to be more like this:

    Members have a method that gets their security level

    e.g.:
    Code:
      public int getSecurityLevel(){
        return permission;
      }
    and gates should have a method that makes them open, but only if the security level is right:

    Code:
      public int openGate(int securityLevelPresented){
        if(securityLevelPresented >= permission_needed){
          return the_zone_they_are_now_in;
        }
        else{
          return -1;
        }
      }
    now, you need some code to manage these 2 objects because they are totally unaware of each other, and dont know how to interact

    Code:
    class SecurityManager{
      String whoIsIt = askUser("what is your name?");
      String whatGate = askUser("what gate do you want to go through?");
    
      Member tempMember = findUser(whoIsIt);
      Gate tempGate = findGate(whatGate);
    
      int secLevel = tempMember.getSecurityLevel()
      int result = tempGate.open( secLevel )
    
      if(result == -1){
        print("sorry "+tempMember.getName()+" but you are not allowed in that gate")
      }
      else{
        tempMember.setNewZone( result );
        print("thankyou "+tempMember.getName()+" you are now in zone number "+tempMember.getCurrentZone();
      }
    and your SecurityManager will run that process in a loop - remember this is an academic excercise, youre not here to write a world-beating entry management system, youre here to learn about objects, get() and set() methods and simple logic..

    now, think about that.. the questions you will no doubt come to as are things like "how can i actually code those FIND methods though?"

    well, you can either keep all your gates/members in an array[] and list through it, calling getName() on each one and finding out if its the right one
    or you can use something that already has a find method built in, like hashtable
    Personally, i think you should do it the first way first, as a learning excercise.

    VERY IMPORTANT NOTE:

    you can not do this to compare strings:

    if(gate.getName() == "gate0")

    == is ONLY for use on one of these:
    int, byte, short, long, double, float, boolean

    they are called primitives. Everything else is an Object, and using == on it simply comapres the MEMORY ADDRESS that the object is found at, with the other object's memory address. hence you might have 2 strings of the same content, at different parts of memory:

    String s1 = "ABC"; //at address 0x1234
    String s2 = "ABC"; //at address 0x7890

    when you do this;
    s1 == s2

    it becomes:
    0x1234 == 0x7890

    the 2 numbers are different, so the result is FALSE

    -

    to compare objects, you must ALWAYS use the .equals() method:

    s1.equals(s2) //returns TRUE
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center