6 Replies - 2374 Views - Last Post: 18 March 2010 - 04:38 AM Rate Topic: -----

#1 d'kid  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 8
  • Joined: 03-March 10

don't know how to stop a loop

Posted 17 March 2010 - 03:37 PM

i have an assignment given which is to test a door.but i dont know how to stop the loop...then,i maybe i don't really understand what the question want.
here is the question:

A computer game usually has many different objects that can be seen and manipulated. One typical object is a door. Whether a player runs through a castle, attacks the forces of an evil empire, or places furniture in a room, a door often comes into play.

Implement a Door class as described below as well as a TestDoor class that instantiates three Door objects labelled “Enter”, “Exit” and “Treasure”. The “Enter” door should be left unlocked and opened. The “Exit” door should be left closed and locked. The “Treasure” door should be left open but locked.

A Door class

A Door object can
• Display an inscription,
• Be either open or closed, and
• Be either locked or unlocked

Here are some rules about how Door works.
• Once the writing on the Door is set, it cannot be changed.
• You may open the Door if and only if it is unlocked and closed.
• You may close the Door if and only if it is open.
• You may lock a Door if and only if it is unlocked, and unlock a Door if and only if it is locked. You should be able to check whether or not a Door is closed, check whether or not it is locked, and look at the writing on the Door if there is any.

The instance variables of a Door class are:
• String inscription
• boolean locked, and
• boolean closed.

The methods (all public) should be:
• Door(String c); //Constructor – initializes the Door with inscription c, closed and locked.
• isClosed(); //Returns true if Door is closed.
• isLocked(); //Returns true if Door is locked.
• open() //Opens a Door if it is closed and unlocked.
• close() //Close door if it is unlocked.
• lock() //Locks a Door if it is unlocked.
• unlock(); //Unlocks a Door if it is locked.

Appropriate error messages should be displayed, if any conditions of the methods are violated.

i already done both of test class and main class,but stuck in how to make it stop to loop..:Attached File  testdoor1.txt (3.34K)
Number of downloads: 173

correct me if i'm wrong...tq ^^

Is This A Good Question/Topic? 0
  • +

Replies To: don't know how to stop a loop

#2 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: don't know how to stop a loop

Posted 17 March 2010 - 03:42 PM

Please post your code here, using the code tags :code:

It also looks like you've got some inconsistencies with your variable naming, but some error messages would be nice
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: don't know how to stop a loop

Posted 17 March 2010 - 09:19 PM

All loops (for, while, do) have a continue and a break statement
Hope this helps
Was This Post Helpful? 0
  • +
  • -

#4 d'kid  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 8
  • Joined: 03-March 10

Re: don't know how to stop a loop

Posted 18 March 2010 - 12:18 AM

Ok....i redo my code cuz juz realize some error in calling the main class.but still the loop cant stop even though i key in option to quit..here is it:
import java.util.Scanner;
public class TestDoor
{
	static Door Door1;
	static Door Door2;
	static Door Door3;
	static Scanner input=new Scanner(System.in);

	public static void main(String[] args)
	{
		Door1=new Door("Enter");
		Door1.setOpen();
		Door1.setUnlocked();
		Door2=new Door("Exit");
		Door3=new Door("Treasure");
		Door3.setOpen();

		boolean done = false;

		int opt;

		do
		{
			chooseDoor();

			opt = input.nextInt();

			switch(opt)
			{
			case 1:
				DOOR1();
				break;
			case 2:
				DOOR2();
				break;
			case 3:
				DOOR3();
			default:
				System.out.println();
			}
		}while(!done);
	}
	public static void Opt()
	{
		System.out.println("***ur option***");
		System.out.println("1.Open door");
		System.out.println("2.Close door");
		System.out.println("3.Lock door");
		System.out.println("4.Unlock door");
		System.out.println("5.Check condition of the door");
		System.out.println("6.Quit");
	}
	public static void chooseDoor()
	{
		System.out.println("Choose Your Door");
		System.out.println("1. Enter");
		System.out.println("2. Exit");
		System.out.println("3. Treasure");
	}

	public static void DOOR1()
	{
		int opt;
		boolean done = false;
		do
		{
			Opt();
			opt=input.nextInt();
			for(int i=0;i<6;i++)
			{
				System.out.println();
			}
			switch(opt)
			{
			case 1:
				Door1.open();
				break;
			case 2:
				Door1.close();
				break;
			case 3:
				Door1.lock();
				break;
			case 4:
				Door1.unlock();
				break;
			case 5:
				if(Door1.isLocked()==true)
					System.out.println("Door is locked");
				else
					System.out.println("Door is unlocked");
				if(Door1.isClosed()==true)
					System.out.println("Door is closed");
				else
					System.out.println("Door is open");
				break;
			case 6:
				done = true;
				break;
			default:
				System.out.print("|Out of option,key in again|\n");
			}
		}while(!done);
	}

