Skip to content

Instantly share code, notes, and snippets.

@EvanHahn
Last active December 13, 2015 17:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save EvanHahn/4945775 to your computer and use it in GitHub Desktop.
Save EvanHahn/4945775 to your computer and use it in GitHub Desktop.
A friend asked for help on some basic Java homework, and so here it is.

Let's first talk about booleans, which you might already know, so I apologize if this is review. If you think you understand booleans completely, skip this puppy.

Booleans

A boolean is a variable type in many programming languages. It usually that something can be true or false, yes or no, on or off, 0 or 1.

I imagine booleans like lightswitches. They can either be on or off -- true or false.

Most languages have true and false, but they vary somewhat. C++ writes true and false, where Python has capitalized True and False. CoffeeScript supplements the classics and adds yes, no, on, and off. C kinda doesn't have them built-in, you have to make your own.

But all programming languages have something that can be true or false, on or off. Like a lightswitch.

A note on Java

Java is fucking weird, and because you're learning Java, I should make sure you're aware. Java kind of has TWO boolean types:

boolean myPrimitiveBoolean = true;
Boolean myObjectBoolean = new Boolean(true);

This opens up a can of worms -- in my opinion, this is a weakness of Java. Java has primitive types (the first one), which are simple and kind of can't do much, but they can sorta be dropped in as replacements and confusing. They also have a class called Boolean which wraps the primitive type and this stuff is just garbage and let's stop talking about it. Know that this distinction exists.

In your examples, you're using the primitive boolean, not the object, so we won't talk about this much more.

Operations with booleans

There are many operations that can be done with booleans. This means we can take a boolean, throw some stuff at it, and get a new and exciting boolean.

First, let's look at the negation operator:

boolean a = true;
boolean b = !a;    // => b is now false, a is unchanged

The ! operator is sometimes called the "not" operator, and it can "flip" a boolean.

The same can just as easily be done in the other direction:

boolean c = false;
boolean d = !c;     // => d is now true, c is unchanged

There are also operations that take two booleans. Perhaps the most important are and and or, represented in Java by && and ||, respectively.

Let's say we have two lightswitches:

boolean a;
boolean b;

We want to know if the room is fully lit. It's fully lit if a is on and b is on. You'd write that like this:

if (a && b) {
    System.out.println("Room is fully lit!");
}

If a and b are both true, then both lightswitches are on, and the room is fully lit. If a is false, the room isn't fully list. If b is false, the room isn't fully lit. If both are false, the room isn't fully lit. The && operator says "hey, are both of these true?".

Now, we want to know if any lights are on, because we want to be able to see something.

if (a || b) {
    System.out.println("I can see!");
} else {
    System.out.println("It's dark in here!");
}

If a and b are both true, the both lightswitches are on. The room is fully lit, and we can see. If only one of them is on, we can still see. But if both are false, then all the lights are off.

A very important insight that I didn't have for much of my programming career is that the stuff inside of if statements (between the parenthesis) is itself a boolean. Here are some examples that illustrate that:

bool fullyLit = a && b;
if (fullyLit) {
    System.out.println("Room is fully lit!");
}

bool canSee = a || b;
if (canSee) {
    System.out.println("I can see!");
}

if (true) {
    System.out.println("This ALWAYS prints!");
}

if (false) {
    System.out.println("This will NEVER happen.");
}

Your code

I'm not going to write your code for you, but I'll try to give you a push in the right direction.

  • andBoolean

    You actually have the answer in there, it's just got some extra stuff in there. Look at your orBoolean function, because it has the right idea.

  • orBoolean

    This actually looks perfectly fine to me. I'm pretty sure this works.

  • intToInt

    You're given an int. What do you have to do to return it as an int? Imagine I messaged you an English sentence and I told you to translate it into English for me. What would you do?

  • doubleToInt

    I don't know a much better way of explaining this than giving you the answer:

      return (int) in;
    

    Java (and other languages) have what's called a cast. Some variable types can be converted to some other variable types. In this case, doubles can be converted to integers and that's how you do it.

    For reference, it's identical in C and C++, but it's different in other languages, and I always Google this kind of thing.

  • stringToInt

    Converting to and from strings is different in every programming language, and I just Google it. Here's a good one I found.

  • booleanToInt

    This kind of begs the question: how to you "translate" true and false into integers? I'd answer this question yourself, but here's some skeleton code:

      if (in) {
          // return the true value
      } else {
          // return the false value
      }
    

    I'm pretty sure I know what this should be, but I'll let you figure it out.

The rest of these are just more conversions. When in doubt, Google how to do it. This is a classic example of stuff I look up -- I program in C++ in all of my CS classes and still forget how to turn a string into a double.

Good luck! Lemme know if you have any questions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment