Download presentation
Presentation is loading. Please wait.
Published byMelissa Sabine Modified over 9 years ago
1
COP 2800 Lake Sumter State College Mark Wilson, Instructor
2
Vocabulary
3
Accessor API Argument Array Byte Casting Class Compiler Conditional Constant Debug Double Encapsulation Expression Field Float Inheritance Instance Variable Integer Interpreter
4
JDK JVM Local Variable Loop Member Method Mutator Object Operator Override Parameter Polymorphism Private Public Statement Static String Structured Construct This Type
5
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
Objects, appropriately, are called objects. Actions are 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 adheres to three primary design principles: Encapsulation Polymorphism Inheritance
8
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
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
way of organizing classes. At each level classification becomes more specialized.
11
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
“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
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
combination of code and English used to express an algorithm before writing algorithm into code
15
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
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
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
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 void setMajor() public int classYear; public: there is no restriction on how you can use the method or instance variable
20
private void setMajor() private int classYear; private: can not directly use the method or instance variable’s name outside the class
21
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
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
public class Rectangle { public int width; public int height; public int area; public void setDimensions( int newWidth, int newHeight) { width = newWidth; height = newHeight; area = width * height; } public int getArea() { return area; } 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!
24
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
public class Student { private String name; private int age; public void setName(String studentName) { name = studentName; } public void setAge(int studentAge) { age = studentAge; } public String getName() { return name; } public int getAge() { return age; } Accessors Mutators
26
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
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
The interface is the same The underlying implementation may be different
29
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
public class Rectangle { private int width; private int height; public void setDimensions( int newWidth, int newHeight) { width = newWidth; height = newHeight; } public int getArea() { return width * height; } public class Rectangle { private int width; private int height; private int area; public void setDimensions( int newWidth, int newHeight) { width = newWidth; height = newHeight; area = width * height; } public int getArea() { return area; }
31
Implementation should not affect behavior described by interface Two classes can have the same behavior but different implementations
32
Instance variables are private Provide public accessor and mutator methods Make helping methods private
33
Variables are local to methods Instance variables are Global for all methods in a class Public instance variables are Global for everyone
34
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.
36
Logic
37
P T F PQ TT TF FT FF PQR TTT TTF TFT TFF FTT FTF FFT FFF
38
P¬P TF FT result = p && q; result = p|| q; PQ P QP Q TTTT TFFT FTFT FFFF result = !p;
39
PQR P Q(P Q) R TTTTT TTFTT TFTFT TFFFF FTTFT FTFFF FFTFT FFFFF result = (p && q) || r; result = p && q || r;
40
PQ P QP Q TTFT TFTT FTTT FFFF
41
byte a;11110000 byte b;10101010 byte (a ^ b);01011010 Exclusive OR byte a;11110000 byte b;10101010 byte (a | b);11111010 Inclusive OR byte a;11110000 byte b;10101010 byte (a & b);10100000 AND
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.