	public static void DOOR2()
	{
		int opt;
		boolean done = false;
		do
		{
			Opt();
			opt=input.nextInt();
			for(int i=0;i<6;i++)
			{
				System.out.println();
			}
			switch(opt)
			{
			case 1:
				Door2.open();
				break;
			case 2:
				Door2.close();
				break;
			case 3:
				Door2.lock();
				break;
			case 4:
				Door2.unlock();
				break;
			case 5:
				if(Door3.isLocked()==true)
					System.out.println("Door is locked");
				else
					System.out.println("Door is unlocked");
				if(Door3.isClosed()==true)
					System.out.println("Door is closed");
				else
					System.out.println("Door is open");
				break;
			case 6:
				done = true;
				break;
			default:
				System.out.print("|Out of option,key in again|\n");
			}
		}while(!done);
	}

	public static void DOOR3()
	{
		int opt;
		boolean done = false;
		do
		{
			Opt();
			opt=input.nextInt();
			for(int i=0;i<6;i++)
			{
				System.out.println();
			}
			switch(opt)
			{
			case 1:
				Door3.open();
				break;
			case 2:
				Door3.close();
				break;
			case 3:
				Door3.lock();
				break;
			case 4:
				Door3.unlock();
				break;
			case 5:
				if(Door3.isLocked()==true)
					System.out.println("Door is locked");
				else
					System.out.println("Door is unlocked");
				if(Door3.isClosed()==true)
					System.out.println("Door is closed");
				else
					System.out.println("Door is open");
				break;
			case 6:
				done = true;
				break;
			default:
				System.out.print("|Out of option,key in again|\n");
			}
		}while(!done);
	}
}

////////////////////////////////////////////////////////
public class Door
{
	private boolean closed;
	private boolean locked;
	private String inscription;


	public Door(String c)
	{
		inscription=c;
		closed=true;
		locked=true;
	}
	public String prtInscription()
	{
		return inscription;
	}
	public boolean isClosed()
	{
		return closed;
	}
	public boolean isLocked()
	{
		return locked;
	}
	public void open()
	{
		if(closed==true&&locked==false)
		{
			System.out.println(".....Door open.....\n");
			closed = false;
		}
		else
			System.out.println("---Closed,door is locked.Try again---\n");
	}
	public void close()
	{
		if(locked==false)
		{
			System.out.println(".....Door close.....\n");
			closed = true;
		}
		else
			System.out.println("---Unlock the door first.Try aqain---\n");
	}
	public void lock()
	{
		if(locked==false)
		{
			System.out.println(".....Door lock.....\n");
			locked = true;
		}
		else
			System.out.println("---Door is already locked.Try again---\n");

	}
	public void unlock()
	{
		if(locked==true)
		{
			System.out.println(".....Door unlock......\n");
			locked = false;
		}
		else
			System.out.println("---Door is already unlocked.Try again---\n");
	}
	public void setOpen()
	{
		closed=false;
	}
	public void setUnlocked()
	{
		locked=false;
	}
}



Was This Post Helpful? 0
  • +
  • -

#5 westforduk  Icon User is offline

  • D.I.C Head

Reputation: 24
  • View blog
  • Posts: 140
  • Joined: 16-August 07

Re: don't know how to stop a loop

Posted 18 March 2010 - 12:58 AM

Have you ensured that you have broken the while(!done) loop in the correct place? This should allow the program to terminate.

This post has been edited by westforduk: 18 March 2010 - 12:59 AM

Was This Post Helpful? 0
  • +
  • -

#6 aniri  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 54
  • View blog
  • Posts: 657
  • Joined: 24-November 09

Re: don't know how to stop a loop

Posted 18 March 2010 - 01:08 AM

In the first loop (the one from main) you do not set the done variable to true. That's why the loop is not stopping.
Was This Post Helpful? 0
  • +
  • -

#7 d'kid  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 8
  • Joined: 03-March 10

Re: don't know how to stop a loop

Posted 18 March 2010 - 04:38 AM

View Postaniri, on 18 March 2010 - 12:08 AM, said:

In the first loop (the one from main) you do not set the done variable to true. That's why the loop is not stopping.


Aaaaa.. i get it now.tq very much... :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1