programming with java
Download
Skip this Video
Download Presentation
Programming with Java

Loading in 2 Seconds...

play fullscreen
1 / 41

Programming with Java - PowerPoint PPT Presentation


  • 56 Views
  • Uploaded on

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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Programming with Java ' - prema


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
programming with java

Programming with Java

COP 2800

Lake Sumter State College

Mark Wilson, Instructor

java terms
Java Terms
  • žAccessor
  • žAPI
  • žArgument
  • žArray
  • žByte
  • žCasting
  • žClass
  • žCompiler
  • žConditional
  • žConstant
  • žDebug
  • žDouble
  • žEncapsulation
  • žExpression
  • žField
  • žFloat
  • žInheritance
  • žInstance Variable
  • žInteger
  • žInterpreter
more java terms
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
object oriented programming
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.

oop terminology
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.
oop design principles
OOP Design Principles
  • OOP adheres to three primary design principles:
    • Encapsulation
    • Polymorphism
    • Inheritance
encapsulation information hiding
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.

accessibility example
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.
inheritance
Inheritance

way of organizing classes.

At each level classification becomes more specialized.

introduction to inheritance
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.

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

pseudocode
Pseudocode

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

pbjs algorithm
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!
pbjs algorithm revisited
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
calling methods from methods
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();
slide18
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
public modifier
Public Modifier

public void setMajor()

public intclassYear;

public: there is no restriction on how you can use the method or instance variable

private modifier
Private Modifier

private void setMajor()

private intclassYear;

private: can not directly use the method or instance variable’s name outside the class

more about private
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
private instance variables
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?

example rectangle
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;

}

}

accessors and mutators
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
example student
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;

}

}

private methods
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
example driving a car
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
encapsulation
Encapsulation

The interface is the same

The underlying implementation may be different

encapsulation in classes
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
example rectangle1
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;

}

}

encapsulation1
Encapsulation

Implementation should not affect behavior described by interface

Two classes can have the same behavior but different implementations

guidelines
Guidelines
  • Instance variables are private
  • Provide public accessor and mutatormethods
  • Make helping methods private
global vs local
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
selective amnesia
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.
using truth tables
Using Truth Tables

result = !p;

result = p && q;

result = p|| q;

more truth
More Truth

result = (p && q) || r;

result = p && q || r;

bitwise
Bitwise

AND

Exclusive OR

Inclusive OR

ad