learning java blair drummond. programming tips (in any language) take your time. -if you're...

42
Learning Java Blair Drummond

Upload: damon-eaton

Post on 05-Jan-2016

221 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Learning Java Blair Drummond. Programming Tips (In any language) Take your time. -If you're lucky, when you do something incorrectly you'll get a syntax

LearningJava

BlairDrummond

Page 2: Learning Java Blair Drummond. Programming Tips (In any language) Take your time. -If you're lucky, when you do something incorrectly you'll get a syntax

Programming Tips(In any language)

Take your time.

-If you're lucky, when you do something incorrectly you'll get a syntax error and you'll find it as soon as you run it... if you're less lucky, then it's a logical error.

...It can take hours to find what went wrong in logical errors.

As I once heard it, these machines are “the most legalistic, persnickety, hard-nosed, unforgiving demander(s) of precision and explicitness in the universe.” (Steven Pinker)

A extra few minutes coding can save hours in debugging

Page 3: Learning Java Blair Drummond. Programming Tips (In any language) Take your time. -If you're lucky, when you do something incorrectly you'll get a syntax

Programming Tips(In any language)

Keep an extra file to test code with.

-If you're fuzzy as to what a for loop with conditions will do, have a file on the side to run that piece of code in. See how it behaves, and if it works the way that you think it should.

Page 4: Learning Java Blair Drummond. Programming Tips (In any language) Take your time. -If you're lucky, when you do something incorrectly you'll get a syntax

Programming Tips(In any language)

When you get confused

-Every problem you run into, there is probably someone on the internet who has already run into it, and had it solved.

-You are never 'Stuck' you just need to do some research to find a way around the problem.

Page 5: Learning Java Blair Drummond. Programming Tips (In any language) Take your time. -If you're lucky, when you do something incorrectly you'll get a syntax

Objectives

Data Types in Java Declaring Variables Precedence (the BEDMAS of Java) Simple commands If, Else, While, and For The types of mistakes Good Style in Java

Page 6: Learning Java Blair Drummond. Programming Tips (In any language) Take your time. -If you're lucky, when you do something incorrectly you'll get a syntax

Data TypesType Java name

(Case sensitive)What it looks like

Integer (int) -1,0,1,8...

Double (double) 3.14159...

Character (char) 'a', '%', '~'

Boolean (boolean) true / false

String (String) “Hello World”

Page 7: Learning Java Blair Drummond. Programming Tips (In any language) Take your time. -If you're lucky, when you do something incorrectly you'll get a syntax

Using types

When you need a piece of information in your code, you have to Declare it.

TYPE name = Value; boolean is_winning = true;

Also, Every command in Java ends in a semi-colon. ( ; <-- this is a semi-colon)

Page 8: Learning Java Blair Drummond. Programming Tips (In any language) Take your time. -If you're lucky, when you do something incorrectly you'll get a syntax

Take a look at the Reference sheetfor the syntax of the commands.

Multiplication, Division, Remainder, Greater than, Equal to, AND, OR,

etc

Page 9: Learning Java Blair Drummond. Programming Tips (In any language) Take your time. -If you're lucky, when you do something incorrectly you'll get a syntax

Using Types

You can also declare variables that you solve later like this.

//This is what a comment looks like in java

//anything behind these two slashes is hidden...

// from the computer

Int count; //This declares that 'count' is an integer

count= 11; // This gives the count the number 11.

Page 10: Learning Java Blair Drummond. Programming Tips (In any language) Take your time. -If you're lucky, when you do something incorrectly you'll get a syntax

Using types: Casting This helps with Integers, Doubles, and Characters When you declare a variable, it is set in stone what that

variable type is. However, you can make a NEW variable that uses that number using casting.

For instance, if you're given a decimal from the user, and you need to make an integer with it, you can cast that double to an integer.

double number_of_pies= 2.5;

//how many full pies?

int pies= (int) number_of_pies; The (int) is the casting operation Pies is now 2.

Page 11: Learning Java Blair Drummond. Programming Tips (In any language) Take your time. -If you're lucky, when you do something incorrectly you'll get a syntax

double something= (5/2);

System.out.print(something);

What do you think this prints?

This is Integer Division, and can sometimes give you problems, hence the utility of casting.

Even though “something” is allowed to be a decimal, 5 and 2 are INTEGERS, so they act as though they behave in integer rules, 5/2 is 2.

However, “5.0/2” is 2.5, as is “5/2.0”. Sometimes you need to cast an integer to double using (double) in order to solve these sorts of problems.

A Common Mistake

Page 12: Learning Java Blair Drummond. Programming Tips (In any language) Take your time. -If you're lucky, when you do something incorrectly you'll get a syntax

Casting Characters?

Page 13: Learning Java Blair Drummond. Programming Tips (In any language) Take your time. -If you're lucky, when you do something incorrectly you'll get a syntax

Characters

Characters are actually just integers in disguise. And they can be used like integers.

System.out.print('a' < 'b'); //prints “true” Int letter = 'a';

