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'

Slides:



Advertisements
Similar presentations
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.
Advertisements

Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
CSCI 1100/1202 April 3, Testing A program should be executed multiple times with various input in an attempt to find errors Debugging is the process.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Immutable Objects and Classes.
Road Map Introduction to object oriented programming. Classes
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Enhancing classes Visibility modifiers and encapsulation revisited
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.
28-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
18-Aug-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming in Java Topic : Objects and Classes (cont)
1 Using Classes Object-Oriented Programming Using C++ Second Edition 5.
Writing Classes (Chapter 4)
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 10 Thinking in Objects.
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.
Puzzle 3 1  Write the class Enigma, which extends Object, so that the following program prints false: public class Conundrum { public static void main(String[]
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
Utilities (Part 2) Implementing static features 1.
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
Programming in Java CSCI-2220 Object Oriented Programming.
Chapter 8 Objects and Classes Object Oriented programming Instructor: Dr. Essam H. Houssein.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
Session 7 Methods Strings Constructors this Inheritance.
HashCode() 1  if you override equals() you must override hashCode()  otherwise, the hashed containers won't work properly  recall that we did not override.
© 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.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
CS170 ygao JAVA, C4Slide 1. CS170 ygao JAVA, C4Slide 2.
Object Oriented Programming with Java 03 - Introduction to Classes and Objects.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Objects and Classes.
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
1  lecture slides online
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Introduction To Objects Oriented Programming Instructor: Mohammed Faisal.
Puzzle 2 1  what does the following program print? public class Puzzle02 { public static void main(String[] args) { final long MICROS_PER_DAY = 24 * 60.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Utilities (Part 2) Implementing static features 1.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Fall 2013 Chapter 10 Thinking.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Chapter 10 Thinking in Objects
Topic: Classes and Objects
Chapter 10 Thinking in Objects
Chapter 10 Thinking in Objects
Object Based Programming
Chapter 9 Thinking in Objects
Classes & Objects: Examples
Group Status Project Status.
Chapter 9 Thinking in Objects
Namespaces, Scopes, Access privileges
OO Programming Concepts
Presentation transcript:

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' + '0' + '3' + '0' + 'z'); } 1

Solution 2  the program prints CSE318 instead of CSE1030Z  the problem occurs because the operator + is defined only for String s and primitive numeric types  the + operator concatenates String s if and only if at least one of the arguments is a String  char is a primitive numeric type

Utilities Implementing static features 3

Goals for Today  initiate the design of simple class  learn about class attributes  public  static  final  learn about preventing class instantiation 4

Motivation 5  you want to produce a software product that is to be used in many different countries  many different systems of measurement; for example  distance  metre/kilometre versus yard/mile  volume  teaspoon/tablespoon/cup versus millilitre/litre  force  newton versus pound-force  currency  CAN dollar versus US dollar versus euro versus …

Errors in Converting Units 6  errors in converting units can have catastrophic consequences  NASA Mars Climate Orbiter  destroyed by navigation error caused by one subcontractor using Imperial units (pound-force) instead of metric (newton)  drug over/underdosing  “Tenfold errors in pediatric doses are not uncommon. ”  Pediatric medication errors: predicting and preventing tenfold disasters. Journal of Clinical Pharmacology, 34(11):1043-5, 1994.

Review: Java Class 7  a class is a model of a thing or concept  in Java, a class is the blueprint for creating objects  attributes  the structure of an object; its components and the information (data) contained by the object  methods  the behaviour of an object; what an object can do

Designing a Class 8  to decide what attributes and methods a class must provide, you need to understand the problem you are trying to solve  the attributes and methods you provide (the abstraction you provide) depends entirely on the requirements of the problem Person appearance voice … draw() talk() … Person age photograph … compatibleWith(Person) contact () … video game persondating service person class name attributes methods

Designing a Class to Convert Distances 9  design a class to convert between kilometres and miles  what attributes are needed?  number of kilometres per mile  note: the number of kilometres in a mile never changes; it is genuinely a constant value  attributes that are constant have all uppercase names DistanceUtility KILOMETRES_PER_MILE : double attribute type

Version 1 10 public class DistanceUtility { // attributes public static final double KILOMETRES_PER_MILE = ; } lay out of a typical class : see [AJ 4.1], [notes 1.2]

Attributes 11  an attribute is a member that holds data  a constant attribute is usually declared by specifying 1. modifiers 1. access modifier public 2. static modifier static 3. final modifier final 2. type double 3. name KILOMETRES_PER_MILE 4. value public static final double KILOMETRES_PER_MILE = ;

Attributes 12  attribute names must be unique in a class  the scope of an attribute is the entire class  [JBA] and [notes] call public attributes fields

public Attributes 13  a public attribute is visible to all clients  public attributes break encapsulation  a NothingToHide object has no control over the value o f x  clients can put a NothingToHide object into an invalid state public class NothingToHide { public int x; // always positive } // client of NothingToHide NothingToHide h = new NothingToHide(); h.x = 100; h.x = -500; // x not positive

public Attributes 14  a public attribute makes a class brittle in the face of change  public attributes are hard to change  they are part of the class API  changing access or type will break exisiting client code public class NothingToHide { private int x; // always positive } // existing client of NothingToHide NothingToHide h = new NothingToHide(); h.x = 100; // no longer compiles

public Attributes 15  Avoid public attributes in production code  except when you want to expose constant value types

static Attributes 16  an attribute that is static is a per-class member  only one copy of the attribute, and the attribute is associated with the class  every object created from a class declaring a static attribute shares the same copy of the attribute  textbook uses the term static variable [AJ 256]  also commonly called class variable

static Attributes 17 DistanceUtility u = new DistanceUtility(); DistanceUtility v = new DistanceUtility(); 64client invocation u see [JBA 4.3.3] for another example 500 DistanceUtility class KILOMETRES_PER_MILE DistanceUtility object ??? 1100 DistanceUtility object ??? v belongs to class no copy of KILOMETRES_PER_MILE

static Attribute Client Access 18  a client can access a public static attribute without requiring an object  use the class name followed by a period followed by the attribute name  it is legal, but considered bad form, to access a public static attribute using an object // client of DistanceUtility double kmPerMi = Distance.KILOMETRES_PER_MILE; // client of DistanceUtility; avoid doing this DistanceUtility u = new DistanceUtility(); double kmPerMi = u.KILOMETRES_PER_MILE;

final Attributes 19  an attribute (or variable) that is final can only be assigned to once  public static final attributes are typically assigned when they are declared public static final double KILOMETRES_PER_MILE = ;  public static final attributes are intended to be constant values that are a meaningful part of the abstraction provided by the class

final Attributes of Primitive Types 20  final attributes of primitive types are constant public class AlsoNothingToHide { public static final int x = 100; } // client of AlsoNothingToHide AlsoNothingToHide.x = 88; // will not compile; // attribute is final and // previously assigned

final Attributes of Immutable Types 21  final attributes of immutable types are constant  also, String is immutable  it has no methods to change its contents public class StillNothingToHide { public static final String x = "peek-a-boo"; } // client of StillNothingToHide StillNothingToHide.x = "i-see-you"; // will not compile; // attribute is final and // previously assigned

final Attributes of Mutable Types 22  final attributes of mutable types are not logically constant; their state can be changed public class ReallyNothingToHide { public static final int[] x = { 0, 1, 2 }; } // client of ReallyNothingToHide int[] y = { 100, 101, 102, 103 }; ReallyNothingToHide.x = y;// will not compile; // attribute is final and // previously assigned ReallyNothingToHide.x[1] = 10000; // works!

final Attributes of Mutable Types 23 ReallyNothingToHide class final x : int[] obj : not final ReallyNothingToHide.x[1] = 10000; 10000

final Attributes of Mutable Types 24  final attributes of mutable types are not logically constant; their state can be changed public class LastNothingToHide { public static final ArrayList x = new ArrayList (); } // client of LastNothingToHide ArrayList y = new ArrayList (); LastNothingToHide.x = y;// will not compile; // attribute is final and // previously assigned LastNothingToHide.x.add( ); // works!

final Attributes 25  Avoid using mutable types as public constants.

new DistanceUtility Objects 26  our DistanceUtility API does not expose a constructor  but DistanceUtility u = new DistanceUtility(); is legal  if you do not define any constructors, Java will generate a default no-argument constructor for you

 our DistanceUtility API exposes only static constants (and methods later on)  its state is constant  there is no benefit in instantiating a DistanceUtility object  a client can access the constants (and methods) without creating a DistanceUtility object double kmPerMi = DistanceUtility.KILOMETRES_PER_MILE;  can prevent instantiation by declaring a private constructor Preventing Instantiation 27

Version 2 (prevent instantiation) 28 public class DistanceUtility { // attributes public static final double KILOMETRES_PER_MILE = ; // constructors // suppress default ctor for non-instantiation private DistanceUtility() {} } [notes 1.2.3]

Version 2.1 (even better) 29 public class DistanceUtility { // attributes public static final double KILOMETRES_PER_MILE = ; // constructors // suppress default ctor for non-instantiation private DistanceUtility() { throw new AssertionError(); } } [notes 1.2.3]

private 30  private attributes, constructors, and methods cannot be accessed by clients  they are not part of the class API  private attributes, constructors, and methods are accessible only inside the scope of the class  a class with only private constructors indicates to clients that they cannot use new to create instances of the class

Puzzle 2 31  what does the following program print? public class Puzzle02 { public static void main(String[] args) { final long MICROS_PER_DAY = 24 * 60 * 60 * 1000 * 1000; final long MILLIS_PER_DAY = 24 * 60 * 60 * 1000; System.out.println(MICROS_PER_DAY / MILLIS_PER_DAY); }