OOP Powerpoint slides from A+ Computer Science

Slides:



Advertisements
Similar presentations
The Point Class public class Point { public double x; public double y; public Point(double x0, double y0) { x = x0; y = y0; } public double distance(Point.
Advertisements

Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 6: A First Look at classes. Object-Oriented Programming  Object-oriented programming is centered on creating objects rather than procedures.
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
1 More on Classes Overview l Overloading l The this keyword l The toString method l The equals method.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Objects Real and Java COMP.
Chapter 6: A First Look at Classes
UML Basics & Access Modifier
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
© A+ Computer Science - Inheritance © A+ Computer Science - Lab 20.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
© A+ Computer Science - public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties.
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.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Computer Science 111 Fundamentals of Computer Programming I Working with our own classes.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Topics Instance variables, set and get methods Encapsulation
Class Fundamentals BCIS 3680 Enterprise Programming.
© A+ Computer Science - In Java, any variable that refers to an Object is a reference variable. The variable stores the memory.
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
Class Definitions: The Fundamentals Chapter 6 3/30/15 & 4/2/15 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education.
Today Encapsulation. Build a fully encapsulated Halloween class, going from Halloween1 to Halloween6 (eventually!): –The final version will have overloaded.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Objects as a programming concept
Classes (Part 1) Lecture 3
Software Development Java Classes and Methods
Chapter 3: Using Methods, Classes, and Objects
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
CS 302 Week 11 Jim Williams, PhD.
CS 200 Creating Classes Jim Williams, PhD.
Chapter 4: Writing classes
Lesson A4 – Object Behavior
Creating Objects in a Few Simple Steps
Outline Writing Classes Copyright © 2012 Pearson Education, Inc.
Classes, Encapsulation, Methods and Constructors (Continued)
Implementing Non-Static Features
© A+ Computer Science - OOP © A+ Computer Science -
Today’s topics UML Diagramming review of terms
Fall 2018 CISC124 2/24/2019 CISC124 Quiz 1 marking is complete. Quiz average was about 40/60 or 67%. TAs are still grading assn 1. Assn 2 due this Friday,
© A+ Computer Science - Classes And Objects © A+ Computer Science -
© A+ Computer Science - OOP Pieces © A+ Computer Science -
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Handout-4b More on Classes
Defining Classes and Methods
CSE 142 Lecture Notes Defining New Types of Objects, cont'd.
CS 200 Creating Classes Jim Williams, PhD.
Defining Classes and Methods
CMPE212 – Reminders Quiz 1 marking done. Assignment 2 due next Friday.
Dr. R Z Khan Handout-3 Classes
CS 200 Creating Classes Jim Williams, PhD.
Presentation transcript:

OOP Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course

Pieces of the OOP Puzzle

instance variables public class Rectangle() { private int recHeight; private int recWidth; ………………. } Constructors are typically used to initialize all of the Object’s data/properties. The default constructor usually sets all data/properties to a zero value. The exact zero value depends on the specified type of each instance variable. Instance variables are used to store information for a given object. Each instance of an object has its own copy of each instance variable.

constructors Default Constructor public Rectangle() { recHeight = 0; recWidth = 0; } Default Constructor Constructors are typically used to initialize all of the Object’s data/properties. The default constructor usually sets all data/properties to a zero value. The exact zero value depends on the specified type of each instance variable. Constructors are similar to methods. Constructors set the properties (instance variables) of an object to an initial state.

constructors Initialization Constructor public Rectangle(int ht, int width) { recHeight = ht; recWidth = width; } Initialization Constructor Constructors are typically used to initialize all of the Object’s data/properties. The initialization constructor usually sets the data/properties to a provided parameter value. Constructors are similar to methods. Constructors set the properties (instance variables) of an object to an initial state.

modifier methods public void setDimensions(int h, int w) { recHeight = h; recWidth = w; } Modifier methods are used to modify the Object’s data/properties. Set methods are modifier methods. Modifier methods are methods that change the properties of an object. Modifier methods are sometimes referred to as Mutator methods.

modifier methods public void setHeight(int h) { recHeight = h; } public void setWidth(int w) recWidth = w; Set methods are modifier methods. Set methods are used to change the data/properties of an Object. Modifier methods are methods that change the properties of an object. Modifier methods are sometimes referred to as Mutator methods.

accessor methods public int getHeight() { return recHeight; } public int getWidth() return recWidth; Accessor methods are used to retrieve the data/properties from the Object. Get methods are accessor methods. Accessor methods do not make changes to the data/properties. Accessor methods are methods that retrieve or grant access to the properties of an object, but do not make any changes.

accessor methods public String toString() { return “Rectangle with height of “ + getHeight() + " and width of " + getWidth(); } The toString() method is an accessor method. The toString() method should return all data/properties. The toString() should not change the data/properties of the Object. The toString method is a special accessor method. It returns the string representation of the object. It does not modify the values of the instance variables.

encapsulation All data members (instance variables) should have private access. The public constructors, accessor methods, and modifier methods should be used to manipulate and retrieve the data. All data is tucked away nicely inside the class.

The public methods give you access to an object’s private encapsulation The public methods give you access to an object’s private data / properties. getIt( ) private data / instance variables / properties Class/ Object setIt( ) toString( )

RectangleTester classes Show Rectangle and RectangleTester classes

Lab/Homework: Movie class Create a Movie class to store information about a movie and a MovieBox class that represents a kiosk to buy movies from.

Object

Object class In Java, all classes are automatically sub classes of the Object class. This adds greater flexibility when writing programs in Java. Object String Movie Monster

All classes extend Object! public class Monster { ………… } The class automatically extends Object. You don’t have to explicitly extend it. extends Object Java automatically applies the extends Object to all new classes created. All classes extend Object.

Object class Because all classes are sub classes of Object, all classes start with the same methods. .equals( ) checks to see if the object points to the same object in memory as another object. .toString( ) returns the string representation of the object. . . . . . and more Object contains quite a few methods, but equals() and toString() are the most commonly used.

Open MonsterOne.java

equals

The equals() method The equals() method is used to see if two objects have the same contents. String one = "comp"; String two = "sci"; if (one.equals(two)) System.out.print(“Strings are equal“); To determine if two Objects are the same, the data/properties of both Objects must be compared.

equals() OUTPUT heights not equal public class Monster { private int height; private double weight; private String name; // constructor method and other methods not shown public boolean equals(Object obj) Monster other = (Monster) obj; if (getHeight() == other.getHeight() ) return true; return false; } //test code in the main method Monster one = new Monster(33, 200.0, “Stu”); Monster two = new Monster(12, 150.0, “Alice”); if (one.equals(two)) System.out.print(“heights are equal”); else System.out.print(“heights not equal”); equals() OUTPUT heights not equal To determine if two Objects are the same, the data/properties of both Objects must be compared. Monster contains a height property only. Monsters are compared by comparing the heights. If the heights of two Monsters are the same, the Monsters are considered equal.

Open Monster and MonsterEqualsTester.java

toString

The toString() method The toString() method is used to return a version of your object in String form. The most common use of this method is when you are printing the object. System.out.print(Monster); Since the print() method requires a String data type, Java automatically calls the toString() method before printing the object. To determine if two Objects are the same, the data/properties of both Objects must be compared.

toString() OUTPUT Sam is 10 ft tall and weighs 200.0 lbs public class Monster { private int height; private double weight; private String name; // constructor method and other methods not shown public String toString() return name + “ is “ + height + “ ft tall and weighs “ + weight + “ lbs”; } // test code in the main Monster one = new Monster(10, 200.0, ”Sam”); System.out.println(one); toString() To determine if two Objects are the same, the data/properties of both Objects must be compared. Monster contains a height property only. Monsters are compared by comparing the heights. If the heights of two Monsters are the same, the Monsters are considered equal. OUTPUT Sam is 10 ft tall and weighs 200.0 lbs

Private/Public

All members with public access can be accessed What does public mean? All members with public access can be accessed inside and outside of the class where they are defined.

All members with private access can only be accessed What does private mean? All members with private access can only be accessed inside of the class where they are defined.

Open Private.java

Constructors

Constructors If you do not provide any constructors, Java will provide a default constructor. This allows you to instantiate the object, but its properties (instance variables) will not be initialized.

Open MonsterTwo.java

Overloading Overloading occurs when you have more than one method or constructor with the same name in the same class. Each method or constructor must have a different parameter list. The number of parameters and the data types of the parameters matter

class Monster { private int height; private double weight; private String name; public Monster(){ height=0; weight=0.0; name=“no name”; } public Monster(int ht){ height=ht; public Monster(double wt){ weight=wt; public Monster(int ht, double wt, String nm){ name=nm; Overloading The Monster constructor has been overloaded as it appears 4 times. Each time Monster() is written, a different set of parameters is provided. Java can differentiate between the Monster() constructors by the parameter list.

Overloading class Monster { private int height; private double weight; private String name; //……constructors not shown public void setProperty(int ht) height = ht; } public void setProperty(double wt) weight = wt; public void setProperty(String nm) name = nm; Overloading The Monster constructor has been overloaded as it appears 4 times. Each time Monster() is written, a different set of parameters is provided. Java can differentiate between the Monster() constructors by the parameter list.

Open MonsterOverload and MonsterOverloadTester

Monster Object Diagram Monster() - constructors setX( ) - modifiers getX( ) - accessors toString( ) - accessor Monster

constructor modifier accessor accessor public class Monster { //instance vars / data fields public Monster(){ //code } public void setX( params ){ public int getX(){ public String toString() { constructor modifier accessor accessor

Constructors class Monster{ // instance variables public Monster(){ code } public Monster(int ht) { code } public Monster(int ht, int wt) { code } public Monster(int ht, int wt, String n) { code } //more methods }

Monster Instantiation 1 Monster m = new Monster(); m MONSTER Properties: height = 0, weight = 0, name = “no name” methods m is a reference variable that refers to a Monster object.

Monster Instantiation 2 Monster m = new Monster(23); 0x234 m MONSTER Properties height = 23, weight = 0, name = “no name” methods 0x234 m is a reference variable that refers to a Monster object.

Monster Instantiation 3 Monster m = new Monster(23, 45); 0x239 m MONSTER Properties height = 23, weight = 45, name = “no name” methods 0x239 m is a reference variable that refers to a Monster object.

Monster Instantiation 4 Monster m = new Monster(23, 45, “Gus”); 0x2B3 m MONSTER Properties height = 23, weight = 45, name = “Gus” methods 0x2B3 m is a reference variable that refers to a Monster object.

Start work on Animal Class