Programming with java
1 / 41

Programming with Java - PowerPoint PPT Presentation

  • 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

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
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.


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.


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


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.


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();


  • 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);


// Output: 50

box.width = 6;


// Output: 50, but wrong answer!

public class Rectangle


public int width;

public int height;

public int area;

public void setDimensions(




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



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


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(




width = newWidth;

height = newHeight;

area = width * height;


public intgetArea()


return area;



public class Rectangle


private int width;

private int height;

public void setDimensions(




width = newWidth;

height = newHeight;


public intgetArea()


return width * height;




Implementation should not affect behavior described by interface

Two classes can have the same behavior but different implementations


  • 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;



Exclusive OR

Inclusive OR