Today’s topics UML Diagramming review of terms

Slides:



Advertisements
Similar presentations
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Advertisements

CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
9-1 Chapter.8 Classes & Objects: A deeper look –Static Class Members –Passing & Returning Objects from Methods –The toString method –Writing an equals.
Chapter 8: A Second Look at Classes and Objects
© 2010 Pearson Addison-Wesley. All rights reserved. 9-1 Chapter Topics Chapter 9 discusses the following main topics: –Static Class Members –Passing Objects.
Chapter 6: A First Look at classes. Object-Oriented Programming  Object-oriented programming is centered on creating objects rather than procedures.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
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.
Chapter 6: A First Look at Classes
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
A First Look At Classes Chapter 6. Procedural Programming In procedural- programming all the program functionality is written in a few modules of code.
Starting Out With Java 5 (Control Structures to Objects) Chapter 6 By Tony Gaddis Copyright © 2005 Pearson Addison-Wesley. All rights reserved.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: From Control Structures through Objects Third Edition.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
Starting Out with Java: From Control Structures through Objects.
I NTRODUCTION TO PROGRAMMING Starting Out with Java: From Control Structures through Objects.
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
9-2  Static Class Members  Passing Objects as Arguments to Methods  Returning Objects from Methods  The toString method  Writing an equals Method.
I NTRODUCTION TO PROGRAMMING Starting Out with Java: From Control Structures through Objects CS 146 Class Notes: Cooper & Ji & Varol Fall 09.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
Chapter 9 – Part Tony Gaddis Modified by Elizabeth Adams.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
© 2010 Pearson Addison-Wesley. All rights reserved. 6-1 Chapter Topics Chapter 6 discusses the following main topics: –Classes and Objects –Instance Fields.
Lecture 19: Introduction to Classes Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 3 A First Look at Classes and Objects.
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 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
Topics Instance variables, set and get methods Encapsulation
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Starting Out With Java Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Chapter 9 Slide #1 Review.
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The Unified Modeling Language
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
CS16: UML’s Unified Modeling Language. UML Class Diagram A UML class diagram is a graphical tool that can aid in the design of a class. The diagram has.
Introduction to Constructors Lecture # 4. Copyright © 2011 Pearson Education, Inc. 3-2 Arguments Passed By Value In Java, all arguments to a method are.
Chapter 9 A Second Look at Classes and Objects - 4.
Chapter 6 A First Look at Classes. 2 Contents 1. Classes and objects 2. Instance Fields and Methods 3. Constructors 4. Overloading Methods and Constructors.
Copyright © 2011 Pearson Education, Inc. Starting Out with Java: Early Objects Fourth Edition by Tony Gaddis Chapter 3: A First Look at Classes and Objects.
Chapter 6 A First Look at Classes. 2 Contents 1. Classes and objects 2. Instance Fields and Methods 3. Constructors 4. Overloading Methods and Constructors.
Chapter 9 A Second Look at Classes and Objects - 2.
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
OOP Powerpoint slides from A+ Computer Science
Chapter 3 Classes & Objects
A first Look at Classes.
Lecture 6 D&D Chapter 7 & 8 Intro to Classes and Objects Date.
Methods Attributes Method Modifiers ‘static’
Topics Static Class Members Overloaded Methods Overloaded Constructors
Introduction to Classes
A Second Look at Classes and Objects - 2
Chapter 6: A First Look at Classes
Chapter 9: A Second Look at Classes and Objects
Chapter 9: A Second Look at Classes and Objects
Introduction to Classes
Implementing Non-Static Features
Defining Classes and Methods
Lecture 5- Classes, Objects and Methods
Dr. Sampath Jayarathna Cal Poly Pomona
Lecture 8 Object Oriented Programming (OOP)
Chapter 6: A First Look at classes
Presentation transcript:

Today’s topics UML Diagramming review of terms stale data or why we don’t store derived data this no-arg or default constructors copy constructors equals or using this and other

UML Diagram Unified Modeling Language (UML) provides a set of standard diagrams for graphically depicting object-oriented systems. Class name goes here Fields are listed here Methods are listed here

UML Diagram for Rectangle class length width setLength() setWidth() getLength() getWidth() getArea()

UML Data Type and Parameter Notation UML diagrams are language independent. UML diagrams use an independent notation to show return types, access modifiers, etc. Rectangle - width : double + setWidth(w : double) : void Access modifiers are denoted as: + public - private

