Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 boolean expression problem!
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
nelsonkkyy




PostPosted: Wed Oct 19, 2011 7:22 pm   Post subject: boolean expression problem!

Quote:

import java.util.*;

public class LockTester
{
public static void main (String[] args)
{
// Scanner scanner = new Scanner (System.in);

Lock lock = new Lock ('C', 'A', 'K');
lock.set ('C'); // first turn
System.out.print ("The lock will open? ");
lock.pull (); // try
System.out.println (lock.isOpen ());
System.out.println ("Expected: false"); // only one turn

lock.set ('A'); // second turn
System.out.print ("The lock will open? ");
lock.pull (); // try
System.out.println (lock.isOpen ());
System.out.println ("Expected: false"); // only two turns

lock.set ('K'); // third turn
System.out.print ("The lock is open? ");
System.out.println (lock.isOpen ());
System.out.println ("Expected: false"); // haven't pulled yet
System.out.print ("The lock will open? ");
lock.pull (); // try
System.out.println (lock.isOpen ());
System.out.println ("Expected: true"); // correct combination

lock.set ('L'); // turning dial while lock is open
System.out.print ("The lock is open? ");
System.out.println (lock.isOpen ());
System.out.println ("Expected: true"); // it is still open

lock.close (); // closing the lock resets dials
System.out.print ("The lock is open? ");
System.out.println (lock.isOpen ());
System.out.println ("Expected: false"); // we just closed it
}
}





Quote:



public class Lock
{

//instance variables
char word;
char password;
boolean status;

public Lock(char x ,char y ,char z )
{

password = (char) (x + y + z);

}

public void set(char x)
{


word = (char)(x);

.
}

public void pull()
{

if (word == password)
status = true;

}

public boolean isOpen()
{


if (status = true)
{System.out.println ("true") ;}
else
{System.out.println ("false") ;}

return status;

}

public void close()

{
status = false ;
}
}
/////////////////////// the out put is always true when the word is not even equal to password, how do i make it prints false when the word is not equal to password????///////////////////
[/quote]
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Wed Oct 19, 2011 7:30 pm   Post subject: RE:boolean expression problem!

1) you already have a question thread for this

2) because the value of "status" is always "true" at the time you print anything.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Zren




PostPosted: Thu Oct 20, 2011 12:55 am   Post subject: RE:boolean expression problem!

= (assignment) is not the same as == (operatator). And yes, you can assign variables inside if statements legally (and lots of other places).

These are equivalent (a is a boolean).
if (a == true) {}
if (a) {}

Assignment inside if statement. The assignment will return the variable being assigned.
if ((a = true) == true) {}
So it'll turn into this afterwards.
if (a == true) {}

So since you had:
if (a = true) {}

It returned:
if (a) {}

Which since you just assigned as true.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: