Learning Java Blair Drummond Programming Tips In any

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 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

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.

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.

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

Data Types Type Java name Integer (int) What it looks like -1, 0, 1, 8. . . Double (double) 3. 14159. . . Character (char) 'a', '%', '~' Boolean (boolean) true / false String (String) “Hello World” (Case sensitive)

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)

Take a look at the Reference sheet for the syntax of the commands. Multiplication, Division, Remainder, Greater than, Equal to, AND, OR, etc

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

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?

double something= (5/2); System. out. print(something); A Common Mistake 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

Casting 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'

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.


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)

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

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.

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 !)

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

So how to we use these in order to create an algorithm? We need commands, tests, and loops. Some basic commands. Getting input. Math


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.

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

If, else, and else if If( x>5) //Test this first Else if is like a second if that { runs ONLY IF System. out. println(“x is greater than 5”); the if DID NOT } RUN Else if(x<5) //test this second Else runs if { System. out. print(“x is less than 5”); nothing above it runs } Else //If neither of the above ran, run this {

IFs: nesting And there is nothing wrong with putting ifs inside of ifs.

Which QUADRANT is (x, y) in? (Assume x!=0 && y!=0) Let's code it!

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"); }

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.

While

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 0 1 2 3 4 done

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.

For loop For( 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); }

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?

What does this print? 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);

It prints 0. 25 Because 1/3 is 0. 1. 0/3 is 0. 333333. . . 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. . . }

A challenge Using input. next. Double(); get inputs from the user for an x 1, y 1, r, x 2, y 2 and make a piece of code that tests if (x 2, y 2) is inside of the circle with radius r, and a midpoint at (x 1, y 1) (Hint: Pythagorean theorem is useful here)

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.

The main types of mistakes Syntax Error Run-Time Error Logic Error

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.

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?

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.
- Slides: 42