Non-static classes 1.  a utility class has features (fields and methods) that are all static  all features belong to the class  therefore, you do not.

Slides:



Advertisements
Similar presentations
Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
Advertisements

Chapter 6 Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and.
The Line Class Suppose you are involved in the development of a large mathematical application, and this application needs an object to represent a Line.
L3:CSC © Dr. Basheer M. Nasef Lecture #3 By Dr. Basheer M. Nasef.
Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
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.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Lecture 2: Object Oriented Programming I
Overloaded Constructors constructors can be overloaded, like other methods – i.e., a class can define several constructors all constructors must have the.
Classes we can write our own classes – to represent and define our objects e.g. class for Complex objects represents a complex number defines its properties.
Applying OO Concepts Using Java. In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The.
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)
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.
Java Classes Appendix C © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Non-static classes Part 2 1. Methods  like constructors, all non-static methods have an implicit parameter named this  for methods, this refers to the.
Copyright © 2002, Systems and Computer Engineering, Carleton University a-JavaReview.ppt * Object-Oriented Software Development Unit.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
1 Object Oriented Programming Lecture II Classes, Objects, Abstract Data Types, Representation Invariants, Mutables and Immutables.
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.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 4 – August 30, 2001.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part C Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
1 Introduction to Classes and Objects Chapter 3 Introduction to Classes and Objects Chapter 3.
Chapter 11: Introduction to Classes. In this chapter you will learn about: – Classes – Basic class functions – Adding class functions – A case study involving.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Inheritance (Part 2) Notes Chapter KomondorBloodHound PureBreedMix Dog Object Dog extends Object PureBreed extends Dog Komondor extends PureBreed.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
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'
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Objects and Classes.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
1 Chapter 6 Programming with Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive.
Chapter 5 Defining Classes II Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Introduction To Objects Oriented Programming Instructor: Mohammed Faisal.
1 Class and Object Lecture 7. 2 Classes Classes are constructs that define objects of the same type. A Java class uses instance variables to define data.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
Utilities (Part 2) Implementing static features 1.
Learners Support Publications Constructors and Destructors.
More constructors 1. Constructors  recall that a public constructor is what a client uses to create an object  the purpose of a constructor is to initialize.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Constructors and Destructors
Lecture 3: Introduction to Object and Classes
Java Programming: Guided Learning with Early Objects
Java Primer 1: Types, Classes and Operators
Chapter 3: Using Methods, Classes, and Objects
This pointer, Dynamic memory allocation, Constructors and Destructor
HIGHLEVEL REVIEW Chapter 9 Objects and Classes
Implementing Non-Static Features
Java Classes and Objects 3rd Lecture
Constructors and Destructors
Outline Anatomy of a Class Encapsulation Anatomy of a Method
OO Programming Concepts
Chapter 6 Objects and Classes
Classes and Objects Object Creation
Chapter 7 Objects and Classes
CMSC 202 Constructors Version 9/10.
Presentation transcript:

Non-static classes 1

 a utility class has features (fields and methods) that are all static  all features belong to the class  therefore, you do not need objects to use those features  a well implemented utility class should have a single, empty private constructor to prevent the creation of objects  most Java classes are not utility classes  they are intended to be used to create to objects  each object has its own copy of all non-static fields  it is useful to imagine that each object has its own copy of all non-static methods 2

Why objects?  each object has its own copy of all non-static fields  this allows objects to have their own state  in Java the state of an object is the set of current values of all of its non-static fields  e.g., we can create multiple Fraction objects that all represent different fraction values 3

4 Fraction x = new Fraction(1, 2); Fraction y = new Fraction(-3, 8); Fraction z = new Fraction(5, 13); 64 client x600 y700 z Fraction class numer denom 600 Fraction object numer1 denom2 700 Fraction object numer-3 denom8 800 Fraction object numer5 denom13

