Data Types primitive, arrays, objects Java overview Primitive data types in Java 46
Recap Day 2! Lessons Learned:! Sample run vs. pseudocode! Java program structure: set-up, then real statements, decent correspondence between pseudo & java, precision is important! Solution design with subroutines! Suppose you had to make a schedule for this course, much like the one posted. How would you go about setting it up? 47
Programming approaches! Can apply top-down or bottom-up staging to either design or coding.! Don t have to do both the same.! Top-down often works best when designing a solution.! Bottom-up coding can be easier to test as you go along. 48
Subroutine summary! Problem decomposition! Should be reusable! Usually need some data! passed as parameters! [later we'll see: available in the class]! May return a value! functions return values! procedures do not! Called "methods" in Java 49
Data Types! How many different types of data can you name on which a program might need to operate? Consider both primitive types of data that represent only one value and complex ones that are used to store multiple values at once.! Discuss with your neighbors, write down your ideas in a plain text or Word document called datatypes. (You ll submit this to Blackboard later.) 50
More data, more methods! Suppose you need to compute some statistics on a set of values: Average(mean), StdDev, Median - what increasingly complex computation and data storage does each of these subroutines require?! Again, discuss with your neighbors, especially if you don t know how these things are computed. Add some ideas to your datatypes file. 51
Algorithm 5 Numerical Average! Set sum to 0! Set howmany to 0! Prompt for the numbers! While there is more data! Read a number! Add number to sum! Add 1 to howmany! Divide sum by howmany! Return/display sum (now the average) 52
Algorithm 6 Standard Deviation! Let s assume data is input and stored in an array! Stddev(array):! Set average to average(array) [call subroutine]! Set sum to 0! For every value in array:! Set value to value average [subtract average from value]! Set value to value * value [square it]! Add value to sum! Divide sum by length of array! Return squareroot of sum 53
Algorithm 7 Median! Median(array):! Sort(array) [let s hope that this subroutine exists]! Get length of the array! If length is odd! Return middle value! Otherwise! Return average of the two middle values 54
Mazes! What would we want a program that handles mazes to do for us?! How do we solve a maze, from cell 0,0 to the bottom right corner (ending cell)?! How can we represent/store a maze in a plain text format?! Write down a few possible solutions in your datatypes file. 55
Mazes! What would we want a program that handles mazes to do for us?! solve a maze! create a maze! read a maze! store a maze! How do we solve a maze, from cell 0,0 to the bottom right corner (ending cell)?! move to a neighbor of the current cell where there isn't a wall! try to solve from the neighbor to the ending cell! if can't solve, back up and try from a different neighbor! this is a "recursive" method 56
Maze representations unsolved 4 5 1001 1010 1100 1011 1100 0101 1011 0010 1000 0110 0101 1001 1000 0110 1101 0011 0110 0011 1010 0110 text file solved 57
Maze Data Object Oriented! Maze is a 2D array of Cells, along with the row and column dimensions! Each Cell contains data (number or something else), and details that tell us which walls it has (north, south, east, west)! Even a Wall can be consider an object with 2 adjoining Cells and an orientation (horizontal or vertical)! We can represent the cell wall data textually in many ways: 1101 for example might mean there are north, east and west walls, but no south wall 58
Overview - data! simple (primitive) variable types: whole numbers, real numbers, characters, booleans! collections of similar data! strings! lists (1D array)! tables (2D array)! objects user defined types! can hold multiple data attributes! data of varying types 60
Overview - processing! Structured programming: input some data, process it, produce output (sequence, decisions, loops)! Procedural programming: pass data, process it, return value(s) functions call each other or themselves! Object-oriented programming: define data attributes & methods that operate on them, creating powerful new data types 61
Java Program Structure! Class file! Data (optional)! Method definitions! Executable statements! Variable declarations! Method calls! Assignment statements! Decision statements! Loops Program class files must have special main method where execution always begins. 62
JAVA Language Elements! Comments! Not executed documentation only! // single line or /* */ block style! Reserved words! Symbols (operators, punctuation)! Identifiers! Used for names of: classes, methods, variables! Contain: letters, $, _, digits but not as first character! Literal values: numbers, characters, strings! Case sensitive! White space 63
JAVA Applications Programming Interface (API)! Collection of pre-existing Java software! Organized into useful class files! Related class files are in common Packages (folders)! Sometimes they need to be explicitly included in order to be used in your program: import java.packagename.classname;! java.lang.* classes are always implicitly included! The JAVA website contains a full list & documentation 64
Java example1: average.java! Download the code (from course schedule)! Open jgrasp! Let's review the posted code and identify the various statement types! Compile & run 65
Wrap-up! Submit your datatypes notes files with answers to the exercises we did today on Blackboard as c01 in Classwork Submission.! Submission will stay open until tomorrow evening for students who just added the course. 66