Logical Error!!! Please help


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Logical Error!!! Please help

  1. #1
    Join Date
    Mar 2007
    Posts
    6

    Logical Error!!! Please help

    Here's the problem and the solution that I could do. It has some LOGICAL errors. Can anyone please correct this program and make it work. Any help is greatly appreciated.


    Problem 2: Write a class called Lock which has methods open() and close() in it. Your class should be written in such a way that no more than one Lock object may be open at any given time. Now, write a program called LockTest.java which creates five Lock objects. Using LockTest.java, test if you can have more than one Lock open at any given time. (Hint: You will need to define a static variable which is common to all objects of a class and which will maintain in it the number of locks which are open. Assume that when a Lock object is created, it is in closed state. You may use a constructor in class Lock but you do not need to.)



    import java.util.*;

    public class Lock
    {
    private static boolean aLockIsOpen;
    private boolean thisLockIsOpen;

    public static boolean isAnyLockOpen()
    {
    return Lock.aLockIsOpen;
    }

    public boolean open()
    {
    if(Lock.aLockIsOpen == false)
    {
    Lock.aLockIsOpen = true;
    thisLockIsOpen = true;
    return true;
    }
    return false;
    }

    public boolean close()
    {
    if(thisLockIsOpen)
    {
    Lock.aLockIsOpen = false;
    thisLockIsOpen = false;
    return true;
    }
    return false;
    }
    }








    import java.util.*;

    public class Lock
    {
    private static boolean aLockIsOpen;
    private boolean thisLockIsOpen;

    public static boolean isAnyLockOpen()
    {
    return Lock.aLockIsOpen;
    }

    public boolean open()
    {
    if(Lock.aLockIsOpen == false)
    {
    Lock.aLockIsOpen = true;
    thisLockIsOpen = true;
    return true;
    }
    return false;
    }

    public boolean close()
    {
    if(thisLockIsOpen)
    {
    Lock.aLockIsOpen = false;
    thisLockIsOpen = false;
    return true;
    }
    return false;
    }
    }

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    You will create five instances of Lock ... only one can be locked at any one time.... the thing with a static data field is that only a single instance of that data member exists for the class - shared across all instances of the class - rather than being contained within the instance. Therefore, you will have lockA, lockB, lockC, lockD, lockE ... the lock class will have lock, unlock, isLocked methods. You should also have a data field which tells that instance that IT locked the lock so that it canUnlock() the lock - not only does your lock method set the static field locked to 1, but it sets its own iLockedIt field to TRUE as well ... and your unLock method will test to see if its own iLockedIt is set to TRUE and, if so, will set locked to 0 and then iLockedIt to FALSE.

    [In looking back at the question I had to edit my response since, in the call of the question, your locked field should be an int rather than a boolean]
    Last edited by nspils; 03-13-2007 at 11:09 PM.

Similar Threads

  1. Logical error ?
    By Aristo in forum Java
    Replies: 4
    Last Post: 10-15-2006, 01:56 PM
  2. How determine Logical Size of HDD
    By meteo in forum VB Classic
    Replies: 2
    Last Post: 02-27-2001, 03:14 PM
  3. How determine Logical Size of HDD
    By meteo in forum VB Classic
    Replies: 0
    Last Post: 02-27-2001, 03:09 AM
  4. Logical compare with a large number
    By Craig in forum ASP.NET
    Replies: 0
    Last Post: 01-31-2001, 02:34 AM
  5. Restore error, logical page problem
    By Aaron Knoph in forum VB Classic
    Replies: 0
    Last Post: 04-11-2000, 02:36 AM

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