CSE 1030: Implementing Mixing static and non-static features

Slides:



Advertisements
Similar presentations
Inheritance // A simple class hierarchy. // A class for two-dimensional objects. class TwoDShape { double width; double height; void showDim() { System.out.println("Width.
Advertisements

Identity and Equality Based on material by Michael Ernst, University of Washington.
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Singleton vs utility class  at first glance, the singleton pattern does not seem to offer any advantages to using a utility class  i.e., a utility class.
5/2/2015 Good Java Programming 1 Bigram: group/word of 2 letters java.util.Set: A collection that contains no duplicate elements.  sets contain no pair.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
UML Basics & Access Modifier
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Creating Simple Classes. Outline of Class Account Class Account Account # Balance Holder name phone# Overdrawn (true/false) Data Members Open Credit Debit.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
A Singleton Puzzle: What is Printed? 1 public class Elvis { public static final Elvis INSTANCE = new Elvis(); private final int beltSize; private static.
Utilities (Part 2) Implementing static features 1.
© A+ Computer Science - public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties.
Checking Equality of Reference Variables. Arrays and objects are both “reference” types n They are allocated a chunk of memory in the address space n.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
Questions? Suggestions?. References References Revisited What happens when we say: int x; double y; char c; ???
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Announcements Final Exam:TBD. public static void main(String [] args) { final int ASIZE = 5; int [] intArray= new int[ASIZE]; for(int i = 0; i < ASIZE;
HashCode() 1  if you override equals() you must override hashCode()  otherwise, the hashed containers won't work properly  recall that we did not override.
Overriding toString()
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Newport Robotics Group 1 Tuesdays, 6:30 – 8:30 PM Newport High School Week 4 10/23/2014.
1  lecture slides online
Puzzle 1  what does the following program print? public class Puzzle01 { public static void main(String[] args) { System.out.print("C" + "S" + "E"); System.out.println('1'
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
ABSTRACT DATA TYPES, ABSTRACT CLASSES AND INTERFACES And abstract art….
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
CSE 1030: Implementing Static Features Mark Shtern.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis & Matt Bauer.
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Topics Instance variables, set and get methods Encapsulation
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
CSE 1020:Using Objects Mark Shtern 1-1. Summary Read API Method binding and overloading Development process Input validation Assert 1-2.
CSC 205 Programming II Lecture 4 Abstract Class. The abstract keyword indicate that a class is not instantiable Defining a type which will be specialized.
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Staples are our staple Building upon our solution.
Topic: Classes and Objects
Chapter 15 Abstract Classes and Interfaces
CompareTo.
Java: Base Types All information has a type or class designation
Software Development Java Classes and Methods
Intro To Classes Review
CS 302 Week 11 Jim Williams, PhD.
CS Week 13 Jim Williams, PhD.
CS 200 Creating Classes Jim Williams, PhD.
CS 302 Week 10 Jim Williams.
Initializing Arrays char [] cArray3 = {'a', 'b', 'c'};
Classes Variables That Are Not of a Built-in Type Are Objects
Classes & Objects: Examples
CSE 1030: Implementing Non-Static Features
CSE 1020:Programming by Delegation
CSE 1030: Data Structure Mark Shtern.
Implementing Non-Static Features
Inheritance.
Singleton Pattern Pattern Name: Singleton Pattern Context
CS 200 Creating Classes Jim Williams, PhD.
CS 112 Programming 2 Lecture 02 Abstract Classes & Interfaces (2)
CS 200 More Classes Jim Williams, PhD.
CS 200 Creating Classes Jim Williams, PhD.
CS 240 – Advanced Programming Concepts
Presentation transcript:

CSE 1030: Implementing Mixing static and non-static features Mark Shtern

Summary Class this parameter Memory Standard methods compareTo toString, equals, hashCode compareTo

Ex10 Design and implement a Rectangle class Attributes Width Height Methods getArea Assessors, Mutators Write application that sorts Rectangles by area

Find a Bug import java.util.*; public class Name { private final String first, last; public Name(String first, String last) { this.first = first; this.last = last; } public boolean equals(Object o) { if (!(o instanceof Name)) return false; Name n = (Name)o; return n.first.equals(first) && n.last.equals(last); public static void main(String[] args) { Set<Name> s = new HashSet<Name>(); s.add(new Name("Mickey", "Mouse")); System.out.println (s.contains(new Name("Mickey", "Mouse"))); }

Avoiding code duplication Constructor Chaining Delegation to Mutators Delegation to Accessors

Attribute Attribute selection Attribute Caching Money  Cents Polynomial  Coefficients Attribute Caching

Summary Objects Utility Class

Utility vs Class State full static this Constructors Methods State less

Utility vs Class Utility Object/Class State less or shared state static No public constructors All static methods Invocation: ClassName.methodName() Object/Class State Public constructors Instance variables/ Attributes Instance Reference variable/this Invocation: objectName.methodName() Instantiation: obj Name= new ClassName(...)

Standard Methods (Object) toString()  returns a string that represents the current object equals()  indicates whether some other object is "equal to" this one hashCode()  returns an integer (hash code value) for the object. This method is supported for the benefit of containers

V Hash Code Examples Object Hash Code Point p1 = new Point(); 1 12 Object Hash Code Point p1 = new Point(); 1 Point p2 = new Point(); V

V V Hash Code Examples Object Hash Code Point p1 = new Point(1,2); 1 12 V Object Hash Code Point p1 = new Point(1,2); 1 Point p2 = new Point(1,1); V

Immutable Objects It is an object whose state cannot be modified after it is created. String, Double, Integer No public mutators

Features Static Feature Non-static Feature Mixing Static and Non-Static Feature

Incorporating an Invocation Counter Refractor the Rectangle class to keep track of the number of times the getArea method is used

Stamping a Serial Number on Objects Design a patter that helps to associate unique serial number with every instance of a class Apply the pattern for Rectangle class. Every rectangle should have a unique serial number that starts at 1 and increments serially.

Singleton Pattern Singleton pattern is restricting the instantiation of a class to one object Singleton pattern is a design pattern public static ClassName getInstance(); private static ClassName instance = new ClassName(..);