1 / 20

Programming with Java

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

kyna
Télécharger la présentation

Programming with Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming with Java COP 2800 Lake Sumter State College Mark Wilson, Instructor

  2. Programming with Java Software Development Process

  3. Building Software Extract requirements Define an architecture Implement the design Code Test Repeat

  4. DRY DRY: Don’t repeat yourself Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

  5. Occam’s Razor Occam’s Razor: “Entities must not be multiplied beyond necessity” Keep It Simple, Stupid

  6. YAGNI Principle “You Ain’tGonna Need It”

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

  8. Programming with Java Arrays

  9. Arrays are Lists Index is zero based

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

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

  12. 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];

  13. Multi-dimensional Arrays type[size][size] name; String[][] names = { {“Mr. “, “Mrs. “, “Ms. “}, {“Smith”, “Jones”} }; Works like an array of (array) objects

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

  15. Programming with Java Attributes, Behaviors and Instances

  16. Some Review State = Characteristics = Attributes = Instance Variables Behavior = Methods Methods use Instance Variables Class is a blueprint for Objects

  17. Instance vs. Local Variables

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

  19. Encapsulation “Data hiding” allows methods to change Keep instance variables “private” Use public methods to access instance variables Accessors == Getters Mutators == Setters

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

More Related