Object Oriented Programming

Slides:



Advertisements
Similar presentations
Classes  All code in a Java program is part of a class  A class has two purposes  Provide functions to do work for the programmer  Represent data.
Advertisements

1 Object Oriented Programming mr. Hanley. 2 Programs built from smaller components Today’s software programs are generally built from pieces of code known.
Inheritance Inheritance Reserved word protected Reserved word super
Classes and Objects: Recap A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Classes and Objects  A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Writing Classes (Chapter 4)
XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
More Object Concepts Chapter 4.  Our class is made up of several students and each student has a name and test grades  How do we assign the variables.
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
The Java Programming Language
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
10-Nov-15 Java Object Oriented Programming What is it?
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Designing Classes Chapter 3. 2 Chapter Contents Encapsulation Specifying Methods Java Interfaces Writing an Interface Implementing an Interface An Interface.
Introducing Objects and stuff GABY and MATT Definition: Object: “a class that is the root of the hierarchy tree for all classes in JAVA.” –An object.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
SE-1010 Dr. Mark L. Hornick 1 Java Programming Basics.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Identifiers Identifiers in Java are composed of a series of letters and digits where the first character must be a letter. –Identifiers should help to.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 5 Introduction to Defining Classes
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
Static?. Static Not dynamic class Widget { static int s; int d; // dynamic // or instance // variable }
SEEM Java – Basic Introduction, Classes and Objects.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
A High Flying Overview CS139 – Fall 2006 How far we have come.
Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
1.1: Objects and Classes msklug.weebly.com. Agenda: Attendance Let’s get started What is Java? Work Time.
1 Guide gus; Creates a “mailbox” to hold the address of a Guide object. null gus.
A High Flying Overview CS139 – Fall 2010 How far we have come.
Chapter 1 Object Orientation: Objects and Classes.
9.1 CLASS (STATIC) VARIABLES AND METHODS Defining classes is only one aspect of object-oriented programming. The real power of object-oriented programming.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
1. Understanding the execution of local variable declaration (in a method body) new expression ( 3 steps ) method call (method frames, call stack) examples.
Introduction to Computing Science and Programming I
Working with Java.
Chapter 3 Syntax, Errors, and Debugging
Software Development Java Classes and Methods
Intro To Classes Review
Agenda Warmup AP Exam Review: Litvin A2
Chapter 3: Using Methods, Classes, and Objects
Java Programming: Guided Learning with Early Objects
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Can perform actions and provide communication
Lecturer: Mukhtar Mohamed Ali “Hakaale”
CS139 – Fall 2010 How far we have come
Java Programming with BlueJ
Chapter 3 Introduction to Classes, Objects Methods and Strings
Can perform actions and provide communication
IFS410: Advanced Analysis and Design
Fundamentals of Programming
Chapter 4 Writing Classes.
Can perform actions and provide communication
Applying OO Concepts Using Java
Focus of the Course Object-Oriented Software Development
Classes CS 21a: Introduction to Computing I
Classes, Objects and Methods
CS 112 Programming 2 Lecture 02 Abstract Classes & Interfaces (2)
Presentation transcript:

Object Oriented Programming 6/4/2018 Object Oriented Programming mr. Hanley

Programs built from smaller components Today’s software programs are generally built from pieces of code known as classes These classes serve all kinds of different purposes, from displaying visual elements on the screen to keeping track of files, dates and times, words and customers, aliens and heroes (including Master Chief of course)

What is a class? But what is a class? Aren’t I in a class? I’m confused!!! A class is similar to a blue print for a house. It includes a set of instructions that describe how a house is made, what kind of elements a house will contain as well as what kinds of things a the house will be able to do once its constructed.

What classes written by other people have we used so far? Scanner System Random Math String

How are these classes? These classes have been provided by various programmers and offered to us for free (all right, got to love a freebie!!!) Each of these is a blue print in java for how a specific software component (object) will behave Now I’m really confused, can you explain further?

Ok, so what is an object? An object is like the constructed house, while a ____________ is like the blue print. Ok, I think I’m starting to get it, can you give me an example? JTextField is a class, radiusTF is an object Inside of a frame, radiusTF is instanciated as a JTextField with the following command; JTextField radiusTF = new JTextField();

An example Object So, what happens when we create an object? radiusTF text “” bounds [120, 80, 50, 20] background color Color.grey So, what happens when we create an object? Our computer allocates memory for the object foreground color Color.black editable true visible true public String getText() public JTextField() public void setText(String txt)

Objects have their own data radiusTF text “” bounds [120, 80, 50, 20] background color Color.grey Each object has its own instance variables or state This object, for example has a background color of grey and is editable foreground color Color.black editable true visible true public String getText() public JTextField() public void setText(String txt)

Objects communicate via messages radiusTF Objects communicate via messages text “Radius will go here” bounds [120, 80, 50, 20] background color Color.grey To control objects, we send them messages To set the text for a JTextField, we send the message setText with a String inside the () radiusTF.setText(“Radius will go here!”); foreground color Color.black editable true visible true public String getText() public JTextField() public void setText(String txt)

You can also examine the state radiusTF You can also examine the state text “Radius will go here” bounds [120, 80, 50, 20] background color Color.grey An object can report its state via a message typically name get----() Example, getText() will send the caller whatever phrase is currently in the JTextField String temp = radiusTF.getText(); foreground color Color.black editable true visible true public String getText() public JTextField() public void setText(String txt)

Constructors are cool Constructors are special methods radiusTF Constructors are cool text “Radius will go here” bounds [120, 80, 50, 20] background color Color.grey Constructors are special methods They give an object its initial state by setting up all of its variables Constructors ALWAYS have the same name as the class!!! foreground color Color.black editable true visible true public String getText() public JTextField() public void setText(String txt)

What happens when multiple objects are created? radiusTF text “1” bounds [120, 80, 50, 20] background color Color.grey foreground color Color.black public String getText() editable true visible true public void setText(String txt) public JTextField() circumferenceTF text “6.28” bounds [120, 280, 50, 20] background color Color.grey foreground color Color.black public String getText() editable false visible true public void setText(String txt) public JTextField() areaTF text “3.14” bounds [120, 480, 50, 20] background color Color.grey foreground color Color.black public String getText() editable false visible true public void setText(String txt) public JTextField()

Cool, what can objects do? As we mentioned before, a blueprint lays out how a house is put together (constructors), what things will be in the house (instance variables) and what kinds of things a house can do (methods) Let’s look at another class to see how these three parts of a blueprint work

public Student(String nm) { name = nm; test1 = 0; test2 = 0; //From page 162 of your text public class Student { //instance variables public String name; private int test1; private int test2; private int test3; //Constructor method //Initialize a student's name to the empty string and his test scores to zero public Student() { name = ""; test1 = 0; test2 = 0; test3 = 0; } //Other methods public Student(String nm) { name = nm; test1 = 0; test2 = 0; test3 = 0; }//set a students name public void setName(String nm) { } /Get a student's name public String getName() { return name;

Compute and return a student's highest score ////Compute and return a student's average public int getAverage() { int average; //will hold the average of the 3 quizzes average = (int)Math.round((test1+test2+test3)/3.0); return average; } Compute and return a student's highest score public int getHighScore() { int highScore; highScore = test1; if (test2 > highScore) highScore = test2; if (test3 > highScore) highScore = test3; return highScore; public void setScore(int i, int score) { if (i == 1) test1 = score; else if (i == 2) test2 = score; else test3 = score; } //Get the score on the indicated test public int getScore(int i) { if (i == 1) return test1; else if (i == 2) return test2; else return test3;

//Return a string representation of a student's name, test scores and average public String toString(){ String str; str = "Name: " + name + "\n" + "Test 1: " + test1 + "\n" + "Test 2: " + test2 + "\n" + "Test 3: " + test3 + "\n" + "Average: " + getAverage(); return str; } } //end of class

How is the student class used? An application can create instances of a class by declaring a variable of that class and by using the new command For example, to create 4 student objects Student s1 = new Student(); Student s2 = new Student(); Student s3 = new Student(); Student s4 = new Student();

First, the house construction Whenever an object is instanciated, a constructor is activated The zero arg constructor simply sets the name to be blank and the test scores to 0 A constructor is called for each object The constructor is called 4 times In the StudentFrame, the one arg constructor is used to pass the Student Name in upon creation

What happens when we instantiate 4 students? name “” test1 0 test2 0 test3 0 getScore() setScore() setName() getName() getAverage() getHighScore() toString() s2 name “” test1 0 test2 0 test3 0 getScore() setScore() setName() getName() getAverage() getHighScore() toString() s3 name “” test1 0 test2 0 test3 0 getScore() setScore() setName() getName() getAverage() getHighScore() toString() s4 name “” test1 0 test2 0 test3 0 getScore() setScore() setName() getName() getAverage() getHighScore() toString()

What kinds of things appear in class files? Import statements Used to identify where classes are coming from Extends relationships Inheritance relationship Builds upon the class it extends All methods and variables from ancestors are inherited

What kinds of things appear in class files? Implements relationships (one or more) Means that certain methods MUST be present These methods have specific names and parameters You can leave them blank at first but must at least “stub code” them Global variable declarations Global variables exist for the life of the object

What kinds of things appear in class files? Methods Methods are sub commands that exist in the class They must be called or invoked in order to begin executing Public methods are callable by client programs Private methods can only be called by this class When a method is called, any parameters that are in the () must be supplied

What kinds of things appear in class files? Constructors Constructors are special methods that get called whenever a new command is issued for this class Constructors MUST have the same name as the class A class may have multiple constructors with different parameters The job of the constructor is to give the object an initial state

What kinds of things appear in class files? Constants Constants are cool, they allow a programmer to define a value that won’t change throughout the program PI, speed of light in a vaccum, number of onces in a liter final double OZSPERLITER = 33.8140227; Capital letters are used by tradition by C and C++ programmers for constants, please stick with this tradition

What kinds of things appear in class files? Program Comments //Used to document the program for other programmers /* This method works for multiline comments*/ Inner classes and other classes Used when they are only needed by this particular class

Summary Classes are used to provide reusable software blueprints to other programmers This concept is incredibly powerful Upon creating instances of these classes, we gain access to all of the data and logic that these blueprints contain