1 / 41

Programming with Java

Programming with Java . COP 2800 Lake Sumter State College Mark Wilson, Instructor. Programming with Java. Vocabulary. Java Terms. ž Accessor žAPI žArgument žArray žByte žCasting žClass žCompiler žConditional žConstant. žDebug žDouble žEncapsulation žExpression žField žFloat

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

  3. Java Terms • žAccessor • žAPI • žArgument • žArray • žByte • žCasting • žClass • žCompiler • žConditional • žConstant • žDebug • žDouble • žEncapsulation • žExpression • žField • žFloat • žInheritance • žInstance Variable • žInteger • žInterpreter

  4. More Java Terms • žJDK • žJVM • žLocal Variable • žLoop • žMember • žMethod • žMutator • žObject • žOperator • žOverride • žParameter • žPolymorphism • žPrivate • žPublic • žStatement • žStatic • žString • žStructured Construct • žThis • žType

  5. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations, etc.). Objects can perform actions which affect themselves and other objects in the world. Object-oriented programming (OOP) treats a program as a collection of objects that interact by means of actions.

  6. OOP Terminology • Objects, appropriately, are called objects. • Actionsare called methods. • Objects of the same kind have the same type and belong to the same class. • Objects within a class have a common set of methods and the same kinds of data • but each object can have it’s own data values.

  7. OOP Design Principles • OOP adheres to three primary design principles: • Encapsulation • Polymorphism • Inheritance

  8. Encapsulation (Information Hiding) packing things up, only seeing part of what is going on Encapsulation provides a means of using the class, but it omits the details of how the class works. Encapsulation often is called information hiding.

  9. Accessibility Example • An automobile consists of several parts and pieces and is capable of doing many useful things. • Awareness of the accelerator pedal, the brake pedal, and the steering wheel is important to the driver. • Awareness of the fuel injectors, the automatic braking control system, and the power steering pump is not important to the driver.

  10. Inheritance way of organizing classes. At each level classification becomes more specialized.

  11. Introduction to Inheritance Classes can be organized using inheritance. A class at lower levels inherits all the characteristics of classes above it in the hierarchy. At each level, classifications become more specialized by adding other characteristics. Higher classes are more inclusive; lower classes are less inclusive.

  12. Polymorphism • “many forms” • Same instruction to mean same thing in different contexts. • Example: “Go play your favorite sport.” • I’d go play lacrosse. • Others of you would play baseball instead. • In programming, this means that the same method name can cause different actions depending on what object it is applied to. • More on this in Chapter 8.

  13. Algorithm A set of instructions for solving a problem By designing methods, programmers provide actions for objects to perform. An algorithm describes a means of performing an action. Once an algorithm is defined, expressing it in Java (or in another programming language) usually is easy.

  14. Pseudocode combination of code and English used to express an algorithm before writing algorithm into code

  15. PBJS Algorithm • Get slice of bread from loaf and put on plate • Repeat until enough peanut butter • Put knife into peanut butter jar and get peanut butter • Transfer peanut butter from knife to slice of bread • Transfer other slice of bread from loaf to plate • Repeat until enough jelly • Put knife into jelly jar and get jelly • Transfer jelly from knife to other slice of bread • Put slice of bread (pb side down) on other slice of bread • Enjoy!

  16. PBJS Algorithm – Revisited • Get slice of bread • Apply peanut butter • Get other slice of bread • Apply jelly • Put slice of bread (pb side down) on other slice of bread • Enjoy! • Apply condiment • Put knife into condiment jar and get condiment • Transfer condiment from knife to slice of bread • Get bread • Get slice of bread from loaf • Put on plate

  17. Calling methods from methods • A method body can call another method • Done the same way:receiving_object.method(); • If calling a method in the same class, do not need receiving_object: • method(); • Alternatively, use the this keyword • this.method();

  18. This • Reference to the current object — the object whose method or constructor is being called • Within a class definition, this is a name for the receiving object • this.age • this.major • this.getAge() • Used when a field is shadowed – local variable with the same name as an instance variable • Used for explicit constructors (more later) • Frequently omitted, but understood to be there

  19. Public Modifier public void setMajor() public intclassYear; public: there is no restriction on how you can use the method or instance variable

  20. Private Modifier private void setMajor() private intclassYear; private: can not directly use the method or instance variable’s name outside the class

  21. More about Private • Hides instance variables and methods inside the class/object. The private variables and methods are still there, holding data for the object. • Invisible to external users of the class • Users cannot access private class members directly • Information hiding

  22. Private Instance Variables Force users of the class to access instance variables only through methods Gives you control of how programmers use your class Why is this important?

  23. Example: Rectangle Rectangle box = new Rectangle(); box.setDimensions(10, 5); System.out.println(box.getArea()); // Output: 50 box.width = 6; System.out.println(box.getArea()); // Output: 50, but wrong answer! public class Rectangle { public int width; public int height; public int area; public void setDimensions( intnewWidth, intnewHeight) { width = newWidth; height = newHeight; area = width * height; } public intgetArea() { return area; } }

  24. Accessors and Mutators • How do you access private instance variables? • Accessor methods (a.k.a. get methods, getters) • Allow you to look at data in private instance variables • Mutator methods (a.k.a. set methods, setters) • Allow you to change data in private instance variables

  25. Example: Student Mutators Accessors public class Student { private String name; private int age; public void setName(String studentName) { name = studentName; } public void setAge(intstudentAge) { age = studentAge; } public String getName() { return name; } public intgetAge() { return age; } }

  26. Private Methods • Helper methods that will only be used from inside a class should be private • External users have no need to call these methods • Encapsulation

  27. Example: driving a car • Accelerate with the accelerator pedal • Decelerate with the brake pedal • Steer with the steering wheel • Does not matter if: • You are driving a gasoline engine car or a hybrid engine car • You have a 4-cylinder engine or a 6-cylinder engine • You still drive the same way

  28. Encapsulation The interface is the same The underlying implementation may be different

  29. Encapsulation in Classes • A class interface tells programmers all they need to know to use the class in a program • The implementation of a class consists of the private elements of the class definition • private instance variables and constants • private methods • bodies of public methods

  30. Example: Rectangle public class Rectangle { private int width; private int height; private int area; public void setDimensions( intnewWidth, intnewHeight) { width = newWidth; height = newHeight; area = width * height; } public intgetArea() { return area; } } public class Rectangle { private int width; private int height; public void setDimensions( intnewWidth, intnewHeight) { width = newWidth; height = newHeight; } public intgetArea() { return width * height; } }

  31. Encapsulation Implementation should not affect behavior described by interface Two classes can have the same behavior but different implementations

  32. Guidelines • Instance variables are private • Provide public accessor and mutatormethods • Make helping methods private

  33. Global vs. Local • Variables are local to methods • Instance variables are Global for all methods in a class • Public instance variables are Global for everyone

  34. Selective Amnesia • Program a memory game involving of a sequence of random numbers between 1 and 10, inclusive, that are called out one at a time. Each player can remember up to 5 previous numbers. When the called number is in a player's memory, that player is awarded a point. If it's not, the player adds the called number to his memory, removing another number if his memory is full. • Both players start with empty memories. Both players always add new missed numbers to their memory but use a different strategy in deciding which number to remove: • One player strategy is to remove the number that hasn't been called in the longest time. • The other player strategy is to remove the number that's been in the memory the longest time.

  35. Selective Amnesia Example

  36. Programming with Java Logic

  37. Truth Table

  38. Using Truth Tables result = !p; result = p && q; result = p|| q;

  39. More Truth result = (p && q) || r; result = p && q || r;

  40. Inclusive and Exclusive OR

  41. Bitwise AND Exclusive OR Inclusive OR

More Related