Java Programming Computer Science 112
Review: Problem solving Class 4 is the Whole Point of Programming. Is there any particular one you'd like to go through? If you are desperately confused still, now is when you have my permission to be worried. Cumulative class, ya know. Could try 120 next semester - goes half as fast since C++ is a two semester class.
Recap: Variables A variable: Is a location in memory Has a type that defines what can be in that memory Has a cool identifier name Is really just a post-it note An Array is just a row of post-it notes The type of every note is the same You can look at any individual note you want
Moar Variables in a row Write a program that can average the grade of everyone in the class. int tpointsone; int tpointstwo; int tpointsthree; int tpointsfour; int tpointsfive; int tpointssix; // add them up and divide by 6 Oops, someone dropped the class. Recompile the program.
Don't wanna An array is a whole line of one type of variable in a row somearray = 3 5 16 4 3 12 11 9 8 0 1 2 3 4 5 6 7 8 The first row of numbers are the data on the string of post-it-sections. The second row is the "index" of each so you can refer to them directly
First glimpse of memory Flip through old assignments looking for the word "new" int tprimitive = 7; // No new here Scanner tkeyboard = new Scanner(System.in); // Eep! When you need a larger or more complicated post-it than what can hold a single int/float/boolean/char, you have to ask for it. Don't stress about the parenthesis yet, we still have a few weeks until that
Say the magic word int[] tgrades = new int[10]; int[] : An array of ints tgrades : Name for the variable = : Initializing the variable to something new : The magic word. I need some space. int : Will match the first word. Type of what is going in there. [10] : How many I want. Good tattoo
Secretly two steps here int[] tgrades = new int[10]; is the same as int[] tgrades; tgrades = new int[10]; Step one is Declaration. Second is Initialization. You can only declare once, but you can re-initialize all you want. This is important to remember because of scope. You can declare at the top of a program and initialize way later. Scope = A variable only exists between its closest set of curly braces.
Java loves you You don't need to know how many spaces you want when you write the program. int tderp = blah blah input; int[] therp = new int[tderp];// WAY more useful than C++ And all of the post-its are set to 0 by default. int tderp = 5; int[] therp = new int[tderp]; therp[4] = 5; therp[0] = 4; for( int i = 0; i < tderp; i++ ) // length - 1 is the last // 0 is the first System.out.println(tHerp[i]); // 4 0 0 0 5
See those indexes? (indices?) therp[4] = 5; therp[0] = 4; // length - 1 is the last // 0 is the first Square brackets are how you refer to individual variables If you refer to an index out of bounds, you crash. Remember how many times you should crash?
Normal variables inside There is nothing special about each individual variable in the array int[] tscores = new int[30]; tscores[4] = 5 + tscores[7]; Once you add the brackets, it is just a single int like before
for-loop is the champion For loops are for when you know how many times you want to do something You can ask an array how big it is Warning, idiom approaching Memorize it Tattoo it Love it int[] tscores = new int[30]; int tsum = 0; for( int i = 0; i < tscores.length; i++ ) tsum += tscores[i]; // += is "add this to me"
== is the goat You can't use == to compare two arrays. You can for int You can't for String An array is an Object, not a primitive type It needs a "new" There isn't a built-in way to check if "every member of both arrays is the same and the arrays are the same size too". What is this? An array for ants?
Sorting arrays! Everyone loves sorting algorithms! Nobody loves sorting algorithms. This is the only time the class will get desperately nerdy. Again, I lie to you. These are really important to know You will hate me in 30 minutes.
Selection Sort Start at spot 0. Find the smallest value in the array to the right and move it to that spot. Starting at the next number, do it again. 5 3 4 9 8 // Point at spot 0. 3 is the lowest, swap it. 3 5 4 9 8 3 4 5 9 8 3 4 5 9 8 3 4 5 8 9
Oh man I'm bored Textbook gets bored here too. Wikipedia has 20 different sorting methods. None of them matter because of:
ArrayList! Of course something so repetitive and pedestrian is built in. Programming is not about Stupid Human Tricks. It is about problem solving. I still have to at least mention some of the tricks though.
ArrayList vs array - Preview ArrayList has sorting and searching built in. But its syntax is goofy And using it for simple ints (Integer) is a pain Arrays take one line to make and you just need square brackets to use it. But it can't change size And it doesn't do any neat tricks ArrayList will be the better pick three weeks from now when everything is an Object I'm bringing this up just to explain why neither I nor the book feel like explaining the code for Bubble Sort.
Back to real life A normal array is a list of variables like we've shown. An array can also be two dimensional, so that it represents a spreadsheet or grid. int [][] tgrid = new int [5][10]; But there is controversy here! If you were to visualize this 5x10 table, is it wide or tall?
There is no spoon Wide or tall is whatever you define it to be. It's just bytes inside the computer. YOU must remember to be consistent. width, height? x, y? row, column? i, j? I'm a computer! Beep beep boop. To me, it is an array of arrays. [1 2 3 4 5] <- an array [[1 2 3 4 5] [1 2 3 4 5] [1 2 3 4 5]] <- An array of arrays. But whatever makes your brain get it is cool.
In-class program in 1D Ask user for size of array Create the array for-loop to fill all the values with a random number between 1 and 6 for-loop to display the array on a single line Remember println vs print Now add a while loop with a prompt to "Pick an index to add one to." Check out of bounds! Re-print the array after each change -1 to exit
In-class program in 2D Make the array 2D Fill up the array using a nested for loop - Imma put that on the board. Display all the values in a square Ask the user for a row and column to add one to This will get you half way through the homework, so it's important.
Let's flowchart the 2nd homework Remember, just draw out boxes using plain English. It's just brainstorming. Don't write any code until your chart handles all cases. Translate the chart one box at a time. And we can talk about the test. Tests are fun.
Now do 10 dimensions! jk Make the array Fill it with dots Pick a random spot for an 'X' Pick a different random spot for an 'o' Draw the two dimensional array Ask for input If entered 'x', print LOSE and break Change location of o (checking out of bounds) If on same spot as 'X', print WIN and break Back to Draw