COP 2800 Lake Sumter State College Mark Wilson, Instructor.

Slides:



Advertisements
Similar presentations
COMP 110: Introduction to Programming Tyler Johnson Feb 18, 2009 MWF 11:00AM-12:15PM Sitterson 014.
Advertisements

L3:CSC © Dr. Basheer M. Nasef Lecture #3 By Dr. Basheer M. Nasef.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Road Map Introduction to object oriented programming. Classes
Introduction to Computers and Java Recitation - 01/11/2008 CS 180 Department of Computer Science, Purdue University.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
Writing Classes (Chapter 4)
Catie Welsh January 19,  Office Hours ◦ After Class Today ◦ Come if you don’t have Eclipse working 2.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Object Oriented Programming Computer Engineering Department JAVA Programming Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 8 Objects and Classes.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
10-Nov-15 Java Object Oriented Programming What is it?
CSSE501 Object-Oriented Development. Chapter 4: Classes and Methods  Chapters 4 and 5 present two sides of OOP: Chapter 4 discusses the static, compile.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Chapter 8 Objects and Classes Object Oriented programming Instructor: Dr. Essam H. Houssein.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
Defining Classes and Methods Chapter 4. Object-Oriented Programming Our world consists of objects (people, trees, cars, cities, airline reservations,
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Object Oriented Programming with Java 03 - Introduction to Classes and Objects.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Object Oriented Programming
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Objects and Classes.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Introduction to Object-Oriented Programming Lesson 2.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Topics Instance variables, set and get methods Encapsulation
COMP Information Hiding and Encapsulation Yi Hong June 03, 2015.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Defining Classes and Methods
Classes (Part 1) Lecture 3
Java Primer 1: Types, Classes and Operators
Chapter 3: Using Methods, Classes, and Objects
PRINCIPALES OF OBJECT ORIENTED PROGRAMMING
Inheritance Basics Programming with Inheritance
COMP 110 Information hiding and encapsulation
Object-Oriented Programming
Object-Oriented Programming
Computer Programming with JAVA
Announcements Program 2 is due tomorrow by noon Lab 4 was due today
Object-Oriented Programming
Session 2: Introduction to Object Oriented Programming
Object-Oriented Programming
Defining Classes and Methods
Defining Classes and Methods
2.1 Introduction to Object-Oriented Programming
CSG2H3 Object Oriented Programming
Presentation transcript:

COP 2800 Lake Sumter State College Mark Wilson, Instructor

Vocabulary

 žAccessor  žAPI  žArgument  žArray  žByte  žCasting  žClass  žCompiler  žConditional  žConstant  žDebug  žDouble  žEncapsulation  žExpression  žField  žFloat  žInheritance  žInstance Variable  žInteger  žInterpreter

 žJDK  žJVM  žLocal Variable  žLoop  žMember  žMethod  žMutator  žObject  žOperator  žOverride  žParameter  žPolymorphism  žPrivate  žPublic  žStatement  žStatic  žString  žStructured Construct  žThis  žType

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

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

 OOP adheres to three primary design principles: Encapsulation Polymorphism Inheritance

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

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

 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

 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!

 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

 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 void setMajor()  public int classYear;  public: there is no restriction on how you can use the method or instance variable

 private void setMajor()  private int classYear;  private: can not directly use the method or instance variable’s name outside the class

 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

 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?

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!

 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

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

 Helper methods that will only be used from inside a class should be private External users have no need to call these methods  Encapsulation

 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

 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

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

 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 mutator methods  Make helping methods private

 Variables are local to methods  Instance variables are Global for all methods in a class  Public instance variables are Global for everyone

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

Logic

P T F PQ TT TF FT FF PQR TTT TTF TFT TFF FTT FTF FFT FFF

P¬P TF FT result = p && q; result = p|| q; PQ P  QP  Q TTTT TFFT FTFT FFFF result = !p;

PQR P  Q(P  Q)  R TTTTT TTFTT TFTFT TFFFF FTTFT FTFFF FFTFT FFFFF result = (p && q) || r; result = p && q || r;

PQ P  QP  Q TTFT TFTT FTTT FFFF

byte a; byte b; byte (a ^ b); Exclusive OR byte a; byte b; byte (a | b); Inclusive OR byte a; byte b; byte (a & b); AND