200 likes | 312 Vues
Programming with Java . COP 2800 Lake Sumter State College Mark Wilson, Instructor. Programming with Java. Software Development Process. Building Software. Extract requirements Define an architecture Implement the design Code Test Repeat. DRY. DRY: Don’t repeat yourself
E N D
Programming with Java COP 2800 Lake Sumter State College Mark Wilson, Instructor
Programming with Java Software Development Process
Building Software Extract requirements Define an architecture Implement the design Code Test Repeat
DRY DRY: Don’t repeat yourself Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.
Occam’s Razor Occam’s Razor: “Entities must not be multiplied beyond necessity” Keep It Simple, Stupid
YAGNI Principle “You Ain’tGonna Need It”
Applied to Coding Use only as many variables as necessary and no more. Perform a given function in only one place. Smaller classes and methods provide greater flexibility If it isn’t in the specification, don’t put it in.
Programming with Java Arrays
Arrays are Lists Index is zero based
Arrays have Type • Any of the primitive types • Byte • Char • Short • Int • Long • String • Float Double • Boolean • Object reference • Object reference is a collection of bits • Acts like a pointer but details are JVM specific • Contents of the array must agree with the array type
Arrays are Objects • Arrays are always objects • Arrays may contain primitives • Arrays may contain object references • Arrays have an attribute…sort of • length • Arrays have methods: • asList • binarySearch • copyOf • copyOf Range • deepEquals • deepHashCode • deepToString • equals • fill • hashCode • sort • toString
Array of Primitives Declaration type name [size]; int scores [10]; type name [] = {value, value, … value}; int scores [] = {1,2,3,4,5,6,7,8,9,10}; type[] name; name = new type[size]; int[] scores; Scores = new int[10];
Multi-dimensional Arrays type[size][size] name; String[][] names = { {“Mr. “, “Mrs. “, “Ms. “}, {“Smith”, “Jones”} }; Works like an array of (array) objects
Array of Objects Declaration ClassName[] Name; Name = new ClassName[number]; Bicycles[] MyBikes; //declare Bicycle array variable MyBikes = new Bicycles[5]; //create a Bicycle array with 5 elements //all the references are null MyBikes[0] = new Bicycle; //create a Bicycle object in element 0 MyBikes[0].make = “Schwinn”; //assign the value “Schwinn” to the //make instance variable
Programming with Java Attributes, Behaviors and Instances
Some Review State = Characteristics = Attributes = Instance Variables Behavior = Methods Methods use Instance Variables Class is a blueprint for Objects
More on Methods • Methods have return type • Primitives • Class • ‘void’ – no value it returned • A method uses parameters • A caller passes arguments • Methods may return values • Methods with a type (not void) must return a value of that type • This constitutes an interface contract • Callers must provide parameters a method expects • Methods, if typed, must return a value • As long as the interface contract is respected, the method is free to accomplish its function any way it wishes
Encapsulation “Data hiding” allows methods to change Keep instance variables “private” Use public methods to access instance variables Accessors == Getters Mutators == Setters
Assignment 1 Part C • Refactor your Nim program to employ objects and encapsulation. Employ DRY and Occam’s Razor in your design. Think about super classes and their potential role. Also consider future reuse. Game play should remain the same. • The program class containing ‘main’ is necessary but not sufficient. • Due 2359 the day before the next class.