Value Type Classes 5  a value type is a class that represents a value  examples of values: name, date, colour, mathematical vector  Java examples: String, Date, Integer  the objects created from a value type class can be:  mutable: the state of the object can change  Date  immutable: the state of the object is constant once it is created  String, Integer (and all of the other primitive wrapper classes)

Imaginary numbers 6

7

Complex numbers 8

9

Why study complex numbers?  applications  any scientific or engineering application that involves vibrations, waves, or signals probably  complex analysis in mathematics  quantum mechanics in physics and chemistry  differential equations  many others  from an EECS1030 perspective  easily implemented value type  also, you can make pretty pictures 10

Mandelbrot set 11

Class Complex  when creating a class you should first analyze the requirements of the class  what fields does each object need?  how do you construct an object?  what methods should each object provide?  this information can be summarized in a UML class diagram 12 Complex  fields  constructors and methods  class name

Class Complex  what fields does each Complex object need?  a field to represent the real part  a field to represent the complex part 13 Complex real imag

Class Complex  what are appropriate types for the fields?  the real part  double  the complex part  double 14 Complex real : double imag : double

Class Complex  how do you create a Complex object?  by specifying the values of the real and imaginary parts 15 Complex real : double imag : double Complex(double, double)

What operations?  there are many possible operations involving complex numbers  implementing them all is impractical for our current purposes  we will consider the following  complex conjugate  absolute value  addition  multiplication 16

Complex conjugate 17

Absolute value 18

Addition 19

Multiplication 20

Class Complex  what methods should Complex provide? 21 Complex real : double imag : double Complex(double, double) conj() : Complex abs() : double add(Complex) : Complex multiply(Complex) : Complex

Class Complex  what other methods might a client find useful?  get the value of the real part  get the value of the imaginary part  set the value of the real part  set the value of the imaginary part  methods that get information about the state of an object are called accessor methods  methods that change the state of an object are called mutator methods 22

Class Complex 23 Complex real : double imag : double Complex(double, double) conj() : Complex abs() : double add(Complex) : Complex mult(Complex) : Complex getReal() : double getImag() : double setReal(double) : void setImag(double) : void

Class Complex  there are three more important methods, but we will look at these later 24

Class and fields  start by creating the class and adding the fields  if you decide to organize your classes into packages, then you should first create the appropriate package 25

26 public class Complex { private double real; private double imag; }

Class and fields  notice that the class is marked public  this means that the class is visible to all clients  notice that the fields are marked private  this means that the fields are visible only inside of the class 27

Constructor  we can now implement the constructor  a constructor:  must have the same name as the class  never returns a value (not even void)  constructors are not methods  can have zero or more parameters  the purpose of a constructor is to initialize the state of an object  it should set the values of the non-static fields to appropriate values  we should set the fields named real and imag 28

29 public class Complex { private double real; private double imag; public Complex(double real, double imag) { this.real = real; this.imag = imag; }

this  every constructor and non-static method has a parameter that does not explicitly appear in the parameter list  the parameter is called an implicit parameter and its name in Java is always this  in a constructor, this is a reference to the object currently being constructed 30

this  in our constructor public Complex(double real, double imag) { this.real = real; this.imag = imag; } this.real refers to the field named real this.imag refers to the field named imag real refers to the parameter named real imag refers to the parameter name imag 31

32 Complex z = new Complex(-1.5, 2.25); 64 client z 600 Complex object real imag 700 Complex constructor this600 real-1.5 imag new allocates memory for a Complex object 2.the Complex constructor is invoked by passing the memory address of the object and the arguments -1.5 and 2.25 to the constructor 3.the constructor runs, setting the values of the fields this.real and this.imag 4.the value of z is set to the memory address of the constructed object fieldsparameters

this  in our constructor public Complex(double real, double imag) { this.real = real; this.imag = imag; } there are parameters with the same names as fields  when this occurs, the parameter has precedence over the field  we say that the parameter shadows the field  when shadowing occurs you must use this to refer to the field 33