Boolean help

K

kml4ever28

I'm taking a class at my college, but I'm afraid I just don't quite
understand Boolean and it's applictation. I know it has either a true
or false value, but that's about it. For example, let me include a
snipet from an hw assignment.

Write a Boolean expression that compares the value held by the variable
letter of type char to the constant 'Z' and yields true if letter
is less then 'Z'.
I really don't think the following is right
char letter = 'Z'
boolean isEqual = (letter) ;
If (isEqual)
System.out.print("False");
Else
System.out.print("True");
And I have no idea how I would even go about this.
Write a Boolean expression that yields true if the variable letter of
type char holds a value that is between 'A' and 'Z' inclusive.

Please, I would appreciate any help in understanding this subject.
 
R

Roedy Green

System.out.print("True");
And I have no idea how I would even go about this.
Write a Boolean expression that yields true if the variable letter of
type char holds a value that is between 'A' and 'Z' inclusive.

See http://mindprod.com/jgloss/boolean.html
and http://mindprod.com/jgloss/binary.html


I often see newbies having trouble with boolean expressions. I never
did. I loved them from the instant I saw them. They made perfect sense
to me. I preferred using them to ifs. Other people reading my code
chastised me for using the "weird" boolean variable feature.

The appeal was the way the state of the universe was so explicit, so
easy to read. In ordinary code, the state of the universe is encoded
in a complex way by where you have managed to lose yourself in an if
nest.

I liked to reduce the problem to a set of booleans, then do a little
logic combining them that to me read like poetry.

Perhaps it comes from my math background.

Anyway, with those hints, perhaps you can take another crack at it.
 
S

Stefan Schulz

I'm taking a class at my college, but I'm afraid I just don't quite
understand Boolean and it's applictation. I know it has either a true
or false value, but that's about it. For example, let me include a
snipet from an hw assignment.

Write a Boolean expression that compares the value held by the variable
letter of type char to the constant 'Z' and yields true if letter
is less then 'Z'.

You need to compare the value of letter to the value of 'Z'. The result
will either be true or false. Do you know how to compare values?
I really don't think the following is right
char letter = 'Z'
boolean isEqual = (letter) ;
If (isEqual)
System.out.print("False");
Else
System.out.print("True");

Why do you include the if? If you completed isEqual correctly, you can
just output / return that.
And I have no idea how I would even go about this.
Write a Boolean expression that yields true if the variable letter of
type char holds a value that is between 'A' and 'Z' inclusive.

When is letter between A and Z inclusive? If it is larger or equal to 'A'
and lesser or equal to 'Z'. Now which operations do you need? A
larger-or-equal, a lesser-or-equal, and an and. Look up in your java
tutorial how to achieve that (hint: Boolean operators, Comparison
operators).
 
T

Thomas Hawtin

Stefan said:
When is letter between A and Z inclusive? If it is larger or equal to 'A'
and lesser or equal to 'Z'. Now which operations do you need? A
larger-or-equal, a lesser-or-equal, and an and. Look up in your java
tutorial how to achieve that (hint: Boolean operators, Comparison
operators).

Technically, you also need to know that the character set used by Java,
Unicode, represents unaccented, capital Latin letters in alphabetical
sequence, with no others between. EBCDIC, for instance, does not have
that property.

Tom Hawtin
 
T

Thomas Fritsch

Write a Boolean expression that compares the value held by the variable
letter of type char to the constant 'Z' and yields true if letter
is less then 'Z'.
The assignment talks about "less than",. You therefore have to use "<".
Let us say you have a variable:
char letter = 'A';
Then the boolean expression you need is: letter < 'Z'
You can use that expression in an if statement:
if (letter < 'Z')
System.out.println("Yeah, it is less!");
Or you can assign it to a boolean variable:
boolean isLess = (letter < 'Z');
 
A

Andrew McDonagh

Thomas said:
The assignment talks about "less than",. You therefore have to use "<".
Let us say you have a variable:
char letter = 'A';
Then the boolean expression you need is: letter < 'Z'
You can use that expression in an if statement:
if (letter < 'Z')
System.out.println("Yeah, it is less!");
Or you can assign it to a boolean variable:
boolean isLess = (letter < 'Z');

the question says 'less than', but the English language* says 'Z' is
'Greater Than A - Y' - so simple boolean logic steps in with,

if (letter == 'Z')
System.out.println("equal to Z");
else
System.out.println("less than Z");


*(I'm taking the OPs language to be UK English - lets not even get
started on language, locale, Casing, etc problems the poor bugger would
have to deal with)
 
T

Thomas G. Marshall

Roedy Green said something like:
See http://mindprod.com/jgloss/boolean.html
and http://mindprod.com/jgloss/binary.html


I often see newbies having trouble with boolean expressions.

I often see the same confusion among the noobs. I think that it comes from
the instructor not emphasising that

if (a < b) ...