Converting the UML Diagram to Code public class Rectangle { private double width; private double length; public void setWidth(double w) } public void setLength(double len) public double getWidth() { return 0.0; public double getLength() public double getArea() The structure of the class can be compiled and tested without having bodies for the methods. Just be sure to put in dummy return values for methods that have a return type other than void. Rectangle width : double length : double + setWidth(w : double) : void + setLength(len : double): void + getWidth() : double + getLength() : double + getArea() : double

Converting the UML Diagram to Code public class Rectangle { private double width; private double length; public void setWidth(double w) { width = w; } public void setLength(double len) { length = len; public double getWidth() { return width; public double getLength() { return length; public double getArea() { return length * width; Once the class structure has been tested, the method bodies can be written and tested. Rectangle width : double length : double + setWidth(w : double) : void + setLength(len : double): void + getWidth() : double + getLength() : double + getArea() : double

UML for the Car class that we worked on in lab. This is every- year : int make : String model : String speed : int -MAX_SPEED: int = 150 UML for the Car class that we worked on in lab. This is every- thing that you need to make stubs. It is not everything you need to build the methods. +Car(inYear: int, make: int, model: int) + accelerate():void + accelerate(amt: int):void + brake(): void + brake(amt: int): void + getModel(): String + getSpeed(): int + getYear(): int + toString(): String

Class Layout Conventions The layout of a source code file can vary by employer or instructor. A common layout is: Fields listed first public constants private constants private attributes Methods listed second Constructors first public next private last

Review of terminology Object – instance of a class Class – provides a blueprint or template for a class of objects Attribute – instance variable – instance field private – public encapsulation data hiding accessor - mutator

Stale Data Some data is the result of a calculation. Consider the area of a rectangle. length × width It would be impractical to use an area variable here. Data that requires the calculation of various factors has the potential to become stale. To avoid stale data, it is best to calculate the value of that data within a method rather than store it in a variable.

Stale Data Rather than use an area variable in a Rectangle class: public double getArea() { return length * width; } This dynamically calculates the value of the rectangle’s area when the method is called. Now, any change to the length or width variables will not leave the area of the rectangle stale.

Constructors Constructors have a few special properties that set them apart from normal methods. Constructors have the same name as the class. Constructors have no return type (not even void). Constructors may not return any values. Constructors are typically public.

The Default Constructor When an object is created, its constructor is always called. If you do not write a constructor, Java provides one when the class is compiled. The constructor that Java provides is known as the default constructor. It sets all of the object’s numeric fields to 0. It sets all of the object’s boolean fields to false. It sets all of the object’s reference variables to the special value null.

The Default Constructor The default constructor is a constructor with no parameters, used to initialize an object in a default configuration. The only time that Java provides a default constructor is when you do not write any constructor for a class. See example: First version of Rectangle.java A default constructor is not provided by Java if a constructor is already written. See example: Rectangle.java with Constructor

Writing Your Own No-Arg Constructor A constructor that does not accept arguments is known as a no-arg constructor. The default constructor (provided by Java) is a no-arg constructor. We can write our own no-arg constructor public Rectangle() { length = 1.0; width = 1.0; }

Overloaded Methods - Method Signature and Binding A method signature consists of the method’s name and the data types of the method’s parameters, in the order that they appear. The return type is not part of the signature. add(int, int) add(String, String) The process of matching a method call with the correct method is known as binding. The compiler uses the method signature to determine which version of the overloaded method to bind the call to. Signatures of the add methods of previous slide

Scope of Instance Fields Variables declared as instance fields in a class can be accessed by any instance method in the same class as the field. If an instance field is declared with the public access specifier, it can also be accessed by code outside the class, as long as an instance of the class exists.

Shadowing A parameter variable is, in effect, a local variable. Within a method, variable names must be unique. A method may have a local variable with the same name as an instance field. This is called shadowing. The local variable will hide the value of the instance field. Shadowing is discouraged and local variable names should not be the same as instance field names.

The this Reference – avoiding the ambiguity of shadowing The this reference is simply a name that an object can use to refer to itself. The this reference can be used to overcome shadowing and allow a parameter to have the same name as an instance field. public void setFeet(int feet) { this.feet = feet; //sets the this instance’s feet field //equal to the parameter feet. } Local parameter variable feet Shadowed instance variable

The toString Method The toString method of a class can be called explicitly: Stock xyzCompany = new Stock ("XYZ", 9.62); System.out.println(xyzCompany.toString()); However, the toString method does not have to be called explicitly but is called implicitly whenever you pass an object of the class to println or print. System.out.println(xyzCompany);

The toString Method All objects have a toString method that returns the class name and a hash of the memory address of the object. We can override the default method with our own to print out more useful information. Examples: Stock.java, StockDemo1.java

The equals Method When the == operator is used with reference variables, the memory address of the objects are compared. The contents of the objects are not compared. All objects have an equals method. The default operation of the equals method is to compare memory addresses of the objects (just like the == operator).

The equals Method Instead of using the == operator to compare two Stock objects, we should use the equals method. Now, objects can be compared by their contents rather than by their memory addresses. See example: StockCompare.java public boolean equals(Stock object2) { boolean status; if(symbol.equals(Object2.symbol && sharePrice == Object2.sharePrice) status = true; else status = false; return status; }

Copy Constructors A copy constructor accepts an existing object of the same class and clones it public Stock(Stock object 2) { symbol = object2.symbol; sharePrice = object2.sharePrice; } // Create a Stock object Stock company1 = new Stock("XYZ", 9.62); //Create company2, a copy of company1 Stock company2 = new Stock(company1);