• Post Reply Bookmark Topic Watch Topic
  • New Topic

write a static method odd() that takes three boolean inputs and returns true if an odd number  RSS feed

 
chavez james
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
write a static method odd() that takes three boolean inputs and returns true if an odd number of inputs are true, and false otherwise.

I was wondering if someone could help me out with the solution for this program.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
chavez james wrote:write a static method odd() that takes three boolean inputs and returns true if an odd number of inputs are true, and false otherwise.

I was wondering if someone could help me out with the solution for this program.


That's a pretty straightforward request -- just exactly what are you having an issue with? We can give you better hints if we know where you are having issues.

Henry
 
chavez james
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having a hard time starting out the program.
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start with this: the method is called "odd". It's static. It takes three boolean arguments. It returns a boolean.

How would you write just that without anything else?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chavez,
Welcome to CodeRanch!

A common technique when stuck is to try writing a smaller program and building up to what you need.

Try these out and let us know where you get stuck:

1) Write a method that takes three boolean inputs and returns true. (yes always returns true)
2) Enhance that method to return whether the first parameter is true.
3) Enhance that method to create a local counter variable
4) Enhance that method to increment that local variable to 1 if the first parameter is true
5) See if you can finish the method from here
 
Anindya Roy
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[code removed by moderator]
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anindya,

Please don't supply "ready-made" solutions. I'm sure your motives were great, but we prefer to teach people here rather than simply supply answers - especially in the Beginning Java forum - so DontBeACodeMill (←click).

And if you really think that a piece of code is the most instructive, please supply some comments with it.

Thanks.

Winston
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another way of doing it is to work out a Boolean algebra formula. A Karnaugh map may help: not sure.

If you don't know any Boolean algebra or about Karnaugh maps, you may wish to ignore this post
 
chavez james
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what i have so far. I don't think i am getting the right output. Please someone help me fix this.

import java.util.Scanner;

public class ThreeBooleans
{
public static boolean odd(boolean a, boolean b, boolean c)
{
return a || b || c ? true : false;
}
public static void main(String[] args)
{
Scanner input = new Scanner (System.in);

System.out.println("Please enter true or false: ");
boolean a = input.nextBoolean();
System.out.println("");
System.out.println("Please enter true or false: ");
boolean b = input.nextBoolean();
System.out.println("");
System.out.println("Please enter true or false: ");
boolean c = input.nextBoolean();
System.out.println("");

System.out.println(odd(a,b,c));
}
}
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
chavez james wrote:I don't think i am getting the right output

So what output are you getting and in what way is it wrong ?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
chavez james wrote:This is what i have so far. I don't think i am getting the right output. Please someone help me fix this.


Try out your method ...



with a few different parameters. Do it on paper (ie. pretend to be the JVM). Does it work? If you can get it to work on paper first, you should be able to easily convert the technique to code.

Henry
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the output I get. It seems fine -- except that it returns true with an even number of trues.

BTW, are you running this from the command line? It may not work in an IDE.


>java ThreeBooleans
Please enter true or false:
true

Please enter true or false:
false

Please enter true or false:
true

true
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
chavez james wrote:This is what i have so far. I don't think i am getting the right output.

There's no "think" about it. Either you are getting the right output in every case, or you aren't. And the only way to find out (as others have already said) is by testing.

Please someone help me fix this.

That's what we are doing, but we won't simply hand you the answer. That's not how we do things here.

However: the crux of your method is 'a || b || c', so the question is: is that correct? What does it do?

As has already been suggested, try it out on paper with all the true/false combinations for a, b and c that you can think of. Is the answer right in every case? If not, then clearly you need to think of another approach.

Question: Do your requirements stop you from using anything else in addition to your 3 booleans?

Winston
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With three booleans, there are eight possibilities. You should be able to test them all.

further, you should be able to test the method without bothering with the command line. Your method "public static boolean odd" doesn't care where the values come from, so make some explicit calls to it:

odd(true,true,ture);
odd(true,true,false);
...etc

you should know what the correct response is in every case. Test it and see if you are getting the right answer.


 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually I think you should go back to things more fundamental. Start by writing a truth table for your formula, so as to get 1 or 3 trues amongst your 3. There may be other ways to do it.
 
chavez james
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my completed code. Is there another way i can do this program?

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's certainly the simplest and clearest way. There are always other ways of doing something. For example, you could have 9 different if statement for each combination of true/false values. However, that would be hard to read and unnecessary complex.

Good job!
 
chavez james
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How could i use this code instead of the one i have above?

public static boolean odd(boolean a, boolean b, boolean c){
if (((a && b) == false) && (c == true))
return true;
if (((a && c) == false) && (b == true))
return true;
if (((b && c) == false) && (a == true))
return true;
if ((a && b && c) == true)
return true;
else{
return false;
}
}
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
chavez james wrote:How could i use this code instead of the one i have above?

public static boolean odd(boolean a, boolean b, boolean c){
if (((a && b) == false) && (c == true))
return true;
if (((a && c) == false) && (b == true))
return true;
if (((b && c) == false) && (a == true))
return true;
if ((a && b && c) == true)
return true;
else{
return false;
}
}

Why would you want to? This code is hard to understand. I KNOW what you are trying to do, and find this hard to read. Your solution above with the "if (a)..." is much easier to follow.
 
chavez james
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just trying to find out other ways of producing the same result.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never never write == true or == false. They are poor style and error‑prone if you write = by mistake.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't write System.out.println(""). Write System.out.println().
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
chavez james wrote:just trying to find out other ways of producing the same result.

There is. Have a look at the logical XOR (^) operator.

However, if you decide to go that route, make sure you test every possible combination and understand why it works before you submit it.

Personally though, I like your 'numberOfOdds' solution. It's simple, and crystal-clear to anyone else who has to read it; so: WELL DONE.

Winston
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree the counting solution is going to be difficult to improve on. I shall give you a challenge. Using the ... operator write a method which takes any number of boolean inputs and returns whether they are odd.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!