is functionally equivalent to

boolean test = (a < b);
if (test) ...

It is critical that the mere notion of

if (a < b) ...

be broken down so that the "a" and "b" do not appear to be part of the "if"
statement, which in the eyes of a newbie it probably will. The student must
always know that the only thing that /ever/ follows an "if" is a boolean
expression.



....[rip]...

--
Puzzle: You are given a deck of cards all face down
except for 10 cards mixed in which are face up.
If you are in a pitch black room, how do you divide
the deck into two piles (may be uneven) that each
contain the same number of face-up cards?
Answer (rot13): Sebz naljurer va gur qrpx, qrny bhg
gra pneqf naq syvc gurz bire.
 
T

Thomas G. Marshall

Andrew McDonagh said something like:
the question says 'less than', but the English language* says 'Z' is
'Greater Than A - Y'

I've been doing this a lonnnnng time and your statement took /me/ a few
goings over to understand.

Greater than A /minus/ Y ? And in single quotes no less?

lol

- so simple boolean logic steps in with,

if (letter == 'Z')
System.out.println("equal to Z");
else
System.out.println("less than Z");


*(I'm taking the OPs language to be UK English - lets not even get
started on language, locale, Casing, etc problems the poor bugger would
have to deal with)



--
Puzzle: You are given a deck of cards all face down
except for 10 cards mixed in which are face up.
If you are in a pitch black room, how do you divide
the deck into two piles (may be uneven) that each
contain the same number of face-up cards?
Answer (rot13): Sebz naljurer va gur qrpx, qrny bhg
gra pneqf naq syvc gurz bire.
 
A

Andrew McDonagh

Thomas said:
Andrew McDonagh said something like:



I've been doing this a lonnnnng time and your statement took /me/ a few
goings over to understand.

Greater than A /minus/ Y ? And in single quotes no less?

lol

:) oh yes - never actually meant that weirdly enough.

Z is Greater than A through to Y.

the written word brilliant eh - clear a day light to one person, clear
as mud to another.

Eats, Shoots & Leaves

(he Zero Tolerance Approach to Punctuation -
http://www.amazon.com/exec/obidos/tg/detail/-/1592400876/002-6991292-9413600?v=glance)
 
?

.

I'm taking a class at my college, but I'm afraid I just don't quite
understand Boolean and it's applictation. I know it has either a true
or false value, but that's about it. For example, let me include a
snipet from an hw assignment.

Boolean is actually more than just true and false. If you want to know a
little history on it, look up George Boole (after which the term Boolean
comes from) and Claude Shannon (an electrical engineer who applied George
Boole's concepts to electrical circuits and electric circuits to solving
Boolean algebra problems). You can also look up terms such as 'calculus of
reasoning', 'propositional calculus', 'boolean algebra' or 'boolean
logic'.

Boolean logic has things like, "If X is true and Y is true then Z is
true." For example, "If [there are clouds when it rains] and [it is
raining right now] then [there are clouds right now]."

There are various laws based on Boolean logic that you can use to rework
your code. For example,

if(X) {
// print success
} else {
// print failure
}

can also be written as:

if(!X) {
// print failure
} else {
// print success
}

I have occasionally seen students code things like:

boolean result = o.determineResult();
if(result) {
;
} else {
o.runImportantMethod();
}

when it could have been written:

boolean result = o.determineResult();
if(!result) {
o.runImportantMethod();
}
Write a Boolean expression that compares the value held by the variable
letter of type char to the constant 'Z' and yields true if letter
is less then 'Z'.
I really don't think the following is right
char letter = 'Z'
boolean isEqual = (letter) ;
If (isEqual)
System.out.print("False");
Else
System.out.print("True");

A few problems. First, you are assigning the value letter, which is a char
to isEqual which is a boolean. If you change the variables to there types
does it make sense? i.e.

isEqual = letter
boolean = char

If letter is 'Z' does it make sure to ask the question, "Is letter true
or false?" It does not. Therefore if isEqual equals letter the
sentence, "Is isEqual true or false?" won't make sense either.

Additionally, the problem has the following data:

a boolean expression
the variable letter
constant 'Z'

You need at least three pieces of data. Note that the variable letter and
the constant 'Z' are both of type char. You can compare char to char. Any
comparison forms a boolean expression.
And I have no idea how I would even go about this.
Write a Boolean expression that yields true if the variable letter of
type char holds a value that is between 'A' and 'Z' inclusive.

Please, I would appreciate any help in understanding this subject.

All symbols, letters, numbers, etc. are stored in the computer as a
numeric value. So if I do something like:

char letter = (char)50;
System.out.println(letter);

it will print out something (on many systems it will print '2'). So if 'A'
has a numeric value of type char and letter has a numeric value of type
char, I can compare the two char and get a boolean expression. Using
boolean logic I should be able to convert the problem expressed in English
into an if statement.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top