System.out.print(letter); //Prints 97 Every letter is just a number in the ASCII or UNICODE

alphabet. Char letter= (char) 97; System.out.print(letter); //Prints 'a'

Page 14: Learning Java Blair Drummond. Programming Tips (In any language) Take your time. -If you're lucky, when you do something incorrectly you'll get a syntax

Booleans and Precedence

Booleans are either true or false, the same thing as propositional logic.

If P then Q P And Q P Or Q Not P or Q

etc.

And the order that they are interpreted in can completely change the result.

Page 15: Learning Java Blair Drummond. Programming Tips (In any language) Take your time. -If you're lucky, when you do something incorrectly you'll get a syntax
Page 16: Learning Java Blair Drummond. Programming Tips (In any language) Take your time. -If you're lucky, when you do something incorrectly you'll get a syntax

This isn't a big problem, because you can get around any ambiguity

with more brackets

(However this makes your code harder to read, which makes

debugging harder)

Page 17: Learning Java Blair Drummond. Programming Tips (In any language) Take your time. -If you're lucky, when you do something incorrectly you'll get a syntax

Booleans

Basically all of programming is dealing with conditions, so it's important to understand these.

If you have a comparison, say

(7>=5) this is a boolean, and it's value is true.

And you can combine this with, other conditions. Say that you want for your value to be greater than or equal to 5, OR for it to be less than or equal to 2.

Then your expression is (x>=5) || (x<=3)

Page 18: Learning Java Blair Drummond. Programming Tips (In any language) Take your time. -If you're lucky, when you do something incorrectly you'll get a syntax

Is this point in the square?

Your square has a side length of 5, and it's bottom left corner is at (5,3).

Take a minute and write a boolean that uses (x,y) and checks if it is in the square or not.

Page 19: Learning Java Blair Drummond. Programming Tips (In any language) Take your time. -If you're lucky, when you do something incorrectly you'll get a syntax

Answer (though there are many)

((x>=5 && x<=(5+5)) && ((y>=3)&&(y<=(5+3))))

x is greater than the bottom, AND less than the top

AND

y is greater than the bottom, AND less than the top.

