S. Freeman

Greenhorn

Posts: 12

posted 4 years ago

hehe.. Howdy! i am trying to get a better grasp of Boolean conditions so i can come up with solutions to problems.

While doing some programming exercises i ran into following assignment. Anyway, right to business:

Write a program that reads four integers and prints “two pairs” if the input consists

of two matching pairs (in some order) and “not two pairs” otherwise. For example,

1 2 2 1 two pairs

1 2 2 3 not two pairs

2 2 2 2 two pairs

x y z k

My attempt:

For the inputs that are shown as examples my Boolean condition enters the if block as it should. But in case i enter 1 3 3 3, or 3 1 3 3, or 3 3 1 3

the if statement is still entered, and it shouldn't be since there are no two pairs and, so the else statements should be executed.

Could someone help me out understand how i should come up with correct boolean condition.

While doing some programming exercises i ran into following assignment. Anyway, right to business:

Write a program that reads four integers and prints “two pairs” if the input consists

of two matching pairs (in some order) and “not two pairs” otherwise. For example,

1 2 2 1 two pairs

1 2 2 3 not two pairs

2 2 2 2 two pairs

x y z k

My attempt:

For the inputs that are shown as examples my Boolean condition enters the if block as it should. But in case i enter 1 3 3 3, or 3 1 3 3, or 3 3 1 3

the if statement is still entered, and it shouldn't be since there are no two pairs and, so the else statements should be executed.

Could someone help me out understand how i should come up with correct boolean condition.

posted 4 years ago

- 1

Here is how I read your if condition:

Let's see how that would evaluate an example that should fail:

What I would do is break down problem into discreet steps. The first thing you need to know is that, with 4 elements, in order to get two pairs, X must be in a pair with one other element:

(x is the same as y) OR (x is the same as z) OR (x is the same as k)

Given that, in order to have two pairs, there is a requirement on the only two remaining elements left - they must be equal as well, so there is a remaining condition that must be met:

((x is the same as y) WHAT (z is the same as k)) OR ((x is the same as z) WHAT (y is the same at k)) OR ((x is the same as k) WHAT (y is the same as z))

Your mission is to determing the word that should be used insted of WHAT, and then to translate that to JAVA.

`if ( ( (any time x is part of a pair) AND ( y is part of a pair that doesn't include x)) OR (z is not the same as k) )`Let's see how that would evaluate an example that should fail:

What I would do is break down problem into discreet steps. The first thing you need to know is that, with 4 elements, in order to get two pairs, X must be in a pair with one other element:

(x is the same as y) OR (x is the same as z) OR (x is the same as k)

Given that, in order to have two pairs, there is a requirement on the only two remaining elements left - they must be equal as well, so there is a remaining condition that must be met:

((x is the same as y) WHAT (z is the same as k)) OR ((x is the same as z) WHAT (y is the same at k)) OR ((x is the same as k) WHAT (y is the same as z))

Your mission is to determing the word that should be used insted of WHAT, and then to translate that to JAVA.

Steve

S. Freeman

Greenhorn

Posts: 12

posted 4 years ago

I must say that i didn't expect such a fast and great response Steve, thank you it really helped, i was worried that my understanding of boolean operations sucked, but it seems that my approach to solving the problem and logic behind my thoughts was the primary reason for my fail.

Thanks!

Thanks!

It is sorta covered in the JavaRanch Style Guide. |