Introduction to Java

Who knew coffee could be so powerful?

  • Course Length: 4 weeks
  • Course Type: Short Course
  • Category:
    • Technology and Computer Science
    • Middle School
    • High School

Schools and Districts: We offer customized programs that won't break the bank. Get a quote.

Get a Quote

Have you ever noticed how quick people are to blame computers and the internet for all of society's problems? Computers don't really have feelings (thank goodness), but if they did they might be feeling a little hurt right about now. Hurt enough to actual turn on us? Who knows?

[shivers]

Luckily, you can be on the right side of the computer apocalypse, and it all starts with a little programming—java, to be specific. In this course for beginners, you'll learn

  • variables
  • arrays
  • methods
  • objects

...and you'll also get programming activities to cement the knowledge. By the end of the course, you'll even be able to program your own game of blackjack. Who knows what the computer overlords will be able to do with that knowledge?

What a terrifying, delightfully computer-y future.


Unit Breakdown

1 Introduction to Java - Introduction to Java

In this beginners' course, you'll learn all the important variables, conditionals, loops, methods, and objects. Yup, we'll do everything but teach you how to make a cup of coffee. (And real talk: you might want to learn that skill if you keep learning how to program.)


Sample Lesson - Introduction

Lesson 1.08: All that and a Loop

Multicolored roller coaster filled with loops.
Ride it enough times and you'll forget you even had a top hat.
(Source)

Rollercoasters have been around since the 1700's. Think about it for a second: men in top hats and women in hoop skirts being rocked around a rickety track on a bench.

That wasn't enough, though. When the initial thrill of the downhill roller coaster wore off, someone decided to add loops to the death traps. Passengers lost consciousness because of the g-forces. Worse yet: they lost their top hats.

Once engineers figured out how to make ones that didn't knock people out, loops became standard. Too standard.

Just like adding loops to rollercoasters, adding loops to our programs makes the code more interesting, minus the puking and loss of consciousness (most of the time). Loops let statements run multiple times, instead of just once. By adding loops to our programming toolkit, we can write programs that do more.

No smelling salts required.


Sample Lesson - Reading

Reading 1.1.08a: Thrown for a Loop

Loops all have two parts, no matter how they're written: 

  • condition
  • statements 

In Java, your standard loop will look something like this.

loop(condition){
	statement;
}

The condition is a boolean—just like in conditionals—but this code runs slightly differently.

When a program finds a loop:

  1. The test condition's evaluated. If the condition's true, the program runs the statements in the braces.
  2. After the statements all run, the program goes back to the test condition and makes sure it still evaluates to true. If it's still true, then the program runs the statements again.
  3. When the condition finally comes out false, the program heads to the next line of code outside the loop.

We're going to look at a few different types of loops, but that's always the basic outline.


Sample Lesson - Reading

Reading 1.1.08b: For Loops

For loops are—wait for it—loops. Not just any loops, though: these ones tell us just how many times we're going to be going through the loop. Say you want to find every integer between two numbers (and conveniently forgot how number lines work). With for loops we can easily see all the numbers…between two numbers. It's a lot more helpful than it sounds.

Let's use that example and see how it looks in Java:

int smallest = 5;
int largest = 10;

for(int i = smallest; i < largest; i++){
	System.out.print(i);
}

This code is going to print:

56789

A for loop repeats for a specific number of times, following that value of i. i is initialized and then checked each time the loop runs. As long as i still fits in the range from 5 to 10, the loop's going to continue running. After every iteration, or run-through of the loop, i increases by one.

The general pseudocode for a loop will look like this:

for (start expression; condition; change expression) {
	statement;
	statement;
	statement;
}

Each time the statements in the loop run is called one iteration. When we counted to five, we went through—guess how many—five iterations.

Right now, the start expression happens before anything else.

int i = 1;

Unlike every other part of the condition, the start expression only runs once at the beginning of the loop, and it initializes the variable i. This time, it's inititializing the int i to equal one.

The condition is a boolean that evaluates whether or not the loop should continue. It judges i before the statements run at each iteration.

Now to the change expression. That expression executes after each trip through the loop. This change expression either increases or decreases the value of i by an increment of one (or some other value). That's what keeps the loop from running infinitely.

You heard us right: infinitely. If i never changes, the code's just going to keep going through the statements until

  • you forcibly stop the program.
  • the program crashes the code environment.
  • the program crashes the computer.
  • the program crashes society as we know it (depending on when it runs).

But more on that later. Let's look at another example.

for (int x = 1; x < 20; x++) {
	System.out.println(x);
}

In this example, 1 to 19 will print to screen. Let's look at this example step by step to see what's happening:

  1. A loop variable, x, is both declared and initialized.
  2. The condition (x < 20) is evaluated and found to be true.
  3. The statement inside the loop, System.out.println(x); prints x to the screen.
  4. x is incremented by one (x++), so its value is now 2.
  5. The condition , (x < 20), is rechecked and found to still be…still true.
  6. System.out.println(x); runs again.
  7. x increments by one (x++), so its value is now 3.
  8. And on and on until x == 20. Once x evaluates to 20, the condition (x < 20) is false, so the program leaves the for loop.

Up until now, if we wanted to create a program with repeated printlns, we'd have to write them all out. That isn't a huge deal if we're dealing with…say, 14. But remember this, Shmooper: coding like that is really brittle. Take this code:

for (int i = 0; i<=14; i++){
	System.out.println(i);
}

What if we needed to print more than 14 print lines? What if we wanted to go from 1 to 100? Or even up to a million? Instead of writing print lines until our fingers bleed, all we need to do is change the condition in the for loop.

for (int i = 0; i<=100; i++){
	System.out.println(i);
}

And now we have a hundred print lines. Think about how much easier it is to go from 100 to 56. Of course, if you really want to copy and paste 34 print lines instead of using a for loop, we guess you can do that. It's just way easier to make changes when you're using for loops.

Also other programmers will judge you if you never use a loop. Just sayin'.

One more example before we go. Although it's common to increment or decrement for loops by one at each iteration, you can use any integer you want.

class CountBySevens {
	public static void main (String args[]) {
		for (int x = 7; x <= 100; x += 7) {
			System.out.println(x);
		}
	}
}

In this program, seven is added during each loop until x is greater than 100. Using a for loop makes stepping through a series of values as easy as…counting by sevens.


Sample Lesson - Activity

Activity 1.08: Just the Five of Us

While you're still here, let's write a program that does some counting—this time without the excessive println statements.

Down in the IDE, write three different loops for us.

First Loop

Write a small program that uses a for loop to count by 5's from 1 to 100. The answer should be 5, 10, 15, 20, 25, etc. Keep in mind that every for loop needs an

  • initialization.
  • condition.
  • increment.

For instance, if Shmoop were writing a for loop counting from one to ten, we'd write:

class TenCounter{
    public static void main(String[] args){
int counter = 0;
  for (int i = 1; i <= 10; i++){ counter++; System.out.println(counter); } } }

Paste your code here.

Second Loop

Now write a loop that displays every five numbers from 30 to 80, inclusive. That means both 30 and 80 should be part of the loop. Your output should be 30, 35, 40…

Third Loop

Last one. Make a loop that prints every five numbers starting at 80 and ending at 30. That means you'll be going down—instead of up—at every iteration. Your output should be 80, 75, 70, 65…

Shmoop IDE

class FiveCounter{ public static void main(String[] args){ //first loop goes here //second loop goes here //third loop goes here } }

Output

* Output from code will be shown here *