Part Zero - review

Lets go over what we did last class. Start java and make a new file and save it as Lesson1.java

Use System.out.println(); to write a program that creates the following output

I cant belive we have to do this again.
This is so easy.
        

Heres how to write the main method again in case you forgot.

public class Lesson1 {
    public static void main( String[] args ) {
        
        // your code goes here =)

    }
}
       

That wasn't too boring was it?

Part One

Now lets learn about variables in java. We use variables to hold data. In java a variable has an identifier (its name), a type and a value.

Valid names in java start with a letter, an underscore or a dollar sign. they can be any length and can consist (after the first character) of letters, numbers, underscores and dollar signs. However best practice says to capitalizeEachWordInYourVariable

Variables also have a type, today we'll look at int, and String. You'll go over more types in lecture.

No more time to waste, lets get programming! Paste this into your main method

// assigns a variable
String myString = "Hello Variables!";

// prints the variable
System.out.println(myString);
        

Any problems? lets try something else

// assigns a variable
int myInteger = 42;

// prints the variable
System.out.println(myInteger);
        

Just one more thing and then we can move on.

// assigns a variable
int myInteger = "23";

// prints the variable
System.out.println(myInteger);
        

Woah.. that didn't work. It's because you're trying to assign a String to a variable of type int. Why would you do something silly like that?

Part Two

Try this piece of code:

public class Lesson1 {
    public static void main( String[] args ) {
        
        // declare variables a and b
        int a, b;

        // initialize a and b
        a = 5;
        b = 6;

        //print a and b
        System.out.println(a);
        System.out.println(b);
    }
}
        

You should take two things away from this example. First that you don't have to give a value to a variable as soon as you declare it. And second that you can declare more than one variable at a time

Part Three

What good is having numbers if we cant add and subtract them? Well we can so stop complaining..

Theres quite a few operators in Java but were going to focus on these ones + - / * % . You can probably guess what all but one of these do.

Make a program with three integers a, b and c . Try this program with each operator

public class Lesson1 {
    public static void main( String[] args ) {
        
      // declare my variables
      int a, b, c;
        
      // initialize a and b
      a = 7;
      b = 2;
      
      // test the operator and initialize c with the result
      c = a + b;

      // print c
      System.out.println(c);

    }
}
        

Part Four

The same way you can add ints together you can also add Strings.

public class Lesson1 {
    public static void main( String[] args ) {
        
        String myString = "adding strings "+"is fun";
        System.out.println(myString);

    }
}
        

This is called string concatenation. Just like ints, strings can only be added to other strings. Now try this:

public class Lesson1 {
    public static void main( String[] args ) {
        
        String myString = "this is the number five: ";
        int myInt = 5;
        
        myString = myString + myInt;

        System.out.println(myString);

    }
}
        

But Greg.. you JUST SAID we could only add strings to other strings..

Thats still true but when using the + operator Java automatically changes your int into a String before it concatenates them

Home Work

Create two variables a and b. Set a = 11 and b = 3. Create a new variable and make its value a divided by b. Create another new variable and make its value the remainder of a divided by b. Then create the following output with what you learned in class.

a divided by b is 3 with remainder 2
        

Paste your code and its output into a word document, print it out and give it to me next class.

.. and if you want to learn more on your own

Figure out what Math.sqrt(4) does.

Figure out what Math.pow(5,2) does.

Look at the Java API Documentation for the Math class and learn some more about it