(So if you had to, how would you find out whether or not a point WASN'T in the square?)

(Ans: add a !)

Page 20: Learning Java Blair Drummond. Programming Tips (In any language) Take your time. -If you're lucky, when you do something incorrectly you'll get a syntax

So now we have a few basic tools

We know how to make variables, and how to evaluate what they're value is.

(7>=5) && (2>=5) is false 13/5 is 2 13.0/5 is 2.6 (I never explicitly mentioned this, but) 13%5 is 3 (It's the remainder in 13/5) 'a'>='z' is false (char) ( ( (int) 'a' ) +1 ) is 'b'

Page 21: Learning Java Blair Drummond. Programming Tips (In any language) Take your time. -If you're lucky, when you do something incorrectly you'll get a syntax

So how to we use these in order to create an algorithm?

We need commands, tests, and loops.

Some basic commands.

Getting input. Math

Page 22: Learning Java Blair Drummond. Programming Tips (In any language) Take your time. -If you're lucky, when you do something incorrectly you'll get a syntax
Page 23: Learning Java Blair Drummond. Programming Tips (In any language) Take your time. -If you're lucky, when you do something incorrectly you'll get a syntax

Math.class

There are too many to include in a ppt, but google Math.class java, and you'll find a list.

Math.PI , Math.sqrt(), and Math.random are among the ones that I find I use often, but it's a treasure trove of cool operations that make you life easier.

Page 24: Learning Java Blair Drummond. Programming Tips (In any language) Take your time. -If you're lucky, when you do something incorrectly you'll get a syntax

If, else, and else if

If is your first test.

If( condition )

{

//run code

}

The condition is a Boolean, and the code is a set of commands

Page 25: Learning Java Blair Drummond. Programming Tips (In any language) Take your time. -If you're lucky, when you do something incorrectly you'll get a syntax

If, else, and else if

If( x>5) //Test this first

{

System.out.println(“x is greater than 5”);

} Else if(x<5) //test this second

{

System.out.print(“x is less than 5”);

} Else //If neither of the above ran, run this

{

System.out.println(“x is 5”);

}

Else if is like a second if that runs ONLY IF the if DID NOT RUN

Else runs if nothing above it runs

Page 26: Learning Java Blair Drummond. Programming Tips (In any language) Take your time. -If you're lucky, when you do something incorrectly you'll get a syntax

IFs: nesting

And there is nothing wrong with putting ifs inside of ifs.

Page 27: Learning Java Blair Drummond. Programming Tips (In any language) Take your time. -If you're lucky, when you do something incorrectly you'll get a syntax

Which QUADRANT is (x,y) in?(Assume x!=0 && y!=0)

Let's code it!

Page 28: Learning Java Blair Drummond. Programming Tips (In any language) Take your time. -If you're lucky, when you do something incorrectly you'll get a syntax

if(x>0) //Notice that NO SEMI-COLON follows these { if(y>0) { System.out.print("(x,y) is in quadrant I"); } else { System.out.print("(x,y) is in quadrant III"); }}else { if(y>0) { System.out.print("(x,y) is in quadrant II"); } else { System.out.print("(x,y) is in quadrant IV"); } }

Page 29: Learning Java Blair Drummond. Programming Tips (In any language) Take your time. -If you're lucky, when you do something incorrectly you'll get a syntax

While and For

These are the LOOPS. You use them to repeat a procedure x number of times. The for loop does this with a counter, the while loop just has a condition.

Page 30: Learning Java Blair Drummond. Programming Tips (In any language) Take your time. -If you're lucky, when you do something incorrectly you'll get a syntax

While

Page 31: Learning Java Blair Drummond. Programming Tips (In any language) Take your time. -If you're lucky, when you do something incorrectly you'll get a syntax

While

Int a=5;

int x=0;

while(x!=a && x>a)

{

System.out.println(x);

x=x+1;

}

System.out.println(“done”);

Note that println is “print line”.

This prints

01234done

Page 32: Learning Java Blair Drummond. Programming Tips (In any language) Take your time. -If you're lucky, when you do something incorrectly you'll get a syntax

A short-hand

“ i++ ” means i=i+1;

“ i-- ” means i=i-1;

“ i+=3 ” means i=i+3; I have to look up the syntax for the last one

when I need it, but the first 2 are almost ubiquitous.

Page 33: Learning Java Blair Drummond. Programming Tips (In any language) Take your time. -If you're lucky, when you do something incorrectly you'll get a syntax

For loopFor( Initialize counter; Termination; Increment)

{

//run

} Generally people use i and j as for loop variables For(int i=0;i<10;i++)

{

System.out.print(i);

}

This prints: 0123456789

Page 34: Learning Java Blair Drummond. Programming Tips (In any language) Take your time. -If you're lucky, when you do something incorrectly you'll get a syntax

An example.

Pi is actually equal to a sum

PI= 1/4(1-1/3+1/5-1/7+1/9-1/11+1/13...)

So can we approximate pi in a for loop?

Page 35: Learning Java Blair Drummond. Programming Tips (In any language) Take your time. -If you're lucky, when you do something incorrectly you'll get a syntax

double pi=0;for(int i=1; i<1000; i+=4) // i+=4 means i=i+4 { pi=pi+(1/i)-(1/(i+2)); // 1/1 - 1/3 + 1/5 - 1/7 ... }pi=pi/4;

System.out.print(pi);

What does this print?

Page 36: Learning Java Blair Drummond. Programming Tips (In any language) Take your time. -If you're lucky, when you do something incorrectly you'll get a syntax

It prints 0.25Because 1/3 is 0. 1.0/3 is 0.33333333333...

The Correct code is

double pi=0;

for(int i=1; i<1000; i+=4) // i+=4 means i=i+4

{

pi=pi+(1 .0/i)-(1 .0/(i+2)); // 1/1 - 1/3 + 1/5 - 1/7 ...

}

pi=pi/4;

System.out.print(pi);

Page 37: Learning Java Blair Drummond. Programming Tips (In any language) Take your time. -If you're lucky, when you do something incorrectly you'll get a syntax

A challenge

Using input.nextDouble(); get inputs from the user for an x1, y1, r, x2, y2 and make a piece of code that tests if (x2,y2) is inside of the circle with radius r, and a midpoint at (x1,y1)

(Hint: Pythagorean theorem is useful here)

Page 38: Learning Java Blair Drummond. Programming Tips (In any language) Take your time. -If you're lucky, when you do something incorrectly you'll get a syntax

A much more challenging challenge

Write a piece of code that takes a height from the user, and prints a diamond.

You might need the knowledge that if you type “/n” in a print statement, it prints a new line.

Page 39: Learning Java Blair Drummond. Programming Tips (In any language) Take your time. -If you're lucky, when you do something incorrectly you'll get a syntax

The main types of mistakes

Syntax Error

Run-Time Error

Logic Error

Page 40: Learning Java Blair Drummond. Programming Tips (In any language) Take your time. -If you're lucky, when you do something incorrectly you'll get a syntax

Syntax Errors

This happens when you incorrectly formatting your code.

Example:

for(int i=0,i<A,i++) //Those are supposed to be semi-colons. This is a syntax error.

These will almost certainly be the most common mistakes you'll make, but they are the most innocent mistakes.

Convientiently, your compiler will generally find them for you.

Page 41: Learning Java Blair Drummond. Programming Tips (In any language) Take your time. -If you're lucky, when you do something incorrectly you'll get a syntax

Run-Time Error

You have probably encountered these once or twice before...

This error crashes your program.

for(int i=0; i<2;i--) System.out.print(i);

What does this code do?

When does it stop?

Page 42: Learning Java Blair Drummond. Programming Tips (In any language) Take your time. -If you're lucky, when you do something incorrectly you'll get a syntax

Logic Error

These are the errors that make debugging hell.

This is what happens when you think your code does one thing... but it does another.

The pi mistake earlier was a logic error. It didn't crash, but it gave an incorrect result.